glean / mozilla.telemetry.glean.private / TimespanMetricType


class TimespanMetricType

This implements the developer facing API for recording timespans.

Instances of this class type are automatically generated by the parsers at build time, allowing developers to record values that were previously registered in the metrics.yaml file.

The timespans API exposes the start, stop and cancel methods.



TimespanMetricType(disabled: Boolean, category: String, lifetime: Lifetime, name: String, sendInPings: List<String>, timeUnit: TimeUnit = TimeUnit.Minute)

The public constructor used by automatically generated metrics.



fun cancel(): Unit

Abort a previous start call. No error is recorded if no start was called.


fun <U> measure(funcToMeasure: () -> U): U

Convenience method to simplify measuring a function or block of code


fun setRawNanos(elapsedNanos: Long): Unit

Explicitly set the timespan value, in nanoseconds.


fun start(): Unit

Start tracking time for the provided metric. This records an error if it’s already tracking time (i.e. start was already called with no corresponding stop): in that case the original start time will be preserved.


fun stop(): Unit

Stop tracking time for the provided metric. Sets the metric to the elapsed time, but does not overwrite an already existing value. This will record an error if no start was called or there is an already existing value.


fun testGetNumRecordedErrors(errorType: ErrorType, pingName: String = sendInPings.first()): Int

Returns the number of errors recorded for the given metric.


fun testGetValue(pingName: String = sendInPings.first()): Long

Returns the stored value for testing purposes only


fun testHasValue(pingName: String = sendInPings.first()): Boolean

Tests whether a value is stored for the metric for testing purposes only