MediaWiki master
Wikimedia\Stats\Metrics\TimingMetric Class Reference

Timing Metric Implementation. More...

Inherits Wikimedia\Stats\Metrics\MetricInterface.

Collaboration diagram for Wikimedia\Stats\Metrics\TimingMetric:

Public Member Functions

 getTypeIndicator ()
 
Returns
string

 observe (float $milliseconds)
 Record a previously calculated observation in milliseconds.
 observeNanoseconds (float $nanoseconds)
 Record a previously calculated observation in nanoseconds.
 observeSeconds (float $seconds)
 Record a previously calculated observation in seconds.
 start ()
 Start the timer.
 stop ()
 Stop the running timer.
Public Member Functions inherited from Wikimedia\Stats\Metrics\MetricInterface
 __construct (BaseMetricInterface $baseMetric, LoggerInterface $logger)
 copyToStatsdAt ( $statsdNamespaces)
 This function existed to support the Graphite->Prometheus transition and is no longer needed.
 fresh ()
 Returns metric with cleared labels.
 getComponent ()
 getLabelKeys ()
 Returns the list of defined label keys.
 getName ()
 getSampleCount ()
 Returns a count of samples recorded by the metric.
 getSampleRate ()
 getSamples ()
 Returns subset of samples corresponding to sample rate setting.
 isHistogram ()
 Indicates the metric instance is used in a Histogram.
 setLabel (string $key, string $value)
 Adds a label $key with $value.
 setLabels (array $labels)
 Convenience function to set a number of labels at once.
 setSampleRate (float $sampleRate)
 Sets sample rate on a new metric instance.

Detailed Description

Timing Metric Implementation.

Timing metrics track duration data which can be broken into histograms. They are identified by type "ms".

Author
Cole White
Since
1.38

Definition at line 24 of file TimingMetric.php.

Member Function Documentation

◆ getTypeIndicator()

Wikimedia\Stats\Metrics\TimingMetric::getTypeIndicator ( )

Returns
string

Implements Wikimedia\Stats\Metrics\MetricInterface.

Definition at line 164 of file TimingMetric.php.

References return.

◆ observe()

Wikimedia\Stats\Metrics\TimingMetric::observe ( float $milliseconds)

Record a previously calculated observation in milliseconds.

NOTE: You MUST pass values converted to milliseconds.

This method is discouraged in new code, because PHP does not measure time in milliseconds. It will be less error-prone if you use start() and stop(), or pass values from hrtime() directly to observeNanoseconds() without manual multiplication to another unit.

Deprecated
since 1.45 Use TimingMetric::start instead, or switch to hrtime() and use TimingMetric::observeNanoseconds.
Parameters
float$milliseconds
Returns
void

Definition at line 150 of file TimingMetric.php.

◆ observeNanoseconds()

Wikimedia\Stats\Metrics\TimingMetric::observeNanoseconds ( float $nanoseconds)

Record a previously calculated observation in nanoseconds.

It is recommended to use TimingMetric::start() and RunningTimer::stop() instead.

Only measure latency yourself if you also need the duration value elsewhere.

Example:

$startTime = ConvertibleTimestamp::hrtime( true );
# work to be measured...
$durationNano = ConvertibleTimestamp::hrtime( true ) - $startTime;
$metric->observeNanoseconds( $durationNano );
$durationMs = $durationNano / 1e6;
$durationSec = $durationNano / 1e9;
Parameters
float$nanoseconds
Returns
void
Since
1.43

Definition at line 106 of file TimingMetric.php.

◆ observeSeconds()

Wikimedia\Stats\Metrics\TimingMetric::observeSeconds ( float $seconds)

Record a previously calculated observation in seconds.

This method is provided to ease recording of externally-generated time values. For example, when a service returns a delta in seconds to you, and you are not measuring or multiplying this value yourself.

To instrument your own code, it is recommended to use TimingMetric::start() and RunningTimer::stop() instead. Or, if measuring by hand, use hrtime() with observeNanoseconds() to guarantee a monotonic clock and not a wall-clock.

Do not measure latency with time() or microtime(), per T245464.

NOTE: If you previously used observeSeconds to store non-time values in a histogram, such as kilobytes or other unrelated quantities, use StatsFactory::getHistogram instead (T348796, T364240, T383208).

Parameters
float$seconds
Returns
void
Since
1.43

Definition at line 131 of file TimingMetric.php.

◆ start()

Wikimedia\Stats\Metrics\TimingMetric::start ( )

Start the timer.

Example:

$timer = StatsFactory->getTiming( 'example_seconds' )
->setLabel( 'foo', 'bar' )
->start();
# work to be measured...
$timer->stop();
This is the primary interface for validating metrics definitions, caching defined metrics,...
getTiming(string $name)
Makes a new TimingMetric or fetches one from cache.

Example with an extra label informed by the work:

$timer = StatsFactory->getTiming( 'example_seconds' )
->start();
# work to be measured...
$timer
->setLabel( 'status', $status->isOK() ? 'ok' : 'error' )
->stop();
Returns
RunningTimer

Definition at line 62 of file TimingMetric.php.

◆ stop()

Wikimedia\Stats\Metrics\TimingMetric::stop ( )

Stop the running timer.

Deprecated
since 1.45 Call RunningTimer::stop on the object returned by start() instead.

Definition at line 72 of file TimingMetric.php.

References if.


The documentation for this class was generated from the following file: