Example #1
0
        /// <summary>
        /// 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.
        /// </summary>
        public void Stop()
        {
            if (disabled)
            {
                return;
            }

            ulong stopTime = HighPrecisionTimestamp.GetTimestamp(TimeUnit.Nanosecond);

            Dispatchers.LaunchAPI(() => {
                LibGleanFFI.glean_timespan_set_stop(handle, stopTime);
            });
        }
        /// <summary>
        /// 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 `StopAndAccumulate`): in that case the original start time will
        /// be preserved.
        /// </summary>
        /// <returns>
        /// The `GleanTimerId` object to associate with this timing or `null`
        /// if the collection was disabled.
        /// </returns>
        public GleanTimerId Start()
        {
            if (disabled)
            {
                return(null);
            }

            // Even though the Rust code for `Start` runs synchronously, the Rust
            // code for `StopAndAccumulate` runs asynchronously, and we need to use the same
            // clock for start and stop. Therefore we take the time on the C# side, both
            // here and in `StopAndAccumulate`.
            ulong startTime = HighPrecisionTimestamp.GetTimestamp(TimeUnit.Nanosecond);

            // No dispatcher, we need the return value
            return((GleanTimerId)LibGleanFFI.glean_timing_distribution_set_start(handle, startTime));
        }
        /// <summary>
        /// Stop tracking time for the provided metric and associated timer id. Add a
        /// count to the corresponding bucket in the timing distribution.
        /// This will record an error if no `Start` was called.
        /// </summary>
        /// <param name="timerId">
        /// The `GleanTimerId` associated with this timing.  This allows for concurrent
        /// timing of events associated with different ids to the same timing distribution.
        /// </param>
        public void StopAndAccumulate(GleanTimerId timerId)
        {
            // `Start` might return null.
            // Accepting that means users of this API don't need to do a null check.
            if (disabled || timerId == null)
            {
                return;
            }

            // The Rust code runs async and might be delayed. We need the time as precise as possible.
            // We also need the same clock for start and stop (`Start` takes the time on the C# side).
            ulong stopTime = HighPrecisionTimestamp.GetTimestamp(TimeUnit.Nanosecond);

            Dispatchers.LaunchAPI(() =>
            {
                LibGleanFFI.glean_timing_distribution_set_stop_and_accumulate(
                    handle,
                    timerId,
                    stopTime
                    );
            });
        }