-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into thread-safe-http-exporters
- Loading branch information
Showing
10 changed files
with
411 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Sources/Exporters/OpenTelemetryProtocolHttp/StableOtlpHTTPExporterBase.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import SwiftProtobuf | ||
import OpenTelemetryProtocolExporterCommon | ||
|
||
public class StableOtlpHTTPExporterBase { | ||
let endpoint: URL | ||
let httpClient: HTTPClient | ||
let envVarHeaders: [(String, String)]? | ||
let config: OtlpConfiguration | ||
|
||
// MARK: - Init | ||
|
||
public init(endpoint: URL, config: OtlpConfiguration = OtlpConfiguration(), useSession: URLSession? = nil, envVarHeaders: [(String, String)]? = EnvVarHeaders.attributes) { | ||
self.envVarHeaders = envVarHeaders | ||
self.endpoint = endpoint | ||
self.config = config | ||
if let providedSession = useSession { | ||
self.httpClient = HTTPClient(session: providedSession) | ||
} else { | ||
self.httpClient = HTTPClient() | ||
} | ||
} | ||
|
||
public func createRequest(body: Message, endpoint: URL) -> URLRequest { | ||
var request = URLRequest(url: endpoint) | ||
|
||
for header in config.headers ?? [] { | ||
request.addValue(header.1, forHTTPHeaderField: header.0) | ||
} | ||
|
||
do { | ||
request.httpMethod = "POST" | ||
request.httpBody = try body.serializedData() | ||
request.setValue(Headers.getUserAgentHeader(), forHTTPHeaderField: Constants.HTTP.userAgent) | ||
request.setValue("application/x-protobuf", forHTTPHeaderField: "Content-Type") | ||
} catch { | ||
print("Error serializing body: \(error)") | ||
} | ||
|
||
return request | ||
} | ||
|
||
public func shutdown(explicitTimeout: TimeInterval? = nil) { } | ||
} |
95 changes: 95 additions & 0 deletions
95
Sources/Exporters/OpenTelemetryProtocolHttp/metric/StableOtlpHTTPMetricExporter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import OpenTelemetrySdk | ||
import OpenTelemetryProtocolExporterCommon | ||
|
||
public func defaultStableOtlpHTTPMetricsEndpoint() -> URL { | ||
URL(string: "http://localhost:4318/v1/metrics")! | ||
} | ||
|
||
public class StableOtlpHTTPMetricExporter: StableOtlpHTTPExporterBase, StableMetricExporter { | ||
var aggregationTemporalitySelector: AggregationTemporalitySelector | ||
var defaultAggregationSelector: DefaultAggregationSelector | ||
|
||
var pendingMetrics: [StableMetricData] = [] | ||
|
||
// MARK: - Init | ||
|
||
public init(endpoint: URL, config: OtlpConfiguration = OtlpConfiguration(), aggregationTemporalitySelector: AggregationTemporalitySelector = AggregationTemporality.alwaysCumulative(), defaultAggregationSelector: DefaultAggregationSelector = AggregationSelector.instance, useSession: URLSession? = nil, envVarHeaders: [(String, String)]? = EnvVarHeaders.attributes) { | ||
|
||
self.aggregationTemporalitySelector = aggregationTemporalitySelector | ||
self.defaultAggregationSelector = defaultAggregationSelector | ||
|
||
super.init(endpoint: endpoint, config: config, useSession: useSession, envVarHeaders: envVarHeaders) | ||
} | ||
|
||
|
||
// MARK: - StableMetricsExporter | ||
|
||
public func export(metrics : [StableMetricData]) -> ExportResult { | ||
pendingMetrics.append(contentsOf: metrics) | ||
let sendingMetrics = pendingMetrics | ||
pendingMetrics = [] | ||
let body = Opentelemetry_Proto_Collector_Metrics_V1_ExportMetricsServiceRequest.with { | ||
$0.resourceMetrics = MetricsAdapter.toProtoResourceMetrics(stableMetricData: sendingMetrics) | ||
} | ||
|
||
let request = createRequest(body: body, endpoint: endpoint) | ||
httpClient.send(request: request) { [weak self] result in | ||
switch result { | ||
case .success(_): | ||
break | ||
case .failure(let error): | ||
self?.pendingMetrics.append(contentsOf: sendingMetrics) | ||
print(error) | ||
} | ||
} | ||
|
||
return .success | ||
} | ||
|
||
public func flush() -> ExportResult { | ||
var exporterResult: ExportResult = .success | ||
|
||
if !pendingMetrics.isEmpty { | ||
let body = Opentelemetry_Proto_Collector_Metrics_V1_ExportMetricsServiceRequest.with { | ||
$0.resourceMetrics = MetricsAdapter.toProtoResourceMetrics(stableMetricData: pendingMetrics) | ||
} | ||
let semaphore = DispatchSemaphore(value: 0) | ||
let request = createRequest(body: body, endpoint: endpoint) | ||
httpClient.send(request: request) { result in | ||
switch result { | ||
case .success(_): | ||
break | ||
case .failure(let error): | ||
print(error) | ||
exporterResult = .failure | ||
} | ||
semaphore.signal() | ||
} | ||
semaphore.wait() | ||
} | ||
|
||
return exporterResult | ||
} | ||
|
||
public func shutdown() -> ExportResult { | ||
return .success | ||
} | ||
|
||
// MARK: - AggregationTemporalitySelectorProtocol | ||
|
||
public func getAggregationTemporality(for instrument: OpenTelemetrySdk.InstrumentType) -> OpenTelemetrySdk.AggregationTemporality { | ||
return aggregationTemporalitySelector.getAggregationTemporality(for: instrument) | ||
} | ||
|
||
// MARK: - DefaultAggregationSelector | ||
|
||
public func getDefaultAggregation(for instrument: OpenTelemetrySdk.InstrumentType) -> OpenTelemetrySdk.Aggregation { | ||
return defaultAggregationSelector.getDefaultAggregation(for: instrument) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.