swift-server / swift-prometheus

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

False prometheusNotBoostrapped error with multiplex handler #86

Open Tyler-Keith-Thompson opened 1 year ago

Tyler-Keith-Thompson commented 1 year ago

Steps to reproduce

Prometheus makes a few too many assumptions about my metrics system factory. I use the multiplex factory that comes from Apple and bootstrap prometheus with it, but then this code tries to cast the multiplexed factory and fails:

guard let prom = self.factory as? PrometheusWrappedMetricsFactory else {
    throw PrometheusError.prometheusFactoryNotBootstrapped(bootstrappedWith: "\(self.factory)")
}
return prom.client

Expected behavior

No error is thrown...cause it was in fact bootstrapped

Actual behavior

It throws the error

Environment

Tyler-Keith-Thompson commented 1 year ago

My temporary workaround:

struct PrometheusClientKey: StorageKey {
    typealias Value = PrometheusClient
}

extension Application {
    var prometheusClient: PrometheusClient? {
        get { storage[PrometheusClientKey.self] }
        set { storage[PrometheusClientKey.self] = newValue }
    }
}

I can just attach the client to the application. It's probably suboptimal, but fits my use-case for now.