mirror of
https://github.com/foomo/gofuncy.git
synced 2025-10-16 12:25:41 +00:00
fix: lint issues
This commit is contained in:
parent
6653d59afe
commit
310d95d293
@ -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
24
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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user