swift-server / swift-prometheus

Prometheus client library for Swift

Home Page:https://swiftpackageindex.com/swift-server/swift-prometheus

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

False prometheusNotBoostrapped error with multiplex handler

Tyler-Keith-Thompson opened this issue · comments

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

  • OS version:
  • Swift version:
  • Serverside Swift Framework:
  • Framework version:

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.