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"
|
contextKeySender contextKey = "sender"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func RootContext(ctx context.Context) context.Context {
|
||||||
|
return injectRoutineIntoContext(ctx, "root")
|
||||||
|
}
|
||||||
|
|
||||||
func injectRoutineIntoContext(ctx context.Context, name string) context.Context {
|
func injectRoutineIntoContext(ctx context.Context, name string) context.Context {
|
||||||
return context.WithValue(ctx, contextKeyRoutine, name)
|
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"
|
||||||
"go.opentelemetry.io/otel/metric"
|
"go.opentelemetry.io/otel/metric"
|
||||||
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
|
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
|
||||||
|
"go.opentelemetry.io/otel/trace"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Options struct {
|
Options struct {
|
||||||
l *slog.Logger
|
l *slog.Logger
|
||||||
ctx context.Context
|
ctx context.Context //nolint:containedctx // required
|
||||||
name string
|
name string
|
||||||
// telemetry
|
// telemetry
|
||||||
meter metric.Meter
|
meter metric.Meter
|
||||||
|
tracer trace.Tracer
|
||||||
counter metric.Int64UpDownCounter
|
counter metric.Int64UpDownCounter
|
||||||
counterName string
|
counterName string
|
||||||
histogram metric.Int64Histogram
|
histogram metric.Int64Histogram
|
||||||
@ -87,7 +89,7 @@ func Go(fn Func, opts ...Option) <-chan error {
|
|||||||
if o.name == "" {
|
if o.name == "" {
|
||||||
if _, file, line, ok := runtime.Caller(0); ok {
|
if _, file, line, ok := runtime.Caller(0); ok {
|
||||||
h := sha256.New()
|
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, "-")
|
o.name, _ = humanhash.Humanize(h.Sum(nil), 2, "-")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -96,6 +98,9 @@ func Go(fn Func, opts ...Option) <-chan error {
|
|||||||
if o.meter == nil {
|
if o.meter == nil {
|
||||||
o.meter = otel.Meter("gofuncy")
|
o.meter = otel.Meter("gofuncy")
|
||||||
}
|
}
|
||||||
|
if o.tracer == nil {
|
||||||
|
o.tracer = otel.Tracer("gofuncy")
|
||||||
|
}
|
||||||
if value, err := o.meter.Int64UpDownCounter(o.counterName); err != nil {
|
if value, err := o.meter.Int64UpDownCounter(o.counterName); err != nil {
|
||||||
o.l.Error("failed to initialize gauge", "error", err)
|
o.l.Error("failed to initialize gauge", "error", err)
|
||||||
} else {
|
} else {
|
||||||
@ -110,18 +115,25 @@ func Go(fn Func, opts ...Option) <-chan error {
|
|||||||
|
|
||||||
err := make(chan error)
|
err := make(chan error)
|
||||||
go func(o *Options, err 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
|
// create telemetry if enabled
|
||||||
if o.counter != nil {
|
if o.counter != nil {
|
||||||
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(o.ctx, -1, metric.WithAttributes(semconv.ProcessRuntimeName(o.name)))
|
defer o.counter.Add(ctx, -1, metric.WithAttributes(semconv.ProcessRuntimeName(o.name)))
|
||||||
}
|
}
|
||||||
if o.histogram != nil {
|
if o.histogram != nil {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
defer func() {
|
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)
|
ctx = injectRoutineIntoContext(ctx, o.name)
|
||||||
err <- fn(ctx)
|
err <- fn(ctx)
|
||||||
}(o, err)
|
}(o, err)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user