forked from open-telemetry/opentelemetry-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3b541b
commit 726b343
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
Sources/Exporters/OpenTelemetryProtocolHttp/logs/SimpleHttpLogExporter.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,65 @@ | ||
// | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import OpenTelemetryProtocolExporterCommon | ||
import OpenTelemetrySdk | ||
#if canImport(FoundationNetworking) | ||
import FoundationNetworking | ||
#endif | ||
|
||
public class SimpleHttpLogExporter: OtlpHttpExporterBase, LogRecordExporter { | ||
override public init(endpoint: URL = defaultOltpHttpLoggingEndpoint(), | ||
config: OtlpConfiguration = OtlpConfiguration(), | ||
useSession: URLSession? = nil, | ||
envVarHeaders: [(String, String)]? = EnvVarHeaders.attributes) { | ||
super.init(endpoint: endpoint, config: config, useSession: useSession, envVarHeaders: envVarHeaders) | ||
} | ||
|
||
public func export(logRecords: [OpenTelemetrySdk.ReadableLogRecord], explicitTimeout: TimeInterval? = nil) -> OpenTelemetrySdk.ExportResult { | ||
let sendingLogRecords: [ReadableLogRecord] = logRecords | ||
|
||
let body = Opentelemetry_Proto_Collector_Logs_V1_ExportLogsServiceRequest.with { request in | ||
request.resourceLogs = LogRecordAdapter.toProtoResourceRecordLog(logRecordList: sendingLogRecords) | ||
} | ||
|
||
var exportResult: ExportResult = .success | ||
let semaphore = DispatchSemaphore(value: 0) | ||
var request = createRequest(body: body, endpoint: endpoint) | ||
if let headers = envVarHeaders { | ||
headers.forEach { key, value in | ||
request.addValue(value, forHTTPHeaderField: key) | ||
} | ||
|
||
} else if let headers = config.headers { | ||
headers.forEach { key, value in | ||
request.addValue(value, forHTTPHeaderField: key) | ||
} | ||
} | ||
request.timeoutInterval = min(explicitTimeout ?? TimeInterval.greatestFiniteMagnitude, config.timeout) | ||
httpClient.send(request: request) { result in | ||
switch result { | ||
case .success: | ||
exportResult = .success | ||
case let .failure(error): | ||
print(error) | ||
exportResult = .failure | ||
} | ||
semaphore.signal() | ||
} | ||
semaphore.wait() | ||
|
||
return exportResult | ||
} | ||
|
||
public func forceFlush(explicitTimeout: TimeInterval? = nil) -> ExportResult { | ||
flush(explicitTimeout: explicitTimeout) | ||
} | ||
|
||
public func flush(explicitTimeout: TimeInterval? = nil) -> ExportResult { | ||
return .success | ||
} | ||
} | ||
|