Skip to content

feat: Move from datadog to generic otel #1567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rename to tracing for real
  • Loading branch information
f0ssel committed May 18, 2022
commit 776d8fc9473631da1ed2d57d14b7802a0b534970
41 changes: 41 additions & 0 deletions tracing/exporter.go
Original file line number Diff line number Diff line change
@@ -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
}
69 changes: 69 additions & 0 deletions tracing/httpmw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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 once request finishes
_, span := tracerProvider.Tracer(name).Start(r.Context(), "http.request")
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 == "" {
route = "unknown"
}
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)))
}
})
}
}