Local observability and metrics
Genkit records a trace of every flow, action, and step it runs. During development, the Genkit Developer UI shows these traces so you can debug a flow step by step. In production, the Firebase plugin sends traces and metrics to Genkit Monitoring in the Firebase console, or you can export them to any OpenTelemetry backend.
Tracing & metrics
Section titled “Tracing & metrics”Genkit traces every run without explicit configuration. Under genkit start,
Genkit sends its spans straight to the Developer UI’s telemetry server, with no
OpenTelemetry SDK on that path, so you can analyze a flow step by step with its
inputs, outputs, and timing. Logs written through the Genkit logger at or
above its level show up in the Developer UI next to the span that produced
them.
Everywhere else, Genkit reports its spans through pluggable instrumentation.
The default provider creates OpenTelemetry spans that carry genkit:*
attributes. Nothing leaves the process until an OpenTelemetry SDK is started,
directly or through a plugin such as enableFirebaseTelemetry().
Metrics travel a separate path. Genkit itself records no metrics. The Firebase
and Google Cloud plugins derive metric families for feature, path, generate,
and engagement activity from Genkit’s spans, including
genkit/feature/latency and the token counters
genkit/ai/generate/input/tokens and genkit/ai/generate/output/tokens. The
full table is in
Telemetry collection. The Developer
UI does not display metrics. The
OpenTelemetry GenAI instrumentation
records the spec’s token usage and duration metrics instead.
Log and export events
Section titled “Log and export events”Genkit provides a centralized logging system that you can configure using the logging module. One advantage of using the Genkit-provided logger is that it automatically exports logs to Genkit Monitoring when the Firebase Telemetry plugin is enabled.
import { logger } from 'genkit/logging';
// Set the desired log levellogger.setLogLevel('debug');Under genkit start, records written through this logger inside a flow are
also sent to the Developer UI, attached to the span that was active when they
were written. Only records at or above the logger’s level are sent, and the
default level is info, so set it to debug, as above, to see debug records
in the Developer UI too.
Choose the instrumentation provider
Section titled “Choose the instrumentation provider”configureInstrumentation from genkit/tracing sets the provider that
receives every Genkit span, and replaces the default OpenTelemetry provider.
Call it once, at startup, before you create Genkit:
import { GenAiInstrumentation } from '@genkit-ai/otel';import { configureInstrumentation } from 'genkit/tracing';
configureInstrumentation(new GenAiInstrumentation());Under genkit start, the Developer UI’s provider runs first, whatever you
configure. @genkit-ai/otel is the ready-made provider for the OpenTelemetry
GenAI semantic conventions; see
OpenTelemetry GenAI semantic conventions.
Write your own provider
Section titled “Write your own provider”A provider implements Instrumentation. Its runInNewSpan wraps the
operation like middleware: it opens a span in its backend, calls next
exactly once, returns its result, and closes the span. next takes an
OpenTelemetry span and a GenkitSpanContext with the provider’s own ids. A
provider that is not OpenTelemetry-based can hand it a non-recording span and
empty ids, and Genkit uses the ids of another provider in the chain. This
example uses @opentelemetry/api (npm i @opentelemetry/api) only to build
that placeholder span:
import { INVALID_SPAN_CONTEXT, trace } from '@opentelemetry/api';import { configureInstrumentation, type Instrumentation, type InstrumentationNext, type InstrumentationSpanInfo,} from 'genkit/tracing';
class TimingInstrumentation implements Instrumentation { async runInNewSpan<T>( info: InstrumentationSpanInfo, next: InstrumentationNext<T> ): Promise<T> { const { name, path } = info.metadata; const start = performance.now(); try { return await next(trace.wrapSpanContext(INVALID_SPAN_CONTEXT), { traceId: '', spanId: '', setMetadata() {}, }); } finally { // metadata.state is 'success' or 'error' once next settles. console.log( `${path ?? name}: ${info.metadata.state} in ${Math.round(performance.now() - start)}ms` ); } }}
configureInstrumentation(new TimingInstrumentation());The contract:
info.metadatadescribes the span:name,path,isRoot,input, and, oncenextsettles,output,state, andisFailureSource.isFailureSourceis true only on the span where an error originated, so a backend that counts failures counts each one once.info.metadata.metadataholds custom metadata, such as values set withsetCustomMetadataAttribute. Read it oncenextsettles.info.labelscarries the span’s Genkit attributes, such asgenkit:type, and any telemetry labels.setMetadataon the context receives the attributes written withspan.setAttributeorspan.setAttributeson the span passed to arunInNewSpancallback.- Errors from
nextmust be rethrown unchanged.
Production observability
Section titled “Production observability”The Genkit Monitoring dashboard helps you understand the overall health of your Genkit features. It is also useful for debugging stability and content issues that may indicate problems with your LLM prompts and/or Genkit Flows. See the Getting Started guide for more details.