Skip to content

Commit 27bc76e

Browse files
committed
Implement uninstrument for opentelemetry-insturmentation-vertexai
1 parent 81eaea5 commit 27bc76e

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

instrumentation-genai/opentelemetry-instrumentation-vertexai/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Implement uninstrument for `opentelemetry-insturmentation-vertexai`
11+
([#3328](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3328))
12+
1013
## Version 2.0b0 (2025-02-24)
1114

1215
- Added Vertex AI spans for request parameters

instrumentation-genai/opentelemetry-instrumentation-vertexai/src/opentelemetry/instrumentation/vertexai/__init__.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
from opentelemetry._events import get_event_logger
4949
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
50+
from opentelemetry.instrumentation.utils import unwrap
5051
from opentelemetry.instrumentation.vertexai.package import _instruments
5152
from opentelemetry.instrumentation.vertexai.patch import (
5253
generate_content_create,
@@ -55,6 +56,11 @@
5556
from opentelemetry.semconv.schemas import Schemas
5657
from opentelemetry.trace import get_tracer
5758

59+
_MODULES = (
60+
"google.cloud.aiplatform_v1beta1.services.prediction_service.client",
61+
"google.cloud.aiplatform_v1.services.prediction_service.client",
62+
)
63+
5864

5965
class VertexAIInstrumentor(BaseInstrumentor):
6066
def instrumentation_dependencies(self) -> Collection[str]:
@@ -77,20 +83,15 @@ def _instrument(self, **kwargs: Any):
7783
event_logger_provider=event_logger_provider,
7884
)
7985

80-
wrap_function_wrapper(
81-
module="google.cloud.aiplatform_v1beta1.services.prediction_service.client",
82-
name="PredictionServiceClient.generate_content",
83-
wrapper=generate_content_create(
84-
tracer, event_logger, is_content_enabled()
85-
),
86-
)
87-
wrap_function_wrapper(
88-
module="google.cloud.aiplatform_v1.services.prediction_service.client",
89-
name="PredictionServiceClient.generate_content",
90-
wrapper=generate_content_create(
91-
tracer, event_logger, is_content_enabled()
92-
),
93-
)
86+
for module in _MODULES:
87+
wrap_function_wrapper(
88+
module=module,
89+
name="PredictionServiceClient.generate_content",
90+
wrapper=generate_content_create(
91+
tracer, event_logger, is_content_enabled()
92+
),
93+
)
9494

9595
def _uninstrument(self, **kwargs: Any) -> None:
96-
"""TODO: implemented in later PR"""
96+
for module in _MODULES:
97+
unwrap(f"{module}.PredictionServiceClient", "generate_content")
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import pytest
2+
from typing import Type
3+
4+
from google.cloud.aiplatform_v1.services.prediction_service import client
5+
from google.cloud.aiplatform_v1.types import (
6+
content,
7+
prediction_service,
8+
)
9+
from google.cloud.aiplatform_v1beta1.services.prediction_service import (
10+
client as client_v1beta1,
11+
)
12+
from google.cloud.aiplatform_v1beta1.types import (
13+
content as content_v1beta1,
14+
)
15+
from google.cloud.aiplatform_v1beta1.types import (
16+
prediction_service as prediction_service_v1beta1,
17+
)
18+
19+
from opentelemetry.instrumentation.vertexai import VertexAIInstrumentor
20+
21+
22+
@pytest.fixture(name="instrumentor")
23+
def fixture_instrumentor():
24+
instrumentor = VertexAIInstrumentor()
25+
instrumentor.instrument()
26+
yield instrumentor
27+
28+
if instrumentor.is_instrumented_by_opentelemetry:
29+
instrumentor.uninstrument()
30+
31+
32+
@pytest.fixture(
33+
name="client_class",
34+
params=[
35+
pytest.param(client.PredictionServiceClient, id="v1"),
36+
pytest.param(client_v1beta1.PredictionServiceClient, id="v1beta1"),
37+
],
38+
)
39+
def fixture_client_class(request):
40+
return request.param
41+
42+
43+
def test_instruments(instrumentor: VertexAIInstrumentor, client_class):
44+
assert hasattr(client_class.generate_content, "__wrapped__")
45+
46+
47+
def test_uninstruments(instrumentor: VertexAIInstrumentor, client_class):
48+
instrumentor.uninstrument()
49+
assert not hasattr(client_class.generate_content, "__wrapped__")

0 commit comments

Comments
 (0)