Skip to content

Commit

Permalink
Adds StdoutLogExporter to match (renamed) StdoutSpanExporter (#559)
Browse files Browse the repository at this point in the history
* Adds StdoutLogExporter to match (renamed) StdoutSpanExporter

* Adds deprecation notice to typealias

* Renames usage of StdoutSpanExporter
  • Loading branch information
atreat authored Jul 23, 2024
1 parent 408af2d commit 3f7c9a1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Examples/Network Sample/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func simpleNetworkCallWithDelegate() {
}


let spanProcessor = SimpleSpanProcessor(spanExporter: StdoutExporter(isDebug: true))
let spanProcessor = SimpleSpanProcessor(spanExporter: StdoutSpanExporter(isDebug: true))
OpenTelemetry.registerTracerProvider(tracerProvider:
TracerProviderBuilder()
.add(spanProcessor: spanProcessor)
Expand Down
49 changes: 49 additions & 0 deletions Sources/Exporters/Stdout/StdoutLogExporter.swift
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 OpenTelemetrySdk

class StdoutLogExporter: LogRecordExporter {
let isDebug: Bool

init(isDebug: Bool) {
self.isDebug = isDebug
}

func export(logRecords: [OpenTelemetrySdk.ReadableLogRecord], explicitTimeout: TimeInterval?) -> OpenTelemetrySdk.ExportResult {
if isDebug {
for logRecord in logRecords {
print(String(repeating: "-", count: 40))
print("Severity: \(String(describing: logRecord.severity))")
print("Body: \(String(describing: logRecord.body))")
print("InstrumentationScopeInfo: \(logRecord.instrumentationScopeInfo)")
print("Timestamp: \(logRecord.timestamp)")
print("ObservedTimestamp: \(String(describing: logRecord.observedTimestamp))")
print("SpanContext: \(String(describing: logRecord.spanContext))")
print("Resource: \(logRecord.resource.attributes)")
print("Attributes: \(logRecord.attributes)")
print(String(repeating: "-", count: 40) + "\n")
}
} else {
do {
let jsonData = try JSONEncoder().encode(logRecords)
if let jsonString = String(data: jsonData, encoding: .utf8) {
print(jsonString)
}
} catch {
print("Failed to serialize LogRecord as JSON: \(error)")
return .failure
}
}
return .success
}

func forceFlush(explicitTimeout: TimeInterval?) -> OpenTelemetrySdk.ExportResult {
return .success
}

func shutdown(explicitTimeout: TimeInterval?) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import Foundation
import OpenTelemetryApi
import OpenTelemetrySdk

public class StdoutExporter: SpanExporter {
@available(*, deprecated, renamed: "StdoutSpanExporter")
public typealias StdoutExporter=StdoutSpanExporter

public class StdoutSpanExporter: SpanExporter {
let isDebug: Bool

public init(isDebug: Bool = false) {
Expand Down

0 comments on commit 3f7c9a1

Please sign in to comment.