go-app/pkg/otel/util.go

37 lines
838 B
Go

package otel
import (
"context"
"os"
"strings"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
)
func GetTracer(ctx context.Context, components ...string) trace.Tracer {
return otel.Tracer(getName(ctx, components...))
}
func GetMeter(ctx context.Context, components ...string) metric.Meter {
return otel.Meter(getName(ctx, components...))
}
func getName(ctx context.Context, components ...string) string {
cfg := config.MustFromCtx(ctx)
serviceName := cfg.Name
if otelEnvName := os.Getenv("OTEL_SERVICE_NAME"); otelEnvName != "" {
serviceName = otelEnvName
}
path := make([]string, 0, len(components)+1)
path = append(path, serviceName)
path = append(path, components...)
return strings.Join(path, ".")
}