MediaWiki  master
Wikimedia\Metrics\MetricsFactory Class Reference
Collaboration diagram for Wikimedia\Metrics\MetricsFactory:

Public Member Functions

 __construct (array $config, LoggerInterface $logger)
 MetricsFactory builds, configures, and caches Metrics. More...
 
 flush ()
 Send all buffered metrics to the target and destroy the cache. More...
 
 getCounter (array $config=[])
 Makes a new CounterMetric or fetches one from cache. More...
 
 getGauge (array $config=[])
 Makes a new GaugeMetric or fetches one from cache. More...
 
 getTiming (array $config=[])
 Makes a new TimingMetric or fetches one from cache. More...
 

Static Public Member Functions

static normalizeArray (array $entities)
 Normalize an array of strings. More...
 
static normalizeString (string $entity)
 Normalize strings to a metrics-compatible format. More...
 

Protected Member Functions

 send (UDPTransport $transport)
 Render the buffer of samples, group them into payloads, and send them through the provided UDPTransport instance. More...
 

Private Member Functions

 getCachedMetric (string $name, string $requested_type)
 Searches the cache for an instance of the requested metric. More...
 
 getFormattedName (string $name, string $extension)
 Get the metric formatted name. More...
 
 getRenderedSamples (array $cache)
 Get all rendered samples from cache. More...
 
 getValidConfig (array $config=[])
 Renders a valid configuration. More...
 

Private Attributes

array< CounterMetric|GaugeMetric|TimingMetric$cache = []
 
string $format
 
LoggerInterface $logger
 
string $prefix
 
string null $target
 

Detailed Description

Definition at line 40 of file MetricsFactory.php.

Constructor & Destructor Documentation

◆ __construct()

Wikimedia\Metrics\MetricsFactory::__construct ( array  $config,
LoggerInterface  $logger 
)

MetricsFactory builds, configures, and caches Metrics.

Parameters
array$configassociative array:
  • prefix (string): The prefix applied to all metrics. This could be the service name.
  • target (string): The URI of the statsd/statsd-exporter server.
  • format (string): The output format. See: MetricsFactory::SUPPORTED_OUTPUT_FORMATS
LoggerInterface$logger
Exceptions
UndefinedPrefixException
UnsupportedFormatException

Definition at line 84 of file MetricsFactory.php.

References Wikimedia\Metrics\MetricsFactory\$logger, and Wikimedia\Metrics\MetricsFactory\normalizeString().

Member Function Documentation

◆ flush()

Wikimedia\Metrics\MetricsFactory::flush ( )

Send all buffered metrics to the target and destroy the cache.

Definition at line 189 of file MetricsFactory.php.

References UDPTransport\newFromString(), and Wikimedia\Metrics\MetricsFactory\send().

◆ getCachedMetric()

Wikimedia\Metrics\MetricsFactory::getCachedMetric ( string  $name,
string  $requested_type 
)
private

Searches the cache for an instance of the requested metric.

Returns null if not found.

If the requested metric type does not match the metric found in cache, log the error and return a NullMetric instance. This is so that exceptions aren't thrown if metric names are reused as different types.

Parameters
string$name
string$requested_type
Returns
CounterMetric|GaugeMetric|TimingMetric|null
Exceptions
TypeError

Definition at line 224 of file MetricsFactory.php.

Referenced by Wikimedia\Metrics\MetricsFactory\getCounter(), Wikimedia\Metrics\MetricsFactory\getGauge(), and Wikimedia\Metrics\MetricsFactory\getTiming().

◆ getCounter()

Wikimedia\Metrics\MetricsFactory::getCounter ( array  $config = [])

Makes a new CounterMetric or fetches one from cache.

If a collision occurs, returns a NullMetric to suppress exceptions.

Parameters
array$configassociative array:
  • name: (string) The metric name
  • extension: (string) The extension generating the metric
  • labels: (array) List of metric dimensional instantiations for filters and aggregations
  • sampleRate: (float) Optional sampling rate to apply
Returns
CounterMetric|NullMetric

Definition at line 113 of file MetricsFactory.php.

References Wikimedia\Metrics\MetricsFactory\getCachedMetric(), Wikimedia\Metrics\MetricsFactory\getFormattedName(), and Wikimedia\Metrics\MetricsFactory\getValidConfig().

◆ getFormattedName()

Wikimedia\Metrics\MetricsFactory::getFormattedName ( string  $name,
string  $extension 
)
private

Get the metric formatted name.

Takes the provided name and constructs a more specific name by combining the service, extension, and name options.

Parameters
string$name
string$extension
Returns
string

Definition at line 274 of file MetricsFactory.php.

