summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/tracing/tracer.go
diff options
context:
space:
mode:
authorAndrii Kushch <andrii.kushch@gmail.com>2022-03-08 20:18:32 +0100
committerGitHub <noreply@github.com>2022-03-08 12:18:32 -0700
commitd0b608af3178bc674936f4b1c6cce00591ebbf09 (patch)
treea8646d0d406acf98df62856815d4eae4d44cdd4f /modules/caddyhttp/tracing/tracer.go
parentd9b1d463259a6f8f520edd6659dac11218c82b4e (diff)
tracing: New OpenTelemetry module (#4361)
* opentelemetry: create a new module * fix imports * fix test * Update modules/caddyhttp/opentelemetry/README.md Co-authored-by: Dave Henderson <dhenderson@gmail.com> * Update modules/caddyhttp/opentelemetry/README.md Co-authored-by: Dave Henderson <dhenderson@gmail.com> * Update modules/caddyhttp/opentelemetry/README.md Co-authored-by: Dave Henderson <dhenderson@gmail.com> * Update modules/caddyhttp/opentelemetry/tracer.go Co-authored-by: Dave Henderson <dhenderson@gmail.com> * rename error ErrUnsupportedTracesProtocol * replace spaces with tabs in the test data * Update modules/caddyhttp/opentelemetry/README.md Co-authored-by: Francis Lavoie <lavofr@gmail.com> * Update modules/caddyhttp/opentelemetry/README.md Co-authored-by: Francis Lavoie <lavofr@gmail.com> * replace spaces with tabs in the README.md * use default values for a propagation and exporter protocol * set http attributes with helper * simplify code * Cleanup modules/caddyhttp/opentelemetry/README.md Co-authored-by: Dave Henderson <dhenderson@gmail.com> * Update link in README.md Co-authored-by: Dave Henderson <dhenderson@gmail.com> * Update documentation in README.md Co-authored-by: Dave Henderson <dhenderson@gmail.com> * Update link to naming spec in README.md Co-authored-by: Dave Henderson <dhenderson@gmail.com> * Rename module from opentelemetry to tracing Co-authored-by: Dave Henderson <dhenderson@gmail.com> * Rename span_name to span Co-authored-by: Dave Henderson <dhenderson@gmail.com> * Rename span_name to span Co-authored-by: Dave Henderson <dhenderson@gmail.com> * Simplify otel resource creation Co-authored-by: Dave Henderson <dhenderson@gmail.com> * handle extra attributes Co-authored-by: Dave Henderson <dhenderson@gmail.com> * update go.opentelemetry.io/otel/semconv to 1.7.0 Co-authored-by: Dave Henderson <dhenderson@gmail.com> * update go.opentelemetry.io/otel version * remove environment variable handling * always use tracecontext,baggage as propagators * extract tracer name into variable * rename OpenTelemetry to Tracing * simplify resource creation * update go.mod * rename package from opentelemetry to tracing * cleanup tests * update Caddyfile example in README.md * update README.md * fix test * fix module name in README.md * fix module name in README.md * change names in README.md and tests * order imports * remove redundant tests * Update documentation README.md Co-authored-by: Dave Henderson <dhenderson@gmail.com> * Fix grammar Co-authored-by: Dave Henderson <dhenderson@gmail.com> * Update comments Co-authored-by: Dave Henderson <dhenderson@gmail.com> * Update comments Co-authored-by: Dave Henderson <dhenderson@gmail.com> * update go.sum * update go.sum * Add otelhttp instrumentation, update OpenTelemetry libraries. * Use otelhttp instrumentation for instrumenting HTTP requests. This change uses context.WithValue to inject the next handler into the request context via a "nextCall" carrier struct, and pass it on to a standard Go HTTP handler returned by otelhttp.NewHandler. The underlying handler will extract the next handler from the context, call it and pass the returned error to the carrier struct. * use zap.Error() for the error log * remove README.md * update dependencies * clean up the code * change comment * move serveHTTP method from separate file * add syntax to the UnmarshalCaddyfile comment * go import the file * admin: Write proper status on invalid requests (#4569) (fix #4561) * update dependencies Co-authored-by: Dave Henderson <dhenderson@gmail.com> Co-authored-by: Francis Lavoie <lavofr@gmail.com> Co-authored-by: Vibhav Pant <vibhavp@gmail.com> Co-authored-by: Alok Naushad <alokme123@gmail.com> Co-authored-by: Cedric Ziel <cedric@cedric-ziel.com>
Diffstat (limited to 'modules/caddyhttp/tracing/tracer.go')
-rw-r--r--modules/caddyhttp/tracing/tracer.go108
1 files changed, 108 insertions, 0 deletions
diff --git a/modules/caddyhttp/tracing/tracer.go b/modules/caddyhttp/tracing/tracer.go
new file mode 100644
index 0000000..ce23944
--- /dev/null
+++ b/modules/caddyhttp/tracing/tracer.go
@@ -0,0 +1,108 @@
+package tracing
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+
+ "github.com/caddyserver/caddy/v2"
+
+ caddycmd "github.com/caddyserver/caddy/v2/cmd"
+ "github.com/caddyserver/caddy/v2/modules/caddyhttp"
+ "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
+ "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.7.0"
+ "go.uber.org/zap"
+)
+
+const (
+ webEngineName = "Caddy"
+ defaultSpanName = "handler"
+ nextCallCtxKey caddy.CtxKey = "nextCall"
+)
+
+// nextCall store the next handler, and the error value return on calling it (if any)
+type nextCall struct {
+ next caddyhttp.Handler
+ err error
+}
+
+// openTelemetryWrapper is responsible for the tracing injection, extraction and propagation.
+type openTelemetryWrapper struct {
+ propagators propagation.TextMapPropagator
+
+ handler http.Handler
+
+ spanName string
+}
+
+// newOpenTelemetryWrapper is responsible for the openTelemetryWrapper initialization using provided configuration.
+func newOpenTelemetryWrapper(
+ ctx context.Context,
+ spanName string,
+) (openTelemetryWrapper, error) {
+ if spanName == "" {
+ spanName = defaultSpanName
+ }
+
+ ot := openTelemetryWrapper{
+ spanName: spanName,
+ }
+
+ res, err := ot.newResource(webEngineName, caddycmd.CaddyVersion())
+ if err != nil {
+ return ot, fmt.Errorf("creating resource error: %w", err)
+ }
+
+ traceExporter, err := otlptracegrpc.New(ctx)
+ if err != nil {
+ return ot, fmt.Errorf("creating trace exporter error: %w", err)
+ }
+
+ ot.propagators = propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
+
+ tracerProvider := globalTracerProvider.getTracerProvider(
+ sdktrace.WithBatcher(traceExporter),
+ sdktrace.WithResource(res),
+ )
+
+ ot.handler = otelhttp.NewHandler(http.HandlerFunc(ot.serveHTTP), ot.spanName, otelhttp.WithTracerProvider(tracerProvider), otelhttp.WithPropagators(ot.propagators))
+ return ot, nil
+}
+
+// serveHTTP injects a tracing context and call the next handler.
+func (ot *openTelemetryWrapper) serveHTTP(w http.ResponseWriter, r *http.Request) {
+ ot.propagators.Inject(r.Context(), propagation.HeaderCarrier(r.Header))
+ next := r.Context().Value(nextCallCtxKey).(*nextCall)
+ next.err = next.next.ServeHTTP(w, r)
+}
+
+// ServeHTTP propagates call to the by wrapped by `otelhttp` next handler.
+func (ot *openTelemetryWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
+ n := &nextCall{
+ next: next,
+ err: nil,
+ }
+ ot.handler.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), nextCallCtxKey, n)))
+
+ return n.err
+}
+
+// cleanup flush all remaining data and shutdown a tracerProvider
+func (ot *openTelemetryWrapper) cleanup(logger *zap.Logger) error {
+ return globalTracerProvider.cleanupTracerProvider(logger)
+}
+
+// newResource creates a resource that describe current handler instance and merge it with a default attributes value.
+func (ot *openTelemetryWrapper) newResource(
+ webEngineName,
+ webEngineVersion string,
+) (*resource.Resource, error) {
+ return resource.Merge(resource.Default(), resource.NewSchemaless(
+ semconv.WebEngineNameKey.String(webEngineName),
+ semconv.WebEngineVersionKey.String(webEngineVersion),
+ ))
+}