-
Notifications
You must be signed in to change notification settings - Fork 871
feat(instrumentation): Adding MCP opentelemetry-instrumentation into traceloop #2829
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
Merged
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a1eed91
Adding MCP opentelemetry-instrumentation into traceloop
fali007 7424934
bugfix
fali007 19298b1
Bugfix
fali007 d3810b2
Addressing Code review comments
fali007 e8a4984
Added unit tests
fali007 609f9e5
Linting
fali007 297cfd8
Fixing poetry lock file
fali007 d9a9811
Linting and addressed review comments
fali007 adbc1c5
Code review comments
fali007 f3a6743
Merge branch 'main' into dev-mcp
fali007 8076643
WIP:MCP server otel instrumentation
fali007 e6112b9
MCP otel instrumentation
fali007 8c61489
MCP otel instrumentation
fali007 bc073af
Merge branch 'main' into dev-mcp
fali007 70b6b82
Removing unwanted print statement
fali007 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| [flake8] | ||
| exclude = | ||
| .git, | ||
| __pycache__, | ||
| build, | ||
| dist, | ||
| .tox, | ||
| venv, | ||
| .venv, | ||
| .pytest_cache | ||
| max-line-length = 120 |
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 3.12.6 |
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 |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| OpenTelemetry MCP Instrumentation | ||
|
|
||
| <a href="https://pypi.org/project/opentelemetry-instrumentation-mcp/"> | ||
| <img src="https://badge.fury.io/py/opentelemetry-instrumentation-mcp.svg"> | ||
| </a> | ||
|
|
||
| This library allows tracing agentic workflows implemented with MCP framework [mcp python sdk](https://github.com/modelcontextprotocol/python-sdk). | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| pip install opentelemetry-instrumentation-mcp | ||
| ``` | ||
|
|
||
| ## Example usage | ||
|
|
||
| ```python | ||
| from opentelemetry.instrumentation.mcp import McpInstrumentor | ||
|
|
||
| McpInstrumentor().instrument() | ||
| ``` | ||
|
|
||
| ## Privacy | ||
|
|
||
| **By default, this instrumentation logs prompts, completions, and embeddings to span attributes**. This gives you a clear visibility into how your LLM application tool usage is working, and can make it easy to debug and evaluate the tool usage. | ||
|
|
||
| However, you may want to disable this logging for privacy reasons, as they may contain highly sensitive data from your users. You may also simply want to reduce the size of your traces. | ||
|
|
||
| To disable logging, set the `TRACELOOP_TRACE_CONTENT` environment variable to `false`. | ||
|
|
||
| ```bash | ||
| TRACELOOP_TRACE_CONTENT=false | ||
| ``` | ||
4 changes: 4 additions & 0 deletions
4
packages/opentelemetry-instrumentation-mcp/opentelemetry/instrumentation/mcp/__init__.py
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 |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from opentelemetry.instrumentation.mcp.version import __version__ | ||
| from opentelemetry.instrumentation.mcp.instrumentation import McpInstrumentor | ||
|
|
||
| __all__ = ["McpInstrumentor", "__version__"] |
152 changes: 152 additions & 0 deletions
152
...es/opentelemetry-instrumentation-mcp/opentelemetry/instrumentation/mcp/instrumentation.py
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 |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| from typing import Collection | ||
| import mcp | ||
| import json | ||
|
|
||
| from opentelemetry.instrumentation.instrumentor import BaseInstrumentor | ||
| from opentelemetry.trace import get_tracer | ||
| from wrapt import wrap_function_wrapper as _W | ||
fali007 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| from opentelemetry.trace.status import Status, StatusCode | ||
| from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator | ||
| from opentelemetry.trace.propagation import set_span_in_context | ||
| from opentelemetry.semconv_ai import SpanAttributes | ||
|
|
||
| from opentelemetry.instrumentation.mcp.version import __version__ | ||
|
|
||
| _instruments = ("mcp >= 1.3.0",) | ||
|
|
||
|
|
||
| class McpInstrumentor(BaseInstrumentor): | ||
| def instrumentation_dependencies(self) -> Collection[str]: | ||
| return _instruments | ||
|
|
||
| def _instrument(self, **kwargs): | ||
| tracer_provider = kwargs.get("tracer_provider") | ||
| tracer = get_tracer(__name__, __version__, tracer_provider) | ||
|
|
||
| _W( | ||
| "mcp.server.lowlevel.server", | ||
| "Server._handle_request", | ||
fali007 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| patch_mcp_server("Server._handle_request", tracer), | ||
| ) | ||
| _W( | ||
| "mcp.shared.session", | ||
| "BaseSession.send_request", | ||
| patch_mcp_client("BaseSession.send_request", tracer), | ||
| ) | ||
|
|
||
| def _uninstrument(self, **kwargs): | ||
| pass | ||
|
|
||
|
|
||
| def with_tracer_wrapper(func): | ||
| """Helper for providing tracer for wrapper functions.""" | ||
|
|
||
| def _with_tracer(operation_name, tracer): | ||
| def wrapper(wrapped, instance, args, kwargs): | ||
| return func(operation_name, tracer, wrapped, instance, args, kwargs) | ||
|
|
||
| return wrapper | ||
|
|
||
| return _with_tracer | ||
|
|
||
|
|
||
| def serialize(request, depth=0, max_depth=2): | ||
fali007 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """Serialize input args to MCP server into JSON. The function accepts input object and converts into JSON keeping depth in mind to prevent creating large nested JSON""" | ||
|
|
||
| if depth > max_depth: | ||
| return {} | ||
| depth += 1 | ||
|
|
||
| def is_serializable(request): | ||
| try: | ||
| json.dumps(request) | ||
| return True | ||
| except Exception: | ||
| return False | ||
|
|
||
| if is_serializable(request): | ||
| return json.dumps(request) | ||
| else: | ||
| result = {} | ||
| try: | ||
| if hasattr(request, "__dict__"): | ||
| for attrib in request.__dict__: | ||
| if type(request.__dict__[attrib]) in [ | ||
| bool, | ||
| str, | ||
| int, | ||
| float, | ||
| type(None), | ||
| ]: | ||
| result[str(attrib)] = request.__dict__[attrib] | ||
| else: | ||
| result[str(attrib)] = serialize(request.__dict__[attrib], depth) | ||
| except Exception: | ||
| pass | ||
| return json.dumps(result) | ||
|
|
||
|
|
||
| @with_tracer_wrapper | ||
| def patch_mcp_server(operation_name, tracer, wrapped, instance, args, kwargs): | ||
| method = args[1].method | ||
| carrier = None | ||
| ctx = None | ||
| if hasattr(args[1], "params"): | ||
| if hasattr(args[1].params, "meta"): | ||
| if hasattr(args[1].params.meta, "traceparent"): | ||
| carrier = {"traceparent": args[1].params.meta.traceparent} | ||
| if carrier: | ||
| ctx = TraceContextTextMapPropagator().extract(carrier=carrier) | ||
| with tracer.start_as_current_span(f"{method}", context=ctx) as span: | ||
| span.set_attribute(SpanAttributes.MCP_METHOD_NAME, f"{method}") | ||
| if hasattr(args[1], "id"): | ||
| span.set_attribute(SpanAttributes.MCP_REQUEST_ID, f"{args[1].id}") | ||
| if hasattr(args[2], "_init_options"): | ||
fali007 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| span.set_attribute( | ||
| SpanAttributes.MCP_SESSION_INIT_OPTIONS, f"{args[2]._init_options}" | ||
| ) | ||
| span.set_attribute(SpanAttributes.MCP_REQUEST_ARGUMENT, f"{serialize(args[1])}") | ||
| try: | ||
| result = wrapped(*args, **kwargs) | ||
| if result: | ||
| span.set_status(Status(StatusCode.OK)) | ||
| return result | ||
| except Exception as e: | ||
| span.record_exception(e) | ||
| span.set_status(Status(StatusCode.ERROR, str(e))) | ||
| raise | ||
|
|
||
|
|
||
| @with_tracer_wrapper | ||
| def patch_mcp_client(operation_name, tracer, wrapped, instance, args, kwargs): | ||
| meta = None | ||
| method = None | ||
| params = None | ||
| if hasattr(args[0].root, "method"): | ||
| method = args[0].root.method | ||
| if hasattr(args[0].root, "params"): | ||
| params = args[0].root.params | ||
| if params is None: | ||
| args[0].root.params = mcp.types.RequestParams() | ||
| meta = {} | ||
| else: | ||
| if hasattr(args[0].root.params, "meta"): | ||
| meta = args[0].root.params.meta | ||
| if meta is None: | ||
| meta = {} | ||
|
|
||
| with tracer.start_as_current_span(f"{method}") as span: | ||
| span.set_attribute(SpanAttributes.MCP_METHOD_NAME, f"{method}") | ||
| span.set_attribute(SpanAttributes.MCP_REQUEST_ARGUMENT, f"{serialize(args[0])}") | ||
| ctx = set_span_in_context(span) | ||
| TraceContextTextMapPropagator().inject(meta, ctx) | ||
| args[0].root.params.meta = meta | ||
| try: | ||
| result = wrapped(*args, **kwargs) | ||
| if result: | ||
| span.set_status(Status(StatusCode.OK)) | ||
| return result | ||
| except Exception as e: | ||
| span.record_exception(e) | ||
| span.set_status(Status(StatusCode.ERROR, str(e))) | ||
| raise | ||
1 change: 1 addition & 0 deletions
1
packages/opentelemetry-instrumentation-mcp/opentelemetry/instrumentation/mcp/version.py
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| __version__ = "0.39.1" |
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.
Uh oh!
There was an error while loading. Please reload this page.