@@ -135,37 +135,62 @@ def make_otel_endpoint(datarobot_endpoint):
135135 return result
136136
137137
138- def setup_tracer (runtime_parameters ):
139- # OTEL disabled by default for now.
140- if not (
141- runtime_parameters .has ("OTEL_SDK_ENABLED" ) and runtime_parameters .get ("OTEL_SDK_ENABLED" )
138+ def setup_tracer (runtime_parameters , options ):
139+ """Setups OTEL tracer if not configured externally.
140+
141+ It is possible to provied OTEL compliant OTEL_EXPORTER_OTLP_ENDPOINT
142+ or OTEL_EXPORTER_OTLP_TRACES_ENDPOINT and override trace collector
143+ to anydd
144+
145+
146+ Parameters
147+ ----------
148+ runtime_parameters: Type[RuntimeParameters] class handles runtime parameters for custom modes
149+ used to check if OTEL configuration from user.
150+ options: argparse.Namespace: object obtained from argparser filled with user supplied
151+ command argumetns
152+ Returns
153+ -------
154+ None
155+ """
156+ if os .environ .get ("OTEL_EXPORTER_OTLP_ENDPOINT" ) or os .environ .get (
157+ "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"
142158 ):
143- return
144- # if deployment_id is not found, most likely this is custom model
145- # testing
146- deployment_id = os .environ .get ("MLOPS_DEPLOYMENT_ID" , os .environ .get ("DEPLOYMENT_ID" ))
147- if not deployment_id :
148- return
149-
150- service_name = f"deployment-{ deployment_id } "
151- resource = Resource .create (
152- {
153- "service.name" : service_name ,
154- "datarobot.deployment_id" : deployment_id ,
155- }
156- )
157- key = os .environ .get ("DATAROBOT_API_TOKEN" )
158- datarobot_endpoint = os .environ .get ("DATAROBOT_ENDPOINT" )
159- if not key or not datarobot_endpoint :
160- return
161- endpoint = make_otel_endpoint (datarobot_endpoint )
162-
163- os .environ ["OTEL_EXPORTER_OTLP_ENDPOINT" ] = endpoint
164- headers = {
165- "Authorization" : f"Bearer { key } " ,
166- "X-DataRobot-Entity-Id" : f"entity=deployment; id={ deployment_id } ;" ,
167- }
168- otlp_exporter = OTLPSpanExporter (headers = headers )
159+ # OTEL configured externaly via env vars
160+ resource = Resource .create ()
161+ else :
162+ gpu_predictor = getattr (options , "gpu_predictor" , None )
163+
164+ # if explicitly asked enable/disable otel
165+ if runtime_parameters .has ("OTEL_SDK_ENABLED" ):
166+ enable_otel = runtime_parameters .get ("OTEL_SDK_ENABLED" )
167+ # if not expliciety specified, enable otel by default for gpu models
168+ elif gpu_predictor :
169+ enable_otel = True
170+ else :
171+ enable_otel = False
172+ # if deployment_id is not found, most likely this is custom model
173+ # testing
174+ deployment_id = os .environ .get ("MLOPS_DEPLOYMENT_ID" , os .environ .get ("DEPLOYMENT_ID" ))
175+ if not enable_otel or not deployment_id :
176+ return
177+
178+ resource = Resource .create (
179+ {
180+ "datarobot.deployment_id" : deployment_id ,
181+ }
182+ )
183+ key = os .environ .get ("DATAROBOT_API_TOKEN" )
184+ datarobot_endpoint = os .environ .get ("DATAROBOT_ENDPOINT" )
185+ if not key or not datarobot_endpoint :
186+ return
187+
188+ endpoint = make_otel_endpoint (datarobot_endpoint )
189+ h = f"X-DataRobot-Api-Key={ key } ,X-DataRobot-Entity-Id=deployment-{ deployment_id } "
190+ os .environ ["OTEL_EXPORTER_OTLP_ENDPOINT" ] = endpoint
191+ os .environ ["OTEL_EXPORTER_OTLP_HEADERS" ] = h
192+
193+ otlp_exporter = OTLPSpanExporter ()
169194 provider = TracerProvider (resource = resource )
170195 provider .add_span_processor (BatchSpanProcessor (otlp_exporter ))
171196 trace .set_tracer_provider (provider )
0 commit comments