8000 build(a2a): add a2a deps and mitigate otel conflict (#232) · random-user-pr/sdk-python1@5fab010 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5fab010

Browse files
authored
build(a2a): add a2a deps and mitigate otel conflict (strands-agents#232)
* build(a2a): add a2a deps and mitigate otel conflict
1 parent 68740c5 commit 5fab010

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

pyproject.toml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ dependencies = [
3535
"watchdog>=6.0.0,<7.0.0",
3636
"opentelemetry-api>=1.30.0,<2.0.0",
3737
"opentelemetry-sdk>=1.30.0,<2.0.0",
38-
"opentelemetry-exporter-otlp-prot 10000 o-http>=1.30.0,<2.0.0",
3938
]
4039

4140
[project.urls]
@@ -78,13 +77,23 @@ ollama = [
7877
openai = [
7978
"openai>=1.68.0,<2.0.0",
8079
]
80+
otel = [
81+
"opentelemetry-exporter-otlp-proto-http>=1.30.0,<2.0.0",
82+
]
83+
a2a = [
84+
"a2a-sdk>=0.2.6",
85+
"uvicorn>=0.34.2",
86+
"httpx>=0.28.1",
87+
"fastapi>=0.115.12",
88+
"starlette>=0.46.2",
89+
]
8190

8291
[tool.hatch.version]
8392
# Tells Hatch to use your version control system (git) to determine the version.
8493
source = "vcs"
8594

8695
[tool.hatch.envs.hatch-static-analysis]
87-
features = ["anthropic", "litellm", "llamaapi", "ollama", "openai"]
96+
features = ["anthropic", "litellm", "llamaapi", "ollama", "openai", "otel"]
8897
dependencies = [
8998
"mypy>=1.15.0,<2.0.0",
9099
"ruff>=0.11.6,<0.12.0",
@@ -107,7 +116,7 @@ lint-fix = [
107116
]
108117

109118
[tool.hatch.envs.hatch-test]
110-
features = ["anthropic", "litellm", "llamaapi", "ollama", "openai"]
119+
features = ["anthropic", "litellm", "llamaapi", "ollama", "openai", "otel"]
111120
extra-dependencies = [
112121
"moto>=5.1.0,<6.0.0",
113122
"pytest>=8.0.0,<9.0.0",
@@ -123,8 +132,11 @@ extra-args = [
123132

124133
[tool.hatch.envs.dev]
125134
dev-mode = true
126-
features = ["dev", "docs", "anthropic", "litellm", "llamaapi", "ollama"]
135+
features = ["dev", "docs", "anthropic", "litellm", "llamaapi", "ollama", "otel"]
127136

137+
[tool.hatch.envs.a2a]
138+
dev-mode = true
139+
features = ["dev", "docs", "anthropic", "litellm", "llamaapi", "ollama", "a2a"]
128140

129141

130142
[[tool.hatch.envs.hatch-test.matrix]]

src/strands/telemetry/tracer.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import opentelemetry.trace as trace_api
1515
from opentelemetry import propagate
1616
from opentelemetry.baggage.propagation import W3CBaggagePropagator
17-
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
1817
from opentelemetry.propagators.composite import CompositePropagator
1918
from opentelemetry.sdk.resources import Resource
2019
from opentelemetry.sdk.trace import TracerProvider as SDKTracerProvider
@@ -30,6 +29,19 @@
3029

3130
logger = logging.getLogger(__name__)
3231

32+
HAS_OTEL_EXPORTER_MODULE = False
33+
OTEL_EXPORTER_MODULE_ERROR = (
34+
"opentelemetry-exporter-otlp-proto-http not detected;"
35+
"please install strands-agents with the optional 'otel' target"
36+
"otel http exporting is currently DISABLED"
37+
)
38+
try:
39+
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
40+
41+
HAS_OTEL_EXPORTER_MODULE = True
42+
except ImportError:
43+
pass
44+
3345

3446
class JSONEncoder(json.JSONEncoder):
3547
"""Custom JSON encoder that handles non-serializable types."""
@@ -181,7 +193,7 @@ def _initialize_tracer(self) -> None:
181193
self.tracer_provider.add_span_processor(console_processor)
182194

183195
# Add OTLP exporter if endpoint is provided
184-
if self.otlp_endpoint and self.tracer_provider:
196+
if HAS_OTEL_EXPORTER_MODULE and self.otlp_endpoint and self.tracer_provider:
185197
try:
186198
# Ensure endpoint has the right format
187199
endpoint = self.otlp_endpoint
@@ -206,6 +218,8 @@ def _initialize_tracer(self) -> None:
206218
logger.info("endpoint=<%s> | OTLP exporter configured with endpoint", endpoint)
207219
except Exception as e:
208220
logger.exception("error=<%s> | Failed to configure OTLP exporter", e)
221+
elif self.otlp_endpoint and self.tracer_provider:
222+
logger.warning(OTEL_EXPORTER_MODULE_ERROR)
209223

210224
# Set as global tracer provider
211225
trace_api.set_tracer_provider(self.tracer_provider)
@@ -294,7 +308,7 @@ def _end_span(
294308
finally:
295309
span.end()
296310
# Force flush to ensure spans are exported
297-
if self.tracer_provider and hasattr(self.tracer_provider, 'force_flush'):
311+
if self.tracer_provider and hasattr(self.tracer_provider, "force_flush"):
298312
try:
299313
self.tracer_provider.force_flush()
300314
except Exception as e:

tests-integ/test_mcp_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ def test_can_reuse_mcp_client():
104104

105105

106106
@pytest.mark.skipif(
107-
condition=os.environ.get("GITHUB_ACTIONS") == 'true',
108-
reason="streamable transport is failing in GitHub actions, debugging if linux compatibility issue"
107+
condition=os.environ.get("GITHUB_ACTIONS") == "true",
108+
reason="streamable transport is failing in GitHub actions, debugging if linux compatibility issue",
109109
)
110110
def test_streamable_http_mcp_client():
111111
server_thread = threading.Thread(

tests/strands/telemetry/test_tracer.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ def mock_set_tracer_provider():
5858

5959
@pytest.fixture
6060
def mock_otlp_exporter():
61-
with mock.patch("strands.telemetry.tracer.OTLPSpanExporter") as mock_exporter:
61+
with (
62+
mock.patch("strands.telemetry.tracer.HAS_OTEL_EXPORTER_MODULE", True),
63+
mock.patch("opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter") as mock_exporter,
64+
):
6265
yield mock_exporter
6366

6467

@@ -199,7 +202,11 @@ def test_initialize_tracer_with_otlp(
199202
mock_resource.create.return_value = mock_resource_instance
200203

201204
# Initialize Tracer
202-
Tracer(otlp_endpoint="http://test-endpoint")
205+
with (
206+
mock.patch("strands.telemetry.tracer.HAS_OTEL_EXPORTER_MODULE", True),
207+
mock.patch("strands.telemetry.tracer.OTLPSpanExporter", mock_otlp_exporter),
208+
):
209+
Tracer(otlp_endpoint="http://test-endpoint")
203210

204211
# Verify the tracer provider was created with correct resource
205212
mock_tracer_provider.assert_called_once_with(resource=mock_resource_instance)
@@ -508,7 +515,11 @@ def test_initialize_tracer_with_invalid_otlp_endpoint(
508515
# This should not raise an exception, but should log an error
509516

510517
# Initialize Tracer
511-
Tracer(otlp_endpoint="http://invalid-endpoint")
518+
with (
519+
mock.patch("strands.telemetry.tracer.HAS_OTEL_EXPORTER_MODULE", True),
520+
mock.patch("strands.telemetry.tracer.OTLPSpanExporter", mock_otlp_exporter),
521+
):
522+
Tracer(otlp_endpoint="http://invalid-endpoint")
512523

513524
# Verify the tracer provider was created with correct resource
514525
mock_tracer_provider.assert_called_once_with(resource=mock_resource_instance)

0 commit comments

Comments
 (0)
0