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 is undefined until a session is initialized.
  • sdk (HoneyHive): An instance of the HoneyHive SDK used for API interactions.

Example Usage

import { HoneyHiveTracer } from "honeyhive";

// Initialize a session
const tracer = await HoneyHiveTracer.init({
    apiKey: "your-api-key",
    project: "Project Name",
    sessionName: "Session Name",
    source: "source_identifier"
});

// Enrich the session with additional data
await tracer.enrichSession({
    metadata: { key: "value" },
    feedback: { feedback: "Session feedback" },
    metrics: { metric_name: "metric_value" }
});

// Trace a function
tracer.trace(() => {
    // Your code here
});

// Trace a function with the traceFunction decorator
const tracedFunction = tracer.traceFunction()((arg1, arg2) => {
    // Your function logic here
    // Enrich a span within a traced function
    tracer.enrichSpan({
        metadata: { key: "value" },
        outputs: { result: "output" }
    });
});

// Flush traces
await tracer.flush();

Methods

init

Initializes a HoneyHive tracing session and sets up the OpenTelemetry tracing environment.

Parameters:

  • apiKey (string): API key for authenticating with the HoneyHive service.
  • project (string): Name of the project associated with this tracing session.
  • sessionName (string, optional): Name for this specific session. If not provided, it will use the name of the main module file or ‘unknown’.
  • source (string, optional): Source identifier, typically describing the environment or component that initiates the session (default: "dev").
  • serverUrl (string, optional): HoneyHive server URL (default: "https://api.honeyhive.ai").

Returns:
Promise<HoneyHiveTracer>: A promise that resolves to an instance of HoneyHiveTracer.

initFromSessionId

Initializes a tracing session using an existing session ID.

Parameters:

  • apiKey (string): API key for authenticating with HoneyHive services.
  • sessionId (string): Pre-existing session ID to continue tracing.
  • serverUrl (string, optional): HoneyHive server URL (default: "https://api.honeyhive.ai").

Returns:
Promise<HoneyHiveTracer>: A promise that resolves to an instance of HoneyHiveTracer.

enrichSession

Enriches the current session with additional data.

Parameters:

  • 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.

Returns:
Promise<void>

traceFunction

Creates a decorator that can be used to trace functions, capturing inputs, outputs, and any errors.

Parameters:

  • eventType (string, optional): Type of event (‘tool’, ‘model’, or ‘chain’).
  • config (any, optional): Configuration options for the tracing.
  • metadata (any, optional): Additional metadata to be included in the trace.

Returns:
A function that takes a function as an argument and returns a traced version of that function.

trace

Traces a function execution, associating it with the current session.

Parameters:

  • fn (() => void): Function to be traced.

Returns:
void

enrichSpan

Enriches the current span with additional data.

Parameters:

  • 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.

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 in enrichSession is currently not supported and will log a warning if used.