Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def _create_invocation(
)

server_address, server_port = get_server_address_and_port(instance)
invocation = handler.start_inference(
invocation = handler.inference(
provider=ANTHROPIC,
request_model=request_model,
server_address=server_address,
Expand Down
2 changes: 1 addition & 1 deletion util/opentelemetry-util-genai/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Apply attribute for sampling on instantiation of all invocation types.
([#4553](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4553))
- Minor code cleanup and changes in preparation of moving google's GenAI instrumentation
library to use this util library ([#4556](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4556))
library to use this util library ([#4556](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4556), [#4570](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4570))

## Version 0.4b0 (2026-05-01)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

import timeit
from abc import ABC, abstractmethod
from contextlib import contextmanager
from contextvars import Token
from dataclasses import asdict
from typing import TYPE_CHECKING, Any, Iterator, Sequence
from types import TracebackType
from typing import TYPE_CHECKING, Any, Sequence

from typing_extensions import Self, TypeAlias
from typing_extensions import TypeAlias

from opentelemetry._logs import Logger, LogRecord
from opentelemetry.context import Context, attach, detach
Expand Down Expand Up @@ -138,6 +138,20 @@ def _call_completion_hook(
log_record=log_record,
)

def __enter__(self):
return self

def __exit__(
self,
type_: type[BaseException] | None,
value: BaseException | None,
traceback: TracebackType | None,
) -> None:
if value:
self.fail(value)
raise
self.stop()

@abstractmethod
def _apply_finish(self, error: Error | None = None) -> None:
"""Apply finish telemetry (attributes, metrics, events)."""
Expand Down Expand Up @@ -165,16 +179,6 @@ def fail(self, error: Error | BaseException) -> None:
error = Error(type=type(error), message=str(error))
self._finish(error)

@contextmanager
def _managed(self) -> Iterator[Self]:
"""Context manager that calls stop() on success or fail() on exception."""
try:
yield self
except Exception as exc:
self.fail(exc)
raise
self.stop()


def get_content_attributes(
*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from opentelemetry.util.genai._invocation import Error, GenAIInvocation
from opentelemetry.util.genai.completion_hook import CompletionHook
from opentelemetry.util.genai.metrics import InvocationMetricsRecorder
from opentelemetry.util.genai.utils import gen_ai_json_dumps


class ToolInvocation(GenAIInvocation):
Expand Down Expand Up @@ -47,6 +48,7 @@ def __init__(
tool_call_id: str | None = None,
tool_type: str | None = None,
tool_description: str | None = None,
tool_result: Any = None,
) -> None:
"""Use handler.start_tool(name) or handler.tool(name) instead of calling this directly."""
_operation_name = GenAI.GenAiOperationNameValues.EXECUTE_TOOL.value
Expand All @@ -63,7 +65,7 @@ def __init__(
self.tool_call_id = tool_call_id
self.tool_type = tool_type
self.tool_description = tool_description
self.tool_result: Any = None
self.tool_result: Any = tool_result
self._start(self._get_base_attributes())

def _get_base_attributes(self) -> dict[str, Any]:
Expand Down Expand Up @@ -94,7 +96,18 @@ def _apply_finish(self, error: Error | None = None) -> None:
(GenAI.GEN_AI_TOOL_CALL_ID, self.tool_call_id),
(GenAI.GEN_AI_TOOL_TYPE, self.tool_type),
(GenAI.GEN_AI_TOOL_DESCRIPTION, self.tool_description),
(GenAI.GEN_AI_TOOL_CALL_ARGUMENTS, self.arguments),
(
GenAI.GEN_AI_TOOL_CALL_ARGUMENTS,
gen_ai_json_dumps(self.arguments)
if self.arguments is not None
else None,
),
(
GenAI.GEN_AI_TOOL_CALL_RESULT,
gen_ai_json_dumps(self.tool_result)
if self.tool_result is not None
else None,
),
)
attributes: dict[str, Any] = {
GenAI.GEN_AI_OPERATION_NAME: self._operation_name,
Expand Down
Loading
Loading