Skip to content
Merged
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 @@ -29,6 +29,7 @@
LLMRequestTypeValues,
SpanAttributes,
Meters,
MetricUtils,
)
from opentelemetry.trace import SpanKind, Tracer, get_tracer
from opentelemetry.trace.status import Status, StatusCode
Expand Down Expand Up @@ -372,24 +373,8 @@ def wrapper(wrapped, instance, args, kwargs):


def _create_metrics(meter: Meter):
token_histogram = meter.create_histogram(
name=Meters.LLM_TOKEN_USAGE,
unit="token",
description="Measures number of input and output tokens used",
)

choice_counter = meter.create_counter(
name=Meters.LLM_GENERATION_CHOICES,
unit="choice",
description="Number of choices returned by chat completions call",
)

duration_histogram = meter.create_histogram(
name=Meters.LLM_OPERATION_DURATION,
unit="s",
description="GenAI operation duration",
)

token_histogram, choice_counter, duration_histogram = MetricUtils.basic_metrics(meter)

exception_counter = meter.create_counter(
name=Meters.LLM_ANTHROPIC_COMPLETION_EXCEPTIONS,
unit="time",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
LLMRequestTypeValues,
SpanAttributes,
Meters,
MetricUtils,
)
from opentelemetry.trace import SpanKind, Tracer, get_tracer
from opentelemetry.trace.status import Status, StatusCode
Expand Down Expand Up @@ -245,24 +246,7 @@ def wrapper(wrapped, instance, args, kwargs):


def _create_metrics(meter: Meter):
token_histogram = meter.create_histogram(
name=Meters.LLM_TOKEN_USAGE,
unit="token",
description="Measures number of input and output tokens used",
)

choice_counter = meter.create_counter(
name=Meters.LLM_GENERATION_CHOICES,
unit="choice",
description="Number of choices returned by chat completions call",
)

duration_histogram = meter.create_histogram(
name=Meters.LLM_OPERATION_DURATION,
unit="s",
description="GenAI operation duration",
)

token_histogram, choice_counter, duration_histogram = MetricUtils.basic_metrics(meter)
return token_histogram, choice_counter, duration_histogram


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,26 @@ class TraceloopSpanKindValues(Enum):
AGENT = "agent"
TOOL = "tool"
UNKNOWN = "unknown"

class MetricUtils:

def basic_metrics(meter):
token_histogram = meter.create_histogram(
name=Meters.LLM_TOKEN_USAGE,
unit="token",
description="Measures number of input and output tokens used",
)

choice_counter = meter.create_counter(
name=Meters.LLM_GENERATION_CHOICES,
unit="choice",
description="Number of choices returned by chat completions call",
)

duration_histogram = meter.create_histogram(
name=Meters.LLM_OPERATION_DURATION,
unit="s",
description="GenAI operation duration",
)

return token_histogram, choice_counter, duration_histogram