47
47
48
48
from opentelemetry ._events import get_event_logger
49
49
from opentelemetry .instrumentation .instrumentor import BaseInstrumentor
50
+ from opentelemetry .instrumentation .utils import unwrap
50
51
from opentelemetry .instrumentation .vertexai .package import _instruments
51
52
from opentelemetry .instrumentation .vertexai .patch import (
52
53
generate_content_create ,
56
57
from opentelemetry .trace import get_tracer
57
58
58
59
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
+
59
77
class VertexAIInstrumentor (BaseInstrumentor ):
60
78
def instrumentation_dependencies (self ) -> Collection [str ]:
61
79
return _instruments
@@ -77,20 +95,15 @@ def _instrument(self, **kwargs: Any):
77
95
event_logger_provider = event_logger_provider ,
78
96
)
79
97
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
+ )
94
106
95
107
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" )
0 commit comments