Skip to content

Commit

Permalink
Added sampling for tracing (#1059)
Browse files Browse the repository at this point in the history
  • Loading branch information
coolwednesday authored Sep 30, 2024
1 parent 2074f0b commit d577a52
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
6 changes: 6 additions & 0 deletions docs/quick-start/observability/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ DB_PORT=3306
# tracing configs
TRACE_EXPORTER=zipkin
TRACER_URL=http://localhost:2005/api/v2/spans
TRACER_RATIO=0.1
LOG_LEVEL=DEBUG
```
Expand Down Expand Up @@ -220,6 +221,7 @@ Add Jaeger Tracer configs in `.env` file, your .env will be updated to
# tracing configs
TRACE_EXPORTER=jaeger
TRACER_URL=localhost:14317
TRACER_RATIO=0.1
```

Open {% new-tab-link title="jaeger" href="http://localhost:16686/trace/" /%} and search by TraceID (correlationID) to see the trace.
Expand All @@ -236,6 +238,7 @@ Add OTLP configs in `.env` file, your .env will be updated to
# tracing configs
TRACE_EXPORTER=otlp
TRACER_URL=localhost:4317
TRACER_RATIO=0.1
```


Expand All @@ -251,6 +254,9 @@ Add GoFr Tracer configs in `.env` file, your .env will be updated to
# tracing configs
TRACE_EXPORTER=gofr
TRACER_RATIO=0.1
```

> NOTE: `TRACER_RATIO` refers to the proportion of traces that are exported through sampling. It ranges between 0 to 1. By default, this ratio is set to 1, meaning all traces are exported.
Open {% new-tab-link title="gofr-tracer" href="https://tracer.gofr.dev/" /%} and search by TraceID (correlationID) to see the trace.
5 changes: 5 additions & 0 deletions docs/references/configs/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ This document lists all the configuration options supported by the GoFr framewor

---

- TRACER_RATIO
- Refers to the proportion of traces that are exported through sampling. It is optional configuration. By default, this ratio is set to 1.

---

- TRACER_AUTH_KEY
- Authorization header for trace exporter requests.
- Supported for zipkin, jaeger.
Expand Down
26 changes: 16 additions & 10 deletions pkg/gofr/gofr.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,16 +385,6 @@ func (a *App) Migrate(migrationsMap map[int64]migration.Migrate) {
}

func (a *App) initTracer() {
tp := sdktrace.NewTracerProvider(
sdktrace.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(a.container.GetAppName()),
)),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
otel.SetErrorHandler(&otelErrorHandler{logger: a.container.Logger})

traceExporter := a.Config.Get("TRACE_EXPORTER")
tracerURL := a.Config.Get("TRACER_URL")

Expand All @@ -406,6 +396,22 @@ func (a *App) initTracer() {
return
}

traceRatio, err := strconv.ParseFloat(a.Config.GetOrDefault("TRACER_RATIO", "1"), 64)
if err != nil {
a.container.Error(err)
}

tp := sdktrace.NewTracerProvider(
sdktrace.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(a.container.GetAppName()),
)),
sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(traceRatio))),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
otel.SetErrorHandler(&otelErrorHandler{logger: a.container.Logger})

exporter, err := a.getExporter(traceExporter, tracerHost, tracerPort, tracerURL)

if err != nil {
Expand Down

0 comments on commit d577a52

Please sign in to comment.