Hi I'm Trying to trace only certain openai api calls with openai instrumentor, Is there a way to stop the openai instrumentor object? my current approach:
# needed imports
obj = OpenAIInstrumentor()
obj.instrument()
<my chat completion api calls which I want to trace>
del objAlthough I'm deleting the OpenAIInstrumentor object it still traces all the api calls.
you can try obj.uninstrument()
del obj doesn’t work because it’s actually cached by OTEL internally
do I have to define uninstrument method?
it should be included
import logging
from importlib.metadata import PackageNotFoundError
from importlib.util import find_spec
from typing import Any
from openinference.instrumentation.openai import OpenAIInstrumentor as Instrumentor
from openinference.semconv.resource import ResourceAttributes
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from phoenix.config import get_env_project_name
from custom_exporter import JSONSpanExporter
logger = logging.getLogger(__name__)
class CustomOpenAIInstrumentor(Instrumentor):
def __init__(self, *args: Any, **kwargs: Any) -> None:
if find_spec("openai") is None:
raise PackageNotFoundError("Missing `openai`. Install with `pip install openai`.")
super().__init__()
self.span_exporter = JSONSpanExporter(filename="spans.jsonl")
def instrument(self) -> None:
tracer_provider = trace_sdk.TracerProvider(
resource=Resource({ResourceAttributes.PROJECT_NAME: get_env_project_name()}),
span_limits=trace_sdk.SpanLimits(max_attributes=10_000),
)
tracer_provider.add_span_processor(SimpleSpanProcessor(self.span_exporter))
super().instrument(skip_dep_check=True, tracer_provider=tracer_provider)This is the custom instrumentor we're using ^^^
from openinference.instrumentation import suppress_tracing
completion = model.chat.completions.create(...) # will be traced
with suppress_tracing():
completion = model.chat.completions.create(...) # will not be traced
completion = model.chat.completions.create(...) # will be tracedCan you try this Anuraag T.?
Using obj.uninstrument() will also work, but it will permanently stop instrumenting.
Oh, I'm doing a lot of API calls so for each do I have to add suppress_tracing before each one?
do I need to modify anything for obj.uninstrument() if I'm using a custom Span Exporter?
I don't believe so. Should just be:
obj = OpenAIInstrumentor()
obj.instrument()
# calls are traced here
obj.uninstrument()
# calls are not traced here