|
36 | 36 | from genkit._core._compat import StrEnum |
37 | 37 | from genkit._core._error import GenkitError |
38 | 38 | from genkit._core._trace._path import build_path |
| 39 | +from genkit._core._trace._suppress import suppress_telemetry |
39 | 40 | from genkit._core._tracing import tracer |
40 | 41 |
|
41 | 42 | # ============================================================================= |
@@ -554,52 +555,60 @@ async def tracing_wrapper( |
554 | 555 | ) -> ActionResponse[Any]: |
555 | 556 | start_time = time.perf_counter() |
556 | 557 |
|
557 | | - with _save_parent_path(): |
558 | | - with tracer.start_as_current_span(name) as span: |
559 | | - # Format trace_id and span_id as hex strings (OpenTelemetry standard format) |
560 | | - trace_id = format(span.get_span_context().trace_id, '032x') |
561 | | - span_id = format(span.get_span_context().span_id, '016x') |
562 | | - if on_trace_start: |
563 | | - on_trace_start(trace_id, span_id) |
564 | | - |
565 | | - # Set telemetry labels as direct span attributes (matches JS/Go behavior) |
566 | | - if telemetry_labels: |
567 | | - for key, value in telemetry_labels.items(): |
568 | | - span.set_attribute(key, str(value)) |
569 | | - |
570 | | - _record_input_metadata( |
571 | | - span=span, |
572 | | - kind=kind, |
573 | | - name=name, |
574 | | - span_metadata=span_metadata, |
575 | | - input=input, |
576 | | - ) |
577 | | - |
578 | | - try: |
579 | | - match n_action_args: |
580 | | - case 0: |
581 | | - output = await fn() |
582 | | - case 1: |
583 | | - output = await fn(input) |
584 | | - case 2: |
585 | | - output = await fn(input, ctx) |
586 | | - case _: |
587 | | - raise ValueError('action fn must have 0-2 args') |
588 | | - except Exception as e: |
589 | | - span.set_attribute('genkit:state', 'error') |
590 | | - span.set_status(status=trace_api.StatusCode.ERROR, description=str(e)) |
591 | | - span.record_exception(e) |
592 | | - # Re-raise existing GenkitError instances to avoid double-wrapping |
593 | | - if isinstance(e, GenkitError): |
594 | | - raise |
595 | | - raise GenkitError( |
596 | | - cause=e, |
597 | | - message=f'Error while running action {name}', |
598 | | - trace_id=trace_id, |
599 | | - ) from e |
600 | | - |
601 | | - output = _record_latency(output, start_time) |
602 | | - _record_output_metadata(span, output=output) |
603 | | - return ActionResponse(response=output, trace_id=trace_id, span_id=span_id) |
| 558 | + suppress = str((telemetry_labels or {}).get('genkitx:ignore-trace', '')).lower() == 'true' |
| 559 | + suppress_token = suppress_telemetry.set(True) if suppress else None |
| 560 | + try: |
| 561 | + with _save_parent_path(): |
| 562 | + with tracer.start_as_current_span(name) as span: |
| 563 | + # Format trace_id and span_id as hex strings (OpenTelemetry standard format) |
| 564 | + trace_id = format(span.get_span_context().trace_id, '032x') |
| 565 | + span_id = format(span.get_span_context().span_id, '016x') |
| 566 | + if on_trace_start: |
| 567 | + on_trace_start(trace_id, span_id) |
| 568 | + |
| 569 | + # Set telemetry labels as direct span attributes (matches JS/Go behavior) |
| 570 | + if telemetry_labels: |
| 571 | + for key, value in telemetry_labels.items(): |
| 572 | + span.set_attribute(key, str(value)) |
| 573 | + |
| 574 | + _record_input_metadata( |
| 575 | + span=span, |
| 576 | + kind=kind, |
| 577 | + name=name, |
| 578 | + span_metadata=span_metadata, |
| 579 | + input=input, |
| 580 | + ) |
| 581 | + |
| 582 | + try: |
| 583 | + match n_action_args: |
| 584 | + case 0: |
| 585 | + output = await fn() |
| 586 | + case 1: |
| 587 | + output = await fn(input) |
| 588 | + case 2: |
| 589 | + output = await fn(input, ctx) |
| 590 | + case _: |
| 591 | + raise ValueError('action fn must have 0-2 args') |
| 592 | + except Exception as e: |
| 593 | + span.set_attribute('genkit:state', 'error') |
| 594 | + # Bundled Dev UI reads timeEvents.exception.attributes only; stash text for export synthesis. |
| 595 | + span.set_status(trace_api.StatusCode.ERROR, description=str(e)) |
| 596 | + span.record_exception(e) |
| 597 | + if isinstance(e, GenkitError): |
| 598 | + span.set_attribute('genkit:error', e.original_message) |
| 599 | + raise |
| 600 | + span.set_attribute('genkit:error', str(e)) |
| 601 | + raise GenkitError( |
| 602 | + cause=e, |
| 603 | + message=f'Error while running action {name}', |
| 604 | + trace_id=trace_id, |
| 605 | + ) from e |
| 606 | + |
| 607 | + output = _record_latency(output, start_time) |
| 608 | + _record_output_metadata(span, output=output) |
| 609 | + return ActionResponse(response=output, trace_id=trace_id, span_id=span_id) |
| 610 | + finally: |
| 611 | + if suppress_token is not None: |
| 612 | + suppress_telemetry.reset(suppress_token) |
604 | 613 |
|
605 | 614 | return tracing_wrapper |
0 commit comments