TracerFX is a powerful tracing and logging library for Go, built on top of OpenTelemetry and Uber FX. It simplifies span management, automatic logging, and attribute extraction using reflection.
pkg/tracerfx: Core provider and options.caller: Logic for automatic function name retrieval from the call stack.delivery/http/middleware: HTTP middleware for automatic trace ID injection and span management.dspan: The core span implementation.log: Logger interface and adapters forzapandzerolog.
go get github.com/dehwyy/tracerfximport (
"github.com/dehwyy/tracerfx/pkg/tracerfx"
"github.com/dehwyy/tracerfx/pkg/tracerfx/log"
)
// ...
app := fx.New(
tracerfx.FxModule(
tracerfx.WithServiceName("my-awesome-service"),
tracerfx.WithLogger(log.NewZapLogger(nil)), // Optional: defaults to zerolog
),
// other modules...
)
app.Run()import (
"context"
"github.com/dehwyy/tracerfx/pkg/tracerfx"
)
func main() {
ctx := context.Background()
provider := tracerfx.NewProvider(
tracerfx.WithServiceName("my-awesome-service"),
tracerfx.WithHost("localhost:4317"),
)
if err := provider.Start(ctx); err != nil {
panic(err)
}
defer provider.Stop(ctx)
}import (
"context"
"github.com/dehwyy/tracerfx/pkg/tracerfx/dspan"
)
func MyFunction(ctx context.Context) {
// Start automatically captures the function name and logs "Span started"
ctx, span := dspan.Start(ctx)
defer span.End() // Automatically logs accumulated attributes and duration
// Your logic here
}WithAttribute uses reflection to extract fields if a struct is provided. It returns the span instance for chaining.
span.WithAttribute("user_id", 123).
WithAttribute("request", inputStruct) // Automatically extracts exported fieldsUse Err(err) to record an error in the span and log it. This does not close the span.
if err := DoSomething(); err != nil {
span.Err(err)
return err
}traceID := span.TraceID()
fmt.Printf("Current Trace ID: %s\n", traceID)The TraceIDHeader middleware automatically starts a span for each request, injects the trace ID into the response header (X-Trace-Id), and cleans up when the request is finished.
import "github.com/dehwyy/tracerfx/pkg/tracerfx/delivery/http/middleware"
mux := http.NewServeMux()
handler := middleware.TraceIDHeader(mux)
http.ListenAndServe(":8080", handler)The project includes a GitHub Action in .github/workflows/release.yml that:
- Runs all tests on every push to
main. - Automatically increments the version (patch) and creates a new GitHub Release if tests pass.