Advanced configuration
This guide focuses on advanced configuration options for deployed features.
Every option is a keyword-only argument to enable_google_cloud_telemetry().
Call it once per process, before you construct Genkit(). A second call raises
GenkitError (FAILED_PRECONDITION).
Default Configuration
Section titled “Default Configuration”Calling enable_google_cloud_telemetry() with no arguments is the same as:
from genkit_google_cloud import enable_google_cloud_telemetry
enable_google_cloud_telemetry( project=None, sampler=None, force_dev_export=False, disable_metrics=False, disable_traces=False, metric_export_interval_ms=None, metric_export_timeout_ms=None,)project(str | None): destination Google Cloud project. When unset, the project is resolved as described in Get started.sampler(Sampler | None): anopentelemetry.sdk.trace.sampling.Samplerfor theTracerProviderthatenable_google_cloud_telemetry()creates when none is registered. Unset, the OpenTelemetry SDK default applies:ParentBased(ALWAYS_ON)unlessOTEL_TRACES_SAMPLERis set.force_dev_export(bool):False, so nothing is exported whileGENKIT_ENV=dev.disable_metrics(bool):False. Traces and logs are unaffected by this switch.disable_traces(bool):False. Only the Cloud Trace exporter is skipped.GenAiInstrumentationis still installed, so the Cloud Monitoring exporter and your own span exporters are unaffected.metric_export_interval_ms(int | None): 5000 in dev, 300000 in production. Values below 5000 are raised to 5000 with a warning.metric_export_timeout_ms(int | None): matchesmetric_export_interval_ms.
To authenticate with a service account key outside Google Cloud, point
GOOGLE_APPLICATION_CREDENTIALS at the key file so Application Default
Credentials picks it up. See
Authentication.
Export local telemetry
Section titled “Export local telemetry”Nothing is exported while the process runs in a dev environment, such as under
genkit start. The Developer UI keeps receiving traces either way. To export
telemetry when running locally, pass force_dev_export=True.
During development and testing, you can decrease latency by adjusting the export interval and timeout.
Note: Shipping to production with a frequent export interval may increase the cost for exported telemetry.
from genkit_google_cloud import enable_google_cloud_telemetry
# 1. Export from a local run, flushing metrics every 10 secondsenable_google_cloud_telemetry( force_dev_export=True, metric_export_interval_ms=10_000, metric_export_timeout_ms=10_000,)Under GENKIT_ENV=dev, each span is exported as soon as it ends
(SimpleSpanProcessor). In production, spans are batched
(BatchSpanProcessor).
Sampling
Section titled “Sampling”Left unset, every trace is kept, which is fine in development and expensive at
production volume. Sample a fraction instead, and wrap it in ParentBased so a
trace that starts sampled keeps its child spans:
from opentelemetry.sdk.trace.sampling import ParentBased, TraceIdRatioBasedfrom genkit_google_cloud import enable_google_cloud_telemetry
# 1. Keep 10% of new traces and follow the caller's sampling decisionenable_google_cloud_telemetry(sampler=ParentBased(TraceIdRatioBased(0.1)))# => ~10% of traces reach Cloud Trace; metrics are not sampledIf your application registers its own TracerProvider, for example to export to
a second OTLP backend, set the sampler on that provider and omit sampler=:
from opentelemetry import tracefrom opentelemetry.sdk.trace import TracerProviderfrom opentelemetry.sdk.trace.sampling import ParentBased, TraceIdRatioBasedfrom genkit_google_cloud import enable_google_cloud_telemetry
# 1. Register your TracerProvider and sampler firsttrace.set_tracer_provider( TracerProvider(sampler=ParentBased(TraceIdRatioBased(0.1))))
# 2. Add Cloud Trace to that provider; no sampler= hereenable_google_cloud_telemetry()Passing sampler= when a TracerProvider is already registered, or together
with disable_traces=True, raises GenkitError (INVALID_ARGUMENT).
Instrumentation beyond Genkit
Section titled “Instrumentation beyond Genkit”enable_google_cloud_telemetry() does not auto-instrument anything. Genkit’s own
actions are the only source of spans unless you add OpenTelemetry
instrumentation libraries yourself, such as
opentelemetry-instrumentation-fastapi or opentelemetry-instrumentation-httpx.
Those libraries use the global TracerProvider, so their spans land in the same
traces as your flows in Cloud Trace. Cloud Trace redaction leaves their
attributes untouched; see
Telemetry collection.
What lands in telemetry
Section titled “What lands in telemetry”Content capture is opt-in, so there is no disable_logging_input_and_output
option. By default, no prompt, response, or tool payload text is exported. The
exception is the OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT environment
variable: set to EVENT_ONLY or SPAN_AND_EVENT, the default
GenAiInstrumentation picks it up and content goes to Cloud Logging.
- Cloud Trace is always redacted:
GcpAdjustingTraceExporterreplaces Genkit content attributes, and the error text on Genkit spans, with'<redacted>', whatever the instrumentation settings. - Cloud Logging content is opt-in: to export prompts, responses, system
instructions, and tool payloads as
gen_ai.client.inference.operation.detailslog records, configureGenAiInstrumentationbefore callingenable_google_cloud_telemetry().SPAN_ONLYcontent never reaches Google Cloud; only span exporters you add yourself receive it.
from genkit.telemetry import configure_instrumentationfrom genkit_google_cloud import enable_google_cloud_telemetryfrom genkit_otel import GenAiInstrumentation
# 1. Opt in to content events and tool spansconfigure_instrumentation( GenAiInstrumentation( content_capturing_mode='EVENT_ONLY', # May contain PII. emit_tool_spans=True, ))
# 2. Cloud telemetry keeps the GenAiInstrumentation you configuredenable_google_cloud_telemetry()Disable telemetry
Section titled “Disable telemetry”Input and output logging is already off by default, as described in “What lands in telemetry” above. Metrics and traces can be disabled independently.
Disable metrics
Section titled “Disable metrics”from genkit_google_cloud import enable_google_cloud_telemetry
enable_google_cloud_telemetry(disable_metrics=True)With this option set, nothing is sent to Cloud Monitoring.
Disable traces
Section titled “Disable traces”from genkit_google_cloud import enable_google_cloud_telemetry
enable_google_cloud_telemetry(disable_traces=True)With this option set, nothing is sent to Cloud Trace. The Cloud Monitoring and Cloud Logging exporters are unaffected.