Skip to content

Commit

Permalink
Remove @_spi(Metrics) annotation from metrics API (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjbeaumont committed Apr 15, 2024
1 parent 0735233 commit 0380acd
Show file tree
Hide file tree
Showing 34 changed files with 107 additions and 233 deletions.
4 changes: 2 additions & 2 deletions Examples/Server/Sources/ServerExample/ServerExample.swift
Expand Up @@ -14,8 +14,8 @@
import Hummingbird
import Logging
import Metrics
@_spi(Metrics) import OTel
@_spi(Metrics) import OTLPGRPC
import OTel
import OTLPGRPC
import Tracing

@main
Expand Down
49 changes: 8 additions & 41 deletions Sources/OTLPCore/Metrics/OTelMetricDataModel+Proto.swift
Expand Up @@ -11,26 +11,23 @@
//
//===----------------------------------------------------------------------===//

@_spi(Metrics) import OTel
import OTel

@_spi(Metrics)
extension Opentelemetry_Proto_Metrics_V1_ResourceMetrics {
package init(_ resrouceMetrics: OTelResourceMetrics) {
if let resource = resrouceMetrics.resource {
package init(_ resourceMetrics: OTelResourceMetrics) {
if let resource = resourceMetrics.resource {
self.resource = .init(resource)
}
scopeMetrics = resrouceMetrics.scopeMetrics.map(Opentelemetry_Proto_Metrics_V1_ScopeMetrics.init)
scopeMetrics = resourceMetrics.scopeMetrics.map(Opentelemetry_Proto_Metrics_V1_ScopeMetrics.init)
}
}

@_spi(Metrics)
extension Opentelemetry_Proto_Resource_V1_Resource {
package init(_ resource: OTelResource) {
attributes = .init(resource.attributes)
}
}

@_spi(Metrics)
extension Opentelemetry_Proto_Metrics_V1_ScopeMetrics {
package init(_ scopeMetrics: OTelScopeMetrics) {
if let scope = scopeMetrics.scope {
Expand All @@ -40,7 +37,6 @@ extension Opentelemetry_Proto_Metrics_V1_ScopeMetrics {
}
}

@_spi(Metrics)
extension Opentelemetry_Proto_Common_V1_InstrumentationScope {
package init(_ instrumentationScope: OTelInstrumentationScope) {
if let name = instrumentationScope.name {
Expand All @@ -54,13 +50,12 @@ extension Opentelemetry_Proto_Common_V1_InstrumentationScope {
}
}

@_spi(Metrics)
extension Opentelemetry_Proto_Metrics_V1_Metric {
package init(_ metric: OTelMetricPoint) {
name = metric.name
description_p = metric.description
unit = metric.unit
switch metric.data {
switch metric.data.data {
case .gauge(let gauge):
self.gauge = .init(gauge)
case .sum(let sum):
Expand All @@ -71,14 +66,12 @@ extension Opentelemetry_Proto_Metrics_V1_Metric {
}
}

@_spi(Metrics)
extension Opentelemetry_Proto_Metrics_V1_Gauge {
package init(_ gauge: OTelGauge) {
dataPoints = .init(gauge.points)
}
}

@_spi(Metrics)
extension Opentelemetry_Proto_Metrics_V1_Sum {
package init(_ sum: OTelSum) {
aggregationTemporality = .init(sum.aggregationTemporality)
Expand All @@ -87,10 +80,9 @@ extension Opentelemetry_Proto_Metrics_V1_Sum {
}
}

@_spi(Metrics)
extension Opentelemetry_Proto_Metrics_V1_AggregationTemporality {
package init(_ aggregationTemporaility: OTelAggregationTemporailty) {
switch aggregationTemporaility {
package init(_ aggregationTemporality: OTelAggregationTemporality) {
switch aggregationTemporality.temporality {
case .cumulative:
self = .cumulative
case .delta:
Expand All @@ -99,83 +91,60 @@ extension Opentelemetry_Proto_Metrics_V1_AggregationTemporality {
}
}

@_spi(Metrics)
extension [Opentelemetry_Proto_Metrics_V1_NumberDataPoint] {
package init(_ points: [OTelNumberDataPoint]) {
self = points.map(Element.init)
}
}

@_spi(Metrics)
extension Opentelemetry_Proto_Metrics_V1_NumberDataPoint {
package init(_ point: OTelNumberDataPoint) {
attributes = .init(point.attributes)
if let startTime = point.startTimeNanosecondsSinceEpoch {
startTimeUnixNano = startTime
}
timeUnixNano = point.timeNanosecondsSinceEpoch
switch point.value {
switch point.value.value {
case .double(let value):
self.value = .asDouble(value)
case .int64(let value):
self.value = .asInt(value)
}
exemplars = .init(point.exemplars)
}
}

@_spi(Metrics)
extension [Opentelemetry_Proto_Common_V1_KeyValue] {
package init(_ attributes: [OTelAttribute]) {
self = attributes.map(Element.init)
}
}

@_spi(Metrics)
extension Opentelemetry_Proto_Common_V1_KeyValue {
package init(_ attribute: OTelAttribute) {
key = attribute.key
value = Opentelemetry_Proto_Common_V1_AnyValue(attribute.value)
}
}

@_spi(Metrics)
extension Opentelemetry_Proto_Common_V1_AnyValue {
package init(_ string: String) {
value = .stringValue(string)
}
}

@_spi(Metrics)
extension Opentelemetry_Proto_Metrics_V1_Exemplar {
package init(_ exemplar: OTelExemplar) {
// TODO:
}
}

@_spi(Metrics)
extension [Opentelemetry_Proto_Metrics_V1_Exemplar] {
package init(_ exemplars: [OTelExemplar]) {
self = exemplars.map(Element.init)
}
}

@_spi(Metrics)
extension Opentelemetry_Proto_Metrics_V1_Histogram {
package init(_ histogram: OTelHistogram) {
aggregationTemporality = .init(histogram.aggregationTemporality)
dataPoints = .init(histogram.points)
}
}

@_spi(Metrics)
extension [Opentelemetry_Proto_Metrics_V1_HistogramDataPoint] {
package init(_ points: [OTelHistogramDataPoint]) {
self = points.map(Element.init)
}
}

@_spi(Metrics)
extension Opentelemetry_Proto_Metrics_V1_HistogramDataPoint {
package init(_ point: OTelHistogramDataPoint) {
attributes = .init(point.attributes)
Expand All @@ -197,11 +166,9 @@ extension Opentelemetry_Proto_Metrics_V1_HistogramDataPoint {
bucketCounts.append(bucket.count)
explicitBounds.append(bucket.upperBound)
}
exemplars = .init(point.exemplars)
}
}

@_spi(Metrics)
extension [Opentelemetry_Proto_Metrics_V1_Metric] {
package init(_ points: [OTelMetricPoint]) {
self = points.map(Element.init)
Expand Down
5 changes: 2 additions & 3 deletions Sources/OTLPGRPC/Metrics/OTLPGRPCMetricExporter.swift
Expand Up @@ -16,11 +16,10 @@ import Logging
import NIO
import NIOHPACK
import NIOSSL
@_spi(Metrics) import OTel
@_spi(Metrics) import OTLPCore
import OTel
import OTLPCore

/// Exports metrics to an OTel collector using OTLP/gRPC.
@_spi(Metrics)
public final class OTLPGRPCMetricExporter: OTelMetricExporter {
private let configuration: OTLPGRPCMetricExporterConfiguration
private let connection: ClientConnection
Expand Down
Expand Up @@ -17,7 +17,6 @@ import OTel
/// Configuration for an ``OTLPGRPCMetricExporter``.
///
/// - TODO: This can probably be refactored to share a bunch of common logic with ``OTLPGRPCSpanExporterConfiguration``.
@_spi(Metrics)
public struct OTLPGRPCMetricExporterConfiguration: Sendable {
let endpoint: OTLPGRPCEndpoint
let headers: HPACKHeaders
Expand Down
3 changes: 1 addition & 2 deletions Sources/OTel/Metrics/MetricStore/OTelMetricRegistry.swift
Expand Up @@ -18,7 +18,6 @@ import struct NIOConcurrencyHelpers.NIOLockedValueBox
///
/// The registry owns the mapping from instrument identfier and attributes to the stateful instrument for recording
/// measurements.
@_spi(Metrics)
public final class OTelMetricRegistry: Sendable {
private let logger = Logger(label: "OTelMetricRegistry")

Expand Down Expand Up @@ -71,7 +70,7 @@ public final class OTelMetricRegistry: Sendable {
public static let crash = Self(behavior: .crash)
}

internal init(duplicateRegistrationHandler: some DuplicateRegistrationHandler) {
init(duplicateRegistrationHandler: some DuplicateRegistrationHandler) {
self.storage = .init(Storage(duplicateRegistrationHandler: duplicateRegistrationHandler))
}

Expand Down
Expand Up @@ -35,9 +35,7 @@ extension Counter: OTelMetricInstrument {
points: [.init(
attributes: attributes.map { OTelAttribute(key: $0.key, value: $0.value) },
timeNanosecondsSinceEpoch: instant.nanosecondsSinceEpoch,
value: .int64(value),
exemplars: [],
flags: []
value: .int64(value)
)],
aggregationTemporality: .cumulative,
monotonic: true
Expand Down
Expand Up @@ -35,9 +35,7 @@ extension FloatingPointCounter: OTelMetricInstrument {
points: [.init(
attributes: attributes.map { OTelAttribute(key: $0.key, value: $0.value) },
timeNanosecondsSinceEpoch: instant.nanosecondsSinceEpoch,
value: .double(value),
exemplars: [],
flags: []
value: .double(value)
)],
aggregationTemporality: .cumulative,
monotonic: true
Expand Down
Expand Up @@ -34,9 +34,7 @@ extension Gauge: OTelMetricInstrument {
points: [.init(
attributes: attributes.map { OTelAttribute(key: $0.key, value: $0.value) },
timeNanosecondsSinceEpoch: instant.nanosecondsSinceEpoch,
value: .double(value),
exemplars: [],
flags: []
value: .double(value)
)]
))
)
Expand Down
Expand Up @@ -44,8 +44,7 @@ extension Histogram: OTelMetricInstrument {
.init(upperBound: $0.bound.bucketRepresentation, count: UInt64($0.count))
} + [
.init(upperBound: .infinity, count: UInt64(state.count)),
],
exemplars: []
]
)]
))
)
Expand Down
Expand Up @@ -12,7 +12,6 @@
//===----------------------------------------------------------------------===//

/// A metric exporter that logs metrics to the console for debugging.
@_spi(Metrics)
public struct OTelConsoleMetricExporter: OTelMetricExporter {
/// Create a new ``OTelConsoleMetricExporter``.
init() {}
Expand Down
Expand Up @@ -14,7 +14,6 @@
/// Exports a batch of metrics.
///
/// - Seealso: [OTel Specification for Metric Exporter](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/metrics/sdk.md#metricexporter)
@_spi(Metrics)
public protocol OTelMetricExporter: Sendable {
/// Export the given batch of metrics.
///
Expand All @@ -33,7 +32,6 @@ public protocol OTelMetricExporter: Sendable {
}

/// An error indicating that an exporter has already been shut down but has been asked to export a batch of metrics.
@_spi(Metrics)
public struct OTelMetricExporterAlreadyShutDownError: Error {
package init() {}
}
Expand Up @@ -12,7 +12,6 @@
//===----------------------------------------------------------------------===//

/// A metric exporter that delegates to multiple other exports.
@_spi(Metrics)
public struct OTelMultiplexMetricExporter: OTelMetricExporter {
private let exporters: [any OTelMetricExporter]

Expand Down

This file was deleted.

0 comments on commit 0380acd

Please sign in to comment.