Skip to content

Commit d559b0c

Browse files
authored
openai: use proper genai suggested buckets for metrics (elastic#72)
* Bump baseline to 1.31.0 in order to have proper bucket advisory support * openai: use explicit_bucket_boundaries_advisory to use genai suggested buckets * Keep an older version of httpx for baseline testing
1 parent 1514a81 commit d559b0c

File tree

7 files changed

+139
-58
lines changed

7 files changed

+139
-58
lines changed

instrumentation/elastic-opentelemetry-instrumentation-openai/dev-requirements-3.9.txt

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ asgiref==3.8.1
1414
# via opentelemetry-test-utils
1515
build==1.2.2.post1
1616
# via pip-tools
17-
certifi==2024.8.30
17+
certifi==2025.1.31
1818
# via
1919
# httpcore
2020
# httpx
21-
click==8.1.7
21+
click==8.1.8
2222
# via pip-tools
23-
deprecated==1.2.15
23+
deprecated==1.2.18
2424
# via
2525
# opentelemetry-api
2626
# opentelemetry-semantic-conventions
@@ -47,31 +47,31 @@ importlib-metadata==8.5.0
4747
# opentelemetry-api
4848
iniconfig==2.0.0
4949
# via pytest
50-
jiter==0.7.1
50+
jiter==0.9.0
5151
# via openai
5252
multidict==6.1.0
5353
# via yarl
5454
numpy==1.24.4
5555
# via elastic-opentelemetry-instrumentation-openai (pyproject.toml)
56-
openai==1.54.4
56+
openai==1.66.5
5757
# via elastic-opentelemetry-instrumentation-openai (pyproject.toml)
58-
opentelemetry-api==1.28.2
58+
opentelemetry-api==1.31.0
5959
# via
6060
# elastic-opentelemetry-instrumentation-openai (pyproject.toml)
6161
# opentelemetry-instrumentation
6262
# opentelemetry-sdk
6363
# opentelemetry-semantic-conventions
6464
# opentelemetry-test-utils
65-
opentelemetry-instrumentation==0.49b2
65+
opentelemetry-instrumentation==0.52b0
6666
# via elastic-opentelemetry-instrumentation-openai (pyproject.toml)
67-
opentelemetry-sdk==1.28.2
67+
opentelemetry-sdk==1.31.0
6868
# via opentelemetry-test-utils
69-
opentelemetry-semantic-conventions==0.49b2
69+
opentelemetry-semantic-conventions==0.52b0
7070
# via
7171
# elastic-opentelemetry-instrumentation-openai (pyproject.toml)
7272
# opentelemetry-instrumentation
7373
# opentelemetry-sdk
74-
opentelemetry-test-utils==0.49b2
74+
opentelemetry-test-utils==0.52b0
7575
# via elastic-opentelemetry-instrumentation-openai (pyproject.toml)
7676
packaging==24.2
7777
# via
@@ -84,15 +84,17 @@ pluggy==1.5.0
8484
# via pytest
8585
propcache==0.2.0
8686
# via yarl
87-
pydantic==2.9.2
88-
# via openai
89-
pydantic-core==2.23.4
87+
pydantic==2.10.6
88+
# via
89+
# elastic-opentelemetry-instrumentation-openai (pyproject.toml)
90+
# openai
91+
pydantic-core==2.27.2
9092
# via pydantic
9193
pyproject-hooks==1.2.0
9294
# via
9395
# build
9496
# pip-tools
95-
pytest==8.3.3
97+
pytest==8.3.5
9698
# via
9799
# elastic-opentelemetry-instrumentation-openai (pyproject.toml)
98100
# pytest-asyncio
@@ -106,14 +108,13 @@ pyyaml==6.0.2
106108
sniffio==1.3.1
107109
# via
108110
# anyio
109-
# httpx
110111
# openai
111-
tomli==2.1.0
112+
tomli==2.2.1
112113
# via
113114
# build
114115
# pip-tools
115116
# pytest
116-
tqdm==4.67.0
117+
tqdm==4.67.1
117118
# via openai
118119
typing-extensions==4.12.2
119120
# via
@@ -131,9 +132,9 @@ vcrpy==6.0.2
131132
# via
132133
# elastic-opentelemetry-instrumentation-openai (pyproject.toml)
133134
# pytest-vcr
134-
wheel==0.45.0
135+
wheel==0.45.1
135136
# via pip-tools
136-
wrapt==1.16.0
137+
wrapt==1.17.2
137138
# via
138139
# deprecated
139140
# elastic-opentelemetry-instrumentation-openai (pyproject.toml)

instrumentation/elastic-opentelemetry-instrumentation-openai/dev-requirements.txt

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
#
77
annotated-types==0.7.0
88
# via pydantic
9-
anyio==4.6.2.post1
9+
anyio==4.9.0
1010
# via
1111
# httpx
1212
# openai
1313
asgiref==3.8.1
1414
# via opentelemetry-test-utils
1515
build==1.2.2.post1
1616
# via pip-tools
17-
certifi==2024.8.30
17+
certifi==2025.1.31
1818
# via
1919
# httpcore
2020
# httpx
21-
click==8.1.7
21+
click==8.1.8
2222
# via pip-tools
23-
deprecated==1.2.15
23+
deprecated==1.2.18
2424
# via
2525
# opentelemetry-api
2626
# opentelemetry-semantic-conventions
@@ -41,35 +41,35 @@ idna==3.10
4141
# anyio
4242
# httpx
4343
# yarl
44-
importlib-metadata==8.5.0
44+
importlib-metadata==8.6.1
4545
# via opentelemetry-api
4646
iniconfig==2.0.0
4747
# via pytest
48-
jiter==0.7.1
48+
jiter==0.9.0
4949
# via openai
50-
multidict==6.1.0
50+
multidict==6.2.0
5151
# via yarl
52-
numpy==2.1.3
52+
numpy==2.2.4
5353
# via elastic-opentelemetry-instrumentation-openai (pyproject.toml)
54-
openai==1.54.4
54+
openai==1.66.5
5555
# via elastic-opentelemetry-instrumentation-openai (pyproject.toml)
56-
opentelemetry-api==1.28.2
56+
opentelemetry-api==1.31.0
5757
# via
5858
# elastic-opentelemetry-instrumentation-openai (pyproject.toml)
5959
# opentelemetry-instrumentation
6060
# opentelemetry-sdk
6161
# opentelemetry-semantic-conventions
6262
# opentelemetry-test-utils
63-
opentelemetry-instrumentation==0.49b2
63+
opentelemetry-instrumentation==0.52b0
6464
# via elastic-opentelemetry-instrumentation-openai (pyproject.toml)
65-
opentelemetry-sdk==1.28.2
65+
opentelemetry-sdk==1.31.0
6666
# via opentelemetry-test-utils
67-
opentelemetry-semantic-conventions==0.49b2
67+
opentelemetry-semantic-conventions==0.52b0
6868
# via
6969
# elastic-opentelemetry-instrumentation-openai (pyproject.toml)
7070
# opentelemetry-instrumentation
7171
# opentelemetry-sdk
72-
opentelemetry-test-utils==0.49b2
72+
opentelemetry-test-utils==0.52b0
7373
# via elastic-opentelemetry-instrumentation-openai (pyproject.toml)
7474
packaging==24.2
7575
# via
@@ -80,22 +80,24 @@ pip-tools==7.4.1
8080
# via elastic-opentelemetry-instrumentation-openai (pyproject.toml)
8181
pluggy==1.5.0
8282
# via pytest
83-
propcache==0.2.0
83+
propcache==0.3.0
8484
# via yarl
85-
pydantic==2.9.2
86-
# via openai
87-
pydantic-core==2.23.4
85+
pydantic==2.10.6
86+
# via
87+
# elastic-opentelemetry-instrumentation-openai (pyproject.toml)
88+
# openai
89+
pydantic-core==2.27.2
8890
# via pydantic
8991
pyproject-hooks==1.2.0
9092
# via
9193
# build
9294
# pip-tools
93-
pytest==8.3.3
95+
pytest==8.3.5
9496
# via
9597
# elastic-opentelemetry-instrumentation-openai (pyproject.toml)
9698
# pytest-asyncio
9799
# pytest-vcr
98-
pytest-asyncio==0.24.0
100+
pytest-asyncio==0.25.3
99101
# via elastic-opentelemetry-instrumentation-openai (pyproject.toml)
100102
pytest-vcr==1.0.2
101103
# via elastic-opentelemetry-instrumentation-openai (pyproject.toml)
@@ -104,14 +106,13 @@ pyyaml==6.0.2
104106
sniffio==1.3.1
105107
# via
106108
# anyio
107-
# httpx
108109
# openai
109-
tomli==2.1.0
110+
tomli==2.2.1
110111
# via
111112
# build
112113
# pip-tools
113114
# pytest
114-
tqdm==4.67.0
115+
tqdm==4.67.1
115116
# via openai
116117
typing-extensions==4.12.2
117118
# via
@@ -122,21 +123,21 @@ typing-extensions==4.12.2
122123
# opentelemetry-sdk
123124
# pydantic
124125
# pydantic-core
125-
urllib3==2.2.3
126+
urllib3==2.3.0
126127
# via vcrpy
127-
vcrpy==6.0.2
128+
vcrpy==7.0.0
128129
# via
129130
# elastic-opentelemetry-instrumentation-openai (pyproject.toml)
130131
# pytest-vcr
131-
wheel==0.45.0
132+
wheel==0.45.1
132133
# via pip-tools
133-
wrapt==1.16.0
134+
wrapt==1.17.2
134135
# via
135136
# deprecated
136137
# elastic-opentelemetry-instrumentation-openai (pyproject.toml)
137138
# opentelemetry-instrumentation
138139
# vcrpy
139-
yarl==1.17.2
140+
yarl==1.18.3
140141
# via vcrpy
141142
zipp==3.21.0
142143
# via importlib-metadata

instrumentation/elastic-opentelemetry-instrumentation-openai/pyproject.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ classifiers = [
2525
"Programming Language :: Python :: 3.12",
2626
]
2727
dependencies = [
28-
# 1.28.2 is required for *robust* Events API/SDK
29-
"opentelemetry-api ~= 1.28",
30-
"opentelemetry-instrumentation ~= 0.49b2",
31-
"opentelemetry-semantic-conventions ~= 0.49b2",
28+
# 1.31.0 is required for proper histogram bucket advisory support
29+
"opentelemetry-api ~= 1.31",
30+
"opentelemetry-instrumentation ~= 0.52b0",
31+
"opentelemetry-semantic-conventions ~= 0.52b0",
3232
"wrapt >= 1.0.0, < 2.0.0",
3333
]
3434

instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/__init__.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@
3636
_send_log_events_from_messages,
3737
_span_name_from_attributes,
3838
)
39+
from opentelemetry.instrumentation.openai.metrics import (
40+
_GEN_AI_CLIENT_OPERATION_DURATION_BUCKETS,
41+
_GEN_AI_CLIENT_TOKEN_USAGE_BUCKETS,
42+
)
3943
from opentelemetry.instrumentation.openai.package import _instruments
4044
from opentelemetry.instrumentation.openai.version import __version__
4145
from opentelemetry.instrumentation.openai.wrappers import StreamWrapper
4246
from opentelemetry.instrumentation.utils import unwrap
4347
from opentelemetry.metrics import get_meter
4448
from opentelemetry.semconv._incubating.metrics.gen_ai_metrics import (
45-
create_gen_ai_client_operation_duration,
46-
create_gen_ai_client_token_usage,
49+
GEN_AI_CLIENT_OPERATION_DURATION,
50+
GEN_AI_CLIENT_TOKEN_USAGE,
4751
)
4852
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
4953
from opentelemetry.semconv.schemas import Schemas
@@ -94,8 +98,19 @@ def _instrument(self, **kwargs):
9498
event_logger_provider = kwargs.get("event_logger_provider")
9599
self.event_logger = get_event_logger(__name__, event_logger_provider)
96100

