HoneyHiveTracer class
TheHoneyHiveTracer class is a utility designed to initialize and manage a tracing session with the HoneyHive API, utilizing OpenTelemetry. This class encapsulates initialization of the tracing environment, capturing telemetry, and sending updates related to feedback, metrics, and metadata.
Our tracer uses OpenTelemetry as a base to auto-trace JavaScript code. A full explanation of how this works in JavaScript can be found here.
A general explanation of what OpenTelemetry is can be found here.
Attributes
- sessionId (
string | undefined): Stores the current session ID. This isundefineduntil a session is initialized. - apiKey (
string | undefined): API key used for authentication (read-only, sourced from initialization orHH_API_KEYenv var). - project (
string | undefined): Project name associated with the session (read-only, sourced from initialization orHH_PROJECTenv var). - serverUrl (
string): HoneyHive server URL (read-only, sourced from initialization,HH_API_URLenv var, or default). - sessionName (
string): Name for the current session (read-only, sourced from initialization,HH_SESSION_NAMEenv var, main module filename, or default). - source (
string): Source identifier for the session (read-only, sourced from initialization,HH_SOURCEenv var, package name, or default). - metadata (
Record<string, any>): Default metadata associated with the session during initialization (read-only). - disableBatch (
boolean): Whether batch exporting of traces is disabled (read-only, sourced from initialization). - verbose (
boolean): Whether verbose logging is enabled (read-only, sourced from initialization). - disableHttpTracing (
boolean): Whether automatic HTTP tracing is disabled (read-only, sourced from initialization).
Example Usage
Here’s a simplified example showing basic initialization and usage:Methods
init
Initializes a HoneyHive tracing session and sets up the OpenTelemetry tracing environment.
Parameters:
- params (
Partial<HoneyHiveTracerProperties>, optional): An object containing initialization options.- apiKey (
string, required ifHH_API_KEYenv var not set): API key for authenticating with the HoneyHive service. - project (
string, required ifHH_PROJECTenv var not set): Name of the project associated with this tracing session. - sessionId (
string, optional): Pre-existing session ID (must be a valid UUID) to continue tracing an existing session. - sessionName (
string, optional): Name for this specific session. Defaults will be inferred from the environment. - source (
string, optional): Source identifier, typically describing the environment or component. Defaults will be inferred from the environment. - serverUrl (
string, optional): HoneyHive server URL. Defaults to"https://api.honeyhive.ai", can also be set viaHH_API_URLenv var. - metadata (
Record<string, any>, optional): Default metadata to associate with the session. - verbose (
boolean, optional): Enable verbose logging (default:false). - disableBatch (
boolean, optional): Disable batch exporting of traces (default:false). - disableHttpTracing (
boolean, optional): Disable automatic tracing for HTTP requests (default:false).
- apiKey (
Promise<HoneyHiveTracer>: A promise that resolves to an instance of HoneyHiveTracer.
enrichSession
Instance method to enrich the current session with additional data.
It is recommended to use the exported
enrichSession function instead for better context handling. - See the exported
enrichSessionfunction for parameters.
Promise<void>
traceFunction
Instance method to create a decorator for tracing functions.
It is recommended to use the exported
traceFunction function instead for better context handling. - See the exported
traceFunctionfunction for parameters.
traceModel
Instance method to trace a function specifically for model operations.
It is recommended to use the exported
traceModel function instead for better context handling. - See the exported
traceModelfunction for parameters.
traceTool
Instance method to trace a function specifically for tool operations.
It is recommended to use the exported
traceTool function instead for better context handling. - See the exported
traceToolfunction for parameters.
traceChain
Instance method to trace a function specifically for chain operations.
It is recommended to use the exported
traceChain function instead for better context handling. - See the exported
traceChainfunction for parameters.
trace
Executes a given function with its arguments, wrapping the execution within the current HoneyHive session’s trace context using OpenTelemetry association properties. This helps ensure that any spans created within the executed function are correctly linked to the session.
It handles both synchronous and asynchronous functions. If the provided function is asynchronous (returns a Promise), trace will also return a Promise.
While this method works, using the specific
traceFunction, traceModel, traceTool, or traceChain decorators/wrappers often provides more structured tracing with automatic event type tagging. trace<A extends unknown[], F extends (...args: A) => ReturnType<F>>(fn: F, ...args: A): ReturnType<F>
Parameters:
- fn (
F): The function to execute and trace. - …args (
A): The arguments to pass to the functionfn.
ReturnType<F>: The result of executing fn(...args), preserving whether it’s a direct value or a Promise.
enrichSpan
Instance method to enrich the current span with additional data.
This method is deprecated. It is strongly recommended to use the exported
enrichSpan function instead. - See the exported
enrichSpanfunction for parameters.
void
flush
Forces a flush of all pending traces.
Returns:
Promise<void>
Notes
- The tracer automatically instruments several popular AI and machine learning libraries, including OpenAI, Anthropic, Azure OpenAI, Cohere, AWS Bedrock, Google AI Platform, Pinecone, LangChain, and Chroma.
- The
enrichSpanmethod should be called within a traced function to ensure there’s an active span to enrich. - The
inputsparameter inenrichSessionis currently not supported and will log a warning if used.
Standalone Functions
After initializing theHoneyHiveTracer once using HoneyHiveTracer.init(), the following exported functions are the preferred way to interact with tracing throughout your application. They automatically handle the necessary context association.
enrichSession
Enriches the current session with additional data. It automatically attempts to find the sessionId from the current context if not provided.
Parameters:
- params (
EnrichSessionParams, optional): An object containing data to enrich the session.- sessionId (
string, optional): The ID of the session to enrich. If not provided, it attempts to get it from the active context. - metadata (
Record<string, any>, optional): Additional metadata for the session. - feedback (
Record<string, any>, optional): Feedback data for the session. - metrics (
Record<string, any>, optional): Metrics data for the session. - config (
Record<string, any>, optional): Configuration data for the session. - inputs (
Record<string, any>, optional): Input data for the session (currently not supported). - outputs (
Record<string, any>, optional): Output data for the session. - userProperties (
Record<string, any>, optional): User properties for the session.
- sessionId (
Promise<void>
enrichSpan
Enriches the currently active span with additional data. Must be called within the context of a traced function (e.g., inside a function decorated with traceFunction, traceModel, etc.).
Parameters:
- params (
EnrichSpanParams, optional): An object containing data to enrich the span.- config (
any, optional): Configuration data for the span. - metadata (
any, optional): Additional metadata for the span. - metrics (
any, optional): Metrics data for the span. - feedback (
any, optional): Feedback data for the span. - inputs (
any, optional): Input data for the span. - outputs (
any, optional): Output data for the span. - error (
any, optional): Error data for the span. - eventName (
string, optional): A specific name to associate with this enrichment event within the span.
- config (
void
traceFunction
Creates a higher-order function (decorator) to wrap and trace another function. Captures inputs, outputs, errors, and associates the execution with the current HoneyHive session context.
Parameters:
- options (
TraceFunctionOptions, optional): Configuration options for the trace.- eventType (
string, optional): Type of event (‘tool’, ‘model’, or ‘chain’). Sets thehoneyhive_event_typeattribute. - config (
any, optional): Configuration data to attach to the span (honeyhive_config). - metadata (
any, optional): Additional metadata to attach to the span (honeyhive_metadata). - eventName (
string, optional): Custom name for the trace span. Defaults to the function name or a generic event name.
- eventType (
func) as input and returns a new, traced version of func.
Example Usage:
traceModel
A convenience wrapper around traceFunction that automatically sets the eventType to “model”.
Parameters:
- func (
Function): The model-related function to trace. - options (
Omit<TraceFunctionOptions, 'eventType'>, optional): Configuration options (excludingeventType).- config (
any, optional): Configuration data for the span. - metadata (
any, optional): Additional metadata for the span. - eventName (
string, optional): Custom name for the trace span.
- config (
func.
Example Usage:
traceTool
A convenience wrapper around traceFunction that automatically sets the eventType to “tool”.
Parameters:
- func (
Function): The tool-related function to trace. - options (
Omit<TraceFunctionOptions, 'eventType'>, optional): Configuration options (excludingeventType).- config (
any, optional): Configuration data for the span. - metadata (
any, optional): Additional metadata for the span. - eventName (
string, optional): Custom name for the trace span.
- config (
func.
Example Usage:
traceChain
A convenience wrapper around traceFunction that automatically sets the eventType to “chain”. Typically used to wrap functions that orchestrate multiple model or tool calls.
Parameters:
- func (
Function): The chain-related function to trace. - options (
Omit<TraceFunctionOptions, 'eventType'>, optional): Configuration options (excludingeventType).- config (
any, optional): Configuration data for the span. - metadata (
any, optional): Additional metadata for the span. - eventName (
string, optional): Custom name for the trace span.
- config (
func.
Example Usage:
Notes
- The
enrichSessionandenrichSpanfunctions should be used within the context of a traced function to ensure the correct session and span are enriched. - The
traceFunction,traceModel,traceTool, andtraceChainfunctions are designed to be used within the context of a traced function to ensure the correct event type is set. - The
tracefunction is a convenience function that can be used to trace any function, but it does not automatically associate the trace with the current session.

