Skip to content

TimerHandler implementation always records nanoseconds in Histogram #70

Open
@t089

Description

@t089

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

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