Skip to content

Commit

Permalink
Don't require to pass a registry on MetricsFactory initialization (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianfett authored Sep 19, 2023
1 parent 77465d7 commit 26e2b29
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
40 changes: 24 additions & 16 deletions Sources/Prometheus/PrometheusMetricsFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ import CoreMetrics

/// A wrapper around ``PrometheusCollectorRegistry`` to implement the `swift-metrics` `MetricsFactory` protocol
public struct PrometheusMetricsFactory: Sendable {
/// The underlying ``PrometheusCollectorRegistry`` that is used to generate
public var client: PrometheusCollectorRegistry
private static let _defaultRegistry = PrometheusCollectorRegistry()

/// The default ``PrometheusCollectorRegistry``, which is used inside the ``PrometheusMetricsFactory``
/// if no other is provided in ``init(client:)`` or set via ``PrometheusMetricsFactory/client``
public static var defaultRegistry: PrometheusCollectorRegistry {
self._defaultRegistry
}

/// The underlying ``PrometheusCollectorRegistry`` that is used to generate the swift-metrics handlers
public var registry: PrometheusCollectorRegistry

/// The default histogram buckets for a ``TimeHistogram``. If there is no explicit overwrite
/// via ``timeHistogramBuckets``, the buckets provided here will be used for any new
Expand All @@ -39,8 +47,8 @@ public struct PrometheusMetricsFactory: Sendable {
/// to overwrite the Metric names in third party packages.
public var labelAndDimensionSanitizer: @Sendable (_ label: String, _ dimensions: [(String, String)]) -> (String, [(String, String)])

public init(client: PrometheusCollectorRegistry) {
self.client = client
public init(client: PrometheusCollectorRegistry = Self.defaultRegistry) {
self.registry = client

self.timeHistogramBuckets = [:]
self.defaultTimeHistogramBuckets = [
Expand Down Expand Up @@ -79,54 +87,54 @@ public struct PrometheusMetricsFactory: Sendable {
extension PrometheusMetricsFactory: CoreMetrics.MetricsFactory {
public func makeCounter(label: String, dimensions: [(String, String)]) -> CoreMetrics.CounterHandler {
let (label, dimensions) = self.labelAndDimensionSanitizer(label, dimensions)
return self.client.makeCounter(name: label, labels: dimensions)
return self.registry.makeCounter(name: label, labels: dimensions)
}

public func makeFloatingPointCounter(label: String, dimensions: [(String, String)]) -> FloatingPointCounterHandler {
let (label, dimensions) = self.labelAndDimensionSanitizer(label, dimensions)
return self.client.makeCounter(name: label, labels: dimensions)
return self.registry.makeCounter(name: label, labels: dimensions)
}

public func makeRecorder(label: String, dimensions: [(String, String)], aggregate: Bool) -> CoreMetrics.RecorderHandler {
let (label, dimensions) = self.labelAndDimensionSanitizer(label, dimensions)
if aggregate {
let buckets = self.valueHistogramBuckets[label] ?? self.defaultValueHistogramBuckets
return self.client.makeValueHistogram(name: label, labels: dimensions, buckets: buckets)
return self.registry.makeValueHistogram(name: label, labels: dimensions, buckets: buckets)
} else {
return self.client.makeGauge(name: label, labels: dimensions)
return self.registry.makeGauge(name: label, labels: dimensions)
}
}

public func makeMeter(label: String, dimensions: [(String, String)]) -> CoreMetrics.MeterHandler {
return self.client.makeGauge(name: label, labels: dimensions)
return self.registry.makeGauge(name: label, labels: dimensions)
}

public func makeTimer(label: String, dimensions: [(String, String)]) -> CoreMetrics.TimerHandler {
let (label, dimensions) = self.labelAndDimensionSanitizer(label, dimensions)
let buckets = self.timeHistogramBuckets[label] ?? self.defaultTimeHistogramBuckets
return self.client.makeDurationHistogram(name: label, labels: dimensions, buckets: buckets)
return self.registry.makeDurationHistogram(name: label, labels: dimensions, buckets: buckets)
}

public func destroyCounter(_ handler: CoreMetrics.CounterHandler) {
guard let counter = handler as? Counter else {
return
}
self.client.destroyCounter(counter)
self.registry.destroyCounter(counter)
}

public func destroyFloatingPointCounter(_ handler: FloatingPointCounterHandler) {
guard let counter = handler as? Counter else {
return
}
self.client.destroyCounter(counter)
self.registry.destroyCounter(counter)
}

public func destroyRecorder(_ handler: CoreMetrics.RecorderHandler) {
switch handler {
case let gauge as Gauge:
self.client.destroyGauge(gauge)
self.registry.destroyGauge(gauge)
case let histogram as Histogram<Double>:
self.client.destroyValueHistogram(histogram)
self.registry.destroyValueHistogram(histogram)
default:
break
}
Expand All @@ -136,13 +144,13 @@ extension PrometheusMetricsFactory: CoreMetrics.MetricsFactory {
guard let gauge = handler as? Gauge else {
return
}
self.client.destroyGauge(gauge)
self.registry.destroyGauge(gauge)
}

public func destroyTimer(_ handler: CoreMetrics.TimerHandler) {
guard let histogram = handler as? Histogram<Duration> else {
return
}
self.client.destroyTimeHistogram(histogram)
self.registry.destroyTimeHistogram(histogram)
}
}
6 changes: 6 additions & 0 deletions Tests/PrometheusTests/PrometheusMetricsFactoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,10 @@ final class PrometheusMetricsFactoryTests: XCTestCase {
)
}

func testTwoMetricFactoriesUseTheSameUnderlyingCollectorRegsitry() {
let first = PrometheusMetricsFactory()
let second = PrometheusMetricsFactory()

XCTAssert(first.registry === second.registry)
}
}

0 comments on commit 26e2b29

Please sign in to comment.