| 1 |
<?php |
| 2 |
/** |
| 3 |
* MCP Observability Handler. |
| 4 |
* |
| 5 |
* Bridges the MCP adapter's observability interface with this library's |
| 6 |
* McpLogger, providing structured request/event metrics without external |
| 7 |
* dependencies. Implements the single-method McpObservabilityHandlerInterface. |
| 8 |
*/ |
| 9 |
|
| 10 |
declare( strict_types=1 ); |
| 11 |
|
| 12 |
namespace WPMedia\MCP\OAuth\Transport; |
| 13 |
|
| 14 |
use WP\MCP\Infrastructure\Observability\Contracts\McpObservabilityHandlerInterface; |
| 15 |
use WP\MCP\Infrastructure\Observability\McpObservabilityHelperTrait; |
| 16 |
use WPMedia\MCP\OAuth\Logging\McpLogger; |
| 17 |
|
| 18 |
/** |
| 19 |
* Observability handler that writes MCP events to the MCP OAuth log. |
| 20 |
*/ |
| 21 |
class McpObservabilityHandler implements McpObservabilityHandlerInterface { |
| 22 |
use McpObservabilityHelperTrait; |
| 23 |
|
| 24 |
/** |
| 25 |
* Record an MCP event, optionally with timing data. |
| 26 |
* |
| 27 |
* Events are written via McpLogger under the 'OBSERVABILITY' scope. |
| 28 |
* All events, including error/failure events, are only written when |
| 29 |
* both WP_DEBUG_LOG and WP_DEBUG are enabled. |
| 30 |
* |
| 31 |
* @param string $event Event name (e.g. 'mcp.request', 'mcp.component.registration'). |
| 32 |
* @param array $tags Key-value context tags (status, method, tool_name, etc.). |
| 33 |
* @param float|null $duration_ms Optional request duration in milliseconds. |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function record_event( string $event, array $tags = [], ?float $duration_ms = null ): void { |
| 37 |
$formatted_event = self::format_metric_name( $event ); |
| 38 |
$merged_tags = self::merge_tags( $tags ); |
| 39 |
|
| 40 |
if ( null !== $duration_ms ) { |
| 41 |
$merged_tags['duration_ms'] = round( $duration_ms, 2 ); |
| 42 |
} |
| 43 |
|
| 44 |
McpLogger::log( 'OBSERVABILITY', $formatted_event, $merged_tags ); |
| 45 |
} |
| 46 |
} |
| 47 |
|