|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import OpenTelemetrySdk |
| 8 | + |
| 9 | +public class StdoutMetricExporter: StableMetricExporter { |
| 10 | + let isDebug: Bool |
| 11 | + var aggregationTemporalitySelector: AggregationTemporalitySelector |
| 12 | + |
| 13 | + public init(isDebug: Bool, aggregationTemporalitySelector: AggregationTemporalitySelector = AggregationTemporality.alwaysCumulative()) { |
| 14 | + self.isDebug = isDebug |
| 15 | + self.aggregationTemporalitySelector = aggregationTemporalitySelector |
| 16 | + } |
| 17 | + |
| 18 | + public func export(metrics: [OpenTelemetrySdk.StableMetricData]) -> OpenTelemetrySdk.ExportResult { |
| 19 | + if isDebug { |
| 20 | + for metric in metrics { |
| 21 | + print(String(repeating: "-", count: 40)) |
| 22 | + print("Name: \(metric.name)") |
| 23 | + print("Description: \(metric.description)") |
| 24 | + print("Unit: \(metric.unit)") |
| 25 | + print("IsMonotonic: \(metric.isMonotonic)") |
| 26 | + print("Resource: \(metric.resource)") |
| 27 | + print("InstrumentationScopeInfo: \(metric.instrumentationScopeInfo)") |
| 28 | + print("Type: \(metric.type)") |
| 29 | + print("AggregationTemporality: \(metric.data.aggregationTemporality)") |
| 30 | + if !metric.data.points.isEmpty { |
| 31 | + print("DataPoints:") |
| 32 | + for point in metric.data.points { |
| 33 | + print(" - StartEpochNanos: \(point.startEpochNanos)") |
| 34 | + print(" EndEpochNanos: \(point.endEpochNanos)") |
| 35 | + print(" Attributes: \(point.attributes)") |
| 36 | + print(" Exemplars:") |
| 37 | + for exemplar in point.exemplars { |
| 38 | + print(" - EpochNanos: \(exemplar.epochNanos)") |
| 39 | + if let ctx = exemplar.spanContext { |
| 40 | + print(" SpanContext: \(ctx)") |
| 41 | + } |
| 42 | + print(" FilteredAttributes: \(exemplar.filteredAttributes)") |
| 43 | + if let e = exemplar as? DoubleExemplarData { |
| 44 | + print(" Value: \(e.value)") |
| 45 | + } |
| 46 | + if let e = exemplar as? LongExemplarData { |
| 47 | + print(" Value: \(e.value)") |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + print(String(repeating: "-", count: 40) + "\n") |
| 53 | + } |
| 54 | + } else { |
| 55 | + let jsonEncoder = JSONEncoder() |
| 56 | + for metric in metrics { |
| 57 | + do { |
| 58 | + let jsonData = try jsonEncoder.encode(metric) |
| 59 | + if let jsonString = String(data: jsonData, encoding: .utf8) { |
| 60 | + print(jsonString) |
| 61 | + } |
| 62 | + } catch { |
| 63 | + print("Failed to serialize Metric as JSON: \(error)") |
| 64 | + return .failure |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return .success |
| 70 | + } |
| 71 | + |
| 72 | + public func flush() -> OpenTelemetrySdk.ExportResult { |
| 73 | + return .success |
| 74 | + } |
| 75 | + |
| 76 | + public func shutdown() -> OpenTelemetrySdk.ExportResult { |
| 77 | + return .success |
| 78 | + } |
| 79 | + |
| 80 | + public func getAggregationTemporality(for instrument: OpenTelemetrySdk.InstrumentType) -> OpenTelemetrySdk.AggregationTemporality { |
| 81 | + return aggregationTemporalitySelector.getAggregationTemporality(for: instrument) |
| 82 | + } |
| 83 | +} |
0 commit comments