diff --git a/packages/opentelemetry-instrumentation-weaviate/opentelemetry/instrumentation/weaviate/__init__.py b/packages/opentelemetry-instrumentation-weaviate/opentelemetry/instrumentation/weaviate/__init__.py index 601393fed1..7698aa4ff1 100644 --- a/packages/opentelemetry-instrumentation-weaviate/opentelemetry/instrumentation/weaviate/__init__.py +++ b/packages/opentelemetry-instrumentation-weaviate/opentelemetry/instrumentation/weaviate/__init__.py @@ -166,7 +166,7 @@ "span_name": "db.weaviate.collections.batch.add_object", }, { - "module": "weaviate.collections.queries.fetch_object_by_id.query", + "module": "weaviate.collections.queries.fetch_object_by_id", "object": "_FetchObjectByIDQuery", "method": "fetch_object_by_id", "span_name": "db.weaviate.collections.query.fetch_object_by_id", @@ -183,6 +183,21 @@ "method": "get", "span_name": "db.weaviate.collections.query.get", }, + { + "module": "weaviate.client", + "object": "WeaviateClient", + "method": "graphql_raw_query", + "span_name": "db.weaviate.client.graphql_raw_query", + }, +] + +#: Methods that existed in the first 4.x releases and were removed later in the +#: same major. Kept out of ``WRAPPED_METHODS_v4`` so that list means "this has to +#: resolve on the client we test against": these only resolve where the SDK +#: still ships them, and the guard in ``_instrument`` skips the rest. Dropping +#: them would lose the spans for the 4.x versions that do have them, which is +#: the same mistake as keeping a stale entry -- a client nobody is looking at. +WRAPPED_METHODS_v4_legacy = [ { "module": "weaviate.gql.filter", "object": "GraphQL", @@ -201,14 +216,10 @@ "method": "do", "span_name": "db.weaviate.gql.get.do", }, - { - "module": "weaviate.client", - "object": "WeaviateClient", - "method": "graphql_raw_query", - "span_name": "db.weaviate.client.graphql_raw_query", - }, ] -WRAPPED_METHODS = WRAPPED_METHODS_v3 + WRAPPED_METHODS_v4 +WRAPPED_METHODS = ( + WRAPPED_METHODS_v3 + WRAPPED_METHODS_v4 + WRAPPED_METHODS_v4_legacy +) class WeaviateInstrumentor(BaseInstrumentor): diff --git a/packages/opentelemetry-instrumentation-weaviate/tests/test_weaviate_instrumentation.py b/packages/opentelemetry-instrumentation-weaviate/tests/test_weaviate_instrumentation.py index 297385eadc..00575304bb 100644 --- a/packages/opentelemetry-instrumentation-weaviate/tests/test_weaviate_instrumentation.py +++ b/packages/opentelemetry-instrumentation-weaviate/tests/test_weaviate_instrumentation.py @@ -1,3 +1,4 @@ +import importlib import json import pytest @@ -6,6 +7,7 @@ # Skip this entire module if weaviate.classes is not available (v3) wvc = pytest.importorskip("weaviate.classes") +from opentelemetry.instrumentation.weaviate import WRAPPED_METHODS_v4 # noqa: E402 from opentelemetry.semconv_ai import SpanAttributes # noqa: E402 @@ -357,3 +359,28 @@ def test_weaviate_query_raw(client, exporter): assert "author" in traced_raw_query assert "text" in traced_raw_query assert len(result.get["Article"]) == 2 + + +@pytest.mark.parametrize( + "wrapped", + WRAPPED_METHODS_v4, + ids=[f"{entry['object']}.{entry['method']}" for entry in WRAPPED_METHODS_v4], +) +def test_every_wrapped_method_resolves_on_the_installed_client(wrapped): + """A path the SDK moved is a span nobody emits, and nothing says so. + + ``_instrument`` skips an entry it cannot resolve instead of raising, which is + the right call in a client library and also why this drifts unnoticed: + ``weaviate.collections.queries.fetch_object_by_id.query`` stopped existing in + a later 4.x, so ``fetch_object_by_id`` quietly stopped producing spans. + ``WRAPPED_METHODS_v4`` is the list that has to hold on the client the package + runs on; the entries that legitimately disappear upstream live in + ``WRAPPED_METHODS_v4_legacy``. + """ + module = importlib.import_module(wrapped["module"]) + holder = getattr(module, wrapped["object"]) + + assert hasattr(holder, wrapped["method"]), ( + f"{wrapped['module']}.{wrapped['object']} has no {wrapped['method']} on " + f"weaviate-client {weaviate.__version__}: no span will be emitted" + )