|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Unit tests for get_llm_request_attributes and span name logic in utils.py.""" |
| 16 | + |
| 17 | +from types import SimpleNamespace |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +from opentelemetry.instrumentation.openai_v2.patch import ( |
| 22 | + _get_embeddings_span_name, |
| 23 | +) |
| 24 | +from opentelemetry.instrumentation.openai_v2.utils import ( |
| 25 | + get_llm_request_attributes, |
| 26 | +) |
| 27 | +from opentelemetry.semconv._incubating.attributes import ( |
| 28 | + gen_ai_attributes as GenAIAttributes, |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture(autouse=True) |
| 33 | +def fixture_vcr(): |
| 34 | + """No VCR needed for these unit tests.""" |
| 35 | + yield |
| 36 | + |
| 37 | + |
| 38 | +def _make_client(base_url=None): |
| 39 | + return SimpleNamespace(_base_url=base_url) |
| 40 | + |
| 41 | + |
| 42 | +def test_model_omitted_when_missing(): |
| 43 | + """When 'model' is not in kwargs, GEN_AI_REQUEST_MODEL should be absent.""" |
| 44 | + attrs = get_llm_request_attributes( |
| 45 | + kwargs={}, |
| 46 | + client_instance=_make_client(), |
| 47 | + latest_experimental_enabled=False, |
| 48 | + ) |
| 49 | + assert GenAIAttributes.GEN_AI_REQUEST_MODEL not in attrs |
| 50 | + |
| 51 | + |
| 52 | +def test_model_omitted_when_missing_experimental(): |
| 53 | + """Same as above but with latest_experimental_enabled=True.""" |
| 54 | + attrs = get_llm_request_attributes( |
| 55 | + kwargs={}, |
| 56 | + client_instance=_make_client(), |
| 57 | + latest_experimental_enabled=True, |
| 58 | + ) |
| 59 | + assert GenAIAttributes.GEN_AI_REQUEST_MODEL not in attrs |
| 60 | + |
| 61 | + |
| 62 | +def test_model_preserved_when_provided(): |
| 63 | + """When 'model' is in kwargs, GEN_AI_REQUEST_MODEL should be set to its value.""" |
| 64 | + attrs = get_llm_request_attributes( |
| 65 | + kwargs={"model": "gpt-4o-mini"}, |
| 66 | + client_instance=_make_client(), |
| 67 | + latest_experimental_enabled=False, |
| 68 | + ) |
| 69 | + assert attrs[GenAIAttributes.GEN_AI_REQUEST_MODEL] == "gpt-4o-mini" |
| 70 | + |
| 71 | + |
| 72 | +def test_span_name_includes_model_when_present(): |
| 73 | + """Span name should be '{operation} {model}' when model is provided.""" |
| 74 | + attrs = get_llm_request_attributes( |
| 75 | + kwargs={"model": "gpt-4o"}, |
| 76 | + client_instance=_make_client(), |
| 77 | + latest_experimental_enabled=False, |
| 78 | + ) |
| 79 | + operation_name = attrs[GenAIAttributes.GEN_AI_OPERATION_NAME] |
| 80 | + model = attrs.get(GenAIAttributes.GEN_AI_REQUEST_MODEL) |
| 81 | + span_name = f"{operation_name} {model}" if model else operation_name |
| 82 | + assert span_name == "chat gpt-4o" |
| 83 | + |
| 84 | + |
| 85 | +def test_span_name_uses_operation_only_when_model_missing(): |
| 86 | + """Span name should be just '{operation}' when model is not provided.""" |
| 87 | + attrs = get_llm_request_attributes( |
| 88 | + kwargs={}, |
| 89 | + client_instance=_make_client(), |
| 90 | + latest_experimental_enabled=False, |
| 91 | + ) |
| 92 | + operation_name = attrs[GenAIAttributes.GEN_AI_OPERATION_NAME] |
| 93 | + model = attrs.get(GenAIAttributes.GEN_AI_REQUEST_MODEL) |
| 94 | + span_name = f"{operation_name} {model}" if model else operation_name |
| 95 | + assert span_name == "chat" |
| 96 | + |
| 97 | + |
| 98 | +def test_embeddings_span_name_includes_model(): |
| 99 | + """Embeddings span name should be '{operation} {model}' when model is present.""" |
| 100 | + span_attributes = { |
| 101 | + GenAIAttributes.GEN_AI_OPERATION_NAME: "embeddings", |
| 102 | + GenAIAttributes.GEN_AI_REQUEST_MODEL: "text-embedding-3-small", |
| 103 | + } |
| 104 | + assert ( |
| 105 | + _get_embeddings_span_name(span_attributes) |
| 106 | + == "embeddings text-embedding-3-small" |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +def test_embeddings_span_name_without_model(): |
| 111 | + """Embeddings span name should be just '{operation}' when model is absent.""" |
| 112 | + span_attributes = { |
| 113 | + GenAIAttributes.GEN_AI_OPERATION_NAME: "embeddings", |
| 114 | + } |
| 115 | + assert _get_embeddings_span_name(span_attributes) == "embeddings" |
0 commit comments