Skip to content

Commit 4397344

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

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

Diff for: instrumentation-genai/opentelemetry-instrumentation-vertexai/CHANGELOG.md

+3
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-instrumentation-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

Diff for: instrumentation-genai/opentelemetry-instrumentation-vertexai/src/opentelemetry/instrumentation/vertexai/__init__.py

+28-15
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,
@@ -56,6 +57,23 @@
5657
from opentelemetry.trace import get_tracer
5758

5859

60+
def _client_classes():
61+
# This import is very slow, do it lazily in case instrument() is not called
62+
63+
# pylint: disable=import-outside-toplevel
64+
from google.cloud.aiplatform_v1.services.prediction_service import (
65+
client,
66+
)
67+
from google.cloud.aiplatform_v1beta1.services.prediction_service import (
68+
client as client_v1beta1,
69+
)
70+
71+
return (
72+
client.PredictionServiceClient,
73+
client_v1beta1.PredictionServiceClient,
74+
)
75+
76+
5977
class VertexAIInstrumentor(BaseInstrumentor):
6078
def instrumentation_dependencies(self) -> Collection[str]:
6179
return _instruments
@@ -77,20 +95,15 @@ def _instrument(self, **kwargs: Any):
7795
event_logger_provider=event_logger_provider,
7896
)
7997

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-
)
98+
for client_class in _client_classes():
99+
wrap_function_wrapper(
100+
client_class,
101+
name="generate_content",
102+
wrapper=generate_content_create(
103+
tracer, event_logger, is_content_enabled()
104+
),
105+
)
94106

95107
def _uninstrument(self, **kwargs: Any) -> None:
96-
"""TODO: implemented in later PR"""
108+
for client_class in _client_classes():
109+
unwrap(client_class, "generate_content")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import pytest
2+
from google.cloud.aiplatform_v1.services.prediction_service import client
3+
from google.cloud.aiplatform_v1beta1.services.prediction_service import (
4+
client as client_v1beta1,
5+
)
6+
7+
from opentelemetry.instrumentation.vertexai import VertexAIInstrumentor
8+
9+
10+
@pytest.fixture(name="instrumentor")
11+
def fixture_instrumentor():
12+
instrumentor = VertexAIInstrumentor()
13+
instrumentor.instrument()
14+
yield instrumentor
15+
16+
if instrumentor.is_instrumented_by_opentelemetry:
17+
instrumentor.uninstrument()
18+
19+
20+
@pytest.fixture(
21+
name="client_class",
22+
params=[
23+
pytest.param(client.PredictionServiceClient, id="v1"),
24+
pytest.param(client_v1beta1.PredictionServiceClient, id="v1beta1"),
25+
],
26+
)
27+
def fixture_client_class(request):
28+
return request.param
29+
30+
31+
def test_instruments(instrumentor: VertexAIInstrumentor, client_class):
32+
assert hasattr(client_class.generate_content, "__wrapped__")
33+
34+
35+
def test_uninstruments(instrumentor: VertexAIInstrumentor, client_class):
36+
instrumentor.uninstrument()
37+
assert not hasattr(client_class.generate_content, "__wrapped__")

0 commit comments

Comments
 (0)