-
Notifications
You must be signed in to change notification settings - Fork 159
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
Showing
5 changed files
with
89 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import Foundation | ||
import OpenTelemetrySdk | ||
|
||
public class StdoutMetricExporter: StableMetricExporter { | ||
let isDebug: Bool | ||
var aggregationTemporalitySelector: AggregationTemporalitySelector | ||
|
||
public init(isDebug: Bool, aggregationTemporalitySelector: AggregationTemporalitySelector = AggregationTemporality.alwaysCumulative()) { | ||
self.isDebug = isDebug | ||
self.aggregationTemporalitySelector = aggregationTemporalitySelector | ||
} | ||
|
||
public func export(metrics: [OpenTelemetrySdk.StableMetricData]) -> OpenTelemetrySdk.ExportResult { | ||
if isDebug { | ||
for metric in metrics { | ||
print(String(repeating: "-", count: 40)) | ||
print("Name: \(metric.name)") | ||
print("Description: \(metric.description)") | ||
print("Unit: \(metric.unit)") | ||
print("IsMonotonic: \(metric.isMonotonic)") | ||
print("Resource: \(metric.resource)") | ||
print("InstrumentationScopeInfo: \(metric.instrumentationScopeInfo)") | ||
print("Type: \(metric.type)") | ||
print("AggregationTemporality: \(metric.data.aggregationTemporality)") | ||
if !metric.data.points.isEmpty { | ||
print("DataPoints:") | ||
for point in metric.data.points { | ||
print(" - StartEpochNanos: \(point.startEpochNanos)") | ||
print(" EndEpochNanos: \(point.endEpochNanos)") | ||
print(" Attributes: \(point.attributes)") | ||
print(" Exemplars:") | ||
for exemplar in point.exemplars { | ||
print(" - EpochNanos: \(exemplar.epochNanos)") | ||
if let ctx = exemplar.spanContext { | ||
print(" SpanContext: \(ctx)") | ||
} | ||
print(" FilteredAttributes: \(exemplar.filteredAttributes)") | ||
if let e = exemplar as? DoubleExemplarData { | ||
print(" Value: \(e.value)") | ||
} | ||
if let e = exemplar as? LongExemplarData { | ||
print(" Value: \(e.value)") | ||
} | ||
} | ||
} | ||
} | ||
print(String(repeating: "-", count: 40) + "\n") | ||
} | ||
} else { | ||
let jsonEncoder = JSONEncoder() | ||
for metric in metrics { | ||
do { | ||
let jsonData = try jsonEncoder.encode(metric) | ||
if let jsonString = String(data: jsonData, encoding: .utf8) { | ||
print(jsonString) | ||
} | ||
} catch { | ||
print("Failed to serialize Metric as JSON: \(error)") | ||
return .failure | ||
} | ||
} | ||
} | ||
|
||
return .success | ||
} | ||
|
||
public func flush() -> OpenTelemetrySdk.ExportResult { | ||
return .success | ||
} | ||
|
||
public func shutdown() -> OpenTelemetrySdk.ExportResult { | ||
return .success | ||
} | ||
|
||
public func getAggregationTemporality(for instrument: OpenTelemetrySdk.InstrumentType) -> OpenTelemetrySdk.AggregationTemporality { | ||
return aggregationTemporalitySelector.getAggregationTemporality(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