UNPKG

openlit

Version:

OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications, facilitating the integration of observability into your GenAI-driven projects

116 lines (115 loc) 5.58 kB
import { Attributes, Context, Span } from '@opentelemetry/api'; /** * Returns true when a framework instrumentor (LangChain, LiteLLM, etc.) * owns the current LLM span. Provider-level wrappers (OpenAI, Anthropic, …) * must skip their own span creation when this returns true. */ export declare function isFrameworkLlmActive(): boolean; /** * Run `fn` with the framework-LLM-active flag set. All provider wrappers * invoked inside `fn` will see `isFrameworkLlmActive() === true`. */ export declare function runWithFrameworkLlm<T>(fn: () => T): T; /** * Set framework-LLM-active flag in the current execution context. * Used by SpanProcessor-based instrumentations (Strands) where * the processor observes spans rather than controlling execution. * Mirrors Python's ContextVar.set(True) in Strands processor. */ export declare function setFrameworkLlmActive(): void; /** * Reset framework-LLM-active flag in the current execution context. * Mirrors Python's ContextVar.reset(token) in Strands processor. */ export declare function resetFrameworkLlmActive(): void; /** * Returns true when a LangGraph wrapper is controlling execution. * LangChain's callback handler skips its own invoke_workflow span when true. */ export declare function isLangGraphActive(): boolean; export declare function runWithLangGraph<T>(fn: () => T): T; /** * Returns true when a create_agent span is already being handled * (prevents duplicate spans between LangChain and LangGraph). */ export declare function isCreateAgentActive(): boolean; export declare function runWithCreateAgent<T>(fn: () => T): T; /** * Propagate conversation ID from invoke_workflow to child node spans. * Mirrors Python's set_langgraph_conversation_id / get_langgraph_conversation_id. */ export declare function getLangGraphConversationId(): string | undefined; export declare function runWithLangGraphConversationId<T>(conversationId: string, fn: () => T): T; /** * Returns the OTel context set by a framework processor (OpenAI Agents, etc.) * so that provider wrappers can create spans as children of framework spans. * Mirrors Python's context_api.attach(set_span_in_context(span)). */ export declare function getFrameworkParentContext(): Context | undefined; /** * Set the OTel parent context for provider span creation. * Called by processor-based frameworks that cannot use context.with(). */ export declare function setFrameworkParentContext(ctx: Context): void; /** * Clear the framework parent context. */ export declare function clearFrameworkParentContext(): void; export declare function getServerAddressForProvider(provider: string): [string, number]; /** * Apply global (from init) and context-scoped (from usingAttributes / * injectAdditionalAttributes) custom attributes to a span. * Global attributes are applied first; context attributes override on conflict. * Matches Python's _apply_custom_span_attributes(). */ export declare function applyCustomSpanAttributes(span: Span): void; /** * Get merged custom attributes (global + context) for use in events. * Returns a flat object; context attributes override global on conflict. */ export declare function getMergedCustomAttributes(): Record<string, any>; /** * Run a function with custom span attributes attached to all * auto-instrumented spans created during its execution. * Matches Python's openlit.inject_additional_attributes(). */ export declare function injectAdditionalAttributes<T>(fn: () => T, attributes: Record<string, any>): T; /** * Context wrapper that adds custom attributes to all auto-instrumented * spans created within its callback scope. * Matches Python's openlit.using_attributes() context manager. * * Usage: * await usingAttributes({"user.id": "u1", "team": "ml"}, async () => { * await client.chat.completions.create(...); * }); */ export declare function usingAttributes<T>(attributes: Record<string, any>, fn: () => T): T; export default class OpenLitHelper { static readonly PROMPT_TOKEN_FACTOR = 1000; static openaiTokens(text: string, model: string): number; static generalTokens(text: string): number; static getChatModelCost(model: string, pricingInfo: any, promptTokens: number, completionTokens: number): number; static getEmbedModelCost(model: string, pricingInfo: any, promptTokens: number): number; static getImageModelCost(model: string, pricingInfo: any, size: string, quality: number): number; static getAudioModelCost(model: string, pricingInfo: any, prompt: string): number; static fetchPricingInfo(pricingJson: any): Promise<any>; /** * Build OTel-spec input messages JSON string from provider messages array. * Format: [{"role": "user", "parts": [{"type": "text", "content": "..."}]}] */ static buildInputMessages(messages: any[], system?: string): string; /** * Build OTel-spec output messages JSON string from provider response. * Format: [{"role": "assistant", "parts": [{"type": "text", "content": "..."}], "finish_reason": "stop"}] */ static buildOutputMessages(text: string, finishReason: string, toolCalls?: any[]): string; /** * Emit an inference event via the LoggerProvider, matching Python SDK's * gen_ai.client.inference.operation.details event. * Falls back to span.addEvent if LoggerProvider is not available. */ static emitInferenceEvent(span: Span, attrs: Attributes): void; static handleException(span: Span, error: Error): void; static createStreamProxy(stream: any, generatorFuncResponse: any): Promise<any>; }