-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add messaging attributes to telemetry spans (#490)
* Downgrade protobuf from v5 to v4 * Refactor telemetry and include attributes * Update * Remove unused vars --------- Co-authored-by: Eric Zhu <[email protected]>
- Loading branch information
1 parent
1ba7a68
commit e25bd2c
Showing
7 changed files
with
429 additions
and
240 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
8 changes: 5 additions & 3 deletions
8
python/packages/autogen-core/src/autogen_core/application/telemetry/__init__.py
This file contains 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 |
---|---|---|
@@ -1,15 +1,17 @@ | ||
from ._tracing import ( | ||
from ._propagation import ( | ||
EnvelopeMetadata, | ||
TelemetryMetadataContainer, | ||
get_telemetry_envelope_metadata, | ||
get_telemetry_grpc_metadata, | ||
trace_block, | ||
) | ||
from ._tracing import TraceHelper | ||
from ._tracing_config import MessageRuntimeTracingConfig | ||
|
||
__all__ = [ | ||
"EnvelopeMetadata", | ||
"get_telemetry_envelope_metadata", | ||
"get_telemetry_grpc_metadata", | ||
"TelemetryMetadataContainer", | ||
"trace_block", | ||
"TraceHelper", | ||
"MessageRuntimeTracingConfig", | ||
] |
1 change: 1 addition & 0 deletions
1
python/packages/autogen-core/src/autogen_core/application/telemetry/_constants.py
This file contains 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 @@ | ||
NAMESPACE = "autogen" |
97 changes: 97 additions & 0 deletions
97
python/packages/autogen-core/src/autogen_core/application/telemetry/_propagation.py
This file contains 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,97 @@ | ||
from dataclasses import dataclass | ||
from typing import Dict, Mapping, Optional | ||
|
||
from opentelemetry.context import Context | ||
from opentelemetry.propagate import extract | ||
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator | ||
|
||
|
||
@dataclass(kw_only=True) | ||
class EnvelopeMetadata: | ||
"""Metadata for an envelope.""" | ||
|
||
traceparent: Optional[str] = None | ||
tracestate: Optional[str] = None | ||
|
||
|
||
def _get_carrier_for_envelope_metadata(envelope_metadata: EnvelopeMetadata) -> Dict[str, str]: | ||
carrier: Dict[str, str] = {} | ||
if envelope_metadata.traceparent is not None: | ||
carrier["traceparent"] = envelope_metadata.traceparent | ||
if envelope_metadata.tracestate is not None: | ||
carrier["tracestate"] = envelope_metadata.tracestate | ||
return carrier | ||
|
||
|
||
def get_telemetry_envelope_metadata() -> EnvelopeMetadata: | ||
""" | ||
Retrieves the telemetry envelope metadata. | ||
Returns: | ||
EnvelopeMetadata: The envelope metadata containing the traceparent and tracestate. | ||
""" | ||
carrier: Dict[str, str] = {} | ||
TraceContextTextMapPropagator().inject(carrier) | ||
return EnvelopeMetadata( | ||
traceparent=carrier.get("traceparent"), | ||
tracestate=carrier.get("tracestate"), | ||
) | ||
|
||
|
||
def _get_carrier_for_remote_call_metadata(remote_call_metadata: Mapping[str, str]) -> Dict[str, str]: | ||
carrier: Dict[str, str] = {} | ||
traceparent = remote_call_metadata.get("traceparent") | ||
tracestate = remote_call_metadata.get("tracestate") | ||
if traceparent: | ||
carrier["traceparent"] = traceparent | ||
if tracestate: | ||
carrier["tracestate"] = tracestate | ||
return carrier | ||
|
||
|
||
def get_telemetry_grpc_metadata(existingMetadata: Optional[Mapping[str, str]] = None) -> Dict[str, str]: | ||
""" | ||
Retrieves the telemetry gRPC metadata. | ||
Args: | ||
existingMetadata (Optional[Mapping[str, str]]): The existing metadata to include in the gRPC metadata. | ||
Returns: | ||
Mapping[str, str]: The gRPC metadata containing the traceparent and tracestate. | ||
""" | ||
carrier: Dict[str, str] = {} | ||
TraceContextTextMapPropagator().inject(carrier) | ||
traceparent = carrier.get("traceparent") | ||
tracestate = carrier.get("tracestate") | ||
metadata: Dict[str, str] = {} | ||
if existingMetadata is not None: | ||
for key, value in existingMetadata.items(): | ||
metadata[key] = value | ||
if traceparent is not None: | ||
metadata["traceparent"] = traceparent | ||
if tracestate is not None: | ||
metadata["tracestate"] = tracestate | ||
return metadata | ||
|
||
|
||
TelemetryMetadataContainer = Optional[EnvelopeMetadata] | Mapping[str, str] | ||
|
||
|
||
def get_telemetry_context(metadata: TelemetryMetadataContainer) -> Context: | ||
""" | ||
Retrieves the telemetry context from the given metadata. | ||
Args: | ||
metadata (Optional[EnvelopeMetadata]): The metadata containing the telemetry context. | ||
Returns: | ||
Context: The telemetry context extracted from the metadata, or an empty context if the metadata is None. | ||
""" | ||
if metadata is None: | ||
return Context() | ||
elif isinstance(metadata, EnvelopeMetadata): | ||
return extract(_get_carrier_for_envelope_metadata(metadata)) | ||
elif hasattr(metadata, "__getitem__"): | ||
return extract(_get_carrier_for_remote_call_metadata(metadata)) | ||
else: | ||
raise ValueError(f"Unknown metadata type: {type(metadata)}") |
Oops, something went wrong.