Skip to content

Commit 1780c32

Browse files
committed
Extract common code from exporters
Signed-off-by: Kevin Earls <[email protected]>
1 parent 42321f3 commit 1780c32

File tree

4 files changed

+56
-61
lines changed

4 files changed

+56
-61
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// Copyright The OpenTelemetry Authors
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
import Foundation
7+
import SwiftProtobuf
8+
9+
public class OtlpHttpExporterBase {
10+
let endpoint: URL
11+
let httpClient: HTTPClient
12+
13+
public init(endpoint: URL, useSession: URLSession? = nil) {
14+
self.endpoint = endpoint
15+
if let providedSession = useSession {
16+
self.httpClient = HTTPClient(session: providedSession)
17+
} else {
18+
self.httpClient = HTTPClient()
19+
}
20+
}
21+
22+
public func createRequest(body: Message, endpoint: URL) -> URLRequest {
23+
var request = URLRequest(url: endpoint)
24+
25+
do {
26+
request.httpMethod = "POST"
27+
request.httpBody = try body.serializedData()
28+
request.setValue("application/x-protobuf", forHTTPHeaderField: "Content-Type")
29+
} catch {
30+
print("Error serializing body: \(error)")
31+
}
32+
33+
return request
34+
}
35+
36+
37+
public func shutdown() {
38+
}
39+
40+
}

Sources/Exporters/OpenTelemetryProtocol/logs/OtlpHttpLogExporter.swift

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,25 @@
44
//
55

66
import Foundation
7-
import Logging
8-
import NIO
9-
import NIOHPACK
10-
import OpenTelemetryApi
117
import OpenTelemetrySdk
128

139
public func defaultOltpHttpLoggingEndpoint() -> URL {
1410
URL(string: "http://localhost:4318/v1/logs")!
1511
}
1612

