Skip to content

Don't require to pass a registry on MetricsFactory initialization #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +27 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want this to be public?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It should! So that users can access what they got by default.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind I guess :)


/// 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)
}
}