opentelemetry-instrumentation-openai 0.24.0__py3-none-any.whl → 0.25.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of opentelemetry-instrumentation-openai might be problematic. Click here for more details.

@@ -1,4 +1,4 @@
1
- from typing import Collection
1
+ from typing import Callable, Collection
2
2
 
3
3
  from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
4
4
 
@@ -18,11 +18,13 @@ class OpenAIInstrumentor(BaseInstrumentor):
18
18
  enrich_assistant: bool = False,
19
19
  enrich_token_usage: bool = False,
20
20
  exception_logger=None,
21
+ get_common_metrics_attributes: Callable[[], dict] = lambda: {},
21
22
  ):
22
23
  super().__init__()
23
24
  Config.enrich_assistant = enrich_assistant
24
25
  Config.enrich_token_usage = enrich_token_usage
25
26
  Config.exception_logger = exception_logger
27
+ Config.get_common_metrics_attributes = get_common_metrics_attributes
26
28
 
27
29
  def instrumentation_dependencies(self) -> Collection[str]:
28
30
  return _instruments
@@ -8,6 +8,7 @@ from importlib.metadata import version
8
8
 
9
9
  from opentelemetry import context as context_api
10
10
 
11
+ from opentelemetry.instrumentation.openai.shared.config import Config
11
12
  from opentelemetry.semconv.ai import SpanAttributes
