From 310d95d293cb415032da9113dfd0b4a8a3c19d1b Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Sun, 4 May 2025 14:50:19 +0200 Subject: [PATCH] fix: lint issues --- context.go | 4 ++++ go.go | 24 ++++++++++++++++++------ value.go | 2 +- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/context.go b/context.go index c3a6463..3dcf4a7 100644 --- a/context.go +++ b/context.go @@ -12,6 +12,10 @@ const ( contextKeySender contextKey = "sender" ) +func RootContext(ctx context.Context) context.Context { + return injectRoutineIntoContext(ctx, "root") +} + func injectRoutineIntoContext(ctx context.Context, name string) context.Context { return context.WithValue(ctx, contextKeyRoutine, name) } diff --git a/go.go b/go.go index 3e9faea..1b04684 100644 --- a/go.go +++ b/go.go @@ -13,15 +13,17 @@ import ( "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/metric" semconv "go.opentelemetry.io/otel/semconv/v1.26.0" + "go.opentelemetry.io/otel/trace" ) type ( Options struct { l *slog.Logger - ctx context.Context + ctx context.Context //nolint:containedctx // required name string // telemetry meter metric.Meter + tracer trace.Tracer counter metric.Int64UpDownCounter counterName string histogram metric.Int64Histogram @@ -87,7 +89,7 @@ func Go(fn Func, opts ...Option) <-chan error { if o.name == "" { if _, file, line, ok := runtime.Caller(0); ok { h := sha256.New() - _, _ = h.Write([]byte(fmt.Sprintf("%s:%d", file, line))) + _, _ = fmt.Fprintf(h, "%s:%d", file, line) o.name, _ = humanhash.Humanize(h.Sum(nil), 2, "-") } } @@ -96,6 +98,9 @@ func Go(fn Func, opts ...Option) <-chan error { if o.meter == nil { o.meter = otel.Meter("gofuncy") } + if o.tracer == nil { + o.tracer = otel.Tracer("gofuncy") + } if value, err := o.meter.Int64UpDownCounter(o.counterName); err != nil { o.l.Error("failed to initialize gauge", "error", err) } else { @@ -110,18 +115,25 @@ func Go(fn Func, opts ...Option) <-chan error { err := make(chan error) go func(o *Options, err chan<- error) { + defer close(err) + ctx := o.ctx + var span trace.Span + if o.tracer != nil { + ctx, span = o.tracer.Start(o.ctx, o.name) + defer span.End() + } // create telemetry if enabled if o.counter != nil { - o.counter.Add(o.ctx, 1, metric.WithAttributes(semconv.ProcessRuntimeName(o.name))) - defer o.counter.Add(o.ctx, -1, metric.WithAttributes(semconv.ProcessRuntimeName(o.name))) + o.counter.Add(ctx, 1, metric.WithAttributes(semconv.ProcessRuntimeName(o.name))) + defer o.counter.Add(ctx, -1, metric.WithAttributes(semconv.ProcessRuntimeName(o.name))) } if o.histogram != nil { start := time.Now() defer func() { - o.histogram.Record(o.ctx, time.Since(start).Milliseconds(), metric.WithAttributes(semconv.ProcessRuntimeName(o.name))) + o.histogram.Record(ctx, time.Since(start).Milliseconds(), metric.WithAttributes(semconv.ProcessRuntimeName(o.name))) }() } - ctx := injectParentRoutineIntoContext(o.ctx, RoutineFromContext(o.ctx)) + ctx = injectParentRoutineIntoContext(ctx, RoutineFromContext(ctx)) ctx = injectRoutineIntoContext(ctx, o.name) err <- fn(ctx) }(o, err) diff --git a/value.go b/value.go index a798225..291fe78 100644 --- a/value.go +++ b/value.go @@ -5,7 +5,7 @@ import ( ) type Value[T any] struct { - ctx context.Context + ctx context.Context //nolint:containedctx // required Data T }