Skip to content
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

TimerHandler implementation always records nanoseconds in Histogram #70

Open
t089 opened this issue Jun 20, 2022 · 0 comments
Open

TimerHandler implementation always records nanoseconds in Histogram #70

t089 opened this issue Jun 20, 2022 · 0 comments

Comments

@t089
Copy link

t089 commented Jun 20, 2022

I am a bit confused about how to use the Histogram style of metrics successfully with swift-metrics.

When interacting with the Timer interface from swift-metrics you can specify the unit of your time measurements. But when configuring PrometheusMetricsFactory I cannot specify the unit of my histogram buckets. If I use the default values ([0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10]), most measurements will end up in the +Inf bucket, because internally the MetricsHistogramTimer always records nanoseconds. I think the only way to make it work right know would be to change the buckets to be in ns, [0.005e9, 0.01e9, 0.025e9, 0.05e9, 0.1e9, 0.25e9, 0.5e9, 1e9, 2.5e9, 5e9, 10e9] which seems quite unintuitive.

Probably it would make sense to default to seconds for the unit and change the MetricsHistogramTimer implementation to convert the nanoseconds to seconds. Since this is a global setting it will apply to ALL "timer" metrics emitted by any library in the process. So maybe this should be part of the TimerImplementation configuration.

Example

let prometheusClient = PrometheusClient()
MetricsSystem.bootstrap(PrometheusMetricsFactory(client: prometheusClient,
                                                 configuration: .init( timerImplementation: .histogram())))

let timer = Timer(label: "my_timer")

timer.record(.seconds(5))

prometheusClient.collect { (metrics : String) -> () in
    print(metrics)
}
# TYPE startup_time histogram
startup_time_bucket{le="0.005"} 0
startup_time_bucket{le="0.01"} 0
startup_time_bucket{le="0.025"} 0
startup_time_bucket{le="0.05"} 0
startup_time_bucket{le="0.1"} 0
startup_time_bucket{le="0.25"} 0
startup_time_bucket{le="0.5"} 0
startup_time_bucket{le="1.0"} 0
startup_time_bucket{le="2.5"} 0
startup_time_bucket{le="5.0"} 0
startup_time_bucket{le="10.0"} 0
startup_time_bucket{le="+Inf"} 1
startup_time_count 1
startup_time_sum 5000000000
  • Swift version: 5.6.1
  • Framework version: 1.0.0
  • swift-metric: 2.3.1
@t089 t089 changed the title TimerHander implementation always records nanoseconds in Histogram TimerHandler implementation always records nanoseconds in Histogram Jun 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants