Skip to content

Commit fb37b35

Browse files
CopilotnikhilNava
andcommitted
Fix ruff format errors, move _get_sdk_version to utils.py, remove set_request_context from BaggageBuilder
Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>
1 parent 5e2435a commit fb37b35

9 files changed

Lines changed: 32 additions & 55 deletions

File tree

libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/inference_scope.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,7 @@ def __init__(
122122
# Set request metadata if provided
123123
if request and request.source_metadata:
124124
self.set_tag_maybe(CHANNEL_NAME_KEY, request.source_metadata.name)
125-
self.set_tag_maybe(
126-
CHANNEL_LINK_KEY, request.source_metadata.description
127-
)
125+
self.set_tag_maybe(CHANNEL_LINK_KEY, request.source_metadata.description)
128126

129127
def record_input_messages(self, messages: List[str]) -> None:
130128
"""Records the input messages for telemetry tracking.

libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/invoke_agent_scope.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,7 @@ def __init__(
131131
if request:
132132
if request.source_metadata:
133133
self.set_tag_maybe(CHANNEL_NAME_KEY, request.source_metadata.name)
134-
self.set_tag_maybe(
135-
CHANNEL_LINK_KEY, request.source_metadata.description
136-
)
134+
self.set_tag_maybe(CHANNEL_LINK_KEY, request.source_metadata.description)
137135

138136
self.set_tag_maybe(
139137
GEN_AI_EXECUTION_TYPE_KEY,

libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/middleware/baggage_builder.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -226,27 +226,6 @@ def _set(self, key: str, value: str | None) -> None:
226226
if value is not None and value.strip():
227227
self._pairs[key] = value
228228

229-
@staticmethod
230-
def set_request_context(
231-
tenant_id: str | None = None,
232-
agent_id: str | None = None,
233-
) -> "BaggageScope":
234-
"""Convenience method to begin a request baggage scope with common fields.
235-
236-
Args:
237-
tenant_id: The tenant ID
238-
agent_id: The agent ID
239-
240-
Returns:
241-
A context manager that restores the previous baggage on exit
242-
"""
243-
return (
244-
BaggageBuilder()
245-
.tenant_id(tenant_id)
246-
.agent_id(agent_id)
247-
.build()
248-
)
249-
250229

251230
class BaggageScope:
252231
"""Context manager for baggage scope.

libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/opentelemetry_scope.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
TELEMETRY_SDK_VERSION_KEY,
4545
TENANT_ID_KEY,
4646
)
47-
from .utils import parse_parent_id_to_context
47+
from .utils import get_sdk_version, parse_parent_id_to_context
4848

4949
if TYPE_CHECKING:
5050
from .agent_details import AgentDetails
@@ -91,20 +91,6 @@ def _datetime_to_ns(dt: datetime | None) -> int | None:
9191
return None
9292
return int(dt.timestamp() * 1_000_000_000)
9393

