Skip to content

Commit

Permalink
Added sampling for tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
coolwednesday committed Sep 27, 2024
1 parent 1d0c225 commit d42aae8
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions pkg/gofr/gofr.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
shutDownTimeout = 30 * time.Second
gofrTraceExporter = "gofr"
gofrTracerURL = "https://tracer.gofr.dev"
defaultTraceRatio = 1
)

// App is the main application in the GoFr framework.
Expand Down Expand Up @@ -383,16 +384,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 @@ -404,6 +395,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 d42aae8

Please sign in to comment.