Package com.tnsai.mcp.observability
Class McpMetricsCollector
java.lang.Object
com.tnsai.mcp.observability.McpMetricsCollector
Thread-safe collector of metrics for MCP operations.
Collects the following metrics:
- mcp.tool.invocations - Counter by tool name and success/failure
- mcp.tool.duration - Histogram (total + count for average) by tool name
- mcp.errors - Counter by error type
- mcp.active.sessions - Gauge of currently active sessions
All methods are thread-safe and suitable for concurrent use.
Example usage:
McpMetricsCollector metrics = new McpMetricsCollector();
metrics.recordToolInvocation("readFile", true, Duration.ofMillis(120));
metrics.recordToolInvocation("writeFile", false, Duration.ofMillis(50));
metrics.recordError("TimeoutException", "readFile");
Map<String, Object> summary = metrics.getSummary();
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidDecrements the active sessions gauge by one.intReturns the current number of active sessions.Returns error counts by error type (or errorType:toolName).Returns a summary of all collected metrics.Returns the average duration per tool in milliseconds.Returns the total invocation count per tool (success + failure).voidIncrements the active sessions gauge by one.voidrecordError(String errorType, String toolName) Records an error occurrence.voidrecordToolInvocation(String toolName, boolean success, Duration duration) Records a tool invocation with its outcome and duration.voidreset()Resets all collected metrics to their initial state.
-
Constructor Details
-
McpMetricsCollector
public McpMetricsCollector()
-
-
Method Details
-
recordToolInvocation
Records a tool invocation with its outcome and duration.- Parameters:
toolName- the name of the tool that was invokedsuccess- true if the invocation succeeded, false otherwiseduration- the duration of the invocation
-
recordError
Records an error occurrence.- Parameters:
errorType- the type/class of the errortoolName- the tool name associated with the error (may be null)
-
incrementActiveSessions
public void incrementActiveSessions()Increments the active sessions gauge by one. -
decrementActiveSessions
public void decrementActiveSessions()Decrements the active sessions gauge by one. -
getToolInvocationCounts
Returns the total invocation count per tool (success + failure).- Returns:
- map of tool name to total invocation count
-
getToolAverageDurations
Returns the average duration per tool in milliseconds.- Returns:
- map of tool name to average duration in milliseconds
-
getErrorCounts
Returns error counts by error type (or errorType:toolName).- Returns:
- map of error key to count
-
getActiveSessions
public int getActiveSessions()Returns the current number of active sessions.- Returns:
- the active session count
-
getSummary
Returns a summary of all collected metrics.- Returns:
- map containing all metric categories
-
reset
public void reset()Resets all collected metrics to their initial state.
-