From ac8d3d951bc6c812209788e13a49e7cc238352c3 Mon Sep 17 00:00:00 2001 From: Trevor Jones Date: Fri, 11 Mar 2022 16:15:16 -0500 Subject: [PATCH] Updated metrics adapter for new spec --- .../metric/MetricsAdapter.swift | 104 ++++++++++-------- .../OtlpMetricExporterTests.swift | 6 +- 2 files changed, 59 insertions(+), 51 deletions(-) diff --git a/Sources/Exporters/OpenTelemetryProtocol/metric/MetricsAdapter.swift b/Sources/Exporters/OpenTelemetryProtocol/metric/MetricsAdapter.swift index fccf1701..fbcc9783 100644 --- a/Sources/Exporters/OpenTelemetryProtocol/metric/MetricsAdapter.swift +++ b/Sources/Exporters/OpenTelemetryProtocol/metric/MetricsAdapter.swift @@ -54,60 +54,63 @@ struct MetricsAdapter { guard let gaugeData = $0 as? SumData else { break } - var protoDataPoint = Opentelemetry_Proto_Metrics_V1_DoubleDataPoint() + var protoDataPoint = Opentelemetry_Proto_Metrics_V1_NumberDataPoint() protoDataPoint.timeUnixNano = gaugeData.timestamp.timeIntervalSince1970.toNanoseconds protoDataPoint.startTimeUnixNano = gaugeData.startTimestamp.timeIntervalSince1970.toNanoseconds - protoDataPoint.value = gaugeData.sum + protoDataPoint.asDouble = gaugeData.sum gaugeData.labels.forEach { - var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue() + var kvp = Opentelemetry_Proto_Common_V1_KeyValue() kvp.key = $0.key - kvp.value = $0.value - protoDataPoint.labels.append(kvp) + kvp.value = Opentelemetry_Proto_Common_V1_AnyValue() + kvp.value.stringValue = $0.value + protoDataPoint.attributes.append(kvp) } - protoMetric.doubleGauge.dataPoints.append(protoDataPoint) + protoMetric.gauge.dataPoints.append(protoDataPoint) case .intGauge: guard let gaugeData = $0 as? SumData else { break } - var protoDataPoint = Opentelemetry_Proto_Metrics_V1_IntDataPoint() + var protoDataPoint = Opentelemetry_Proto_Metrics_V1_NumberDataPoint() - protoDataPoint.value = Int64(gaugeData.sum) + protoDataPoint.asInt = Int64(gaugeData.sum) protoDataPoint.timeUnixNano = gaugeData.timestamp.timeIntervalSince1970.toNanoseconds protoDataPoint.startTimeUnixNano = gaugeData.startTimestamp.timeIntervalSince1970.toNanoseconds gaugeData.labels.forEach { - var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue() + var kvp = Opentelemetry_Proto_Common_V1_KeyValue() kvp.key = $0.key - kvp.value = $0.value - protoDataPoint.labels.append(kvp) + kvp.value = Opentelemetry_Proto_Common_V1_AnyValue() + kvp.value.stringValue = $0.value + protoDataPoint.attributes.append(kvp) } - protoMetric.intGauge.dataPoints.append(protoDataPoint) + protoMetric.gauge.dataPoints.append(protoDataPoint) case .doubleSum: guard let sumData = $0 as? SumData else { break } - var protoDataPoint = Opentelemetry_Proto_Metrics_V1_DoubleDataPoint() - protoDataPoint.value = sumData.sum + var protoDataPoint = Opentelemetry_Proto_Metrics_V1_NumberDataPoint() + protoDataPoint.asDouble = sumData.sum protoDataPoint.timeUnixNano = sumData.timestamp.timeIntervalSince1970.toNanoseconds protoDataPoint.startTimeUnixNano = sumData.startTimestamp.timeIntervalSince1970.toNanoseconds sumData.labels.forEach { - var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue() + var kvp = Opentelemetry_Proto_Common_V1_KeyValue() kvp.key = $0.key - kvp.value = $0.value - protoDataPoint.labels.append(kvp) + kvp.value = Opentelemetry_Proto_Common_V1_AnyValue() + kvp.value.stringValue = $0.value + protoDataPoint.attributes.append(kvp) } - protoMetric.doubleSum.aggregationTemporality = .cumulative - protoMetric.doubleSum.dataPoints.append(protoDataPoint) + protoMetric.sum.aggregationTemporality = .cumulative + protoMetric.sum.dataPoints.append(protoDataPoint) case .doubleSummary: guard let summaryData = $0 as? SummaryData else { break } - var protoDataPoint = Opentelemetry_Proto_Metrics_V1_DoubleSummaryDataPoint() + var protoDataPoint = Opentelemetry_Proto_Metrics_V1_SummaryDataPoint() protoDataPoint.sum = summaryData.sum protoDataPoint.count = UInt64(summaryData.count) @@ -115,53 +118,56 @@ struct MetricsAdapter { protoDataPoint.timeUnixNano = summaryData.timestamp.timeIntervalSince1970.toNanoseconds summaryData.labels.forEach { - var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue() + var kvp = Opentelemetry_Proto_Common_V1_KeyValue() kvp.key = $0.key - kvp.value = $0.value - protoDataPoint.labels.append(kvp) + kvp.value = Opentelemetry_Proto_Common_V1_AnyValue() + kvp.value.stringValue = $0.value + protoDataPoint.attributes.append(kvp) } - protoMetric.doubleSummary.dataPoints.append(protoDataPoint) + protoMetric.summary.dataPoints.append(protoDataPoint) case .intSum: guard let sumData = $0 as? SumData else { break } - var protoDataPoint = Opentelemetry_Proto_Metrics_V1_IntDataPoint() - protoDataPoint.value = Int64(sumData.sum) + var protoDataPoint = Opentelemetry_Proto_Metrics_V1_NumberDataPoint() + protoDataPoint.asInt = Int64(sumData.sum) protoDataPoint.timeUnixNano = sumData.timestamp.timeIntervalSince1970.toNanoseconds protoDataPoint.startTimeUnixNano = sumData.startTimestamp.timeIntervalSince1970.toNanoseconds sumData.labels.forEach { - var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue() + var kvp = Opentelemetry_Proto_Common_V1_KeyValue() kvp.key = $0.key - kvp.value = $0.value - protoDataPoint.labels.append(kvp) + kvp.value = Opentelemetry_Proto_Common_V1_AnyValue() + kvp.value.stringValue = $0.value + protoDataPoint.attributes.append(kvp) } - protoMetric.intSum.aggregationTemporality = .cumulative - protoMetric.intSum.dataPoints.append(protoDataPoint) + protoMetric.sum.aggregationTemporality = .cumulative + protoMetric.sum.dataPoints.append(protoDataPoint) case .intSummary: guard let summaryData = $0 as? SummaryData else { break } - var protoDataPoint = Opentelemetry_Proto_Metrics_V1_DoubleSummaryDataPoint() + var protoDataPoint = Opentelemetry_Proto_Metrics_V1_SummaryDataPoint() protoDataPoint.sum = Double(summaryData.sum) protoDataPoint.count = UInt64(summaryData.count) protoDataPoint.startTimeUnixNano = summaryData.startTimestamp.timeIntervalSince1970.toNanoseconds protoDataPoint.timeUnixNano = summaryData.timestamp.timeIntervalSince1970.toNanoseconds summaryData.labels.forEach { - var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue() + var kvp = Opentelemetry_Proto_Common_V1_KeyValue() kvp.key = $0.key - kvp.value = $0.value - protoDataPoint.labels.append(kvp) + kvp.value = Opentelemetry_Proto_Common_V1_AnyValue() + kvp.value.stringValue = $0.value + protoDataPoint.attributes.append(kvp) } - protoMetric.doubleSummary.dataPoints.append(protoDataPoint) + protoMetric.summary.dataPoints.append(protoDataPoint) case .intHistogram: guard let histogramData = $0 as? HistogramData else { break } - var protoDataPoint = Opentelemetry_Proto_Metrics_V1_DoubleHistogramDataPoint() + var protoDataPoint = Opentelemetry_Proto_Metrics_V1_HistogramDataPoint() protoDataPoint.sum = Double(histogramData.sum) protoDataPoint.count = UInt64(histogramData.count) protoDataPoint.startTimeUnixNano = histogramData.startTimestamp.timeIntervalSince1970.toNanoseconds @@ -170,19 +176,20 @@ struct MetricsAdapter { protoDataPoint.bucketCounts = histogramData.buckets.counts.map { UInt64($0) } histogramData.labels.forEach { - var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue() + var kvp = Opentelemetry_Proto_Common_V1_KeyValue() kvp.key = $0.key - kvp.value = $0.value - protoDataPoint.labels.append(kvp) + kvp.value = Opentelemetry_Proto_Common_V1_AnyValue() + kvp.value.stringValue = $0.value + protoDataPoint.attributes.append(kvp) } - protoMetric.doubleHistogram.aggregationTemporality = .cumulative - protoMetric.doubleHistogram.dataPoints.append(protoDataPoint) + protoMetric.histogram.aggregationTemporality = .cumulative + protoMetric.histogram.dataPoints.append(protoDataPoint) case .doubleHistogram: guard let histogramData = $0 as? HistogramData else { break } - var protoDataPoint = Opentelemetry_Proto_Metrics_V1_DoubleHistogramDataPoint() + var protoDataPoint = Opentelemetry_Proto_Metrics_V1_HistogramDataPoint() protoDataPoint.sum = Double(histogramData.sum) protoDataPoint.count = UInt64(histogramData.count) protoDataPoint.startTimeUnixNano = histogramData.startTimestamp.timeIntervalSince1970.toNanoseconds @@ -191,14 +198,15 @@ struct MetricsAdapter { protoDataPoint.bucketCounts = histogramData.buckets.counts.map { UInt64($0) } histogramData.labels.forEach { - var kvp = Opentelemetry_Proto_Common_V1_StringKeyValue() + var kvp = Opentelemetry_Proto_Common_V1_KeyValue() kvp.key = $0.key - kvp.value = $0.value - protoDataPoint.labels.append(kvp) + kvp.value = Opentelemetry_Proto_Common_V1_AnyValue() + kvp.value.stringValue = $0.value + protoDataPoint.attributes.append(kvp) } - protoMetric.doubleHistogram.aggregationTemporality = .cumulative - protoMetric.doubleHistogram.dataPoints.append(protoDataPoint) + protoMetric.histogram.aggregationTemporality = .cumulative + protoMetric.histogram.dataPoints.append(protoDataPoint) } } return protoMetric diff --git a/Tests/ExportersTests/OpenTelemetryProtocol/OtlpMetricExporterTests.swift b/Tests/ExportersTests/OpenTelemetryProtocol/OtlpMetricExporterTests.swift index 901b35ed..12b6be02 100644 --- a/Tests/ExportersTests/OpenTelemetryProtocol/OtlpMetricExporterTests.swift +++ b/Tests/ExportersTests/OpenTelemetryProtocol/OtlpMetricExporterTests.swift @@ -49,12 +49,12 @@ class OtlpMetricExproterTests: XCTestCase { XCTAssertEqual(fakeCollector.receivedMetrics.count, 1) let otlpMetric = fakeCollector.receivedMetrics[0].instrumentationLibraryMetrics[0].metrics[0] XCTAssertEqual(metric.name, otlpMetric.name) - XCTAssertEqual(otlpMetric.intGauge.dataPoints.count, 1) - let dataPoint = otlpMetric.intGauge.dataPoints[0] + XCTAssertEqual(otlpMetric.gauge.dataPoints.count, 1) + let dataPoint = otlpMetric.gauge.dataPoints[0] let sum = metric.data[0] as! SumData XCTAssertEqual(sum.timestamp.timeIntervalSince1970.toNanoseconds, dataPoint.timeUnixNano) XCTAssertEqual(sum.startTimestamp.timeIntervalSince1970.toNanoseconds, dataPoint.startTimeUnixNano) - XCTAssertEqual(sum.sum, Int(dataPoint.value)) + XCTAssertEqual(sum.sum, Int(dataPoint.asInt)) } func testExportMultipleMetrics() {