12
13
  from opentelemetry.instrumentation.openai.utils import (
13
14
  dont_throw,
@@ -256,10 +257,13 @@ def _token_type(token_type: str):
256
257
  return None
257
258
 
258
259
 
259
- def _metric_shared_attributes(
260
+ def metric_shared_attributes(
260
261
  response_model: str, operation: str, server_address: str, is_streaming: bool = False
261
262
  ):
263
+ attributes = Config.get_common_metrics_attributes()
264
+
262
265
  return {
266
+ **attributes,
263
267
  SpanAttributes.LLM_SYSTEM: "openai",
264
268
  SpanAttributes.LLM_RESPONSE_MODEL: response_model,
265
269
  "gen_ai.operation.name": operation,
@@ -1,12 +1,17 @@
1
1
  import json
2
2
  import logging
3
3
  import time
4
+ from opentelemetry.instrumentation.openai.shared.config import Config
4
5
  from wrapt import ObjectProxy
5
6
 
6
7
 
7
8
  from opentelemetry import context as context_api
8
9
  from opentelemetry.metrics import Counter, Histogram
9
- from opentelemetry.semconv.ai import SpanAttributes, LLMRequestTypeValues
10
+ from opentelemetry.semconv.ai import (
11
+ SUPPRESS_LANGUAGE_MODEL_INSTRUMENTATION_KEY,
12
+ SpanAttributes,
13
+ LLMRequestTypeValues,
14
+ )
10
15
 
11
16
  from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
12
17
  from opentelemetry.instrumentation.openai.utils import (
@@ -14,7 +19,7 @@ from opentelemetry.instrumentation.openai.utils import (
14
19
  dont_throw,
15
20
  )
16
21
  from opentelemetry.instrumentation.openai.shared import (
17
- _metric_shared_attributes,
22
+ metric_shared_attributes,
18
23
  _set_client_attributes,
19
24
  _set_request_attributes,
20
25
  _set_span_attribute,
@@ -56,7 +61,9 @@ def chat_wrapper(
56
61
  args,
57
62
  kwargs,
58
63
  ):
59
- if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY):
64
+ if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY) or context_api.get_value(
65
+ SUPPRESS_LANGUAGE_MODEL_INSTRUMENTATION_KEY
66
+ ):
60
67
  return wrapped(*args, **kwargs)
61
68
 
62
69
  # span needs to be opened and closed manually because the response is a generator
@@ -146,7 +153,9 @@ async def achat_wrapper(
146
153
  args,
147
154
  kwargs,
148
155
  ):
149
- if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY):
156
+ if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY) or context_api.get_value(
157
+ SUPPRESS_LANGUAGE_MODEL_INSTRUMENTATION_KEY
158
+ ):
150
159
  return wrapped(*args, **kwargs)
151
160
 
152
161
  span = tracer.start_span(
@@ -164,7 +173,9 @@ async def achat_wrapper(
164
173
  end_time = time.time()
165
174
  duration = end_time - start_time if "start_time" in locals() else 0
166
175
 
176
+ common_attributes = Config.get_common_metrics_attributes()
167
177
  attributes = {
178
+ **common_attributes,
168
179
  "error.type": e.__class__.__name__,
169
180
  }
170
181
 
@@ -269,7 +280,7 @@ def _handle_response(
269
280
  def _set_chat_metrics(
270
281
  instance, token_counter, choice_counter, duration_histogram, response_dict, duration
271
282
  ):
272
- shared_attributes = _metric_shared_attributes(
283
+ shared_attributes = metric_shared_attributes(
273
284
  response_model=response_dict.get("model") or None,
274
285
  operation="chat",
275
286
  server_address=_get_openai_base_url(instance),
@@ -546,7 +557,7 @@ class ChatStream(ObjectProxy):
546
557
  _accumulate_stream_items(item, self._complete_response)
547
558
 
548
559
  def _shared_attributes(self):
549
- return _metric_shared_attributes(
560
+ return metric_shared_attributes(
550
561
  response_model=self._complete_response.get("model")
551
562
  or self._request_kwargs.get("model")
552
563
  or None,
@@ -2,7 +2,11 @@ import logging
2
2
 
3
3
  from opentelemetry import context as context_api
4
4
 
5
- from opentelemetry.semconv.ai import SpanAttributes, LLMRequestTypeValues
5
+ from opentelemetry.semconv.ai import (
6
+ SUPPRESS_LANGUAGE_MODEL_INSTRUMENTATION_KEY,
7
+ SpanAttributes,
8
+ LLMRequestTypeValues,
9
+ )
6
10
 
7
11
  from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
8
12
  from opentelemetry.instrumentation.openai.utils import _with_tracer_wrapper, dont_throw
@@ -33,7 +37,9 @@ logger = logging.getLogger(__name__)
33
37
 
34
38
  @_with_tracer_wrapper
35
39
  def completion_wrapper(tracer, wrapped, instance, args, kwargs):
36
- if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY):
40
+ if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY) or context_api.get_value(
41
+ SUPPRESS_LANGUAGE_MODEL_INSTRUMENTATION_KEY
42
+ ):
37
43
  return wrapped(*args, **kwargs)
38
44
 
39
45
  # span needs to be opened and closed manually because the response is a generator
@@ -58,7 +64,9 @@ def completion_wrapper(tracer, wrapped, instance, args, kwargs):
58
64
 
59
65
  @_with_tracer_wrapper
60
66
  async def acompletion_wrapper(tracer, wrapped, instance, args, kwargs):
61
- if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY):
67
+ if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY) or context_api.get_value(
68
+ SUPPRESS_LANGUAGE_MODEL_INSTRUMENTATION_KEY
69
+ ):
62
70
  return wrapped(*args, **kwargs)
63
71
 
64
72
  span = tracer.start_span(
@@ -1,4 +1,8 @@
1
+ from typing import Callable
2
+
3
+
1
4
  class Config:
2
5
  enrich_token_usage = False
3
6
  enrich_assistant = False
4
7
  exception_logger = None
8
+ get_common_metrics_attributes: Callable[[], dict] = lambda: {}
@@ -3,7 +3,11 @@ import time
3
3
 
4
4
  from opentelemetry import context as context_api
5
5
  from opentelemetry.metrics import Counter, Histogram
6
- from opentelemetry.semconv.ai import SpanAttributes, LLMRequestTypeValues
6
+ from opentelemetry.semconv.ai import (
7
+ SUPPRESS_LANGUAGE_MODEL_INSTRUMENTATION_KEY,
8
+ SpanAttributes,
9
+ LLMRequestTypeValues,
10
+ )
7
11
 
8
12
  from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
9
13
  from opentelemetry.instrumentation.openai.utils import (
@@ -12,7 +16,7 @@ from opentelemetry.instrumentation.openai.utils import (
12
16
  _with_embeddings_telemetry_wrapper,
13
17
  )
14
18
  from opentelemetry.instrumentation.openai.shared import (
15
- _metric_shared_attributes,
19
+ metric_shared_attributes,
16
20
  _set_client_attributes,
17
21
  _set_request_attributes,
18
22
  _set_span_attribute,
@@ -46,7 +50,9 @@ def embeddings_wrapper(
46
50
  args,
47
51
  kwargs,
48
52
  ):
49
- if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY):
53
+ if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY) or context_api.get_value(
54
+ SUPPRESS_LANGUAGE_MODEL_INSTRUMENTATION_KEY
55
+ ):
50
56
  return wrapped(*args, **kwargs)
51
57
 
52
58
  with tracer.start_as_current_span(
@@ -103,7 +109,9 @@ async def aembeddings_wrapper(
103
109
  args,
104
110
  kwargs,
105
111
  ):
106
- if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY):
112
+ if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY) or context_api.get_value(
113
+ SUPPRESS_LANGUAGE_MODEL_INSTRUMENTATION_KEY
114
+ ):
107
115
  return wrapped(*args, **kwargs)
108
116
 
109
117
  async with start_as_current_span_async(
@@ -190,7 +198,7 @@ def _set_embeddings_metrics(
190
198
  response_dict,
191
199
  duration,
192
200
  ):
193
- shared_attributes = _metric_shared_attributes(
201
+ shared_attributes = metric_shared_attributes(
194
202
  response_model=response_dict.get("model") or None,
195
203
  operation="embeddings",
196
204
  server_address=_get_openai_base_url(instance),
@@ -4,7 +4,7 @@ from opentelemetry import context as context_api
4
4
  from opentelemetry.instrumentation.openai import is_openai_v1
5
5
  from opentelemetry.instrumentation.openai.shared import (
6
6
  _get_openai_base_url,
7
- _metric_shared_attributes,
7
+ metric_shared_attributes,
8
8
  model_as_dict,
9
9
  )
10
10
  from opentelemetry.instrumentation.openai.utils import (
@@ -12,6 +12,7 @@ from opentelemetry.instrumentation.openai.utils import (
12
12
  )
13
13
  from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
14
14
  from opentelemetry.metrics import Counter, Histogram
15
+ from opentelemetry.semconv.ai import SUPPRESS_LANGUAGE_MODEL_INSTRUMENTATION_KEY
15
16
 
16
17
 
17
18
  @_with_image_gen_metric_wrapper
@@ -23,7 +24,9 @@ def image_gen_metrics_wrapper(
23
24
  args,
24
25
  kwargs,
25
26
  ):
26
- if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY):
27
+ if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY) or context_api.get_value(
28
+ SUPPRESS_LANGUAGE_MODEL_INSTRUMENTATION_KEY
29
+ ):
27
30
  return wrapped(*args, **kwargs)
28
31
 
29
32
  try:
@@ -52,7 +55,7 @@ def image_gen_metrics_wrapper(
52
55
  response_dict = response
53
56
 
54
57
  # not provide response.model in ImagesResponse response, use model in request kwargs
55
- shared_attributes = _metric_shared_attributes(
58
+ shared_attributes = metric_shared_attributes(
56
59
  response_model=kwargs.get("model") or None,
57
60
  operation="image_gen",
58
61
  server_address=_get_openai_base_url(instance),
@@ -1 +1 @@
1
- __version__ = "0.24.0"
1
+ __version__ = "0.25.1"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: opentelemetry-instrumentation-openai
3
- Version: 0.24.0
3
+ Version: 0.25.1
4
4
  Summary: OpenTelemetry OpenAI instrumentation
5
5
  Home-page: https://github.com/traceloop/openllmetry/tree/main/packages/opentelemetry-instrumentation-openai
6
6
  License: Apache-2.0
@@ -17,7 +17,7 @@ Provides-Extra: instruments
17
17
  Requires-Dist: opentelemetry-api (>=1.25.0,<2.0.0)
18
18
  Requires-Dist: opentelemetry-instrumentation (>=0.46b0,<0.47)
19
19
  Requires-Dist: opentelemetry-semantic-conventions (>=0.46b0,<0.47)
20
- Requires-Dist: opentelemetry-semantic-conventions-ai (==0.3.3)
20
+ Requires-Dist: opentelemetry-semantic-conventions-ai (==0.3.4)
21
21
  Requires-Dist: tiktoken (>=0.6.0,<1)
22
22
  Project-URL: Repository, https://github.com/traceloop/openllmetry/tree/main/packages/opentelemetry-instrumentation-openai
23
23
  Description-Content-Type: text/markdown
@@ -0,0 +1,17 @@
1
+ opentelemetry/instrumentation/openai/__init__.py,sha256=w0gjgORccQ3EsMiK2dzZg6V7J3pa6PcAuDvy1oofQ5A,1503
2
+ opentelemetry/instrumentation/openai/shared/__init__.py,sha256=U6_JB0RXrK6XK2Xhm450RtgHhZT1qMMSDSLtHjc57mY,8224
3
+ opentelemetry/instrumentation/openai/shared/chat_wrappers.py,sha256=Oy4LGQGY4_khL9_wPBHm1DwU8NwleZw7dUAPc61cAXs,25471
4
+ opentelemetry/instrumentation/openai/shared/completion_wrappers.py,sha256=UnygloMuP0udgMAE0ic6NJUVAqJ1CJKT-YjSe_9Ds0g,6834
5
+ opentelemetry/instrumentation/openai/shared/config.py,sha256=_4AkHau8C44uBc6-fNBl9EzCGPBuj9yDIngTuwSW6ZE,199
6
+ opentelemetry/instrumentation/openai/shared/embeddings_wrappers.py,sha256=FvOKk8FzEJV01bUKqH-dwWe0k5DRhhpCG99ah4AUyUQ,6924
7
+ opentelemetry/instrumentation/openai/shared/image_gen_wrappers.py,sha256=GFgBqTiMcJFUmwKwkHDGpsnM3UXw_UJLBSVcxLbbr9Q,2122
8
+ opentelemetry/instrumentation/openai/utils.py,sha256=3ONjsia3xG4rRMOwjU91liOVpZxoRE39CxJ-C5HxIgQ,3464
9
+ opentelemetry/instrumentation/openai/v0/__init__.py,sha256=FYq3xhtaIdvy7mwCPzxaqNNGzfHEi0Q2JFQl51s6yNo,5475
10
+ opentelemetry/instrumentation/openai/v1/__init__.py,sha256=wDO1rjgeZRNVXXA3IJUdqYVXRsvst7_JTtAjBK-m1Gc,7693
11
+ opentelemetry/instrumentation/openai/v1/assistant_wrappers.py,sha256=4BDLcqOfwl0LFUdAjLE_PgRcWsQYKoCM_okWLCU8A9U,6277
12
+ opentelemetry/instrumentation/openai/v1/event_handler_wrapper.py,sha256=SAzYoun2yyOloofyOWtxpm8E2M9TL3Nm8TgKdNyXHuY,2779
13
+ opentelemetry/instrumentation/openai/version.py,sha256=ACu2Z3Q3TFgYpAno_eu9ssJ1QULjNXvjGvyqDSHrQ_o,23
14
+ opentelemetry_instrumentation_openai-0.25.1.dist-info/METADATA,sha256=k9Wos9c9FrnzFQzDB0YGRoGO235q4F8HaZj7fg_An9k,2255
15
+ opentelemetry_instrumentation_openai-0.25.1.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
16
+ opentelemetry_instrumentation_openai-0.25.1.dist-info/entry_points.txt,sha256=vTBfiX5yXji5YHikuJHEOoBZ1TFdPQ1EI4ctd2pZSeE,93
17
+ opentelemetry_instrumentation_openai-0.25.1.dist-info/RECORD,,
@@ -1,17 +0,0 @@
1
- opentelemetry/instrumentation/openai/__init__.py,sha256=xl3Kvqry9glVhu8VtdknfUE9FpXQ7KWAFqtVlpjE-40,1344
2
- opentelemetry/instrumentation/openai/shared/__init__.py,sha256=vRPcdI_4Tseg7mBYRrDdWzRcnec3vZeC2tPYpLlT2Xc,8076
3
- opentelemetry/instrumentation/openai/shared/chat_wrappers.py,sha256=kUXkZhCwgZSD9UIuls22nSijrJu_J_esdIXnyPV3Avc,25074
4
- opentelemetry/instrumentation/openai/shared/completion_wrappers.py,sha256=-JHfgyxic5I3Wr3Uc_L-U7ztDVFcyovtF37tNLtaW3s,6604
5
- opentelemetry/instrumentation/openai/shared/config.py,sha256=5uekQEnmYo1o6tsTD2IGc-cVmHUo5KmUC4pOVdrFFNk,102
6
- opentelemetry/instrumentation/openai/shared/embeddings_wrappers.py,sha256=gzKmjwI7xhGUUDiIxwuyvSH-YDCTIaa0dXECpPc_vaY,6696
7
- opentelemetry/instrumentation/openai/shared/image_gen_wrappers.py,sha256=uREeAWW0xsCsyCDLVMj3LVbHtP5vlm74uVIpZT3YKzc,1959
8
- opentelemetry/instrumentation/openai/utils.py,sha256=3ONjsia3xG4rRMOwjU91liOVpZxoRE39CxJ-C5HxIgQ,3464
9
- opentelemetry/instrumentation/openai/v0/__init__.py,sha256=FYq3xhtaIdvy7mwCPzxaqNNGzfHEi0Q2JFQl51s6yNo,5475
10
- opentelemetry/instrumentation/openai/v1/__init__.py,sha256=wDO1rjgeZRNVXXA3IJUdqYVXRsvst7_JTtAjBK-m1Gc,7693
11
- opentelemetry/instrumentation/openai/v1/assistant_wrappers.py,sha256=4BDLcqOfwl0LFUdAjLE_PgRcWsQYKoCM_okWLCU8A9U,6277
12
- opentelemetry/instrumentation/openai/v1/event_handler_wrapper.py,sha256=SAzYoun2yyOloofyOWtxpm8E2M9TL3Nm8TgKdNyXHuY,2779
13
- opentelemetry/instrumentation/openai/version.py,sha256=DxtMZD542lg_xb6icrE2d5JOY8oUi-v34i2Ar63ddvs,23
14
- opentelemetry_instrumentation_openai-0.24.0.dist-info/METADATA,sha256=L5fXqUJ0zfCQCdgXyDoWkYt6ISc2njm562DlTmHfgqs,2255
15
- opentelemetry_instrumentation_openai-0.24.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
16
- opentelemetry_instrumentation_openai-0.24.0.dist-info/entry_points.txt,sha256=vTBfiX5yXji5YHikuJHEOoBZ1TFdPQ1EI4ctd2pZSeE,93
17
- opentelemetry_instrumentation_openai-0.24.0.dist-info/RECORD,,