Migrate to opentelemetry

This commit is contained in:
Jesse Haka 2024-01-08 10:10:06 +02:00 committed by GitHub
parent 45bb00be04
commit 4ddef9830b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
89 changed files with 2113 additions and 3898 deletions

View file

@ -2,182 +2,213 @@ package tracing
import (
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"strconv"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/rs/zerolog/log"
"github.com/traefik/traefik/v3/pkg/config/static"
"github.com/traefik/traefik/v3/pkg/tracing/opentelemetry"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"go.opentelemetry.io/otel/trace"
)
type contextKey int
const (
// SpanKindNoneEnum Span kind enum none.
SpanKindNoneEnum ext.SpanKindEnum = "none"
tracingKey contextKey = iota
)
// WithTracing Adds Tracing into the context.
func WithTracing(ctx context.Context, tracing *Tracing) context.Context {
return context.WithValue(ctx, tracingKey, tracing)
}
// FromContext Gets Tracing from context.
func FromContext(ctx context.Context) (*Tracing, error) {
if ctx == nil {
panic("nil context")
}
tracer, ok := ctx.Value(tracingKey).(*Tracing)
if !ok {
return nil, errors.New("unable to find tracing in the context")
}
return tracer, nil
}
// Backend is an abstraction for tracking backend (Jaeger, Zipkin, ...).
// Backend is an abstraction for tracking backend (OpenTelemetry, ...).
type Backend interface {
Setup(componentName string) (opentracing.Tracer, io.Closer, error)
}
// Tracing middleware.
type Tracing struct {
ServiceName string `description:"Sets the name for this service" export:"true"`
SpanNameLimit int `description:"Sets the maximum character limit for span names (default 0 = no limit)" export:"true"`
tracer opentracing.Tracer
closer io.Closer
Setup(serviceName string, sampleRate float64, globalAttributes map[string]string, headers map[string]string) (trace.Tracer, io.Closer, error)
}
// NewTracing Creates a Tracing.
func NewTracing(serviceName string, spanNameLimit int, tracingBackend Backend) (*Tracing, error) {
tracing := &Tracing{
ServiceName: serviceName,
SpanNameLimit: spanNameLimit,
func NewTracing(conf *static.Tracing) (trace.Tracer, io.Closer, error) {
var backend Backend
if conf.OTLP != nil {
backend = conf.OTLP
}
var err error
tracing.tracer, tracing.closer, err = tracingBackend.Setup(serviceName)
if backend == nil {
log.Debug().Msg("Could not initialize tracing, using OpenTelemetry by default")
defaultBackend := &opentelemetry.Config{}
backend = defaultBackend
}
return backend.Setup(conf.ServiceName, conf.SampleRate, conf.GlobalAttributes, conf.Headers)
}
// TracerFromContext extracts the trace.Tracer from the given context.
func TracerFromContext(ctx context.Context) trace.Tracer {
span := trace.SpanFromContext(ctx)
if span != nil && span.TracerProvider() != nil {
return span.TracerProvider().Tracer("github.com/traefik/traefik")
}
return nil
}
// ExtractCarrierIntoContext reads cross-cutting concerns from the carrier into a Context.
func ExtractCarrierIntoContext(ctx context.Context, headers http.Header) context.Context {
propagator := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
return propagator.Extract(ctx, propagation.HeaderCarrier(headers))
}
// InjectContextIntoCarrier sets cross-cutting concerns from the request context into the request headers.
func InjectContextIntoCarrier(req *http.Request) {
propagator := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
propagator.Inject(req.Context(), propagation.HeaderCarrier(req.Header))
}
// SetStatusErrorf flags the span as in error and log an event.
func SetStatusErrorf(ctx context.Context, format string, args ...interface{}) {
if span := trace.SpanFromContext(ctx); span != nil {
span.SetStatus(codes.Error, fmt.Sprintf(format, args...))
}
}
// LogClientRequest used to add span attributes from the request as a Client.
// TODO: the semconv does not implement Semantic Convention v1.23.0.
func LogClientRequest(span trace.Span, r *http.Request) {
if r == nil || span == nil {
return
}
// Common attributes https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md#common-attributes
span.SetAttributes(semconv.HTTPRequestMethodKey.String(r.Method))
span.SetAttributes(semconv.NetworkProtocolVersion(proto(r.Proto)))
// Client attributes https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md#http-client
span.SetAttributes(semconv.URLFull(r.URL.String()))
span.SetAttributes(semconv.URLScheme(r.URL.Scheme))
span.SetAttributes(semconv.UserAgentOriginal(r.UserAgent()))
host, port, err := net.SplitHostPort(r.URL.Host)
if err != nil {
return nil, err
}
return tracing, nil
}
// StartSpan delegates to opentracing.Tracer.
func (t *Tracing) StartSpan(operationName string, opts ...opentracing.StartSpanOption) opentracing.Span {
return t.tracer.StartSpan(operationName, opts...)
}
// StartSpanf delegates to StartSpan.
func (t *Tracing) StartSpanf(r *http.Request, spanKind ext.SpanKindEnum, opPrefix string, opParts []string, separator string, opts ...opentracing.StartSpanOption) (opentracing.Span, *http.Request, func()) {
operationName := generateOperationName(opPrefix, opParts, separator, t.SpanNameLimit)
return StartSpan(r, operationName, spanKind, opts...)
}
// Inject delegates to opentracing.Tracer.
func (t *Tracing) Inject(sm opentracing.SpanContext, format, carrier interface{}) error {
return t.tracer.Inject(sm, format, carrier)
}
// Extract delegates to opentracing.Tracer.
func (t *Tracing) Extract(format, carrier interface{}) (opentracing.SpanContext, error) {
return t.tracer.Extract(format, carrier)
}
// IsEnabled determines if tracing was successfully activated.
func (t *Tracing) IsEnabled() bool {
return t != nil && t.tracer != nil
}
// Close tracer.
func (t *Tracing) Close() {
if t.closer != nil {
err := t.closer.Close()
if err != nil {
log.Warn().Err(err).Send()
span.SetAttributes(attribute.String("network.peer.address", host))
span.SetAttributes(semconv.ServerAddress(r.URL.Host))
switch r.URL.Scheme {
case "http":
span.SetAttributes(attribute.String("network.peer.port", "80"))
span.SetAttributes(semconv.ServerPort(80))
case "https":
span.SetAttributes(attribute.String("network.peer.port", "443"))
span.SetAttributes(semconv.ServerPort(443))
}
} else {
span.SetAttributes(attribute.String("network.peer.address", host))
span.SetAttributes(attribute.String("network.peer.port", port))
intPort, _ := strconv.Atoi(port)
span.SetAttributes(semconv.ServerAddress(host))
span.SetAttributes(semconv.ServerPort(intPort))
}
}
// LogRequest used to create span tags from the request.
func LogRequest(span opentracing.Span, r *http.Request) {
if span != nil && r != nil {
ext.HTTPMethod.Set(span, r.Method)
ext.HTTPUrl.Set(span, r.URL.String())
span.SetTag("http.host", r.Host)
// LogServerRequest used to add span attributes from the request as a Server.
// TODO: the semconv does not implement Semantic Convention v1.23.0.
func LogServerRequest(span trace.Span, r *http.Request) {
if r == nil {
return
}
// Common attributes https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md#common-attributes
span.SetAttributes(semconv.HTTPRequestMethodKey.String(r.Method))
span.SetAttributes(semconv.NetworkProtocolVersion(proto(r.Proto)))
// Server attributes https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md#http-server-semantic-conventions
span.SetAttributes(semconv.HTTPRequestBodySize(int(r.ContentLength)))
span.SetAttributes(semconv.URLPath(r.URL.Path))
span.SetAttributes(semconv.URLQuery(r.URL.RawQuery))
span.SetAttributes(semconv.URLScheme(r.Header.Get("X-Forwarded-Proto")))
span.SetAttributes(semconv.UserAgentOriginal(r.UserAgent()))
span.SetAttributes(semconv.ServerAddress(r.Host))
host, port, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
span.SetAttributes(semconv.ClientAddress(r.RemoteAddr))
span.SetAttributes(attribute.String("network.peer.address", r.RemoteAddr))
} else {
span.SetAttributes(attribute.String("network.peer.address", host))
span.SetAttributes(attribute.String("network.peer.port", port))
span.SetAttributes(semconv.ClientAddress(host))
intPort, _ := strconv.Atoi(port)
span.SetAttributes(semconv.ClientPort(intPort))
}
span.SetAttributes(semconv.ClientSocketAddress(r.Header.Get("X-Forwarded-For")))
}
func proto(proto string) string {
switch proto {
case "HTTP/1.0":
return "1.0"
case "HTTP/1.1":
return "1.1"
case "HTTP/2":
return "2"
case "HTTP/3":
return "3"
default:
return proto
}
}
// LogResponseCode used to log response code in span.
func LogResponseCode(span opentracing.Span, code int) {
func LogResponseCode(span trace.Span, code int, spanKind trace.SpanKind) {
if span != nil {
ext.HTTPStatusCode.Set(span, uint16(code))
if code >= http.StatusInternalServerError {
ext.Error.Set(span, true)
var status codes.Code
var desc string
switch spanKind {
case trace.SpanKindServer:
status, desc = ServerStatus(code)
case trace.SpanKindClient:
status, desc = ClientStatus(code)
default:
status, desc = DefaultStatus(code)
}
span.SetStatus(status, desc)
if code > 0 {
span.SetAttributes(semconv.HTTPResponseStatusCode(code))
}
}
}
// GetSpan used to retrieve span from request context.
func GetSpan(r *http.Request) opentracing.Span {
return opentracing.SpanFromContext(r.Context())
}
// InjectRequestHeaders used to inject OpenTracing headers into the request.
func InjectRequestHeaders(r *http.Request) {
if span := GetSpan(r); span != nil {
err := opentracing.GlobalTracer().Inject(
span.Context(),
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(r.Header))
if err != nil {
log.Ctx(r.Context()).Error().Err(err).Send()
}
// ServerStatus returns a span status code and message for an HTTP status code
// value returned by a server. Status codes in the 400-499 range are not
// returned as errors.
func ServerStatus(code int) (codes.Code, string) {
if code < 100 || code >= 600 {
return codes.Error, fmt.Sprintf("Invalid HTTP status code %d", code)
}
}
// LogEventf logs an event to the span in the request context.
func LogEventf(r *http.Request, format string, args ...interface{}) {
if span := GetSpan(r); span != nil {
span.LogKV("event", fmt.Sprintf(format, args...))
if code >= 500 {
return codes.Error, ""
}
return codes.Unset, ""
}
// StartSpan starts a new span from the one in the request context.
func StartSpan(r *http.Request, operationName string, spanKind ext.SpanKindEnum, opts ...opentracing.StartSpanOption) (opentracing.Span, *http.Request, func()) {
span, ctx := opentracing.StartSpanFromContext(r.Context(), operationName, opts...)
switch spanKind {
case ext.SpanKindRPCClientEnum:
ext.SpanKindRPCClient.Set(span)
case ext.SpanKindRPCServerEnum:
ext.SpanKindRPCServer.Set(span)
case ext.SpanKindProducerEnum:
ext.SpanKindProducer.Set(span)
case ext.SpanKindConsumerEnum:
ext.SpanKindConsumer.Set(span)
default:
// noop
// ClientStatus returns a span status code and message for an HTTP status code
// value returned by a server. Status codes in the 400-499 range are not
// returned as errors.
func ClientStatus(code int) (codes.Code, string) {
if code < 100 || code >= 600 {
return codes.Error, fmt.Sprintf("Invalid HTTP status code %d", code)
}
r = r.WithContext(ctx)
return span, r, func() { span.Finish() }
}
// SetError flags the span associated with this request as in error.
func SetError(r *http.Request) {
if span := GetSpan(r); span != nil {
ext.Error.Set(span, true)
if code >= 400 {
return codes.Error, ""
}
return codes.Unset, ""
}
// SetErrorWithEvent flags the span associated with this request as in error and log an event.
func SetErrorWithEvent(r *http.Request, format string, args ...interface{}) {
SetError(r)
LogEventf(r, format, args...)
// DefaultStatus returns a span status code and message for an HTTP status code
// value generated internally.
func DefaultStatus(code int) (codes.Code, string) {
if code < 100 || code >= 600 {
return codes.Error, fmt.Sprintf("Invalid HTTP status code %d", code)
}
if code >= 500 {
return codes.Error, ""
}
return codes.Unset, ""
}