Skip to content

Commit

Permalink
- Add OpenInference span kind attribute to Guardrails telemetry spans.
Browse files Browse the repository at this point in the history
- Updated version to 0.6.3 in pyproject.toml.
  • Loading branch information
abhishek9sharma committed Jan 25, 2025
1 parent 6283992 commit 2e91405
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
15 changes: 13 additions & 2 deletions guardrails/telemetry/guard_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Union,
)

from openinference.semconv.trace import SpanAttributes
from opentelemetry import context, trace
from opentelemetry.trace import StatusCode, Tracer, Span, Link, get_tracer

Expand Down Expand Up @@ -153,6 +154,9 @@ def trace_stream_guard(
guard_span = new_span
add_guard_attributes(guard_span, history, res)
add_user_attributes(guard_span)
new_span.set_attribute(
SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL"
)
yield res
except StopIteration:
next_exists = False
Expand All @@ -179,7 +183,9 @@ def trace_guard_execution(
guard_span.set_attribute("guardrails.version", GUARDRAILS_VERSION)
guard_span.set_attribute("type", "guardrails/guard")
guard_span.set_attribute("guard.name", guard_name)

guard_span.set_attribute(
SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL"
)
try:
result = _execute_fn(*args, **kwargs)
if isinstance(result, Iterator) and not isinstance(
Expand Down Expand Up @@ -218,6 +224,9 @@ async def trace_async_stream_guard(

add_guard_attributes(guard_span, history, res)
add_user_attributes(guard_span)
guard_span.set_attribute(
SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL"
)
yield res
except StopIteration:
next_exists = False
Expand Down Expand Up @@ -259,7 +268,9 @@ async def trace_async_guard_execution(
guard_span.set_attribute("guardrails.version", GUARDRAILS_VERSION)
guard_span.set_attribute("type", "guardrails/guard")
guard_span.set_attribute("guard.name", guard_name)

guard_span.set_attribute(
SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL"
)
try:
result = await _execute_fn(*args, **kwargs)
if isinstance(result, AsyncIterator):
Expand Down
3 changes: 2 additions & 1 deletion guardrails/telemetry/open_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
recursive_key_operation,
redact,
)
from openinference.semconv.trace import SpanAttributes


def trace_operation(
Expand Down Expand Up @@ -82,7 +83,7 @@ def trace_llm_call(

if current_span is None:
return

current_span.set_attribute(SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL")
ser_function_call = serialize(function_call)
if ser_function_call:
current_span.set_attribute("llm.function_call", ser_function_call)
Expand Down
11 changes: 11 additions & 0 deletions guardrails/telemetry/runner_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Iterator,
Optional,
)
from openinference.semconv.trace import SpanAttributes

from opentelemetry import context, trace
from opentelemetry.trace import StatusCode, Span
Expand All @@ -26,6 +27,7 @@
)
from guardrails.utils.safe_get import safe_get
from guardrails.version import GUARDRAILS_VERSION
from openinference.semconv.trace import SpanAttributes

import sys

Expand Down Expand Up @@ -83,6 +85,9 @@ def trace_step_wrapper(*args, **kwargs) -> Iteration:
name="step", # type: ignore
context=current_otel_context, # type: ignore
) as step_span:
step_span.set_attribute(
SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL"
)
try:
response = fn(*args, **kwargs)
add_step_attributes(step_span, response, *args, **kwargs)
Expand Down Expand Up @@ -111,6 +116,7 @@ def trace_stream_step_generator(
name="step", # type: ignore
context=current_otel_context, # type: ignore
) as step_span:
step_span.set_attribute(SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL")
try:
gen = fn(*args, **kwargs)
next_exists = True
Expand Down Expand Up @@ -157,10 +163,14 @@ async def trace_async_step_wrapper(*args, **kwargs) -> Iteration:
name="step", # type: ignore
context=current_otel_context, # type: ignore
) as step_span:
step_span.set_attribute(
SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL"
)
try:
response = await fn(*args, **kwargs)
add_user_attributes(step_span)
add_step_attributes(step_span, response, *args, **kwargs)

return response
except Exception as e:
step_span.set_status(status=StatusCode.ERROR, description=str(e))
Expand All @@ -186,6 +196,7 @@ async def trace_async_stream_step_generator(
name="step", # type: ignore
context=current_otel_context, # type: ignore
) as step_span:
step_span.set_attribute(SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL")
try:
gen = fn(*args, **kwargs)
next_exists = True
Expand Down
9 changes: 9 additions & 0 deletions guardrails/telemetry/validator_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from opentelemetry import context, trace
from opentelemetry.trace import StatusCode, Tracer, Span
from openinference.semconv.trace import SpanAttributes


from guardrails.settings import settings
Expand Down Expand Up @@ -103,6 +104,10 @@ def trace_validator_wrapper(*args, **kwargs):
name=validator_span_name, # type: ignore
context=current_otel_context, # type: ignore
) as validator_span:
validator_span.set_attribute(
SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL"
)

try:
resp = fn(*args, **kwargs)
add_user_attributes(validator_span)
Expand Down Expand Up @@ -167,6 +172,10 @@ async def trace_validator_wrapper(*args, **kwargs):
name=validator_span_name, # type: ignore
context=current_otel_context, # type: ignore
) as validator_span:
validator_span.set_attribute(
SpanAttributes.OPENINFERENCE_SPAN_KIND, "GUARDRAIL"
) # see here for a list of span kinds: https://github.com/Arize-ai/openinference/blob/main/python/openinference-semantic-conventions/src/openinference/semconv/trace/__init__.py#L271

try:
resp = await fn(*args, **kwargs)
add_user_attributes(validator_span)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "guardrails-ai"
version = "0.6.2"
version = "0.6.3"
description = "Adding guardrails to large language models."
authors = ["Guardrails AI <[email protected]>"]
license = "Apache License 2.0"
Expand Down

0 comments on commit 2e91405

Please sign in to comment.