94-
@staticmethod
95-
def _get_sdk_version() -> str:
96-
"""Get the SDK version from package metadata.
97-
98-
Returns:
99-
The SDK version string, or "0.0.0-unknown" if not found
100-
"""
101-
try:
102-
from importlib.metadata import version
103-
104-
return version("microsoft-agents-a365-observability-core")
105-
except Exception:
106-
return "0.0.0-unknown"
107-
10894
def __init__(
10995
self,
11096
kind: str,
@@ -184,7 +170,7 @@ def __init__(
184170
# Set telemetry SDK attributes
185171
self._span.set_attribute(TELEMETRY_SDK_NAME_KEY, TELEMETRY_SDK_NAME_VALUE)
186172
self._span.set_attribute(TELEMETRY_SDK_LANGUAGE_KEY, TELEMETRY_SDK_LANGUAGE_VALUE)
187-
self._span.set_attribute(TELEMETRY_SDK_VERSION_KEY, self._get_sdk_version())
173+
self._span.set_attribute(TELEMETRY_SDK_VERSION_KEY, get_sdk_version())
188174

189175
# Set agent details if provided
190176
if agent_details:

libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import warnings
1010
from collections.abc import Callable, Hashable, Iterable, Iterator, Mapping
1111
from enum import Enum
12+
from importlib.metadata import PackageNotFoundError, version
1213
from ipaddress import AddressValueError, ip_address
1314
from threading import RLock
1415
from typing import Any, Generic, TypeVar, cast
@@ -299,6 +300,18 @@ def wrapper(*args, **kwargs):
299300
return decorator
300301

301302

303+
def get_sdk_version() -> str:
304+
"""Get the SDK version from package metadata.
305+
306+
Returns:
307+
The SDK version string, or "0.0.0-unknown" if not found
308+
"""
309+
try:
310+
return version("microsoft-agents-a365-observability-core")
311+
except PackageNotFoundError:
312+
return "0.0.0-unknown"
313+
314+
302315
def validate_and_normalize_ip(ip_string: str | None) -> str | None:
303316
"""Validate and normalize an IP address string.
304317

libraries/microsoft-agents-a365-observability-hosting/microsoft_agents_a365/observability/hosting/middleware/output_logging_middleware.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,8 @@ async def handler(
193193
# Set additional attributes on the scope
194194
output_scope.set_tag_maybe(GEN_AI_CONVERSATION_ID_KEY, conversation_id)
195195
output_scope.set_tag_maybe(GEN_AI_EXECUTION_TYPE_KEY, execution_type)
196-
output_scope.set_tag_maybe(
197-
CHANNEL_NAME_KEY, source_metadata.get("name")
198-
)
199-
output_scope.set_tag_maybe(
200-
CHANNEL_LINK_KEY, source_metadata.get("description")
201-
)
196+
output_scope.set_tag_maybe(CHANNEL_NAME_KEY, source_metadata.get("name"))
197+
output_scope.set_tag_maybe(CHANNEL_LINK_KEY, source_metadata.get("description"))
202198

203199
if caller_details:
204200
output_scope.set_tag_maybe(GEN_AI_CALLER_ID_KEY, caller_details.caller_id)

tests/observability/core/test_baggage_builder.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,7 @@ def test_source_metadata_description_method(self):
241241
# Should set channel description baggage through delegation
242242
with self.builder.source_metadata_description("test-description").build():
243243
current_baggage = baggage.get_all()
244-
self.assertEqual(
245-
current_baggage.get(CHANNEL_LINK_KEY), "test-description"
246-
)
244+
self.assertEqual(current_baggage.get(CHANNEL_LINK_KEY), "test-description")
247245

248246
def test_session_id_method(self):
249247
"""Test session_id method sets session ID baggage."""

tests/observability/extensions/agentframework/integration/test_agentframework_trace_processor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ def _validate_span_attributes(self, agent365_config):
201201
assert attributes[TENANT_ID_KEY] == agent365_config["tenant_id"]
202202

203203
# Check for LLM spans (generation spans)
204-
if GEN_AI_PROVIDER_NAME_KEY in attributes and attributes[GEN_AI_PROVIDER_NAME_KEY] == "openai":
204+
if (
205+
GEN_AI_PROVIDER_NAME_KEY in attributes
206+
and attributes[GEN_AI_PROVIDER_NAME_KEY] == "openai"
207+
):
205208
if GEN_AI_REQUEST_MODEL_KEY in attributes:
206209
llm_spans_found += 1
207210
# Validate LLM span attributes

tests/observability/extensions/openai/integration/test_openai_trace_processor.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,10 @@ def _validate_span_attributes(self, agent365_config):
322322
assert attributes[GEN_AI_AGENT_ID_KEY] == agent365_config["agent_id"]
323323

324324
# Check for LLM spans (generation spans)
325-
if GEN_AI_PROVIDER_NAME_KEY in attributes and attributes[GEN_AI_PROVIDER_NAME_KEY] == "openai":
325+
if (
326+
GEN_AI_PROVIDER_NAME_KEY in attributes
327+
and attributes[GEN_AI_PROVIDER_NAME_KEY] == "openai"
328+
):
326329
if GEN_AI_REQUEST_MODEL_KEY in attributes:
327330
llm_spans_found += 1
328331
# Validate LLM span attributes
@@ -369,7 +372,10 @@ def _validate_tool_span_attributes(self, agent365_config):
369372
assert attributes[GEN_AI_AGENT_ID_KEY] == agent365_config["agent_id"]
370373

371374
# Check for LLM spans (generation spans)
372-
if GEN_AI_PROVIDER_NAME_KEY in attributes and attributes[GEN_AI_PROVIDER_NAME_KEY] == "openai":
375+
if (
376+
GEN_AI_PROVIDER_NAME_KEY in attributes
377+
and attributes[GEN_AI_PROVIDER_NAME_KEY] == "openai"
378+
):
373379
if GEN_AI_REQUEST_MODEL_KEY in attributes:
374380
llm_spans_found += 1
375381
print(f"✓ Found LLM span with model: {attributes[GEN_AI_REQUEST_MODEL_KEY]}")

0 commit comments

Comments
 (0)