Documentation
¶
Overview ¶
Package phos provides lightweight request-scoped tracing with optional export, W3C Trace Context propagation, and CLI-oriented trace rendering.
Spans are stored in context: use NewSpan (or InSpan / InSpanE) and call Span.End exactly once per span; Span.End is idempotent. While a span is open, Span.Attrs, Span.Event, Span.Error, and Span.Fail are safe for concurrent use; after Span.End, further mutations are ignored.
Use WithExporter to attach an Exporter to a context (e.g. per request). Use SetExporter for a process-wide default when no context exporter is set. SetExporter uses an internal read-write lock so it is safe to call concurrently with NewSpan and span completion.
Propagation helpers InjectTraceContext and ExtractTraceContext work with any Carrier implementation, such as HTTPHeaderCarrier or MapCarrier.
Index ¶
- Constants
- func Attrs(ctx context.Context, attrs ...slog.Attr)
- func Error(ctx context.Context, err error, attrs ...slog.Attr)
- func Event(ctx context.Context, name string, attrs ...slog.Attr)
- func ExtractTraceContext(ctx context.Context, carrier Carrier) context.Context
- func Fail(ctx context.Context)
- func InSpan(ctx context.Context, name string, fn func(context.Context), attrs ...slog.Attr)
- func InSpanE(ctx context.Context, name string, fn func(context.Context) error, ...) error
- func InjectTraceContext(ctx context.Context, carrier Carrier)
- func RenderTrace(spans []Snapshot) string
- func RenderTraces(spans []Snapshot) string
- func SetExporter(exp Exporter) func()
- func WithExporter(ctx context.Context, exp Exporter) context.Context
- type Carrier
- type Exporter
- type HTTPHeaderCarrier
- type InMemExportImporter
- type MapCarrier
- type NoopExporter
- type Snapshot
- type SnapshotError
- type SnapshotEvent
- type Span
- type TraceParent
Examples ¶
Constants ¶
const ( TraceParentHeader = "traceparent" TraceStateHeader = "tracestate" )
Variables ¶
This section is empty.
Functions ¶
func ExtractTraceContext ¶
func InjectTraceContext ¶
func RenderTrace ¶
RenderTrace renders one trace as a CLI-friendly timeline.
func RenderTraces ¶
RenderTraces renders all spans grouped by trace in a CLI-friendly timeline.
func SetExporter ¶
func SetExporter(exp Exporter) func()
SetExporter replaces the package-level exporter and returns a restore function for callers that need to put the previous exporter back. It is safe for concurrent use with NewSpan and span completion.
Types ¶
type HTTPHeaderCarrier ¶
func (HTTPHeaderCarrier) Keys ¶
func (c HTTPHeaderCarrier) Keys() []string
func (HTTPHeaderCarrier) Set ¶
func (c HTTPHeaderCarrier) Set(key string, value string)
type InMemExportImporter ¶
type InMemExportImporter struct {
// contains filtered or unexported fields
}
func NewInMemExportImporter ¶
func NewInMemExportImporter() *InMemExportImporter
func (*InMemExportImporter) Export ¶
func (e *InMemExportImporter) Export(data Snapshot)
func (*InMemExportImporter) Snapshot ¶
func (e *InMemExportImporter) Snapshot() map[string]Snapshot
Snapshot returns a deep copy of all exported spans keyed by span ID.
type MapCarrier ¶
func (MapCarrier) Keys ¶
func (c MapCarrier) Keys() []string
func (MapCarrier) Set ¶
func (c MapCarrier) Set(key string, value string)
type NoopExporter ¶
type NoopExporter struct{}
func (*NoopExporter) Export ¶
func (e *NoopExporter) Export(_ Snapshot)
type Snapshot ¶
type Snapshot struct {
ID string `json:"id"`
Name string `json:"name"`
ParentID string `json:"parent_id"`
TraceID string `json:"trace_id"`
TimeStart time.Time `json:"time_start"`
TimeEnd time.Time `json:"time_end"`
Failed bool `json:"failed"`
Attrs []slog.Attr `json:"attrs"`
Events []SnapshotEvent `json:"events"`
Errors []SnapshotError `json:"errors"`
Root bool `json:"root"`
}
type SnapshotError ¶
func (SnapshotError) MarshalJSON ¶
func (e SnapshotError) MarshalJSON() ([]byte, error)
type SnapshotEvent ¶
type Span ¶
type Span struct {
// contains filtered or unexported fields
}
func NewSpan ¶
Example ¶
package main
import (
"context"
"log/slog"
"github.com/johan-st/phos"
)
func main() {
ctx := context.Background()
ctx, span := phos.NewSpan(ctx, "request", slog.String("method", "GET"))
defer span.End()
phos.Attrs(ctx, slog.String("user", "alice"))
phos.Event(ctx, "authorized")
}
Output:
type TraceParent ¶
func ParseTraceParent ¶
func ParseTraceParent(v string) (TraceParent, error)
func (TraceParent) String ¶
func (t TraceParent) String() string