Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stdout metrics exporter #666

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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