$attributes * @param array $metrics * @return array */ public static function make( string $type, string $source, string $priority, array $attributes = array(), array $metrics = array(), ?string $correlation_id = null, ?float $occurred_at = null ): array { $ts = $occurred_at ?? microtime( true ); $m = array(); foreach ( $metrics as $k => $v ) { if ( is_int( $v ) || is_float( $v ) ) { if ( is_finite( (float) $v ) ) { $m[ substr( (string) $k, 0, 64 ) ] = $v; } } } $event = array( 'schema_version' => self::SCHEMA_VERSION, 'event_id' => self::uuid7( $ts ), 'occurred_at' => gmdate( 'Y-m-d\TH:i:s', (int) floor( $ts ) ) . sprintf( '.%03dZ', (int) ( ( $ts - floor( $ts ) ) * 1000 ) ), 'event_type' => $type, 'source' => $source, 'priority' => $priority, 'attributes' => (object) Redactor::attributes( $attributes ), 'metrics' => (object) $m, ); if ( $correlation_id !== null ) { $event['correlation_id'] = Redactor::text( $correlation_id, 128 ); } return $event; } /** RFC 9562 UUIDv7 (time-ordered); generated locally so the cloud can deduplicate. */ public static function uuid7( ?float $ts = null ): string { $ms = (int) floor( ( $ts ?? microtime( true ) ) * 1000 ); $rand = random_bytes( 10 ); $hex = sprintf( '%012x', $ms ) . bin2hex( $rand ); // version 7 and RFC variant bits $hex[12] = '7'; $hex[16] = dechex( ( hexdec( $hex[16] ) & 0x3 ) | 0x8 ); return substr( $hex, 0, 8 ) . '-' . substr( $hex, 8, 4 ) . '-' . substr( $hex, 12, 4 ) . '-' . substr( $hex, 16, 4 ) . '-' . substr( $hex, 20, 12 ); } }