Skip to content

Commit

Permalink
Updated metrics adapter for new spec
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-dialpad committed Mar 14, 2022
1 parent 2240e6f commit ac8d3d9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 51 deletions.
104 changes: 56 additions & 48 deletions Sources/Exporters/OpenTelemetryProtocol/metric/MetricsAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,114 +54,120 @@ struct MetricsAdapter {
guard let gaugeData = $0 as? SumData<Double> 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<Int> 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<Double> 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<Double> 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)

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 .intSum:
guard let sumData = $0 as? SumData<Int> 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<Int> 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<Int> 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
Expand All @@ -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<Double> 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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>
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() {
Expand Down

0 comments on commit ac8d3d9

Please sign in to comment.