Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RecordError ¶
RecordError marks a span as having encountered an error. If the error is nil, this function does nothing. This should be called whenever an error occurs within a traced operation to ensure that errors are properly recorded in the tracing system.
Example:
ctx, span := tracing.Start(ctx, "database.Query")
defer span.End()
result, err := db.Query(ctx, "SELECT * FROM users WHERE id = ?", userID)
if err != nil {
tracing.RecordError(span, err)
return nil, err
}
func RecordErrorUnless ¶
RecordErrorUnless marks a span as having encountered an error, unless the error matches one of the ignored errors. Use this for expected errors like sql.ErrNoRows that represent normal conditions.
Example:
result, err := db.QueryRow(ctx, "SELECT * FROM users WHERE id = ?", userID)
if err != nil {
tracing.RecordErrorUnless(span, err, sql.ErrNoRows)
return nil, err
}
func SetGlobalTraceProvider ¶
func SetGlobalTraceProvider(t trace.TracerProvider)
SetGlobalTraceProvider sets the global trace provider for the application. This should be called early in the application startup, typically during the initialization of the observability systems.
Example:
// During application initialization traceExporter, _ := otlptrace.New(ctx, otlptracehttp.NewClient()) traceProvider := trace.NewTracerProvider(trace.WithBatcher(traceExporter)) tracing.SetGlobalTraceProvider(traceProvider)
func Start ¶
func Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span)
Start creates a new span and context. It uses the global tracer provider to create spans with the specified name. Additional trace options can be provided for customization.
The caller is responsible for ending the span with span.End().
Example:
func ProcessOrder(ctx context.Context, orderID string) error {
ctx, span := tracing.Start(ctx, "orders.ProcessOrder")
defer span.End()
// Span now tracks this function execution
// Add more details to the span as needed
span.SetAttributes(attribute.String("order_id", orderID))
// Proceed with actual business logic
// ...
}
Types ¶
This section is empty.