forked from traceloop/openllmetry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(instrumentation): implement event-based tracking for AlephAlpha (t…
…raceloop#2456) - Add event-based tracking support while maintaining legacy behavior - Implement use_legacy_attributes config parameter - Add event logger integration with proper trace context - Add streaming support with event buffering - Include comprehensive test coverage - Add sample app integration Part of the larger effort to support event-based tracking across all packages. Signed-off-by: David Anyatonwu <[email protected]>
- Loading branch information
1 parent
fc46e7d
commit 95228e3
Showing
12 changed files
with
1,194 additions
and
134 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,26 @@ | ||
from opentelemetry._events import Event, EventLogger | ||
from opentelemetry.semconv._incubating.attributes import gen_ai_attributes as GenAIAttributes | ||
|
||
def create_prompt_event(prompt: dict, system: str, capture_content: bool = True) -> Event: | ||
"""Creates a standardized prompt event""" | ||
attributes = { | ||
GenAIAttributes.GEN_AI_SYSTEM: system | ||
} | ||
|
||
body = { | ||
"role": prompt.get("role", "user"), | ||
} | ||
|
||
if capture_content and "content" in prompt: | ||
body["content"] = prompt["content"] | ||
|
||
return Event( | ||
name="gen_ai.prompt", | ||
attributes=attributes, | ||
body=body | ||
) | ||
|
||
def create_completion_event(completion: dict, system: str, capture_content: bool = True) -> Event: | ||
"""Creates a standardized completion event""" | ||
# Similar implementation for completions | ||
pass |
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,8 @@ | ||
class AnthropicInstrumentor(BaseInstrumentor): | ||
def __init__(self): | ||
super().__init__() | ||
self.use_legacy_attributes = True | ||
|
||
def _instrument(self, **kwargs): | ||
self.use_legacy_attributes = kwargs.get('use_legacy_attributes', True) | ||
# ... rest of instrumentation |
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,22 @@ | ||
from opentelemetry.instrumentation.ai_providers.utils import create_prompt_event | ||
|
||
|
||
def wrap_completion(tracer, event_logger, capture_content): | ||
def wrapper(wrapped, instance, args, kwargs): | ||
with tracer.start_as_current_span(...) as span: | ||
# Emit prompt event if not using legacy | ||
if not instance.use_legacy_attributes: | ||
event_logger.emit( | ||
create_prompt_event( | ||
kwargs.get("prompt"), | ||
system="anthropic", | ||
capture_content=capture_content | ||
) | ||
) | ||
|
||
# Existing attribute-based logic if using legacy | ||
if instance.use_legacy_attributes: | ||
span.set_attribute("ai.prompt", kwargs.get("prompt")) | ||
|
||
# ... rest of the wrapper | ||
return wrapper |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
3 changes: 3 additions & 0 deletions
3
...entelemetry-instrumentation-alephalpha/opentelemetry/instrumentation/alephalpha/config.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,2 +1,5 @@ | ||
class Config: | ||
"""Config for AlephAlpha instrumentation""" | ||
|
||
exception_logger = None | ||
use_legacy_attributes = True # Controls whether to use legacy attribute-based approach or new event-based approach |
Oops, something went wrong.