Skip to content

Commit be87996

Browse files
committed
Add tracing
1 parent 992b0cb commit be87996

8 files changed

Lines changed: 725 additions & 67 deletions

File tree

poetry.lock

Lines changed: 186 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mistralai"
3-
version = "1.9.11"
3+
version = "1.9.12"
44
description = "Python Client SDK for the Mistral AI API."
55
authors = [{ name = "Mistral" },]
66
readme = "README-PYPI.md"
@@ -13,6 +13,10 @@ dependencies = [
1313
"typing-inspection >=0.4.0",
1414
"pyyaml (>=6.0.2,<7.0.0)",
1515
"invoke (>=2.2.0,<3.0.0)",
16+
"opentelemetry-sdk (>=1.33.1,<2.0.0)",
17+
"opentelemetry-api (>=1.33.1,<2.0.0)",
18+
"opentelemetry-exporter-otlp-proto-http (>=1.37.0,<2.0.0)",
19+
"opentelemetry-semantic-conventions (>=0.59b0,<0.60)",
1620
]
1721

1822
[tool.poetry]

src/mistralai/_hooks/registration.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .custom_user_agent import CustomUserAgentHook
22
from .deprecation_warning import DeprecationWarningHook
3+
from .tracing import TracingHook
34
from .types import Hooks
45

56
# This file is only ever generated once on the first generation and then is free to be modified.
@@ -13,5 +14,9 @@ def init_hooks(hooks: Hooks):
1314
with an instance of a hook that implements that specific Hook interface
1415
Hooks are registered per SDK instance, and are valid for the lifetime of the SDK instance
1516
"""
17+
tracing_hook = TracingHook()
1618
hooks.register_before_request_hook(CustomUserAgentHook())
1719
hooks.register_after_success_hook(DeprecationWarningHook())
20+
hooks.register_after_success_hook(tracing_hook)
21+
hooks.register_before_request_hook(tracing_hook)
22+
hooks.register_after_error_hook(tracing_hook)

src/mistralai/_hooks/tracing.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from .types import (
2+
AfterErrorContext,
3+
AfterErrorHook,
4+
AfterSuccessContext,
5+
AfterSuccessHook,
6+
BeforeRequestContext,
7+
BeforeRequestHook,
8+
)
9+
from ..extra.observability.otel import (
10+
get_traced_request_and_span,
11+
get_traced_response,
12+
get_response_and_error,
13+
get_or_create_otel_tracer,
14+
)
15+
16+
from opentelemetry.trace import Span
17+
from typing import Optional, Tuple, Union
18+
19+
import httpx
20+
import logging
21+
22+
logger = logging.getLogger(__name__)
23+
24+
25+
class TracingHook(BeforeRequestHook, AfterSuccessHook, AfterErrorHook):
26+
def __init__(self) -> None:
27+
self.tracing_enabled, self.tracer = get_or_create_otel_tracer()
28+
self.request_span: Optional[Span] = None
29+
30+
def before_request(
31+
self, hook_ctx: BeforeRequestContext, request: httpx.Request
32+
) -> Union[httpx.Request, Exception]:
33+
request, self.request_span = get_traced_request_and_span(tracing_enabled=self.tracing_enabled, tracer=self.tracer, span=self.request_span, operation_id=hook_ctx.operation_id, request=request)
34+
return request
35+
36+
def after_success(
37+
self, hook_ctx: AfterSuccessContext, response: httpx.Response
38+
) -> Union[httpx.Response, Exception]:
39+
response = get_traced_response(tracing_enabled=self.tracing_enabled, tracer=self.tracer, span=self.request_span, operation_id=hook_ctx.operation_id, response=response)
40+
return response
41+
42+
def after_error(
43+
self,
44+
hook_ctx: AfterErrorContext,
45+
response: Optional[httpx.Response],
46+
error: Optional[Exception],
47+
) -> Union[Tuple[Optional[httpx.Response], Optional[Exception]], Exception]:
48+
if response:
49+
response, error = get_response_and_error(tracing_enabled=self.tracing_enabled, tracer=self.tracer, span=self.request_span, operation_id=hook_ctx.operation_id, response=response, error=error)
50+
return response, error

0 commit comments

Comments
 (0)