- Notifications
You must be signed in to change notification settings - Fork9
Move span methods to LaminarSpan, propagate association properties down to the context#225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
10 commits Select commitHold shift + click to select a range
bf22d8a start span params, global metadata, span object, add_tags
dinmukhamedm959a29c properly propagate association properties
dinmukhamedm8b738bf pass other association props in LaminarSpanContext
dinmukhamedm08c348a fix laminar span context when attributes not available
dinmukhamedmc957918 tiny refactor
dinmukhamedm3dca2c4 remove global metadata accessor
dinmukhamedmed85a5d refactor one function, rename get_current_span, more span nesting in …
dinmukhamedmff9b8ed fix content detachment if inner use_span fails
dinmukhamedm503ee39 fix small bugs
dinmukhamedm4d9a957 revert json_dumps change in observe (caused double dumps) issues
dinmukhamedmFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
3 changes: 2 additions & 1 deletionpyproject.toml
This file contains hidden or 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
2 changes: 2 additions & 0 deletionssrc/lmnr/__init__.py
This file contains hidden or 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
2 changes: 0 additions & 2 deletionssrc/lmnr/opentelemetry_lib/__init__.py
This file contains hidden or 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
136 changes: 56 additions & 80 deletionssrc/lmnr/opentelemetry_lib/decorators/__init__.py
This file contains hidden or 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,75 +1,40 @@ | ||
| from functools import wraps | ||
| import types | ||
| from typing import Any, AsyncGenerator, Callable, Generator, Literal, TypeVar | ||
| from opentelemetry import context as context_api | ||
| from opentelemetry.trace import Span, Status, StatusCode | ||
| from lmnr.opentelemetry_lib.tracing.context import ( | ||
| CONTEXT_METADATA_KEY, | ||
| attach_context, | ||
| detach_context, | ||
| get_event_attributes_from_context, | ||
| ) | ||
| from lmnr.opentelemetry_lib.tracing.span import LaminarSpan | ||
| from lmnr.opentelemetry_lib.tracing.utils import set_association_props_in_context | ||
| from lmnr.sdk.utils import get_input_from_func_args, is_method | ||
| from lmnr.opentelemetry_lib.tracing.tracer import get_tracer_with_context | ||
| from lmnr.opentelemetry_lib.tracing.attributes import ( | ||
| ASSOCIATION_PROPERTIES, | ||
| METADATA, | ||
| SPAN_TYPE, | ||
| ) | ||
| from lmnr.opentelemetry_lib.tracing import TracerWrapper | ||
| from lmnr.sdk.log import get_default_logger | ||
| from lmnr.sdk.utils import is_otel_attribute_value_type, json_dumps | ||
| logger = get_default_logger(__name__) | ||
| F = TypeVar("F", bound=Callable[..., Any]) | ||
| def _setup_span( | ||
| span_name: str, | ||
| span_type: str, | ||
| association_properties: dict[str, Any] | None, | ||
| preserve_global_context: bool = False, | ||
| metadata: dict[str, Any] | None = None, | ||
| ): | ||
| """Set up a span with the given name, type, and association properties.""" | ||
| with get_tracer_with_context() as (tracer, isolated_context): | ||
| @@ -80,6 +45,17 @@ def _setup_span( | ||
| attributes={SPAN_TYPE: span_type}, | ||
| ) | ||
| ctx_metadata = context_api.get_value(CONTEXT_METADATA_KEY, isolated_context) | ||
| merged_metadata = { | ||
| **(ctx_metadata or {}), | ||
| **(metadata or {}), | ||
| } | ||
| for key, value in merged_metadata.items(): | ||
| span.set_attribute( | ||
| f"{ASSOCIATION_PROPERTIES}.{METADATA}.{key}", | ||
| (value if is_otel_attribute_value_type(value) else json_dumps(value)), | ||
| ) | ||
| if association_properties is not None: | ||
| for key, value in association_properties.items(): | ||
| span.set_attribute(f"{ASSOCIATION_PROPERTIES}.{key}", value) | ||
| @@ -103,23 +79,18 @@ def _process_input( | ||
| try: | ||
| if input_formatter is not None: | ||
| inp = input_formatter(*args, **kwargs) | ||
| else: | ||
| inp = get_input_from_func_args( | ||
| fn, | ||
| is_method=is_method(fn), | ||
| func_args=args, | ||
| func_kwargs=kwargs, | ||
| ignore_inputs=ignore_inputs, | ||
| ) | ||
| if not isinstance(span, LaminarSpan): | ||
| span = LaminarSpan(span) | ||
| span.set_input(inp) | ||
dinmukhamedm marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| except Exception: | ||
| msg = "Failed to process input, ignoring" | ||
| if input_formatter is not None: | ||
| @@ -144,15 +115,12 @@ def _process_output( | ||
| try: | ||
| if output_formatter is not None: | ||
| output = output_formatter(result) | ||
| else: | ||
| output = result | ||
| if not isinstance(span, LaminarSpan): | ||
| span = LaminarSpan(span) | ||
| span.set_output(output) | ||
| except Exception: | ||
| msg = "Failed to process output, ignoring" | ||
| if output_formatter is not None: | ||
| @@ -177,6 +145,7 @@ def observe_base( | ||
| ignore_inputs: list[str] | None = None, | ||
| ignore_output: bool = False, | ||
| span_type: Literal["DEFAULT", "LLM", "TOOL"] = "DEFAULT", | ||
| metadata: dict[str, Any] | None = None, | ||
| association_properties: dict[str, Any] | None = None, | ||
| input_formatter: Callable[..., str] | None = None, | ||
| output_formatter: Callable[..., str] | None = None, | ||
| @@ -192,17 +161,20 @@ def wrap(*args, **kwargs): | ||
| wrapper = TracerWrapper() | ||
| span = _setup_span( | ||
| span_name, | ||
| span_type, | ||
| association_properties, | ||
| preserve_global_context, | ||
| metadata, | ||
| ) | ||
| # Set association props in context before push_span_context | ||
| # so child spans inherit them | ||
| assoc_props_token = set_association_props_in_context(span) | ||
| if assoc_props_token and isinstance(span, LaminarSpan): | ||
| span._lmnr_assoc_props_token = assoc_props_token | ||
| new_context = wrapper.push_span_context(span) | ||
| # Some auto-instrumentations are not under our control, so they | ||
| # don't have access to our isolated context. We attach the context | ||
| # to the OTEL global context, so that spans know their parent | ||
| @@ -255,6 +227,7 @@ def async_observe_base( | ||
| ignore_inputs: list[str] | None = None, | ||
| ignore_output: bool = False, | ||
| span_type: Literal["DEFAULT", "LLM", "TOOL"] = "DEFAULT", | ||
| metadata: dict[str, Any] | None = None, | ||
| association_properties: dict[str, Any] | None = None, | ||
| input_formatter: Callable[..., str] | None = None, | ||
| output_formatter: Callable[..., str] | None = None, | ||
| @@ -270,17 +243,20 @@ async def wrap(*args, **kwargs): | ||
| wrapper = TracerWrapper() | ||
| span = _setup_span( | ||
| span_name, | ||
| span_type, | ||
| association_properties, | ||
| preserve_global_context, | ||
| metadata, | ||
| ) | ||
| # Set association props in context before push_span_context | ||
| # so child spans inherit them | ||
| assoc_props_token = set_association_props_in_context(span) | ||
| if assoc_props_token and isinstance(span, LaminarSpan): | ||
| span._lmnr_assoc_props_token = assoc_props_token | ||
| new_context = wrapper.push_span_context(span) | ||
| # Some auto-instrumentations are not under our control, so they | ||
| # don't have access to our isolated context. We attach the context | ||
| # to the OTEL global context, so that spans know their parent | ||
2 changes: 1 addition & 1 deletionsrc/lmnr/opentelemetry_lib/litellm/__init__.py
This file contains hidden or 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
3 changes: 1 addition & 2 deletionssrc/lmnr/opentelemetry_lib/opentelemetry/instrumentation/claude_agent/__init__.py
This file contains hidden or 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
2 changes: 1 addition & 1 deletionsrc/lmnr/opentelemetry_lib/opentelemetry/instrumentation/cua_agent/__init__.py
This file contains hidden or 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
3 changes: 1 addition & 2 deletionssrc/lmnr/opentelemetry_lib/opentelemetry/instrumentation/cua_computer/__init__.py
This file contains hidden or 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
2 changes: 1 addition & 1 deletionsrc/lmnr/opentelemetry_lib/opentelemetry/instrumentation/google_genai/__init__.py
This file contains hidden or 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
3 changes: 1 addition & 2 deletionssrc/lmnr/opentelemetry_lib/opentelemetry/instrumentation/kernel/__init__.py
This file contains hidden or 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
2 changes: 1 addition & 1 deletionsrc/lmnr/opentelemetry_lib/opentelemetry/instrumentation/kernel/utils.py
This file contains hidden or 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
2 changes: 1 addition & 1 deletionsrc/lmnr/opentelemetry_lib/opentelemetry/instrumentation/openai/v1/responses_wrappers.py
This file contains hidden or 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
3 changes: 1 addition & 2 deletionssrc/lmnr/opentelemetry_lib/opentelemetry/instrumentation/openhands_ai/__init__.py
This file contains hidden or 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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.