import { Link, Span, SpanAttributes } from '@opentelemetry/api'; import { SpanMetadata } from './types.mjs'; import 'zod'; /** * Copyright 2025 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * The genkit attribute namespace, e.g. `genkit:name`, `genkit:path`. * @hidden */ declare const ATTR_PREFIX = "genkit"; /** @hidden */ declare const SPAN_TYPE_ATTR: string; /** @hidden */ declare const TRACER_NAME = "genkit-tracer"; /** @hidden */ declare const TRACER_VERSION = "v1"; /** ALS key under which the dispatcher stores the current span context. */ declare const spanMetadataAlsKey = "core.tracing.instrumentation.span"; /** * A backend-independent view of the span handed to the wrapped function. * * `traceId` / `spanId` are the composite-resolved ids (first non-empty across * the active instrumentation chain); both are '' when nothing is instrumented. * * `setMetadata` receives custom attribute writes made on the span handed to the * `runInNewSpan` callback (`span.setAttribute(s)`). * * @hidden */ interface GenkitSpanContext { readonly traceId: string; readonly spanId: string; setMetadata(values: Record): void; } /** * Everything an {@link Instrumentation} provider needs to open a span. The * dispatcher owns Genkit semantics (path, isRoot, custom metadata); providers * only encode this into their backend. * * @hidden */ interface InstrumentationSpanInfo { metadata: SpanMetadata; labels?: Record; links?: Link[]; } /** * Continuation passed to a provider's `runInNewSpan`. Providers must call it * exactly once and return its result. * * @hidden */ type InstrumentationNext = (span: Span, ctx: GenkitSpanContext) => Promise; /** * A pluggable telemetry instrumentation provider. Middleware over span * creation: implementations open a backend span, call `next`, and finalize. * * @hidden */ interface Instrumentation { runInNewSpan(info: InstrumentationSpanInfo, next: InstrumentationNext): Promise; } /** * A correlation-free log record. Each provider attaches trace/span correlation * from its own context source (OTel active context vs Genkit ALS). * * @hidden */ interface GenkitLogRecord { severity: 'debug' | 'info' | 'warn' | 'error'; body: unknown; attributes?: Record; } /** * Optional capability: a provider that also records logs (mirrors span * recording). `logging.ts` fans out to providers implementing this. * * @hidden */ interface LogRecordingInstrumentation { recordLog(record: GenkitLogRecord): void; } /** @hidden */ declare function hasRecordLog(p: Instrumentation): p is Instrumentation & LogRecordingInstrumentation; /** An OTel id made entirely of zeros is the no-op / unknown id. */ declare function isValidId(id: string | undefined): id is string; /** First valid (non-empty, non-zero) id across the chain, or ''. */ declare function firstNonEmpty(ids: (string | undefined)[]): string; /** * Maps SpanMetadata to the flat `genkit:*` attribute set persisted on a span. * Shared by all providers so encodings stay identical. * * @hidden */ declare function metadataToAttributes(metadata: SpanMetadata): Record; /** * Normalizes custom attribute writes (`GenkitSpanContext.setMetadata`) into * valid OTel attribute values. Primitives and homogeneous primitive arrays pass * through as-is, matching what `span.setAttribute` would record; anything else * is JSON-encoded. undefined/null are dropped. * * @hidden */ declare function toSpanAttributes(values: Record): SpanAttributes; /** @hidden */ declare function getErrorMessage(e: any): string; /** @hidden */ declare function isDisableRootSpanDetection(): boolean; /** * Disables Genkit's custom root span detection and leaves default Otel root span. * * This function attempts to control Genkit's internal OTel instrumentation behaviour, * since internal implementation details are subject to change at any time consider * this function "unstable" and subject to breaking changes as well. * * @hidden */ declare function disableOTelRootSpanDetection(): void; export { ATTR_PREFIX, type GenkitLogRecord, type GenkitSpanContext, type Instrumentation, type InstrumentationNext, type InstrumentationSpanInfo, type LogRecordingInstrumentation, SPAN_TYPE_ATTR, TRACER_NAME, TRACER_VERSION, disableOTelRootSpanDetection, firstNonEmpty, getErrorMessage, hasRecordLog, isDisableRootSpanDetection, isValidId, metadataToAttributes, spanMetadataAlsKey, toSpanAttributes };