-
Notifications
You must be signed in to change notification settings - Fork 301
Rename span attributes to MCP OTEL semantic conventions #3748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+342
−234
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,12 @@ import ( | |
| const ( | ||
| // instrumentationName is the name of this instrumentation package | ||
| instrumentationName = "github.com/stacklok/toolhive/pkg/telemetry" | ||
| // methodPromptsGet is the MCP method name for prompts/get | ||
| methodPromptsGet = "prompts/get" | ||
| // networkTransportTCP is the OTEL value for TCP transport | ||
| networkTransportTCP = "tcp" | ||
| // networkProtocolHTTP is the OTEL value for HTTP protocol | ||
| networkProtocolHTTP = "http" | ||
| ) | ||
|
|
||
| // HTTPMiddleware provides OpenTelemetry instrumentation for HTTP requests. | ||
|
|
@@ -125,7 +131,10 @@ func (m *HTTPMiddleware) Handler(next http.Handler) http.Handler { | |
| )) | ||
|
|
||
| // Create span name based on MCP method if available, otherwise use HTTP method + path | ||
| spanName := m.createSpanName(ctx, r) | ||
| spanName := m.createSpanName(ctx) | ||
| if spanName == "" { | ||
| spanName = fmt.Sprintf("%s %s", r.Method, r.URL.Path) | ||
| } | ||
| ctx, span := m.tracer.Start(ctx, spanName, trace.WithSpanKind(trace.SpanKindServer)) | ||
| defer span.End() | ||
|
|
||
|
|
@@ -158,19 +167,35 @@ func (m *HTTPMiddleware) Handler(next http.Handler) http.Handler { | |
| }) | ||
| } | ||
|
|
||
| // createSpanName creates an appropriate span name based on available context. | ||
| func (*HTTPMiddleware) createSpanName(ctx context.Context, r *http.Request) string { | ||
| // Try to get MCP method from parsed data | ||
| if mcpMethod := mcpparser.GetMCPMethod(ctx); mcpMethod != "" { | ||
| return fmt.Sprintf("mcp.%s", mcpMethod) | ||
| func (*HTTPMiddleware) createSpanName(ctx context.Context) string { | ||
| parsedMCP := mcpparser.GetParsedMCPRequest(ctx) | ||
| if parsedMCP == nil || parsedMCP.Method == "" { | ||
| return "" | ||
| } | ||
|
|
||
| // Fall back to HTTP method + path | ||
| return fmt.Sprintf("%s %s", r.Method, r.URL.Path) | ||
| return parsedMCP.Method | ||
| } | ||
|
|
||
| // addHTTPAttributes adds standard HTTP attributes to the span. | ||
| func (m *HTTPMiddleware) addHTTPAttributes(span trace.Span, r *http.Request) { | ||
| // New OTEL HTTP semantic convention attributes (always emitted) | ||
| span.SetAttributes( | ||
| attribute.String("http.request.method", r.Method), | ||
| attribute.String("url.full", r.URL.String()), | ||
| attribute.String("url.scheme", r.URL.Scheme), | ||
| attribute.String("server.address", r.Host), | ||
| attribute.String("url.path", r.URL.Path), | ||
| attribute.String("user_agent.original", r.UserAgent()), | ||
| ) | ||
|
|
||
| if r.ContentLength > 0 { | ||
| span.SetAttributes(attribute.Int64("http.request.body.size", r.ContentLength)) | ||
| } | ||
|
|
||
| if r.URL.RawQuery != "" { | ||
| span.SetAttributes(attribute.String("url.query", r.URL.RawQuery)) | ||
| } | ||
|
|
||
| // Legacy attribute names (emitted only when UseLegacyAttributes is true) | ||
| if m.config.UseLegacyAttributes { | ||
| span.SetAttributes( | ||
| attribute.String("http.method", r.Method), | ||
|
|
@@ -181,12 +206,10 @@ func (m *HTTPMiddleware) addHTTPAttributes(span trace.Span, r *http.Request) { | |
| attribute.String("http.user_agent", r.UserAgent()), | ||
| ) | ||
|
|
||
| // Add content length if available | ||
| if contentLength := r.Header.Get("Content-Length"); contentLength != "" { | ||
| span.SetAttributes(attribute.String("http.request_content_length", contentLength)) | ||
| } | ||
|
|
||
| // Add query parameters if present | ||
| if r.URL.RawQuery != "" { | ||
| span.SetAttributes(attribute.String("http.query", r.URL.RawQuery)) | ||
| } | ||
|
|
@@ -219,20 +242,37 @@ func (m *HTTPMiddleware) addMCPAttributes(ctx context.Context, span trace.Span, | |
| return | ||
| } | ||
|
|
||
| // New OTEL MCP semantic convention attributes (always emitted) | ||
| span.SetAttributes( | ||
| attribute.String("mcp.method.name", parsedMCP.Method), | ||
| attribute.String("rpc.system.name", "jsonrpc"), | ||
| attribute.String("jsonrpc.protocol.version", "2.0"), | ||
| ) | ||
|
|
||
| if parsedMCP.ID != nil { | ||
| span.SetAttributes(attribute.String("jsonrpc.request.id", formatRequestID(parsedMCP.ID))) | ||
| } | ||
|
|
||
| // Resource URI: only set for resource-related methods | ||
| if parsedMCP.ResourceID != "" { | ||
| switch parsedMCP.Method { | ||
| case "resources/read", "resources/subscribe", "resources/unsubscribe", "notifications/resources/updated": | ||
| span.SetAttributes(attribute.String("mcp.resource.uri", parsedMCP.ResourceID)) | ||
| } | ||
| } | ||
|
|
||
| // Legacy attribute names (emitted only when UseLegacyAttributes is true) | ||
| if m.config.UseLegacyAttributes { | ||
| // Add basic MCP attributes | ||
| span.SetAttributes( | ||
| attribute.String("mcp.method", parsedMCP.Method), | ||
| attribute.String("rpc.system", "jsonrpc"), | ||
| attribute.String("rpc.service", "mcp"), | ||
| ) | ||
|
|
||
| // Add request ID if available | ||
| if parsedMCP.ID != nil { | ||
| span.SetAttributes(attribute.String("mcp.request.id", formatRequestID(parsedMCP.ID))) | ||
| } | ||
|
|
||
| // Add resource ID if available | ||
| if parsedMCP.ResourceID != "" { | ||
| span.SetAttributes(attribute.String("mcp.resource.id", parsedMCP.ResourceID)) | ||
| } | ||
|
|
@@ -241,15 +281,21 @@ func (m *HTTPMiddleware) addMCPAttributes(ctx context.Context, span trace.Span, | |
| // Add method-specific attributes | ||
| m.addMethodSpecificAttributes(span, parsedMCP) | ||
|
|
||
| // Extract server name from the request, defaulting to the middleware's configured server name | ||
| // Extract server name from the request | ||
| serverName := m.extractServerName(r) | ||
| span.SetAttributes(attribute.String("mcp.server.name", serverName)) | ||
|
|
||
| // Determine backend transport type | ||
| // Note: ToolHive supports multiple transport types including stdio, sse, streamable-http | ||
| // The transport should never be empty as both CLI and API have fallbacks to "streamable-http" | ||
| // If transport is still empty, it indicates a configuration issue in middleware construction | ||
| // Determine backend transport type and map to OTEL network.transport | ||
| backendTransport := m.extractBackendTransport(r) | ||
| networkTransport, protocolName, protocolVersion := mapTransport(backendTransport) | ||
| span.SetAttributes(attribute.String("network.transport", networkTransport)) | ||
| if protocolName != "" { | ||
| span.SetAttributes(attribute.String("network.protocol.name", protocolName)) | ||
| } | ||
| if protocolVersion != "" { | ||
| span.SetAttributes(attribute.String("network.protocol.version", protocolVersion)) | ||
| } | ||
|
|
||
| if m.config.UseLegacyAttributes { | ||
| span.SetAttributes(attribute.String("mcp.transport", backendTransport)) | ||
| } | ||
|
|
@@ -262,35 +308,43 @@ func (m *HTTPMiddleware) addMCPAttributes(ctx context.Context, span trace.Span, | |
|
|
||
| // addMethodSpecificAttributes adds attributes specific to certain MCP methods. | ||
| func (m *HTTPMiddleware) addMethodSpecificAttributes(span trace.Span, parsedMCP *mcpparser.ParsedMCPRequest) { | ||
| if !m.config.UseLegacyAttributes { | ||
| return | ||
| } | ||
|
|
||
| switch parsedMCP.Method { | ||
| case string(mcp.MethodToolsCall): | ||
| // For tool calls, the ResourceID is the tool name | ||
| // New gen_ai namespace attributes (always emitted) | ||
| if parsedMCP.ResourceID != "" { | ||
| span.SetAttributes(attribute.String("mcp.tool.name", parsedMCP.ResourceID)) | ||
| span.SetAttributes(attribute.String("gen_ai.tool.name", parsedMCP.ResourceID)) | ||
| } | ||
| // Add sanitized arguments | ||
| if args := m.sanitizeArguments(parsedMCP.Arguments); args != "" { | ||
| span.SetAttributes(attribute.String("mcp.tool.arguments", args)) | ||
| span.SetAttributes(attribute.String("gen_ai.operation.name", "execute_tool")) | ||
|
|
||
| sanitizedArgs := m.sanitizeArguments(parsedMCP.Arguments) | ||
| if sanitizedArgs != "" { | ||
| span.SetAttributes(attribute.String("gen_ai.tool.call.arguments", sanitizedArgs)) | ||
| } | ||
|
|
||
| case "resources/read": | ||
| // For resource reads, the ResourceID is the URI | ||
| if parsedMCP.ResourceID != "" { | ||
| span.SetAttributes(attribute.String("mcp.resource.uri", parsedMCP.ResourceID)) | ||
| // Legacy names | ||
| if m.config.UseLegacyAttributes { | ||
| if parsedMCP.ResourceID != "" { | ||
| span.SetAttributes(attribute.String("mcp.tool.name", parsedMCP.ResourceID)) | ||
| } | ||
| if sanitizedArgs != "" { | ||
| span.SetAttributes(attribute.String("mcp.tool.arguments", sanitizedArgs)) | ||
| } | ||
| } | ||
|
|
||
| case "prompts/get": | ||
| // For prompt gets, the ResourceID is the prompt name | ||
| case methodPromptsGet: | ||
| // New gen_ai namespace attribute (always emitted) | ||
| if parsedMCP.ResourceID != "" { | ||
| span.SetAttributes(attribute.String("mcp.prompt.name", parsedMCP.ResourceID)) | ||
| span.SetAttributes(attribute.String("gen_ai.prompt.name", parsedMCP.ResourceID)) | ||
| } | ||
|
|
||
| // Legacy name | ||
| if m.config.UseLegacyAttributes { | ||
| if parsedMCP.ResourceID != "" { | ||
| span.SetAttributes(attribute.String("mcp.prompt.name", parsedMCP.ResourceID)) | ||
| } | ||
| } | ||
|
|
||
| case "initialize": | ||
| // For initialize, the ResourceID is the client name | ||
| if parsedMCP.ResourceID != "" { | ||
| span.SetAttributes(attribute.String("mcp.client.name", parsedMCP.ResourceID)) | ||
| } | ||
|
|
@@ -331,6 +385,19 @@ func (m *HTTPMiddleware) extractBackendTransport(r *http.Request) string { | |
| return m.transport | ||
| } | ||
|
|
||
| func mapTransport(mcpTransport string) (networkTransport, protocolName, protocolVersion string) { | ||
| switch mcpTransport { | ||
| case "stdio": | ||
| return "pipe", "", "" | ||
| case "sse": | ||
| return networkTransportTCP, networkProtocolHTTP, "1.1" | ||
| case "streamable-http": | ||
| return networkTransportTCP, networkProtocolHTTP, "2" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it always http/2 for streamable http or "usually" ? |
||
| default: | ||
| return networkTransportTCP, networkProtocolHTTP, "" | ||
| } | ||
| } | ||
|
|
||
| // sanitizeArguments converts arguments to a safe string representation. | ||
| func (m *HTTPMiddleware) sanitizeArguments(arguments map[string]interface{}) string { | ||
| if len(arguments) == 0 { | ||
|
|
@@ -397,7 +464,13 @@ func formatRequestID(id interface{}) string { | |
|
|
||
| // finalizeSpan adds response attributes and sets the span status. | ||
| func (m *HTTPMiddleware) finalizeSpan(span trace.Span, rw *responseWriter, duration time.Duration) { | ||
| // Add response attributes | ||
| // New OTEL HTTP semantic convention response attributes (always emitted) | ||
| span.SetAttributes( | ||
| attribute.Int("http.response.status_code", rw.statusCode), | ||
| attribute.Int64("http.response.body.size", rw.bytesWritten), | ||
| ) | ||
|
|
||
| // Legacy response attributes | ||
| if m.config.UseLegacyAttributes { | ||
| span.SetAttributes( | ||
| attribute.Int("http.status_code", rw.statusCode), | ||
|
|
@@ -534,10 +607,18 @@ func (m *HTTPMiddleware) recordSSEConnection(ctx context.Context, r *http.Reques | |
| m.addHTTPAttributes(span, r) | ||
|
|
||
| // Add SSE-specific attributes | ||
| networkTransport, protocolName, protocolVersion := mapTransport(m.transport) | ||
| span.SetAttributes( | ||
| attribute.String("sse.event_type", "connection_established"), | ||
| attribute.String("mcp.server.name", m.serverName), | ||
| attribute.String("network.transport", networkTransport), | ||
| ) | ||
| if protocolName != "" { | ||
| span.SetAttributes(attribute.String("network.protocol.name", protocolName)) | ||
| } | ||
| if protocolVersion != "" { | ||
| span.SetAttributes(attribute.String("network.protocol.version", protocolVersion)) | ||
| } | ||
| if m.config.UseLegacyAttributes { | ||
| span.SetAttributes(attribute.String("mcp.transport", m.transport)) | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If someone like me has no idea where these come from: https://opentelemetry.io/docs/specs/semconv/gen-ai/mcp/