fix: lint issues

This commit is contained in:
Kevin Franklin Kim 2025-05-04 14:50:19 +02:00
parent 6653d59afe
commit 310d95d293
No known key found for this signature in database
3 changed files with 23 additions and 7 deletions

View File

@ -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)
}

24
go.go
View File

@ -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)

View File

@ -5,7 +5,7 @@ import (
)
type Value[T any] struct {
ctx context.Context
ctx context.Context //nolint:containedctx // required
Data T
}