Skip to content

Commit 241fdef

Browse files
Vivek Nairstainless-app[bot]
authored andcommitted
chore: Add otel_setup configuration in init() and check diff (#324)
* feat: Add OpenAI Agents instrumentation example (#322) * feat: Add OpenAI Agents instrumentation example * feat: Add automatic tracing for OpenAI Agents SDK * fix: Update default pipeline id value in interaction definition * chore: Add otel_setup configuration in init() and check diff
1 parent 7c76bf2 commit 241fdef

File tree

2 files changed

+99
-40
lines changed

2 files changed

+99
-40
lines changed

src/gentrace/lib/init.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ def init(
7979
_set_client_instances(sync_g_client, async_g_client)
8080

8181
# Set a global flag to indicate that init() has been called
82+
# Also store the otel_setup configuration
8283
import sys
83-
8484
setattr(sys.modules["gentrace"], "__gentrace_initialized", True)
85-
85+
setattr(sys.modules["gentrace"], "__gentrace_otel_setup_config", otel_setup)
86+
8687
# Configure OpenTelemetry if requested
8788
if otel_setup is not False:
8889
# Extract OpenTelemetry configuration options

src/gentrace/lib/utils.py

Lines changed: 96 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -318,20 +318,65 @@ def check_otel_config_and_warn() -> None:
318318
if _otel_config_warning_issued:
319319
return
320320

321+
# Check if otel_setup was configured in init()
322+
import sys
323+
otel_setup_config = getattr(sys.modules.get('gentrace'), '__gentrace_otel_setup_config', None)
324+
325+
# Only show warning if otel_setup was explicitly set to False
326+
# If undefined, the user hasn't called init() yet
327+
# If True or a dict, OpenTelemetry setup was requested
328+
if otel_setup_config is not False:
329+
return
330+
321331
provider = trace_api.get_tracer_provider()
322332

323333
if not isinstance(provider, SDKTracerProvider):
324-
otel_setup_url = "https://github.com/gentrace/gentrace-python/blob/main/README.md#opentelemetry-integration"
325-
link_text = "Gentrace OpenTelemetry Setup Guide"
334+
console = get_console()
335+
336+
# Create a warning panel with rich formatting
337+
warning_content = Group(
338+
Text("⚠ Gentrace Configuration Warning", style="bold yellow"),
339+
Text(),
340+
Text("OpenTelemetry SDK does not appear to be configured. This means that Gentrace features"),
341+
Text("like @interaction, @eval, @traced, and eval_dataset() will not record any data to the"),
342+
Text("Gentrace UI."),
343+
Text(),
344+
Text("You have two options to fix this:"),
345+
)
326346

327-
# OTEL starter code that users can copy and use directly
328-
otel_starter_code = """import os
347+
warning_panel = Panel(
348+
warning_content,
349+
title="[yellow]⚠ Gentrace Configuration Warning[/yellow]",
350+
border_style="yellow",
351+
title_align="left",
352+
padding=(1, 2),
353+
)
354+
355+
# Init example code (recommended)
356+
init_example_code = """import gentrace
357+
358+
gentrace.init(
359+
api_key="your-api-key",
360+
# otel_setup=True is the default, can be omitted
361+
)"""
362+
363+
# Manual setup code
364+
manual_setup_code = """import os
365+
import atexit
366+
import gentrace
329367
from opentelemetry import trace
330368
from opentelemetry.sdk.trace import TracerProvider
331369
from opentelemetry.sdk.resources import Resource
332370
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
333371
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
334372
373+
# Initialize Gentrace without OpenTelemetry setup
374+
gentrace.init(
375+
api_key="your-api-key",
376+
base_url="https://gentrace.ai/api", # or your custom endpoint
377+
otel_setup=False
378+
)
379+
335380
# Set up the resource with service name
336381
resource = Resource(attributes={"service.name": "your-service-name"})
337382
@@ -348,61 +393,74 @@ def check_otel_config_and_warn() -> None:
348393
# Add the span processor
349394
trace.get_tracer_provider().add_span_processor(SimpleSpanProcessor(span_exporter))
350395
351-
print("OpenTelemetry SDK started – spans will be sent to Gentrace.")"""
352-
353-
console = get_console()
396+
# Ensure graceful shutdown
397+
def shutdown_handler():
398+
provider = trace.get_tracer_provider()
399+
if hasattr(provider, 'shutdown'):
400+
provider.shutdown()
401+
print("OpenTelemetry SDK shut down successfully")
354402
355-
# Create a warning panel with rich formatting
356-
warning_content = Group(
357-
Text("OpenTelemetry SDK (TracerProvider) does not appear to be configured.", style="yellow"),
358-
Text(),
359-
Text("Gentrace tracing features may not record data:"),
360-
Text(" • @interaction", style="on grey30"),
361-
Text(" • @eval", style="on grey30"),
362-
Text(" • @traced", style="on grey30"),
363-
Text(" • eval_dataset()", style="on grey30"),
364-
Text(),
365-
Text("Please ensure OpenTelemetry is set up as per the:"),
366-
Text(link_text, style=f"underline #90EE90 link {otel_setup_url}"),
367-
)
403+
# Register shutdown handler
404+
atexit.register(shutdown_handler)
368405
369-
warning_panel = Panel(
370-
warning_content,
371-
title="[yellow]⚠ Gentrace Configuration Warning[/yellow]",
372-
border_style="yellow",
373-
title_align="left",
374-
padding=(1, 2),
375-
)
406+
print("OpenTelemetry SDK started – spans will be sent to Gentrace.")"""
376407

377408
try:
378409
console.console.print(warning_panel)
379410
console.console.print() # Add spacing
380411

381-
# Display the formatted OTEL starter code
382-
console.console.print(Text("Here's the OTEL starter code you can use:", style="bold cyan"))
412+
# Display the recommended init() approach with star emoji
413+
console.console.print(Text("⭐ Option 1: Use Gentrace's automatic OpenTelemetry setup (recommended):", style="bold green"))
383414
console.console.print()
384415

385-
syntax = Syntax(
386-
otel_starter_code,
416+
syntax_init = Syntax(
417+
init_example_code,
387418
"python",
388419
theme="monokai",
389420
line_numbers=True,
390421
word_wrap=True,
391422
background_color="default",
392423
)
393-
console.console.print(syntax)
424+
console.console.print(syntax_init)
425+
console.console.print()
426+
427+
# Display the manual setup option
428+
console.console.print(Text("Option 2: If you have otel_setup=False, manually configure OpenTelemetry:", style="gray"))
429+
console.console.print()
430+
431+
syntax_manual = Syntax(
432+
manual_setup_code,
433+
"python",
434+
theme="monokai",
435+
line_numbers=True,
436+
word_wrap=True,
437+
background_color="default",
438+
)
439+
console.console.print(syntax_manual)
394440
console.console.print()
395441

396442
console.console.print(
397-
Text("💡 Copy the code above and add it to your application startup.", style="bold green")
443+
Text("Tip: Copy the code above and add it to your application setup.", style="gray")
398444
)
399445

400446
except Exception: # Fallback if rich formatting/printing fails
401-
fallback_message = (
402-
f"Gentrace: OpenTelemetry SDK (TracerProvider) does not appear to be configured. "
403-
f"Gentrace tracing features (e.g., @interaction, @eval, @traced, and eval_dataset()) may not record data. "
404-
f"Please ensure OpenTelemetry is set up as per the {otel_setup_url}."
405-
)
447+
fallback_message = """Gentrace: OpenTelemetry SDK does not appear to be configured. This means that Gentrace features like @interaction, @eval, @traced, and eval_dataset() will not record any data to the Gentrace UI.
448+
449+
You have two options:
450+
451+
⭐ Option 1: Use Gentrace's automatic OpenTelemetry setup (recommended):
452+
453+
import gentrace
454+
455+
gentrace.init(
456+
api_key="your-api-key",
457+
# otel_setup=True is the default, can be omitted
458+
)
459+
460+
Option 2: If you have otel_setup=False, manually configure OpenTelemetry with the Gentrace endpoint.
461+
462+
See the documentation for the complete setup code.
463+
"""
406464
warnings.warn(fallback_message, UserWarning, stacklevel=2)
407465

408466
_otel_config_warning_issued = True

0 commit comments

Comments
 (0)