component = $component; $this->name = StatsUtils::normalizeString( $name ); } /** @inheritDoc */ public function addSample( Sample $sample ): void { $this->samples[] = $sample; } /** @inheritDoc */ public function setSampleRate( float $sampleRate ): void { if ( $this->hasSamples() ) { throw new IllegalOperationException( "Stats: Cannot change sample rate on metric with recorded samples." ); } StatsUtils::validateNewSampleRate( $sampleRate ); $this->sampleRate = $sampleRate; } /** @inheritDoc */ public function getName(): string { return $this->name; } /** @inheritDoc */ public function getSampleRate(): float { return $this->sampleRate; } /** @inheritDoc */ public function getSamples(): array { return StatsUtils::getFilteredSamples( $this->sampleRate, $this->samples ); } /** @inheritDoc */ public function getSampleCount(): int { return count( $this->samples ); } /** @inheritDoc */ public function addLabel( string $key, string $value ): void { StatsUtils::validateLabelValue( $value ); $key = StatsUtils::normalizeString( $key ); StatsUtils::validateLabelKey( $key ); $this->addLabelKey( $key ); $this->workingLabels[$key] = StatsUtils::normalizeString( $value ); } /** @inheritDoc */ public function getStatsdDataFactory() { return $this->statsdDataFactory; } /** @inheritDoc */ public function withStatsdDataFactory( $statsdDataFactory ): BaseMetric { $this->statsdDataFactory = $statsdDataFactory; return $this; } /** @inheritDoc */ public function setStatsdNamespaces( $statsdNamespaces ): void { if ( $this->statsdDataFactory === null ) { return; } $statsdNamespaces = is_array( $statsdNamespaces ) ? $statsdNamespaces : [ $statsdNamespaces ]; foreach ( $statsdNamespaces as $namespace ) { if ( $namespace === '' ) { throw new InvalidArgumentException( "Stats: StatsD namespace cannot be empty." ); } if ( !is_string( $namespace ) ) { throw new InvalidArgumentException( "Stats: StatsD namespace must be a string." ); } } $this->statsdNamespaces = $statsdNamespaces; } /** @inheritDoc */ public function getStatsdNamespaces(): array { return $this->statsdNamespaces; } /** * Registers a label key * * @param string $key * @return void */ private function addLabelKey( string $key ): void { if ( in_array( $key, $this->labelKeys, true ) ) { return; // key already exists } if ( $this->hasSamples() ) { throw new IllegalOperationException( "Stats: Cannot add labels to a metric containing samples for '" . $this->name . "'" ); } $this->labelKeys[] = $key; } /** @return string[] */ public function getLabelKeys(): array { return $this->labelKeys; } /** * Get label values in the order of labelKeys. * * @return string[] */ public function getLabelValues(): array { $output = []; # make sure all labels are accounted for if ( array_diff( $this->labelKeys, array_keys( $this->workingLabels ) ) ) { throw new IllegalOperationException( "Stats: Cannot associate label keys with label values: " . "Not all initialized labels have an assigned value." ); } foreach ( $this->labelKeys as $labelKey ) { $output[] = $this->workingLabels[$labelKey]; } return $output; } /** @inheritDoc */ public function clearLabels(): void { $this->workingLabels = []; } /** @inheritDoc */ public function getComponent(): string { return $this->component; } private function hasSamples(): bool { return $this->samples !== []; } }