TypeScript
Reference documentation for the HoneyHiveTracer class
HoneyHiveTracer class
The HoneyHiveTracer
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 isundefined
until a session is initialized. - apiKey (
string | undefined
): API key used for authentication (read-only, sourced from initialization orHH_API_KEY
env var). - project (
string | undefined
): Project name associated with the session (read-only, sourced from initialization orHH_PROJECT
env var). - serverUrl (
string
): HoneyHive server URL (read-only, sourced from initialization,HH_API_URL
env var, or default). - sessionName (
string
): Name for the current session (read-only, sourced from initialization,HH_SESSION_NAME
env var, main module filename, or default). - source (
string
): Source identifier for the session (read-only, sourced from initialization,HH_SOURCE
env 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_KEY
env var not set): API key for authenticating with the HoneyHive service. - project (
string
, required ifHH_PROJECT
env 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_URL
env 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 (
Returns:
Promise<HoneyHiveTracer>
: A promise that resolves to an instance of HoneyHiveTracer.
enrichSession
Instance method to enrich the current session with additional data.
enrichSession
function instead for better context handling. Parameters:
- See the exported
enrichSession
function for parameters.
Returns:
Promise<void>
traceFunction
Instance method to create a decorator for tracing functions.
traceFunction
function instead for better context handling. Parameters:
- See the exported
traceFunction
function for parameters.
Returns: A function that takes a function as an argument and returns a traced version of that function.
traceModel
Instance method to trace a function specifically for model operations.
traceModel
function instead for better context handling. Parameters:
- See the exported
traceModel
function for parameters.
Returns: A traced version of the input function with model-specific tracing.
Example:
traceTool
Instance method to trace a function specifically for tool operations.
traceTool
function instead for better context handling. Parameters:
- See the exported
traceTool
function for parameters.
Returns: A traced version of the input function with tool-specific tracing.
Example:
traceChain
Instance method to trace a function specifically for chain operations.
traceChain
function instead for better context handling. Parameters:
- See the exported
traceChain
function for parameters.
Returns: A traced version of the input function with chain-specific tracing.
Example:
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.
traceFunction
, traceModel
, traceTool
, or traceChain
decorators/wrappers often provides more structured tracing with automatic event type tagging. Signature:
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
.
Returns:
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.
enrichSpan
function instead. Parameters:
- See the exported
enrichSpan
function for parameters.
Returns:
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
enrichSpan
method should be called within a traced function to ensure there’s an active span to enrich. - The
inputs
parameter inenrichSession
is currently not supported and will log a warning if used.
Standalone Functions
After initializing the HoneyHiveTracer
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 (
Returns:
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 (
Returns:
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_type
attribute. - 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 (
Returns:
A function that takes the target function (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 (
Returns:
The traced version of the input 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 (
Returns:
The traced version of the input 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 (
Returns:
The traced version of the input func
.
Example Usage:
Notes
- The
enrichSession
andenrichSpan
functions should be used within the context of a traced function to ensure the correct session and span are enriched. - The
traceFunction
,traceModel
,traceTool
, andtraceChain
functions are designed to be used within the context of a traced function to ensure the correct event type is set. - The
trace
function is a convenience function that can be used to trace any function, but it does not automatically associate the trace with the current session.