20declare( strict_types=1 );
24use InvalidArgumentException;
48 if ( $newSampleRate < 0.0 || $newSampleRate > 1.0 ) {
49 throw new InvalidArgumentException(
"Sample rate can only be between 0.0 and 1.0. Got: " . $newSampleRate );
61 if ( $sampleRate === 1.0 ) {
65 $randMax = mt_getrandmax();
66 foreach ( $samples as $sample ) {
67 if ( mt_rand() / $randMax < $sampleRate ) {
84 throw new InvalidArgumentException(
"Stats: Metric name cannot be empty." );
86 if ( !preg_match( self::RE_VALID_NAME_AND_LABEL_NAME, $name ) ) {
101 throw new InvalidArgumentException(
"Stats: Label key cannot be empty." );
103 if ( !preg_match( self::RE_VALID_NAME_AND_LABEL_NAME, $key ) ) {
109 if ( $value ===
"" ) {
110 throw new InvalidArgumentException(
"Stats: Label value cannot be empty." );
121 public static function mergeLabels( array $leftLabels, array $rightLabels ): array {
123 foreach ( $leftLabels as $key => $value ) {
124 $output[$key] = $value;
126 foreach ( $rightLabels as $key => $value ) {
127 if ( array_key_exists( $key, $output ) ) {
130 $output[$key] = $value;
142 $normalizedEntities = [];
143 foreach ( $entities as $entity ) {
144 $normalizedEntities[] = self::normalizeString( $entity );
146 return $normalizedEntities;
160 $entity = preg_replace(
"/[^a-z0-9]/i",
"_", $entity );
161 $entity = preg_replace(
"/_+/",
"_", $entity );
162 return trim( $entity,
"_" );
if(!defined('MW_SETUP_CALLBACK'))