Skip to content

Commit b2bad7c

Browse files
committed
Fix lint errors
1 parent 19b25ce commit b2bad7c

File tree

4 files changed

+139
-170
lines changed

4 files changed

+139
-170
lines changed

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ exclude =
1919
CVS
2020
.venv*/
2121
venv*/
22+
**/venv*/
23+
**/.venv*/
24+
aws-opentelemetry-distro/src/amazon/opentelemetry/distro/mcpinstrumentor/venv
2225
target
2326
__pycache__
2427
mock_collector_service_pb2.py

aws-opentelemetry-distro/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ dependencies = [
4848
"opentelemetry-instrumentation-boto == 0.54b1",
4949
"opentelemetry-instrumentation-boto3sqs == 0.54b1",
5050
"opentelemetry-instrumentation-botocore == 0.54b1",
51+
"opentelemetry-instrumentation-cassandra == 0.54b1",
5152
"opentelemetry-instrumentation-celery == 0.54b1",
5253
"opentelemetry-instrumentation-confluent-kafka == 0.54b1",
5354
"opentelemetry-instrumentation-dbapi == 0.54b1",
@@ -81,9 +82,9 @@ dependencies = [
8182
"opentelemetry-instrumentation-urllib == 0.54b1",
8283
"opentelemetry-instrumentation-urllib3 == 0.54b1",
8384
"opentelemetry-instrumentation-wsgi == 0.54b1",
84-
"opentelemetry-instrumentation-cassandra == 0.54b1",
8585
]
8686

87+
8788
[project.optional-dependencies]
8889
# The 'patch' optional dependency is used for applying patches to specific libraries.
8990
# If a new patch is added into the list, it must also be added into tox.ini, dev-requirements.txt and _instrumentation_patch

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/mcpinstrumentor/mcpinstrumentor.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import logging
2-
from typing import Any, AsyncGenerator, Callable, Collection, Tuple, cast
2+
from typing import Any, Collection
33

44
from openinference.instrumentation.mcp.package import _instruments
5-
from wrapt import ObjectProxy, register_post_import_hook, wrap_function_wrapper
5+
from wrapt import register_post_import_hook, wrap_function_wrapper
66

7-
from opentelemetry import context, propagate, trace
7+
from opentelemetry import trace
88
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
99
from opentelemetry.instrumentation.utils import unwrap
10-
from opentelemetry.sdk.resources import Resource
1110

1211

1312
def setup_loggertwo():
@@ -69,15 +68,13 @@ def handle_attributes(self, span, request, is_client=True):
6968
span.set_attribute("mcp.list_tools", True)
7069
elif isinstance(request, types.CallToolRequest):
7170
if hasattr(request, "params") and hasattr(request.params, "name"):
72-
operation = request.params.name
71+
operation = request.params.name
7372
span.set_attribute("mcp.call_tool", True)
7473
if is_client:
7574
self._add_client_attributes(span, operation, request)
7675
else:
7776
self._add_server_attributes(span, operation, request)
7877

79-
80-
8178
def _add_client_attributes(self, span, operation, request):
8279
span.set_attribute("span.kind", "CLIENT")
8380
span.set_attribute("aws.remote.service", "Appsignals MCP Server")
@@ -103,23 +100,22 @@ def _send_request_wrapper(self, wrapped, instance, args, kwargs):
103100
"""
104101
Changes made:
105102
The wrapper intercepts the request before sending, injects distributed tracing context into the
106-
request's params._meta field and creates OpenTelemetry spans. The wrapper does not change anything else from the original function's
107-
behavior because it reconstructs the request object with the same type and calling the original function with identical parameters.
103+
request's params._meta field and creates OpenTelemetry spans. The wrapper does not change anything
104+
else from the original function's behavior because it reconstructs the request object with the same
105+
type and calling the original function with identical parameters.
108106
"""
109107

110108
async def async_wrapper():
111109
if self.tracer_provider is None:
112110
tracer = trace.get_tracer("mcp.client")
113111
else:
114112
tracer = self.tracer_provider.get_tracer("mcp.client")
115-
with tracer.start_as_current_span(
116-
"client.send_request", kind=trace.SpanKind.CLIENT
117-
) as span:
113+
with tracer.start_as_current_span("client.send_request", kind=trace.SpanKind.CLIENT) as span:
118114
span_ctx = span.get_span_context()
119115
request = args[0] if len(args) > 0 else kwargs.get("request")
120116
if request:
121117
req_root = request.root if hasattr(request, "root") else request
122-
118+
123119
self.handle_attributes(span, req_root, True)
124120
request_data = request.model_dump(by_alias=True, mode="json", exclude_none=True)
125121
self._inject_trace_context(request_data, span_ctx)
@@ -155,24 +151,30 @@ async def _server_handle_request_wrapper(self, wrapped, instance, args, kwargs):
155151
"""
156152
Changes made:
157153
This wrapper intercepts requests before processing, extracts distributed tracing context from
158-
the request's params._meta field, and creates server-side OpenTelemetry spans linked to the client spans. The wrapper
159-
also does not change the original function's behavior by calling it with identical parameters
154+
the request's params._meta field, and creates server-side OpenTelemetry spans linked to the client spans.
155+
The wrapper also does not change the original function's behavior by calling it with identical parameters
160156
ensuring no breaking changes to the MCP server functionality.
161157
"""
162158
req = args[1] if len(args) > 1 else None
163159
trace_context = None
164-
160+
165161
if req and hasattr(req, "params") and req.params and hasattr(req.params, "meta") and req.params.meta:
166162
trace_context = req.params.meta.trace_context
167163
if trace_context:
168-
164+
169165
if self.tracer_provider is None:
170166
tracer = trace.get_tracer("mcp.server")
171167
else:
172168
tracer = self.tracer_provider.get_tracer("mcp.server")
173169
trace_id = trace_context.get("trace_id")
174170
span_id = trace_context.get("span_id")
175-
span_context = trace.SpanContext(trace_id=trace_id, span_id=span_id, is_remote=True,trace_flags=trace.TraceFlags(trace.TraceFlags.SAMPLED),trace_state=trace.TraceState())
171+
span_context = trace.SpanContext(
172+
trace_id=trace_id,
173+
span_id=span_id,
174+
is_remote=True,
175+
trace_flags=trace.TraceFlags(trace.TraceFlags.SAMPLED),
176+
trace_state=trace.TraceState(),
177+
)
176178
span_name = self.getname(req)
177179
with tracer.start_as_current_span(
178180
span_name,
@@ -183,5 +185,7 @@ async def _server_handle_request_wrapper(self, wrapped, instance, args, kwargs):
183185
result = await wrapped(*args, **kwargs)
184186
return result
185187
else:
186-
return await wrapped(*args, **kwargs,)
187-
188+
return await wrapped(
189+
*args,
190+
**kwargs,
191+
)

0 commit comments

Comments
 (0)