common/tracing/tracing.go

129 lines
3.4 KiB
Go

package tracing
import (
"context"
"crypto/tls"
"crypto/x509"
"os"
"go.ntppool.org/common/logger"
"go.ntppool.org/common/version"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
otelsdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"go.opentelemetry.io/otel/trace"
)
// https://github.com/open-telemetry/opentelemetry-go/blob/main/exporters/otlp/otlptrace/otlptracehttp/example_test.go
type TpShutdownFunc func(ctx context.Context) error
func Tracer() trace.Tracer {
traceProvider := otel.GetTracerProvider()
return traceProvider.Tracer("ntppool-tracer")
}
func Start(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
return Tracer().Start(ctx, spanName, opts...)
}
type GetClientCertificate func(*tls.CertificateRequestInfo) (*tls.Certificate, error)
type TracerConfig struct {
ServiceName string
Environment string
CertificateProvider GetClientCertificate
RootCAs *x509.CertPool
}
var emptyTpShutdownFunc = func(_ context.Context) error {
return nil
}
func InitTracer(ctx context.Context, cfg *TracerConfig) (TpShutdownFunc, error) {
log := logger.Setup()
// exporter, err := srv.newStdoutExporter(os.Stdout)
var err error
var exporter otelsdktrace.SpanExporter
if otlpEndPoint := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT"); len(otlpEndPoint) > 0 {
exporter, err = newOLTPExporter(ctx, cfg)
}
if err != nil {
return emptyTpShutdownFunc, err
}
if exporter == nil {
log.Warn("tracing not configured")
return emptyTpShutdownFunc, nil
}
tp := otelsdktrace.NewTracerProvider(
otelsdktrace.WithSampler(otelsdktrace.AlwaysSample()),
otelsdktrace.WithBatcher(exporter),
otelsdktrace.WithResource(newResource(cfg)),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
return tp.Shutdown, nil
}
func newOLTPExporter(ctx context.Context, cfg *TracerConfig) (otelsdktrace.SpanExporter, error) {
opts := []otlptracehttp.Option{otlptracehttp.WithCompression(otlptracehttp.GzipCompression)}
if cfg.CertificateProvider != nil {
opts = append(opts, otlptracehttp.WithTLSClientConfig(&tls.Config{
GetClientCertificate: cfg.CertificateProvider,
RootCAs: cfg.RootCAs,
}))
}
client := otlptracehttp.NewClient(opts...)
exporter, err := otlptrace.New(ctx, client)
if err != nil {
logger.Setup().Error("creating OTLP trace exporter", "err", err)
}
return exporter, err
}
// func (srv *Server) newStdoutExporter(w io.Writer) (sdktrace.SpanExporter, error) {
// return stdouttrace.New(
// stdouttrace.WithWriter(w),
// // Use human-readable output.
// stdouttrace.WithPrettyPrint(),
// // Do not print timestamps for the demo.
// stdouttrace.WithoutTimestamps(),
// )
// }
// newResource returns a resource describing this application.
func newResource(cfg *TracerConfig) *resource.Resource {
r, err := resource.Merge(
resource.Default(),
resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(cfg.ServiceName),
semconv.ServiceVersionKey.String(version.Version()),
attribute.String("environment", cfg.Environment),
),
)
if err != nil {
panic(err)
}
return r
}