/
OTLPGRPCLogEntryExporter.swift
175 lines (154 loc) · 6.72 KB
/
OTLPGRPCLogEntryExporter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift OTel open source project
//
// Copyright (c) 2024 Moritz Lang and the Swift OTel project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import GRPC
import Logging
import NIO
import NIOHPACK
import NIOSSL
@_spi(Logging) import OTel
@_spi(Logging) import OTLPCore
/// Exports logs to an OTel collector using OTLP/gRPC.
@_spi(Logging)
public final class OTLPGRPCLogEntryExporter: OTelLogEntryExporter {
private let configuration: OTLPGRPCLogEntryExporterConfiguration
private let connection: ClientConnection
private let client: Opentelemetry_Proto_Collector_Logs_V1_LogsServiceAsyncClient
private let logger = Logger(label: String(describing: OTLPGRPCLogEntryExporter.self))
public init(
configuration: OTLPGRPCLogEntryExporterConfiguration,
group: EventLoopGroup = MultiThreadedEventLoopGroup.singleton,
requestLogger: Logger = ._otelDisabled,
backgroundActivityLogger: Logger = ._otelDisabled
) {
self.configuration = configuration
var connectionConfiguration = ClientConnection.Configuration.default(
target: .host(configuration.endpoint.host, port: configuration.endpoint.port),
eventLoopGroup: group
)
if configuration.endpoint.isInsecure {
logger.debug("Using insecure connection.", metadata: [
"host": "\(configuration.endpoint.host)",
"port": "\(configuration.endpoint.port)",
])
}
// TODO: Support OTEL_EXPORTER_OTLP_CERTIFICATE
// TODO: Support OTEL_EXPORTER_OTLP_CLIENT_KEY
// TODO: Support OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE
var headers = configuration.headers
if !headers.isEmpty {
logger.trace("Configured custom request headers.", metadata: [
"keys": .array(headers.map { "\($0.name)" }),
])
}
headers.replaceOrAdd(name: "user-agent", value: "OTel-OTLP-Exporter-Swift/\(OTelLibrary.version)")
connectionConfiguration.backgroundActivityLogger = backgroundActivityLogger
connection = ClientConnection(configuration: connectionConfiguration)
client = Opentelemetry_Proto_Collector_Logs_V1_LogsServiceAsyncClient(
channel: connection,
defaultCallOptions: .init(customMetadata: headers, logger: requestLogger)
)
}
public func export(_ batch: some Collection<OTelLogEntry> & Sendable) async throws {
if case .shutdown = connection.connectivity.state {
throw OTelLogEntryExporterAlreadyShutDownError()
}
guard !batch.isEmpty else { return }
let request = Opentelemetry_Proto_Collector_Logs_V1_ExportLogsServiceRequest.with { request in
request.resourceLogs = [
Opentelemetry_Proto_Logs_V1_ResourceLogs.with { resourceLog in
resourceLog.scopeLogs = [
Opentelemetry_Proto_Logs_V1_ScopeLogs.with { scopeLog in
scopeLog.logRecords = batch.map { log in
Opentelemetry_Proto_Logs_V1_LogRecord.with { logRecord in
logRecord.timeUnixNano = log.timeNanosecondsSinceEpoch
logRecord.observedTimeUnixNano = log.timeNanosecondsSinceEpoch
logRecord.severityNumber = switch log.level {
case .trace: .trace
case .debug: .debug
case .info: .info
case .notice: .info4
case .warning: .warn
case .error: .error
case .critical: .fatal
}
logRecord.severityText = switch log.level {
case .trace: "TRACE"
case .debug: "DEUG"
case .info: "INFO"
case .notice: "NOTICE"
case .warning: "WARNING"
case .error: "ERROR"
case .critical: "CRITICAL"
}
if let metadata = log.metadata {
logRecord.attributes = .init(metadata)
}
logRecord.body = .with { body in
body.stringValue = log.body
}
}
}
}
]
}
]
}
_ = try await client.export(request)
}
public func forceFlush() async throws {}
public func shutdown() async {
let promise = connection.eventLoop.makePromise(of: Void.self)
connection.closeGracefully(deadline: .now() + .milliseconds(500), promise: promise)
try? await promise.futureResult.get()
}
}
@_spi(Logging)
extension [Opentelemetry_Proto_Common_V1_KeyValue] {
package init(_ metadata: Logger.Metadata) {
self = metadata.map { key, value in
return .with { attribute in
attribute.key = key
attribute.value = .init(value)
}
}
}
}
@_spi(Logging)
extension Opentelemetry_Proto_Common_V1_KeyValueList {
package init(_ metadata: Logger.Metadata) {
self = .with { keyValueList in
keyValueList.values = .init(metadata)
}
}
}
@_spi(Logging)
extension Opentelemetry_Proto_Common_V1_AnyValue {
package init(_ value: Logger.Metadata.Value) {
self = .with { attributeValue in
attributeValue.value = switch value {
case .string(let string): .stringValue(string)
case .stringConvertible(let stringConvertible): .stringValue(stringConvertible.description)
case .dictionary(let metadata): .kvlistValue(.init(metadata))
case .array(let values): .arrayValue(.init(values))
}
}
}
}
@_spi(Logging)
extension Opentelemetry_Proto_Common_V1_ArrayValue {
package init(_ values: [Logger.Metadata.Value]) {
self = .with { valueList in
valueList.values = values.map(Opentelemetry_Proto_Common_V1_AnyValue.init)
}
}
}