-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> * Update modules/caddyhttp/opentelemetry/README.md Co-authored-by: Dave Henderson <[email protected]> * Update modules/caddyhttp/opentelemetry/README.md Co-authored-by: Dave Henderson <[email protected]> * Update modules/caddyhttp/opentelemetry/tracer.go Co-authored-by: Dave Henderson <[email protected]> * rename error ErrUnsupportedTracesProtocol * replace spaces with tabs in the test data * Update modules/caddyhttp/opentelemetry/README.md Co-authored-by: Francis Lavoie <[email protected]> * Update modules/caddyhttp/opentelemetry/README.md Co-authored-by: Francis Lavoie <[email protected]> * 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 <[email protected]> * Update link in README.md Co-authored-by: Dave Henderson <[email protected]> * Update documentation in README.md Co-authored-by: Dave Henderson <[email protected]> * Update link to naming spec in README.md Co-authored-by: Dave Henderson <[email protected]> * Rename module from opentelemetry to tracing Co-authored-by: Dave Henderson <[email protected]> * Rename span_name to span Co-authored-by: Dave Henderson <[email protected]> * Rename span_name to span Co-authored-by: Dave Henderson <[email protected]> * Simplify otel resource creation Co-authored-by: Dave Henderson <[email protected]> * handle extra attributes Co-authored-by: Dave Henderson <[email protected]> * update go.opentelemetry.io/otel/semconv to 1.7.0 Co-authored-by: Dave Henderson <[email protected]> * 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 <[email protected]> * Fix grammar Co-authored-by: Dave Henderson <[email protected]> * Update comments Co-authored-by: Dave Henderson <[email protected]> * Update comments Co-authored-by: Dave Henderson <[email protected]> * 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 <[email protected]> Co-authored-by: Francis Lavoie <[email protected]> Co-authored-by: Vibhav Pant <[email protected]> Co-authored-by: Alok Naushad <[email protected]> Co-authored-by: Cedric Ziel <[email protected]>
- Loading branch information
1 parent
d9b1d46
commit d0b608a
Showing
11 changed files
with
632 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
:80 { | ||
tracing /myhandler { | ||
span my-span | ||
} | ||
} | ||
---------- | ||
{ | ||
"apps": { | ||
"http": { | ||
"servers": { | ||
"srv0": { | ||
"listen": [ | ||
":80" | ||
], | ||
"routes": [ | ||
{ | ||
"match": [ | ||
{ | ||
"path": [ | ||
"/myhandler" | ||
] | ||
} | ||
], | ||
"handle": [ | ||
{ | ||
"handler": "tracing", | ||
"span": "my-span" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package tracing | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/caddyserver/caddy/v2" | ||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" | ||
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile" | ||
"github.com/caddyserver/caddy/v2/modules/caddyhttp" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func init() { | ||
caddy.RegisterModule(Tracing{}) | ||
httpcaddyfile.RegisterHandlerDirective("tracing", parseCaddyfile) | ||
} | ||
|
||
// Tracing implements an HTTP handler that adds support for distributed tracing, | ||
// using OpenTelemetry. This module is responsible for the injection and | ||
// propagation of the trace context. Configure this module via environment | ||
// variables (see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md). | ||
// Some values can be overwritten in the configuration file. | ||
type Tracing struct { | ||
// SpanName is a span name. It should follow the naming guidelines here: | ||
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#span | ||
SpanName string `json:"span"` | ||
|
||
// otel implements opentelemetry related logic. | ||
otel openTelemetryWrapper | ||
|
||
logger *zap.Logger | ||
} | ||
|
||
// CaddyModule returns the Caddy module information. | ||
func (Tracing) CaddyModule() caddy.ModuleInfo { | ||
return caddy.ModuleInfo{ | ||
ID: "http.handlers.tracing", | ||
New: func() caddy.Module { return new(Tracing) }, | ||
} | ||
} | ||
|
||
// Provision implements caddy.Provisioner. | ||
func (ot *Tracing) Provision(ctx caddy.Context) error { | ||
ot.logger = ctx.Logger(ot) | ||
|
||
var err error | ||
ot.otel, err = newOpenTelemetryWrapper(ctx, ot.SpanName) | ||
|
||
return err | ||
} | ||
|
||
// Cleanup implements caddy.CleanerUpper and closes any idle connections. It | ||
// calls Shutdown method for a trace provider https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#shutdown. | ||
func (ot *Tracing) Cleanup() error { | ||
if err := ot.otel.cleanup(ot.logger); err != nil { | ||
return fmt.Errorf("tracerProvider shutdown: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
// ServeHTTP implements caddyhttp.MiddlewareHandler. | ||
func (ot *Tracing) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { | ||
return ot.otel.ServeHTTP(w, r, next) | ||
} | ||
|
||
// UnmarshalCaddyfile sets up the module from Caddyfile tokens. Syntax: | ||
// | ||
// tracing { | ||
// [span <span_name>] | ||
// } | ||
// | ||
func (ot *Tracing) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { | ||
setParameter := func(d *caddyfile.Dispenser, val *string) error { | ||
if d.NextArg() { | ||
*val = d.Val() | ||
} else { | ||
return d.ArgErr() | ||
} | ||
if d.NextArg() { | ||
return d.ArgErr() | ||
} | ||
return nil | ||
} | ||
|
||
// paramsMap is a mapping between "string" parameter from the Caddyfile and its destination within the module | ||
paramsMap := map[string]*string{ | ||
"span": &ot.SpanName, | ||
} | ||
|
||
for d.Next() { | ||
args := d.RemainingArgs() | ||
if len(args) > 0 { | ||
return d.ArgErr() | ||
} | ||
|
||
for d.NextBlock(0) { | ||
if dst, ok := paramsMap[d.Val()]; ok { | ||
if err := setParameter(d, dst); err != nil { | ||
return err | ||
} | ||
} else { | ||
return d.ArgErr() | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) { | ||
var m Tracing | ||
err := m.UnmarshalCaddyfile(h.Dispenser) | ||
return &m, err | ||
} | ||
|
||
// Interface guards | ||
var ( | ||
_ caddy.Provisioner = (*Tracing)(nil) | ||
_ caddyhttp.MiddlewareHandler = (*Tracing)(nil) | ||
_ caddyfile.Unmarshaler = (*Tracing)(nil) | ||
) |
Oops, something went wrong.