Documentation
¶
Overview ¶
Package trace provides distributed tracing functionality for Braintrust experiments.
This package is built on OpenTelemetry and integrates with the Braintrust Client for session-based authentication.
To enable tracing, create a TracerProvider and Braintrust client:
tp := trace.NewTracerProvider()
defer tp.Shutdown(context.Background())
bt, err := braintrust.New(tp,
braintrust.WithAPIKey(os.Getenv("BRAINTRUST_API_KEY")),
braintrust.WithProject("my-project"),
)
if err != nil {
log.Fatal(err)
}
Once the client is created, create spans using OpenTelemetry:
tracer := bt.Tracer("my-app")
ctx, span := tracer.Start(ctx, "my-operation")
span.SetAttributes(attribute.String("user.id", "123"))
span.End()
Index ¶
- Constants
- func AddSpanProcessor(tp *sdktrace.TracerProvider, session *auth.Session, cfg Config) error
- func GetSpanProcessor(session *auth.Session, cfg Config) (sdktrace.SpanProcessor, error)
- func Permalink(span oteltrace.Span) (string, error)
- func SetParent(ctx context.Context, parent Parent) context.Context
- type Config
- type Parent
- type ParentType
- type SpanFilterFunc
Constants ¶
const ParentOtelAttrKey = "braintrust.parent"
ParentOtelAttrKey is the OpenTelemetry attribute key used to associate spans with Braintrust parents. This enables spans to be grouped under specific projects or experiments in the Braintrust platform. Parents are formatted as "project_id:{uuid}" or "experiment_id:{uuid}".
Variables ¶
This section is empty.
Functions ¶
func AddSpanProcessor ¶
AddSpanProcessor creates and registers a Braintrust span processor.
func GetSpanProcessor ¶
GetSpanProcessor creates a Braintrust span processor.
func SetParent ¶
SetParent will add a parent to the given context. Any span started with that context will be marked with that parent, and sent to the given project or experiment in Braintrust.
The parent is stored in both context values (for same-process access) and W3C baggage (for distributed tracing across process boundaries).
Example:
ctx = trace.SetParent(ctx, trace.Parent{Type: trace.ParentTypeProjectName, ID: "my-project"})
_, span := tracer.Start(ctx, "database-query")
Types ¶
type Config ¶
type Config struct {
// Default parent for spans
DefaultProjectID string
DefaultProjectName string
// Span filtering
FilterAISpans bool
SpanFilterFuncs []SpanFilterFunc
// Debug
EnableConsoleLog bool
// Test override: provide custom exporter (e.g., memory exporter for tests)
Exporter sdktrace.SpanExporter
// Logger
Logger logger.Logger
}
Config holds configuration for Braintrust tracing
type Parent ¶
type Parent struct {
Type ParentType
ID string
}
Parent represents where data goes in Braintrust - a project, an experiment, etc.
func GetParent ¶
GetParent returns the parent from the context and a boolean indicating if it was set. It first checks the context value (for same-process access), then falls back to baggage (for distributed tracing across process boundaries).
type ParentType ¶
type ParentType string
ParentType represents the different places spans can be sent to in Braintrust - projects, experiments, etc.
const ( // ParentTypeProjectName is the type of parent that represents a project by name. ParentTypeProjectName ParentType = "project_name" // ParentTypeProjectID is the type of parent that represents a project by ID. ParentTypeProjectID ParentType = "project_id" // ParentTypeExperimentID is the type of parent that represents an experiment by ID. ParentTypeExperimentID ParentType = "experiment_id" )
func (ParentType) IsValid ¶
func (p ParentType) IsValid() bool
IsValid returns true if the ParentType is a valid type.
type SpanFilterFunc ¶
type SpanFilterFunc func(span sdktrace.ReadOnlySpan) int
SpanFilterFunc decides which spans to send to Braintrust. Return >0 to keep, <0 to drop, 0 to not influence.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package attachment provides utilities for creating and managing attachments in Braintrust traces.
|
Package attachment provides utilities for creating and managing attachments in Braintrust traces. |
|
contrib
|
|
|
anthropic
Package anthropic provides OpenTelemetry middleware for tracing Anthropic API calls.
|
Package anthropic provides OpenTelemetry middleware for tracing Anthropic API calls. |
|
genai
Package genai provides OpenTelemetry tracing for Google Gemini API calls.
|
Package genai provides OpenTelemetry tracing for Google Gemini API calls. |
|
langchaingo
Package langchaingo provides OpenTelemetry tracing for LangChainGo applications.
|
Package langchaingo provides OpenTelemetry tracing for LangChainGo applications. |
|
openai
Package openai provides OpenTelemetry middleware for tracing OpenAI API calls.
|
Package openai provides OpenTelemetry middleware for tracing OpenAI API calls. |
|
a2a
module
|
|
|
adk
module
|
|
|
all
module
|
|
|
bedrockruntime
module
|
|
|
cloudwego/eino
module
|
|
|
genkit
module
|
|
|
Package internal provides shared middleware functionality for OpenTelemetry tracers.
|
Package internal provides shared middleware functionality for OpenTelemetry tracers. |