Skip to content

Commit 56d4fe9

Browse files
authored
elastic-opentelemetry-instrumentation-openai: assert exported schema version (#24)
1 parent c385498 commit 56d4fe9

File tree

4 files changed

+89
-26
lines changed

4 files changed

+89
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
interactions:
2+
- request:
3+
body: '{"messages": [{"role": "user", "content": "Answer in up to 3 words: Which
4+
ocean contains the falkland islands?"}], "model": "qwen2.5:0.5b"}'
5+
headers:
6+
accept:
7+
- application/json
8+
accept-encoding:
9+
- gzip, deflate
10+
authorization:
11+
- Bearer test_openai_api_key
12+
connection:
13+
- keep-alive
14+
content-length:
15+
- '139'
16+
content-type:
17+
- application/json
18+
host:
19+
- localhost:11434
20+
user-agent:
21+
- OpenAI/Python 1.34.0
22+
x-stainless-arch:
23+
- x64
24+
x-stainless-async:
25+
- 'false'
26+
x-stainless-lang:
27+
- python
28+
x-stainless-os:
29+
- Linux
30+
x-stainless-package-version:
31+
- 1.34.0
32+
x-stainless-runtime:
33+
- CPython
34+
x-stainless-runtime-version:
35+
- 3.10.12
36+
method: POST
37+
uri: http://localhost:11434/v1/chat/completions
38+
response:
39+
body:
40+
string: '{"id":"chatcmpl-833","object":"chat.completion","created":1729263413,"model":"qwen2.5:0.5b","system_fingerprint":"fp_ollama","choices":[{"index":0,"message":{"role":"assistant","content":"The
41+
Falklands Islands are located in which ocean?"},"finish_reason":"stop"}],"usage":{"prompt_tokens":46,"completion_tokens":11,"total_tokens":57}}
42+
43+
'
44+
headers:
45+
Content-Length:
46+
- '336'
47+
Content-Type:
48+
- application/json
49+
Date:
50+
- Fri, 18 Oct 2024 14:56:53 GMT
51+
Set-Cookie: test_set_cookie
52+
openai-organization: test_openai_org_key
53+
status:
54+
code: 200
55+
message: OK
56+
version: 1

instrumentation/elastic-opentelemetry-instrumentation-openai/tests/conftest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ def vcr_cassette_name(request):
216216
# TODO: we can probably rebuild the name from the request
217217
# request.node.name format: test_basic[ollama_provider_chat_completions-Atlantic Ocean.-24-4-5]
218218
test_name = re.match(r"(\w+\[\w+)", request.node.name)
219-
return f"{test_name.group()}]"
219+
if test_name:
220+
return f"{test_name.group()}]"
221+
# if we don't use parametrization return the default one
222+
return request.node.name
220223

221224

222225
class AzureProvider:

instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_chat_completions.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3312,3 +3312,32 @@ def test_with_model_not_found(
33123312
assert_error_operation_duration_metric(
33133313
provider, operation_duration_metric, attributes=attributes, data_point=duration
33143314
)
3315+
3316+
3317+
@pytest.mark.vcr()
3318+
def test_exported_schema_version(
3319+
ollama_provider_chat_completions,
3320+
trace_exporter,
3321+
metrics_reader,
3322+
):
3323+
client = ollama_provider_chat_completions.get_client()
3324+
3325+
messages = [
3326+
{
3327+
"role": "user",
3328+
"content": "Answer in up to 3 words: Which ocean contains the falkland islands?",
3329+
}
3330+
]
3331+
3332+
client.chat.completions.create(model="qwen2.5:0.5b", messages=messages)
3333+
3334+
spans = trace_exporter.get_finished_spans()
3335+
(span,) = spans
3336+
assert span.instrumentation_scope.schema_url == "https://opentelemetry.io/schemas/1.27.0"
3337+
3338+
metrics_data = metrics_reader.get_metrics_data()
3339+
resource_metrics = metrics_data.resource_metrics
3340+
3341+
for metrics in resource_metrics:
3342+
for scope_metrics in metrics.scope_metrics:
3343+
assert scope_metrics.schema_url == "https://opentelemetry.io/schemas/1.27.0"

instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_instrumentor.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
from unittest import mock
18-
19-
import opentelemetry.instrumentation.openai
2017
from opentelemetry.instrumentation.openai import OpenAIInstrumentor
2118

2219

@@ -31,25 +28,3 @@ def test_can_override_capture_content_programmatically(instrument):
3128
instrumentor.instrument(capture_content=True)
3229
assert instrumentor.capture_content
3330
instrumentor.uninstrument()
34-
35-
36-
def test_get_tracer_is_called_with_a_string_schema(instrument):
37-
instrument.uninstrument()
38-
instrumentor = OpenAIInstrumentor()
39-
with mock.patch.object(opentelemetry.instrumentation.openai, "get_tracer") as get_tracer_mock:
40-
instrumentor.instrument()
41-
instrumentor.uninstrument()
42-
get_tracer_mock.assert_called_once_with(
43-
"opentelemetry.instrumentation.openai", mock.ANY, None, schema_url="https://opentelemetry.io/schemas/1.27.0"
44-
)
45-
46-
47-
def test_get_meter_is_called_with_a_string_schema(instrument):
48-
instrument.uninstrument()
49-
instrumentor = OpenAIInstrumentor()
50-
with mock.patch.object(opentelemetry.instrumentation.openai, "get_meter") as get_meter_mock:
51-
instrumentor.instrument()
52-
instrumentor.uninstrument()
53-
get_meter_mock.assert_called_once_with(
54-
"opentelemetry.instrumentation.openai", mock.ANY, None, schema_url="https://opentelemetry.io/schemas/1.27.0"
55-
)

0 commit comments

Comments
 (0)