Referenced by Wikimedia\Metrics\MetricsFactory\getCounter(), Wikimedia\Metrics\MetricsFactory\getGauge(), and Wikimedia\Metrics\MetricsFactory\getTiming().

◆ getGauge()

Wikimedia\Metrics\MetricsFactory::getGauge ( array  $config = [])

Makes a new GaugeMetric or fetches one from cache.

If a collision occurs, returns a NullMetric to suppress exceptions.

Parameters
array$configassociative array: name: (string) The metric name. extension: (string) The extension generating the metric. labels: (array) Labels that further identify the metric.
Returns
GaugeMetric|NullMetric

Definition at line 141 of file MetricsFactory.php.

References Wikimedia\Metrics\MetricsFactory\getCachedMetric(), Wikimedia\Metrics\MetricsFactory\getFormattedName(), and Wikimedia\Metrics\MetricsFactory\getValidConfig().

◆ getRenderedSamples()

Wikimedia\Metrics\MetricsFactory::getRenderedSamples ( array  $cache)
private

Get all rendered samples from cache.

Parameters
array$cache
Returns
string[] Flattened list

Definition at line 202 of file MetricsFactory.php.

◆ getTiming()

Wikimedia\Metrics\MetricsFactory::getTiming ( array  $config = [])

Makes a new TimingMetric or fetches one from cache.

If a collision occurs, returns a NullMetric to suppress exceptions.

Parameters
array$configassociative array:
  • name: (string) The metric name
  • extension: (string) The extension generating the metric
  • labels: (array) List of metric dimensional instantiations for filters and aggregations
  • sampleRate: (float) Optional sampling rate to apply
Returns
TimingMetric|NullMetric

Definition at line 170 of file MetricsFactory.php.

References Wikimedia\Metrics\MetricsFactory\getCachedMetric(), Wikimedia\Metrics\MetricsFactory\getFormattedName(), and Wikimedia\Metrics\MetricsFactory\getValidConfig().

◆ getValidConfig()

Wikimedia\Metrics\MetricsFactory::getValidConfig ( array  $config = [])
private

Renders a valid configuration.

  1. Checks for required options.
  2. Normalize provided options.
  3. Merge provided configuration with default configuration.
Parameters
array$configassociative array:
  • name: (string) The metric name
  • extension: (string) The extension generating the metric
  • labels: (array) List of metric dimensional instantiations for filters and aggregations
  • sampleRate: (float) Optional sampling rate to apply
Returns
array
Exceptions
InvalidConfigurationException

Definition at line 296 of file MetricsFactory.php.

Referenced by Wikimedia\Metrics\MetricsFactory\getCounter(), Wikimedia\Metrics\MetricsFactory\getGauge(), and Wikimedia\Metrics\MetricsFactory\getTiming().

◆ normalizeArray()

static Wikimedia\Metrics\MetricsFactory::normalizeArray ( array  $entities)
static

Normalize an array of strings.

Parameters
string[]$entities
Returns
string[]

Definition at line 339 of file MetricsFactory.php.

Referenced by Wikimedia\Metrics\GaugeMetric\set().

◆ normalizeString()

static Wikimedia\Metrics\MetricsFactory::normalizeString ( string  $entity)
static

Normalize strings to a metrics-compatible format.

Replace any other non-alphanumeric characters with underscores. Eliminate repeated underscores. Trim leading or trailing underscores.

Parameters
string$entity
Returns
string

Definition at line 327 of file MetricsFactory.php.

Referenced by Wikimedia\Metrics\MetricsFactory\__construct().

◆ send()

Wikimedia\Metrics\MetricsFactory::send ( UDPTransport  $transport)
protected

Render the buffer of samples, group them into payloads, and send them through the provided UDPTransport instance.

Parameters
UDPTransport$transport

Definition at line 246 of file MetricsFactory.php.

Referenced by Wikimedia\Metrics\MetricsFactory\flush().

Member Data Documentation

◆ $cache

array<CounterMetric|GaugeMetric|TimingMetric> Wikimedia\Metrics\MetricsFactory::$cache = []
private

Definition at line 59 of file MetricsFactory.php.

◆ $format

string Wikimedia\Metrics\MetricsFactory::$format
private

Definition at line 65 of file MetricsFactory.php.

◆ $logger

LoggerInterface Wikimedia\Metrics\MetricsFactory::$logger
private

Definition at line 71 of file MetricsFactory.php.

Referenced by Wikimedia\Metrics\MetricsFactory\__construct().

◆ $prefix

string Wikimedia\Metrics\MetricsFactory::$prefix
private

Definition at line 68 of file MetricsFactory.php.

◆ $target

string null Wikimedia\Metrics\MetricsFactory::$target
private

Definition at line 62 of file MetricsFactory.php.


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