feat: Move from datadog to generic otel (#1567)

This commit is contained in:
Garrett Delfosse
2022-05-19 17:43:07 -05:00
committed by GitHub
parent 2a85d3d083
commit 376c6819e0
7 changed files with 169 additions and 306 deletions

View File

@ -15,7 +15,7 @@ import (
"golang.org/x/xerrors"
"google.golang.org/api/idtoken"
chitrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/go-chi/chi.v5"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"cdr.dev/slog"
"github.com/coder/coder/buildinfo"
@ -25,6 +25,7 @@ import (
"github.com/coder/coder/coderd/httpapi"
"github.com/coder/coder/coderd/httpmw"
"github.com/coder/coder/coderd/rbac"
"github.com/coder/coder/coderd/tracing"
"github.com/coder/coder/coderd/turnconn"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/site"
@ -51,6 +52,7 @@ type Options struct {
SSHKeygenAlgorithm gitsshkey.Algorithm
TURNServer *turnconn.Server
Authorizer rbac.Authorizer
TracerProvider *sdktrace.TracerProvider
}
// New constructs the Coder API into an HTTP handler.
@ -92,7 +94,7 @@ func New(options *Options) (http.Handler, func()) {
})
},
httpmw.Prometheus,
chitrace.Middleware(),
tracing.HTTPMW(api.TracerProvider, "coderd.http"),
)
r.Route("/api/v2", func(r chi.Router) {

View File

@ -0,0 +1,41 @@
package tracing
import (
"context"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"golang.org/x/xerrors"
)
// TracerProvider creates a grpc otlp exporter and configures a trace provider.
// Caller is responsible for calling TracerProvider.Shutdown to ensure all data is flushed.
func TracerProvider(ctx context.Context, service string) (*sdktrace.TracerProvider, error) {
res, err := resource.New(ctx,
resource.WithAttributes(
// the service name used to display traces in backends
semconv.ServiceNameKey.String(service),
),
)
if err != nil {
return nil, xerrors.Errorf("creating otlp resource: %w", err)
}
// By default we send span data to a local otel collector.
// The endpoint we push to can be configured with env vars.
// See https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md
exporter, err := otlptrace.New(ctx, otlptracegrpc.NewClient(otlptracegrpc.WithInsecure()))
if err != nil {
return nil, xerrors.Errorf("creating otlp exporter: %w", err)
}
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(res),
)
return tracerProvider, nil
}

68
coderd/tracing/httpmw.go Normal file
View File

@ -0,0 +1,68 @@
package tracing
import (
"fmt"
"net/http"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/chi/v5"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
// HTTPMW adds tracing to http routes.
func HTTPMW(tracerProvider *sdktrace.TracerProvider, name string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
// do not trace if exporter has not be initialized
if tracerProvider == nil {
next.ServeHTTP(rw, r)
return
}
// start span with default span name. Span name will be updated to "method route" format once request finishes.
_, span := tracerProvider.Tracer(name).Start(r.Context(), fmt.Sprintf("%s %s", r.Method, r.RequestURI))
defer span.End()
wrw := middleware.NewWrapResponseWriter(rw, r.ProtoMajor)
// pass the span through the request context and serve the request to the next middleware
next.ServeHTTP(rw, r)
// set the resource name as we get it only once the handler is executed
route := chi.RouteContext(r.Context()).RoutePattern()
if route != "" {
span.SetName(fmt.Sprintf("%s %s", r.Method, route))
}
span.SetAttributes(attribute.KeyValue{
Key: "http.method",
Value: attribute.StringValue(r.Method),
})
span.SetAttributes(attribute.KeyValue{
Key: "http.route",
Value: attribute.StringValue(route),
})
span.SetAttributes(attribute.KeyValue{
Key: "http.path",
Value: attribute.StringValue(r.URL.EscapedPath()),
})
// set the status code
status := wrw.Status()
// 0 status means one has not yet been sent in which case net/http library will write StatusOK
if status == 0 {
status = http.StatusOK
}
span.SetAttributes(attribute.KeyValue{
Key: "http.status_code",
Value: attribute.IntValue(status),
})
// if 5XX we set the span to "error" status
if status >= 500 {
span.SetStatus(codes.Error, fmt.Sprintf("%d: %s", status, http.StatusText(status)))
}
})
}
}