20declare( strict_types=1 );
24use InvalidArgumentException;
39 public const RE_VALID_NAME_AND_LABEL_NAME =
"/^[a-zA-Z_][a-zA-Z0-9_]*$/";
42 public const DEFAULT_SAMPLE_RATE = 1.0;
51 if ( $newSampleRate < 0.0 || $newSampleRate > 1.0 ) {
52 throw new InvalidArgumentException(
"Sample rate can only be between 0.0 and 1.0. Got: " . $newSampleRate );
64 if ( $sampleRate === 1.0 ) {
68 $randMax = mt_getrandmax();
69 foreach ( $samples as $sample ) {
70 if ( mt_rand() / $randMax < $sampleRate ) {
87 throw new InvalidArgumentException(
"Stats: Metric name cannot be empty." );
89 if ( !preg_match( self::RE_VALID_NAME_AND_LABEL_NAME, $name ) ) {
104 throw new InvalidArgumentException(
"Stats: Label key cannot be empty." );
106 if ( !preg_match( self::RE_VALID_NAME_AND_LABEL_NAME, $key ) ) {
112 if ( $value ===
"" ) {
113 throw new InvalidArgumentException(
"Stats: Label value cannot be empty." );
124 public static function mergeLabels( array $leftLabels, array $rightLabels ): array {
126 foreach ( $leftLabels as $key => $value ) {
127 $output[$key] = $value;
129 foreach ( $rightLabels as $key => $value ) {
130 if ( array_key_exists( $key, $output ) ) {
133 $output[$key] = $value;
145 $normalizedEntities = [];
146 foreach ( $entities as $entity ) {
147 $normalizedEntities[] = self::normalizeString( $entity );
149 return $normalizedEntities;
163 $entity = preg_replace(
"/[^a-z0-9]/i",
"_", $entity );
164 $entity = preg_replace(
"/_+/",
"_", $entity );
165 return trim( $entity,
"_" );
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.