17-
public class OtlpHttpLogExporter : LogRecordExporter {
18-
let endpoint: URL
19-
private let httpClient: HTTPClient
13+
public class OtlpHttpLogExporter : OtlpHttpExporterBase, LogRecordExporter {
2014
var pendingLogRecords: [ReadableLogRecord] = []
2115

16+
override
2217
public init(endpoint: URL = defaultOltpHttpLoggingEndpoint(), useSession: URLSession? = nil) {
23-
self.endpoint = endpoint
24-
if let providedSession = useSession {
25-
self.httpClient = HTTPClient(session: providedSession)
26-
} else {
27-
self.httpClient = HTTPClient()
28-
}
18+
super.init(endpoint: endpoint, useSession: useSession)
2919
}
3020

3121
public func export(logRecords: [OpenTelemetrySdk.ReadableLogRecord]) -> OpenTelemetrySdk.ExportResult {
3222
pendingLogRecords.append(contentsOf: logRecords)
3323
return self.flush()
3424
}
35-
36-
public func shutdown() {
37-
}
38-
25+
3926
public func forceFlush() -> OpenTelemetrySdk.ExportResult {
4027
self.flush()
4128
}
@@ -48,12 +35,8 @@ public class OtlpHttpLogExporter : LogRecordExporter {
4835
let body = Opentelemetry_Proto_Collector_Logs_V1_ExportLogsServiceRequest.with { request in
4936
request.resourceLogs = LogRecordAdapter.toProtoResourceRecordLog(logRecordList: logRecords)
5037
}
51-
52-
var request = URLRequest(url: endpoint)
53-
request.httpMethod = "POST"
54-
request.httpBody = try? body.serializedData()
55-
request.setValue("application/x-protobuf", forHTTPHeaderField: "Content-Type")
56-
38+
39+
let request = createRequest(body: body, endpoint: endpoint)
5740
httpClient.send(request: request) { result in
5841
switch result {
5942
case .success(_):

Sources/Exporters/OpenTelemetryProtocol/metric/OltpHTTPMetricExporter.swift

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,12 @@ public func defaultOltpHTTPMetricsEndpoint() -> URL {
1010
URL(string: "http://localhost:4318/v1/metrics")!
1111
}
1212

13-
public class OtlpHttpMetricExporter: MetricExporter {
14-
let endpoint: URL
15-
private let httpClient: HTTPClient
13+
public class OtlpHttpMetricExporter: OtlpHttpExporterBase, MetricExporter {
1614
var pendingMetrics: [Metric] = []
1715

16+
override
1817
public init(endpoint: URL = defaultOltpHTTPMetricsEndpoint(), useSession: URLSession? = nil) {
19-
self.endpoint = endpoint
20-
if let providedSession = useSession {
21-
self.httpClient = HTTPClient(session: providedSession)
22-
} else {
23-
self.httpClient = HTTPClient()
24-
}
18+
super.init(endpoint: endpoint, useSession: useSession)
2519
}
2620

2721
public func export(metrics: [Metric], shouldCancel: (() -> Bool)?) -> MetricExporterResultCode {
@@ -39,11 +33,7 @@ public class OtlpHttpMetricExporter: MetricExporter {
3933
$0.resourceMetrics = MetricsAdapter.toProtoResourceMetrics(metricDataList: metrics)
4034
}
4135

42-
var request = URLRequest(url: endpoint)
43-
request.httpMethod = "POST"
44-
request.httpBody = try? body.serializedData()
45-
request.setValue("application/x-protobuf", forHTTPHeaderField: "Content-Type")
46-
36+
let request = createRequest(body: body, endpoint: endpoint)
4737
httpClient.send(request: request) { result in
4838
switch result {
4939
case .success(_):
@@ -55,9 +45,4 @@ public class OtlpHttpMetricExporter: MetricExporter {
5545
}
5646
return exporterResult
5747
}
58-
59-
60-
public func shutdown() {
61-
62-
}
6348
}

Sources/Exporters/OpenTelemetryProtocol/trace/OtlpHTttpTraceExporter.swift

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,14 @@ public func defaultOltpHttpTracesEndpoint() -> URL {
1010
URL(string: "http://localhost:4318/v1/traces")!
1111
}
1212

13-
public class OtlpHttpTraceExporter: SpanExporter {
14-
let endpoint: URL
15-
private let httpClient: HTTPClient
13+
public class OtlpHttpTraceExporter: OtlpHttpExporterBase, SpanExporter {
1614
var pendingSpans: [SpanData] = []
17-
15+
16+
override
1817
public init(endpoint: URL = defaultOltpHttpTracesEndpoint(), useSession: URLSession? = nil) {
19-
self.endpoint = endpoint
20-
if let providedSession = useSession {
21-
self.httpClient = HTTPClient(session: providedSession)
22-
} else {
23-
self.httpClient = HTTPClient()
24-
}
18+
super.init(endpoint: endpoint, useSession: useSession)
2519
}
26-
20+
2721
public func export(spans: [SpanData]) -> SpanExporterResultCode {
2822
pendingSpans.append(contentsOf: spans)
2923
return self.flush()
@@ -37,11 +31,7 @@ public class OtlpHttpTraceExporter: SpanExporter {
3731
$0.resourceSpans = SpanAdapter.toProtoResourceSpans(spanDataList: spans)
3832
}
3933

40-
var request = URLRequest(url: endpoint)
41-
request.httpMethod = "POST"
42-
request.httpBody = try? body.serializedData()
43-
request.setValue("application/x-protobuf", forHTTPHeaderField: "Content-Type")
44-
34+
let request = createRequest(body: body, endpoint: endpoint)
4535
httpClient.send(request: request) { result in
4636
switch result {
4737
case .success(_):
@@ -53,7 +43,4 @@ public class OtlpHttpTraceExporter: SpanExporter {
5343
}
5444
return exporterResult
5545
}
56-
57-
public func shutdown() {
58-
}
5946
}

0 commit comments

Comments
 (0)