Custom Spans
How to trace custom spans with HoneyHive.
While the HoneyHive tracer automatically instruments all of the most common LLM application components, you might sometimes want to trace parts of your code beyond the automated coverage.
To do this, you can use HoneyHive’s manual tracing tools, which we’ll walk through in this guide.
Prerequisites
Please complete the Quickstart and make sure you have initialized your tracer using HoneyHiveTracer.init()
.
Overview
HoneyHive gives you full customizability over your traces.
You can trace any function in your code and see its inputs, outputs, errors, duration, etc. by decorating it with the @trace
decorator.
@atrace
decorator is identical in functionality to the @trace
decorator, but is used for async functions.Enriching Custom Spans
You can enrich your custom spans with additional properties to provide more context to your traces. There are 2 ways to enrich spans in HoneyHive:-
-
Enriching before execution: The decorator-based enrichment below shows how you can enrich static properties before the code executes.
-
Enriching during execution: You can enrich dynamic properties on a session/trace or event/span level using
enrich_session
andenrich_span
functions. Please refer to the Enrich Traces documentation for more details on enrichment during execution.
Enriching Before Execution Using Decorators
Here is an example of how to enrich the Configuration, Metadata, and Event Type fields on the decorator:
Enriching During Execution Using enrich_span
or enrich_session
You can also enrich your traces and custom spans with various propertires during execution using the enrich_span
or enrich_session
functions.
Please refer to the Enrich Traces documentation for more details on enrichment during execution. You can find the complete documentation for this in the Python SDK reference.
Async Function Tracing with @atrace
For async functions in Python, use the @atrace
decorator instead of @trace
. The @atrace
decorator is specifically designed to handle async functions properly:
The @atrace
decorator supports the same enrichment options as @trace
:
Best practices
- Use descriptive function names, as they will be used as span names in your traces
- Be mindful of sensitive data when tracing functions - avoid capturing passwords or other secrets
By strategically adding the @trace
decorator to key functions in your application, you can gain valuable insights into your custom code’s performance and behavior, complementing HoneyHive’s automatic instrumentation of LLM components.
Visualizing Your Traces
All traces can be visualized in the HoneyHive web app under Data Store in the session’s specified project. The session tree view shows you the execution flow of your trace, as well all the automatically captured and manually enriched attributes.
Next Up: Enrich Your Traces
Here are some ways in which you can enrich your traces:
Enrichments Overview
An overview of the types of enrichments available for your traces and spans
Logging Client-Side Evaluations
Add client-side metrics, guardrail results, or evaluations to your traces
Logging User Feedback
Enrich traces with feedback or additional annotations
Logging Configuration Details
Include config details, model hyperparameters, or prompt templates
Logging User Properties
Add user properties or tracking IDs to your traces
Logging Custom Metadata
Enrich traces with arbitrary metadata or JSON
While the HoneyHive tracer automatically instruments all of the most common LLM application components, you might sometimes want to trace parts of your code beyond the automated coverage.
To do this, you can use HoneyHive’s manual tracing tools, which we’ll walk through in this guide.
Prerequisites
Please complete the Quickstart and make sure you have initialized your tracer using HoneyHiveTracer.init()
.
Overview
HoneyHive gives you full customizability over your traces.
You can trace any function in your code and see its inputs, outputs, errors, duration, etc. by decorating it with the @trace
decorator.
@atrace
decorator is identical in functionality to the @trace
decorator, but is used for async functions.Enriching Custom Spans
You can enrich your custom spans with additional properties to provide more context to your traces. There are 2 ways to enrich spans in HoneyHive:-
-
Enriching before execution: The decorator-based enrichment below shows how you can enrich static properties before the code executes.
-
Enriching during execution: You can enrich dynamic properties on a session/trace or event/span level using
enrich_session
andenrich_span
functions. Please refer to the Enrich Traces documentation for more details on enrichment during execution.
Enriching Before Execution Using Decorators
Here is an example of how to enrich the Configuration, Metadata, and Event Type fields on the decorator:
Enriching During Execution Using enrich_span
or enrich_session
You can also enrich your traces and custom spans with various propertires during execution using the enrich_span
or enrich_session
functions.
Please refer to the Enrich Traces documentation for more details on enrichment during execution. You can find the complete documentation for this in the Python SDK reference.
Async Function Tracing with @atrace
For async functions in Python, use the @atrace
decorator instead of @trace
. The @atrace
decorator is specifically designed to handle async functions properly:
The @atrace
decorator supports the same enrichment options as @trace
:
Best practices
- Use descriptive function names, as they will be used as span names in your traces
- Be mindful of sensitive data when tracing functions - avoid capturing passwords or other secrets
By strategically adding the @trace
decorator to key functions in your application, you can gain valuable insights into your custom code’s performance and behavior, complementing HoneyHive’s automatic instrumentation of LLM components.
Visualizing Your Traces
All traces can be visualized in the HoneyHive web app under Data Store in the session’s specified project. The session tree view shows you the execution flow of your trace, as well all the automatically captured and manually enriched attributes.
Next Up: Enrich Your Traces
Here are some ways in which you can enrich your traces:
Enrichments Overview
An overview of the types of enrichments available for your traces and spans
Logging Client-Side Evaluations
Add client-side metrics, guardrail results, or evaluations to your traces
Logging User Feedback
Enrich traces with feedback or additional annotations
Logging Configuration Details
Include config details, model hyperparameters, or prompt templates
Logging User Properties
Add user properties or tracking IDs to your traces
Logging Custom Metadata
Enrich traces with arbitrary metadata or JSON
While HoneyHive automatically instruments many common LLM application components, you may sometimes want to trace specific parts of your code beyond the automated coverage. HoneyHive provides flexible tracing methods including the general-purpose traceFunction
as well as event-specific methods (traceModel
, traceTool
, traceChain
) that allow you to easily add custom spans to your TypeScript application. These methods capture function inputs, outputs, and additional metadata, providing deeper insights into your application’s behavior.
Using the traceFunction
method
To use the traceFunction
method, you first need to initialize the HoneyHiveTracer
:
Then you can use the traceFunction
method to wrap any function you want to trace:
This will automatically create a span for the function, capturing its inputs and outputs.
Enriching Custom Spans
You can enrich your custom spans with additional properties to provide more context to your traces. There are 2 ways to enrich spans in HoneyHive:-
-
Enriching before execution: The decorator-based enrichment below shows how you can enrich static properties before the code executes.
-
Enriching during execution: You can enrich dynamic properties on a session/trace or event/span level using
enrich_session
andenrich_span
functions. Please refer to the Enrich Traces documentation for more details on enrichment during execution.
Enriching Before Execution
You can provide additional configuration and metadata to your traced functions before execution:
The config
and metadata
parameters allow you to add custom attributes to the span, which can be useful for filtering and analyzing your traces later.
Enriching During Execution Using enrich_span
or enrich_session
You can also enrich your traces and custom spans with various propertires during execution using the enrich_span
or enrich_session
functions.
Please refer to the Enrich Traces documentation for more details on enrichment during execution. You can find the complete documentation for this in the TypeScript SDK reference.
How it works
The traceFunction
method performs the following actions:
- Creates a new span with the function’s name (or ‘anonymous’ if the function is unnamed)
- Captures all function inputs (parameters) as span attributes
- Adds any provided
config
andmetadata
as span attributes - Executes the function
- Captures the function’s return value as a span attribute
- Ends the span
All captured data is automatically sent to your HoneyHive project, where you can view and analyze it alongside your other traces.
Handling asynchronous functions
The traceFunction
method automatically handles both synchronous and asynchronous functions. When working with async functions, remember to:
- Mark your function as
async
- Use
await
when calling the traced function
Event-Specific Tracing Functions
While traceFunction
provides a general-purpose solution for tracing any function, HoneyHive also offers specialized tracing methods for different types of events: traceModel
, traceTool
, and traceChain
.
These methods are designed to trace specific operations in your application, such as LLM requests, deterministic functions, or pipelines, and automatically set the appropriate event type for the span.
traceModel
The traceModel
method is used to trace operations involving LLM requests. It automatically sets the event type to model and allows you to capture inputs, outputs, and metadata specific to the model operation.
In this example, the traceModel
method captures the function’s inputs (e.g., the prompt), outputs (e.g., the generated text), and additional metadata (e.g., the model name and configuration).
traceTool
The traceTool
method is used to trace deterministic operations, such as database queries, API requests, or regex parsing. It automatically sets the event type to tool
and allows you to enrich the span with relevant metadata.
traceChain
The traceChain
method is used to trace pipelines or workflows that group multiple model and tool events into a single composable unit. It automatically sets the event type to chain and allows you to capture the overall behavior of the pipeline.
This method is particularly useful for monitoring and analyzing complex workflows, such as retrieval pipelines or post-processing pipelines.
Best practices
- Use named functions when possible, as they will be used as span names in your traces
- Be mindful of sensitive data when tracing functions - avoid capturing passwords or other secrets
- Use the
config
parameter for settings that might affect the function’s behavior - Use the
metadata
parameter for additional context that could be useful for analysis
By strategically using the traceFunction, traceModel, traceTool, and traceChain methods on key functions in your application, you can gain valuable insights into your custom code’s performance and behavior, complementing HoneyHive’s automatic instrumentation of LLM components.
Visualizing Your Traces
All traces can be visualized in the HoneyHive web app under Data Store in the session’s specified project. The session tree view shows you the execution flow of your trace, as well all the automatically captured and manually enriched attributes.
Next Up: Enrich Your Traces
Here are some ways in which you can enrich your traces:
Enrichments Overview
An overview of the types of enrichments available for your traces and spans
Logging Client-Side Evaluations
Add client-side metrics, guardrail results, or evaluations to your traces
Logging User Feedback
Enrich traces with feedback or additional annotations
Logging Configuration Details
Include config details, model hyperparameters, or prompt templates
Logging User Properties
Add user properties or tracking IDs to your traces
Logging Custom Metadata
Enrich traces with arbitrary metadata or JSON