Skip to content

Commit 89b9a27

Browse files
committed
lint & test fixes
Signed-off-by: Shoumi <shoumimukherjee@gmail.com>
1 parent cd0542b commit 89b9a27

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

mcpgateway/middleware/http_auth_middleware.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
# Standard
1010
import logging
11-
import uuid
1211

1312
# Third-Party
1413
from fastapi import Request

mcpgateway/services/a2a_service.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from mcpgateway.services.logging_service import LoggingService
2929
from mcpgateway.services.team_management_service import TeamManagementService
3030
from mcpgateway.services.tool_service import ToolService
31-
from mcpgateway.utils.correlation_id import get_correlation_id
3231
from mcpgateway.utils.create_slug import slugify
3332
from mcpgateway.utils.services_auth import encode_auth # ,decode_auth
3433

mcpgateway/services/logging_service.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
from mcpgateway.services.log_storage_service import LogStorageService
2828
from mcpgateway.utils.correlation_id import get_correlation_id
2929

30+
# Optional OpenTelemetry support
31+
try:
32+
# Third-Party
33+
from opentelemetry import trace # type: ignore[import-untyped]
34+
except ImportError:
35+
trace = None # type: ignore[assignment]
36+
3037
AnyioClosedResourceError: Optional[type] # pylint: disable=invalid-name
3138
try:
3239
# Optional import; only used for filtering a known benign upstream error
@@ -72,20 +79,19 @@ def add_fields(self, log_record: dict, record: logging.LogRecord, message_dict:
7279
log_record["request_id"] = correlation_id
7380

7481
# Add OpenTelemetry trace context if available
75-
try:
76-
from opentelemetry import trace
77-
78-
span = trace.get_current_span()
79-
if span and span.is_recording():
80-
span_context = span.get_span_context()
81-
if span_context.is_valid:
82-
# Format trace_id and span_id as hex strings
83-
log_record["trace_id"] = format(span_context.trace_id, "032x")
84-
log_record["span_id"] = format(span_context.span_id, "016x")
85-
log_record["trace_flags"] = format(span_context.trace_flags, "02x")
86-
except (ImportError, Exception):
87-
# OpenTelemetry not available or error accessing span
88-
pass
82+
if trace is not None:
83+
try:
84+
span = trace.get_current_span()
85+
if span and span.is_recording():
86+
span_context = span.get_span_context()
87+
if span_context.is_valid:
88+
# Format trace_id and span_id as hex strings
89+
log_record["trace_id"] = format(span_context.trace_id, "032x")
90+
log_record["span_id"] = format(span_context.span_id, "016x")
91+
log_record["trace_flags"] = format(span_context.trace_flags, "02x")
92+
except Exception:
93+
# Error accessing span context
94+
pass
8995

9096

9197
# Create a JSON formatter with correlation ID support

tests/unit/mcpgateway/services/test_correlation_id_json_formatter.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ def test_formatter_correlation_id_with_trace_context(logger_with_formatter):
208208
mock_span.is_recording.return_value = True
209209
mock_span.get_span_context.return_value = mock_span_context
210210

211-
with patch("opentelemetry.trace.get_current_span") as mock_get_span:
212-
mock_get_span.return_value = mock_span
211+
with patch("mcpgateway.services.logging_service.trace") as mock_trace:
212+
mock_trace.get_current_span.return_value = mock_span
213213

214214
# Log a message
215215
logger.info("Test with both IDs")
@@ -290,8 +290,8 @@ def test_formatter_handles_invalid_span_context(logger_with_formatter):
290290
mock_span.is_recording.return_value = True
291291
mock_span.get_span_context.return_value = mock_span_context
292292

293-
with patch("opentelemetry.trace.get_current_span") as mock_get_span:
294-
mock_get_span.return_value = mock_span
293+
with patch("mcpgateway.services.logging_service.trace") as mock_trace:
294+
mock_trace.get_current_span.return_value = mock_span
295295

296296
# Log a message
297297
logger.info("Test with invalid span")

0 commit comments

Comments
 (0)