Skip to content

Awkward collection API #118

Open
Open
@ptoffy

Description

@ptoffy

I've switched from v1 to v2 and this was the switch I've had to make to collect metrics

- return try await MetricsSystem.prometheus().collect()
+ var buffer = [UInt8]()
+ (MetricsSystem.factory as? PrometheusMetricsFactory)?.registry.emit(into: &buffer)
+ return String(bytes: buffer, encoding: .utf8) ?? ""

which seems quite the downgrade in terms of usability.
It would be cool if the library provided some helpers to collect data.
I created this

extension MetricsSystem {
    static var prometheus: PrometheusMetricsFactory {
        get throws {
            guard let factory = Self.factory as? PrometheusMetricsFactory else {
                throw Abort(.internalServerError, reason: "Metrics factory is not Prometheus")
            }
            return factory
        }
    }
}

extension PrometheusMetricsFactory {
    func emit() -> String {
        var buffer = [UInt8]()
        self.registry.emit(into: &buffer)
        return String(bytes: buffer, encoding: .utf8) ?? ""
    }
}

to account for it and the result is quite a bit simpler

try MetricsSystem.prometheus.emit()

so I was wondering if a PR would be accepted with this or a similar addition, maybe a [UInt8] version too if needed, especially as the use case is often likely to be an endpoint which we just want to return the data too and we probably won't need a buffer. I'm happy to be proven wrong in case there's a simpler way already or a reason for it to just be like this!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions