Skip to content

Commit

Permalink
Add stdout metrics exporter (#666)
Browse files Browse the repository at this point in the history
  • Loading branch information
martin308 authored Jan 13, 2025
1 parent bf9cd75 commit 0db61a3
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 6 deletions.
83 changes: 83 additions & 0 deletions Sources/Exporters/Stdout/StdoutMetricExporter.swift
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class AggregationTemporalitySelector : AggregationTemporalitySelectorProt
public var aggregationTemporalitySelector: (InstrumentType) -> AggregationTemporality
}

public enum AggregationTemporality {
public enum AggregationTemporality: Codable {
case delta
case cumulative

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import Foundation
import OpenTelemetryApi

public class ExemplarData: Equatable {
public class ExemplarData: Equatable, Encodable {
internal init(epochNanos: UInt64, filteredAttributes: [String: AttributeValue], spanContext: SpanContext? = nil) {
self.filteredAttributes = filteredAttributes
self.epochNanos = epochNanos
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import Foundation
import OpenTelemetryApi

public class PointData: Equatable {
public class PointData: Equatable, Encodable {
internal init(startEpochNanos: UInt64, endEpochNanos: UInt64, attributes: [String: AttributeValue], exemplars: [ExemplarData]) {
self.startEpochNanos = startEpochNanos
self.endEpochNanos = endEpochNanos
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import Foundation
import OpenTelemetryApi

public enum MetricDataType {
public enum MetricDataType: Codable {
case LongGauge
case DoubleGauge
case LongSum
Expand All @@ -16,7 +16,7 @@ public enum MetricDataType {
case ExponentialHistogram
}

public struct StableMetricData: Equatable {
public struct StableMetricData: Equatable, Encodable {
public private(set) var resource: Resource
public private(set) var instrumentationScopeInfo: InstrumentationScopeInfo
public private(set) var name: String
Expand All @@ -28,7 +28,7 @@ public struct StableMetricData: Equatable {

public static let empty = StableMetricData(resource: Resource.empty, instrumentationScopeInfo: InstrumentationScopeInfo(), name: "", description: "", unit: "", type: .Summary, isMonotonic: false, data: StableMetricData.Data(aggregationTemporality: .cumulative, points: [PointData]()))

public class Data: Equatable {
public class Data: Equatable, Encodable {
public private(set) var points: [PointData]
public private(set) var aggregationTemporality: AggregationTemporality

Expand Down

0 comments on commit 0db61a3

Please sign in to comment.