Skip to content

Commit

Permalink
change instrumentation name & fix some nits
Browse files Browse the repository at this point in the history
  • Loading branch information
alizenhom committed Sep 6, 2024
1 parent ec3c320 commit 706c6f2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ def instrumentation_dependencies(self) -> Collection[str]:
def _instrument(self, **kwargs):
"""Enable OpenAI instrumentation."""
tracer_provider = kwargs.get("tracer_provider")
tracer = get_tracer(__name__, "", tracer_provider)
tracer = get_tracer(
__name__,
"",
tracer_provider,
schema_url="https://opentelemetry.io/schemas/1.27.0",
)
wrap_function_wrapper(
module="openai.resources.chat.completions",
name="Completions.create",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
# limitations under the License.


from opentelemetry import trace
from opentelemetry.trace import SpanKind, Span
from opentelemetry.trace.status import Status, StatusCode
from opentelemetry.trace.propagation import set_span_in_context
from .span_attributes import LLMSpanAttributes
from opentelemetry.semconv._incubating.attributes import (
gen_ai_attributes as GenAIAttributes,
error_attributes as ErrorAttributes,
)
from .utils import (
silently_fail,
Expand Down Expand Up @@ -52,11 +51,7 @@ def traced_method(wrapped, instance, args, kwargs):

span_name = f"{attributes.gen_ai_operation_name} {attributes.gen_ai_request_model}"

span = tracer.start_span(
name=span_name,
kind=SpanKind.CLIENT,
context=set_span_in_context(trace.get_current_span()),
)
span = tracer.start_span(name=span_name, kind=SpanKind.CLIENT)
_set_input_attributes(span, attributes)

try:
Expand All @@ -75,7 +70,9 @@ def traced_method(wrapped, instance, args, kwargs):

except Exception as error:
span.set_status(Status(StatusCode.ERROR, str(error)))
span.set_attribute("error.type", error.__class__.__name__)
span.set_attribute(
ErrorAttributes.ERROR_TYPE, type(error).__qualname__
)
span.end()
raise

Expand All @@ -93,7 +90,6 @@ def _set_response_attributes(span, result):
set_span_attribute(
span, GenAIAttributes.GEN_AI_RESPONSE_MODEL, result.model
)
print(result)
if getattr(result, "choices", None):
choices = result.choices
responses = [
Expand Down Expand Up @@ -140,11 +136,6 @@ def _set_response_attributes(span, result):
GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS,
result.usage.completion_tokens,
)
set_span_attribute(
span,
"gen_ai.usage.total_tokens",
result.usage.total_tokens,
)


class StreamWrapper:
Expand Down Expand Up @@ -200,11 +191,6 @@ def cleanup(self):
GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS,
self.completion_tokens,
)
set_span_attribute(
self.span,
"gen_ai.usage.total_tokens",
self.prompt_tokens + self.completion_tokens,
)
set_event_completion(
self.span,
[
Expand All @@ -215,7 +201,6 @@ def cleanup(self):
],
)

self.span.set_status(StatusCode.OK)
self.span.end()
self._span_started = False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

__version__ = "0.48b0.dev"
__version__ = "0.49b0.dev"

0 comments on commit 706c6f2

Please sign in to comment.