2022-12-11 20:39:37 +00:00
|
|
|
package tracing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
2023-04-05 13:51:07 +00:00
|
|
|
tlsutils "github.com/kyverno/kyverno/pkg/utils/tls"
|
2022-12-11 20:39:37 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/version"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"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"
|
2024-08-28 17:09:58 +00:00
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
|
2022-12-11 20:39:37 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewTraceConfig generates the initial tracing configuration with 'address' as the endpoint to connect to the Opentelemetry Collector
|
2023-01-26 09:39:52 +00:00
|
|
|
func NewTraceConfig(log logr.Logger, tracerName, address, certs string, kubeClient kubernetes.Interface) (func(), error) {
|
2022-12-11 20:39:37 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
var client otlptrace.Client
|
|
|
|
if certs != "" {
|
|
|
|
// here the certificates are stored as configmaps
|
2023-04-05 13:51:07 +00:00
|
|
|
transportCreds, err := tlsutils.FetchCert(ctx, certs, kubeClient)
|
2022-12-11 20:39:37 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err, "Error fetching certificate from secret")
|
|
|
|
}
|
|
|
|
client = otlptracegrpc.NewClient(
|
|
|
|
otlptracegrpc.WithEndpoint(address),
|
|
|
|
otlptracegrpc.WithTLSCredentials(transportCreds),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
client = otlptracegrpc.NewClient(
|
|
|
|
otlptracegrpc.WithEndpoint(address),
|
|
|
|
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")
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-06-28 14:25:57 +00:00
|
|
|
res, err := resource.New(
|
|
|
|
ctx,
|
|
|
|
resource.WithTelemetrySDK(),
|
|
|
|
resource.WithAttributes(
|
2023-01-26 09:39:52 +00:00
|
|
|
semconv.ServiceNameKey.String(tracerName),
|
2023-06-30 23:27:06 +00:00
|
|
|
semconv.ServiceVersionKey.String(version.Version()),
|
2022-12-11 20:39:37 +00:00
|
|
|
),
|
2023-06-28 14:25:57 +00:00
|
|
|
resource.WithFromEnv(),
|
2022-12-11 20:39:37 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err, "failed creating resource")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// create controller and bind the exporter with it
|
|
|
|
tp := sdktrace.NewTracerProvider(
|
|
|
|
sdktrace.WithBatcher(traceExp),
|
|
|
|
sdktrace.WithResource(res),
|
|
|
|
)
|
|
|
|
// set global propagator to tracecontext (the default is no-op).
|
|
|
|
otel.SetTracerProvider(tp)
|
2023-08-24 06:45:58 +00:00
|
|
|
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
|
2022-12-11 20:39:37 +00: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
|
|
|
|
}
|