mirror of
https://github.com/argoproj/argo-cd.git
synced 2026-02-20 01:28:45 +01:00
* feat(opentelemetry): ✨ support for secured OTLP endpoint and headers Signed-off-by: Prashant Shahi <me@prashantshahi.dev> * docs(opentelemetry): 📝 include new otlp headers in docs Signed-off-by: Prashant Shahi <me@prashantshahi.dev> * docs(opentelemetry): 📝 update readme docs as per integration tests Signed-off-by: Prashant Shahi <me@prashantshahi.dev> * docs(opentelemetry): 📝 update readme docs as per integration tests Signed-off-by: Prashant Shahi <me@prashantshahi.dev> * chore: resolve indentation issues Signed-off-by: Prashant Shahi <me@prashantshahi.dev> * chore: fix indentation issues Signed-off-by: Prashant Shahi <me@prashantshahi.dev> * chore: include OTLP options in deployment manifests Signed-off-by: Prashant Shahi <me@prashantshahi.dev> * fix: update manifests to resolve failing CI Signed-off-by: Prashant Shahi <me@prashantshahi.dev> --------- Signed-off-by: Prashant Shahi <me@prashantshahi.dev>
79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
package trace
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"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.6.1"
|
|
"google.golang.org/grpc/credentials"
|
|
)
|
|
|
|
// InitTracer initializes the trace provider and the otel grpc exporter.
|
|
func InitTracer(ctx context.Context, serviceName, otlpAddress string, otlpInsecure bool, otlpHeaders map[string]string, otlpAttrs []string) (func(), error) {
|
|
attrs := make([]attribute.KeyValue, 0, len(otlpAttrs))
|
|
for i := range otlpAttrs {
|
|
attr := otlpAttrs[i]
|
|
slice := strings.Split(attr, ":")
|
|
if len(slice) != 2 {
|
|
log.Warnf("OTLP attr '%s' split with ':' length not 2", attr)
|
|
continue
|
|
}
|
|
attrs = append(attrs, attribute.String(slice[0], slice[1]))
|
|
}
|
|
|
|
res, err := resource.New(ctx,
|
|
resource.WithAttributes(
|
|
semconv.ServiceNameKey.String(serviceName),
|
|
),
|
|
resource.WithAttributes(attrs...),
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create resource: %w", err)
|
|
}
|
|
|
|
// set up grpc options based on secure/insecure connection
|
|
var secureOption otlptracegrpc.Option
|
|
if otlpInsecure {
|
|
secureOption = otlptracegrpc.WithInsecure()
|
|
} else {
|
|
secureOption = otlptracegrpc.WithTLSCredentials(credentials.NewClientTLSFromCert(nil, ""))
|
|
}
|
|
|
|
// set up a trace exporter
|
|
exporter, err := otlptracegrpc.New(ctx,
|
|
secureOption,
|
|
otlptracegrpc.WithEndpoint(otlpAddress),
|
|
otlptracegrpc.WithHeaders(otlpHeaders),
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create trace exporter: %w", err)
|
|
}
|
|
|
|
// Register the trace exporter with a TracerProvider, using a batch
|
|
// span processor to aggregate spans before export.
|
|
bsp := sdktrace.NewBatchSpanProcessor(exporter)
|
|
provider := sdktrace.NewTracerProvider(
|
|
sdktrace.WithSampler(sdktrace.AlwaysSample()),
|
|
sdktrace.WithResource(res),
|
|
sdktrace.WithSpanProcessor(bsp),
|
|
)
|
|
|
|
// set global propagator to tracecontext (the default is no-op).
|
|
otel.SetTextMapPropagator(propagation.TraceContext{})
|
|
otel.SetTracerProvider(provider)
|
|
|
|
return func() {
|
|
if err := exporter.Shutdown(ctx); err != nil {
|
|
log.Errorf("failed to stop exporter: %v", err)
|
|
}
|
|
}, nil
|
|
}
|