97-
self.token_usage_metric = create_gen_ai_client_token_usage(self.meter)
98-
self.operation_duration_metric = create_gen_ai_client_operation_duration(self.meter)
101+
self.token_usage_metric = self.meter.create_histogram(
102+
name=GEN_AI_CLIENT_TOKEN_USAGE,
103+
description="Measures number of input and output tokens used",
104+
unit="{token}",
105+
explicit_bucket_boundaries_advisory=_GEN_AI_CLIENT_TOKEN_USAGE_BUCKETS,
106+
)
107+
108+
self.operation_duration_metric = self.meter.create_histogram(
109+
name=GEN_AI_CLIENT_OPERATION_DURATION,
110+
description="GenAI operation duration",
111+
unit="s",
112+
explicit_bucket_boundaries_advisory=_GEN_AI_CLIENT_OPERATION_DURATION_BUCKETS,
113+
)
99114

100115
register_post_import_hook(self._patch, "openai")
101116

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
# or more contributor license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
_GEN_AI_CLIENT_OPERATION_DURATION_BUCKETS = [
18+
0.01,
19+
0.02,
20+
0.04,
21+
0.08,
22+
0.16,
23+
0.32,
24+
0.64,
25+
1.28,
26+
2.56,
27+
5.12,
28+
10.24,
29+
20.48,
30+
40.96,
31+
81.92,
32+
]
33+
34+
_GEN_AI_CLIENT_TOKEN_USAGE_BUCKETS = [
35+
1,
36+
4,
37+
16,
38+
64,
39+
256,
40+
1024,
41+
4096,
42+
16384,
43+
65536,
44+
262144,
45+
1048576,
46+
4194304,
47+
16777216,
48+
67108864,
49+
]

0 commit comments

Comments
 (0)