swift-server / swift-prometheus

Prometheus client library for Swift
https://swiftpackageindex.com/swift-server/swift-prometheus
Apache License 2.0
142 stars 30 forks source link

defaultRecorderBuckets not respected with swift-metrics #74

Open Sherlouk opened 2 years ago

Sherlouk commented 2 years ago

Steps to reproduce

Setup Prometheus and Swift Metrics:

        let client = PrometheusClient()

        // Setup Factory
        let factory = PrometheusMetricsFactory(client: client, configuration: .init(defaultRecorderBuckets: [
            0.05, 0.1, 0.5, 1, 5, 10, 25, 50, 75, 100
        ]))

        MetricsSystem.bootstrap(factory)

        // Collect Metrics
        app.get("metrics") { _ async throws -> String in
            return await client.collect()
        }

Create a Recorder and record a value:

Recorder(label: "x").record(100)

View output and see that bucket sizes are the default bucket sizes rather than the ones I've provided in the setup.

Expected behavior

Bucket sizes should be those which I provided to the factory

Actual behavior

Bucket sizes are instead the defaulted bucket sizes hard coded into the library

Environment

Solution

The root cause appears to be that the makeHistogram function calls createHistogram which has a variable called buckets: Buckets = .defaultBuckets. This defaults to defaultBuckets which is incorrect, it should instead be set to configuration.defaultRecorderBuckets (which itself defaults to defaultBuckets but can be overridden by the user, me, as above).