diff --git a/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/cassettes/.test_chat_completions/test_exported_schema_version.yaml b/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/cassettes/.test_chat_completions/test_exported_schema_version.yaml new file mode 100644 index 0000000..fbadb06 --- /dev/null +++ b/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/cassettes/.test_chat_completions/test_exported_schema_version.yaml @@ -0,0 +1,56 @@ +interactions: +- request: + body: '{"messages": [{"role": "user", "content": "Answer in up to 3 words: Which + ocean contains the falkland islands?"}], "model": "qwen2.5:0.5b"}' + headers: + accept: + - application/json + accept-encoding: + - gzip, deflate + authorization: + - Bearer test_openai_api_key + connection: + - keep-alive + content-length: + - '139' + content-type: + - application/json + host: + - localhost:11434 + user-agent: + - OpenAI/Python 1.34.0 + x-stainless-arch: + - x64 + x-stainless-async: + - 'false' + x-stainless-lang: + - python + x-stainless-os: + - Linux + x-stainless-package-version: + - 1.34.0 + x-stainless-runtime: + - CPython + x-stainless-runtime-version: + - 3.10.12 + method: POST + uri: http://localhost:11434/v1/chat/completions + response: + body: + 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 + Falklands Islands are located in which ocean?"},"finish_reason":"stop"}],"usage":{"prompt_tokens":46,"completion_tokens":11,"total_tokens":57}} + + ' + headers: + Content-Length: + - '336' + Content-Type: + - application/json + Date: + - Fri, 18 Oct 2024 14:56:53 GMT + Set-Cookie: test_set_cookie + openai-organization: test_openai_org_key + status: + code: 200 + message: OK +version: 1 diff --git a/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/conftest.py b/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/conftest.py index ace6ee6..9e42125 100644 --- a/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/conftest.py +++ b/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/conftest.py @@ -216,7 +216,10 @@ def vcr_cassette_name(request): # TODO: we can probably rebuild the name from the request # request.node.name format: test_basic[ollama_provider_chat_completions-Atlantic Ocean.-24-4-5] test_name = re.match(r"(\w+\[\w+)", request.node.name) - return f"{test_name.group()}]" + if test_name: + return f"{test_name.group()}]" + # if we don't use parametrization return the default one + return request.node.name class AzureProvider: diff --git a/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_chat_completions.py b/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_chat_completions.py index 70d7f14..2b0c344 100644 --- a/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_chat_completions.py +++ b/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_chat_completions.py @@ -3312,3 +3312,32 @@ def test_with_model_not_found( assert_error_operation_duration_metric( provider, operation_duration_metric, attributes=attributes, data_point=duration ) + + +@pytest.mark.vcr() +def test_exported_schema_version( + ollama_provider_chat_completions, + trace_exporter, + metrics_reader, +): + client = ollama_provider_chat_completions.get_client() + + messages = [ + { + "role": "user", + "content": "Answer in up to 3 words: Which ocean contains the falkland islands?", + } + ] + + client.chat.completions.create(model="qwen2.5:0.5b", messages=messages) + + spans = trace_exporter.get_finished_spans() + (span,) = spans + assert span.instrumentation_scope.schema_url == "https://opentelemetry.io/schemas/1.27.0" + + metrics_data = metrics_reader.get_metrics_data() + resource_metrics = metrics_data.resource_metrics + + for metrics in resource_metrics: + for scope_metrics in metrics.scope_metrics: + assert scope_metrics.schema_url == "https://opentelemetry.io/schemas/1.27.0" diff --git a/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_instrumentor.py b/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_instrumentor.py index 14952f4..54fa4c2 100644 --- a/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_instrumentor.py +++ b/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_instrumentor.py @@ -14,9 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from unittest import mock - -import opentelemetry.instrumentation.openai from opentelemetry.instrumentation.openai import OpenAIInstrumentor @@ -31,25 +28,3 @@ def test_can_override_capture_content_programmatically(instrument): instrumentor.instrument(capture_content=True) assert instrumentor.capture_content instrumentor.uninstrument() - - -def test_get_tracer_is_called_with_a_string_schema(instrument): - instrument.uninstrument() - instrumentor = OpenAIInstrumentor() - with mock.patch.object(opentelemetry.instrumentation.openai, "get_tracer") as get_tracer_mock: - instrumentor.instrument() - instrumentor.uninstrument() - get_tracer_mock.assert_called_once_with( - "opentelemetry.instrumentation.openai", mock.ANY, None, schema_url="https://opentelemetry.io/schemas/1.27.0" - ) - - -def test_get_meter_is_called_with_a_string_schema(instrument): - instrument.uninstrument() - instrumentor = OpenAIInstrumentor() - with mock.patch.object(opentelemetry.instrumentation.openai, "get_meter") as get_meter_mock: - instrumentor.instrument() - instrumentor.uninstrument() - get_meter_mock.assert_called_once_with( - "opentelemetry.instrumentation.openai", mock.ANY, None, schema_url="https://opentelemetry.io/schemas/1.27.0" - )