2022-07-11 23:19:47 +05:30
|
|
|
package tracing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-18 15:21:15 +01:00
|
|
|
"time"
|
2022-07-11 23:19:47 +05:30
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
|
|
|
"github.com/kyverno/kyverno/pkg/utils/kube"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
|
|
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
|
|
|
|
"go.opentelemetry.io/otel/propagation"
|
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
)
|
|
|
|
|
2022-11-18 15:21:15 +01:00
|
|
|
// NewTraceConfig generates the initial tracing configuration with 'address' as the endpoint to connect to the Opentelemetry Collector
|
|
|
|
func NewTraceConfig(log logr.Logger, name, address, certs string, kubeClient kubernetes.Interface) (func(), error) {
|
2022-07-11 23:19:47 +05:30
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
var client otlptrace.Client
|
|
|
|
|
|
|
|
if certs != "" {
|
|
|
|
// here the certificates are stored as configmaps
|
|
|
|
transportCreds, err := kube.FetchCert(ctx, certs, kubeClient)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err, "Error fetching certificate from secret")
|
|
|
|
}
|
|
|
|
|
|
|
|
client = otlptracegrpc.NewClient(
|
2022-11-18 15:21:15 +01:00
|
|
|
otlptracegrpc.WithEndpoint(address),
|
2022-07-11 23:19:47 +05:30
|
|
|
otlptracegrpc.WithTLSCredentials(transportCreds),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
client = otlptracegrpc.NewClient(
|
2022-11-18 15:21:15 +01:00
|
|
|
otlptracegrpc.WithEndpoint(address),
|
2022-07-11 23:19:47 +05:30
|
|
|
otlptracegrpc.WithInsecure(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create New Exporter for exporting metrics
|
|
|
|
traceExp, err := otlptrace.New(ctx, client)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err, "Failed to create the collector exporter")
|
2022-07-25 14:55:26 +05:30
|
|
|
return nil, err
|
2022-07-11 23:19:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
res, err := resource.New(context.Background(),
|
2022-11-18 15:21:15 +01:00
|
|
|
resource.WithAttributes(semconv.ServiceNameKey.String(name)),
|
2022-07-11 23:19:47 +05:30
|
|
|
resource.WithSchemaURL(semconv.SchemaURL),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err, "failed creating resource")
|
2022-07-25 14:55:26 +05:30
|
|
|
return nil, err
|
2022-07-11 23:19:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bsp := sdktrace.NewBatchSpanProcessor(traceExp)
|
|
|
|
// create controller and bind the exporter with it
|
|
|
|
tp := sdktrace.NewTracerProvider(
|
|
|
|
sdktrace.WithSampler(sdktrace.AlwaysSample()),
|
|
|
|
sdktrace.WithSpanProcessor(bsp),
|
|
|
|
sdktrace.WithResource(res),
|
|
|
|
)
|
|
|
|
|
|
|
|
// set global propagator to tracecontext (the default is no-op).
|
|
|
|
otel.SetTextMapPropagator(propagation.TraceContext{})
|
|
|
|
otel.SetTracerProvider(tp)
|
2022-11-18 15:21:15 +01:00
|
|
|
return func() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
// pushes any last exports to the receiver
|
|
|
|
if err := tp.Shutdown(ctx); err != nil {
|
|
|
|
otel.Handle(err)
|
|
|
|
}
|
|
|
|
}, nil
|
2022-07-11 23:19:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// DoInSpan executes function doFn inside new span with `operationName` name and hooking as child to a span found within given context if any.
|
|
|
|
func DoInSpan(ctx context.Context, tracerName string, operationName string, doFn func(context.Context)) {
|
|
|
|
newCtx, span := otel.Tracer(tracerName).Start(ctx, operationName)
|
|
|
|
defer span.End()
|
|
|
|
doFn(newCtx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartSpan creates a span from a context with `operationName` name
|
2022-11-17 15:41:49 +01:00
|
|
|
func StartSpan(ctx context.Context, tracerName string, operationName string, attributes ...attribute.KeyValue) (context.Context, trace.Span) {
|
|
|
|
return otel.Tracer(tracerName).Start(ctx, operationName, trace.WithAttributes(attributes...))
|
2022-07-11 23:19:47 +05:30
|
|
|
}
|
2022-11-18 10:18:00 +01:00
|
|
|
|
|
|
|
// Span executes function doFn inside new span with `operationName` name and hooking as child to a span found within given context if any.
|
|
|
|
func Span(ctx context.Context, tracerName string, operationName string, doFn func(context.Context, trace.Span), opts ...trace.SpanStartOption) {
|
|
|
|
newCtx, span := otel.Tracer(tracerName).Start(ctx, operationName, opts...)
|
|
|
|
defer span.End()
|
|
|
|
doFn(newCtx, span)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Span executes function doFn inside new span with `operationName` name and hooking as child to a span found within given context if any.
|
|
|
|
func Span1[T any](ctx context.Context, tracerName string, operationName string, doFn func(context.Context, trace.Span) T, opts ...trace.SpanStartOption) T {
|
|
|
|
newCtx, span := otel.Tracer(tracerName).Start(ctx, operationName, opts...)
|
|
|
|
defer span.End()
|
|
|
|
return doFn(newCtx, span)
|
|
|
|
}
|