mishrabp commited on
Commit
8aedc51
·
verified ·
1 Parent(s): d5b73da

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. src/chatbot/app.py +15 -12
src/chatbot/app.py CHANGED
@@ -38,6 +38,20 @@ else:
38
 
39
  if enable_otel:
40
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  # Set up telemetry span exporter.
42
  # otel_exporter = OTLPSpanExporter(endpoint=otel_endpoint, insecure=True)
43
  otel_exporter = OTLPSpanExporter(endpoint=otel_endpoint)
@@ -48,19 +62,8 @@ if enable_otel:
48
  tracer_provider.add_span_processor(span_processor)
49
  trace_api.set_tracer_provider(tracer_provider)
50
 
51
- # Custom hook to filter out Omit/NotGiven types from attributes
52
- def request_hook(span, kwargs):
53
- if span and span.is_recording():
54
- for key, value in kwargs.items():
55
- # Check for "Omit" or "NotGiven" types which OTEL can't serialize
56
- type_name = type(value).__name__
57
- if type_name in ["Omit", "NotGiven"]:
58
- # Setup correct attribute name expected by semantic conventions or just use key
59
- # The instrumentation might use gen_ai.request.{key}
60
- span.set_attribute(f"gen_ai.request.{key}", str(value))
61
-
62
  # Instrument the OpenAI Python library
63
- OpenAIInstrumentor().instrument(request_hook=request_hook)
64
  print(f"OpenTelemetry enabled with endpoint: {otel_endpoint}")
65
  except Exception as e:
66
  print(f"Failed to initialize OpenTelemetry: {e}")
 
38
 
39
  if enable_otel:
40
  try:
41
+ # Monkeypatch the internals of the OpenAI instrumentation to fix "Invalid type Omit" error
42
+ # This is necessary because the official instrumentation library doesn't handle 'Omit' types correctly
43
+ # and blindly tries to set them as attributes, causing console errors.
44
+ import opentelemetry.instrumentation.openai.shared
45
+ original_set_span_attribute = opentelemetry.instrumentation.openai.shared._set_span_attribute
46
+
47
+ def patched_set_span_attribute(span, name, value):
48
+ # Check for Omit/NotGiven types by name to avoid importing internal types
49
+ if value is not None and type(value).__name__ in ["Omit", "NotGiven"]:
50
+ return
51
+ original_set_span_attribute(span, name, value)
52
+
53
+ opentelemetry.instrumentation.openai.shared._set_span_attribute = patched_set_span_attribute
54
+
55
  # Set up telemetry span exporter.
56
  # otel_exporter = OTLPSpanExporter(endpoint=otel_endpoint, insecure=True)
57
  otel_exporter = OTLPSpanExporter(endpoint=otel_endpoint)
 
62
  tracer_provider.add_span_processor(span_processor)
63
  trace_api.set_tracer_provider(tracer_provider)
64
 
 
 
 
 
 
 
 
 
 
 
 
65
  # Instrument the OpenAI Python library
66
+ OpenAIInstrumentor().instrument()
67
  print(f"OpenTelemetry enabled with endpoint: {otel_endpoint}")
68
  except Exception as e:
69
  print(f"Failed to initialize OpenTelemetry: {e}")