You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-11-25 22:41:46 +02:00
Add filters for othttp plugin (#556)
* Add request filtering capability to othhtp.Handler * Add simple and useful filters for othttp plugin * Add note that all requests are traced in the absence of any filters * Add copyright notice to plugin/othttp/filters/filters_test.go Co-Authored-By: Tyler Yahn <MrAlias@users.noreply.github.com> * Add package docstring for filters package Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Rahul Patel <rahulpa@google.com>
This commit is contained in:
committed by
GitHub
parent
217a97d9b6
commit
2ef25ea570
@@ -41,6 +41,10 @@ const (
|
||||
WriteErrorKey = core.Key("http.write_error") // if an error occurred while writing a reply, the string of the error (io.EOF is not recorded)
|
||||
)
|
||||
|
||||
// Filter is a predicate used to determine whether a given http.request should
|
||||
// be traced. A Filter must return true if the request should be traced.
|
||||
type Filter func(*http.Request) bool
|
||||
|
||||
// Handler is http middleware that corresponds to the http.Handler interface and
|
||||
// is designed to wrap a http.Mux (or equivalent), while individual routes on
|
||||
// the mux are wrapped with WithRouteTag. A Handler will add various attributes
|
||||
@@ -54,6 +58,7 @@ type Handler struct {
|
||||
spanStartOptions []trace.StartOption
|
||||
readEvent bool
|
||||
writeEvent bool
|
||||
filters []Filter
|
||||
}
|
||||
|
||||
// Option function used for setting *optional* Handler properties
|
||||
@@ -93,6 +98,18 @@ func WithSpanOptions(opts ...trace.StartOption) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// WithFilter adds a filter to the list of filters used by the handler.
|
||||
// If any filter indicates to exclude a request then the request will not be
|
||||
// traced. All filters must allow a request to be traced for a Span to be created.
|
||||
// If no filters are provided then all requests are traced.
|
||||
// Filters will be invoked for each processed request, it is advised to make them
|
||||
// simple and fast.
|
||||
func WithFilter(f Filter) Option {
|
||||
return func(h *Handler) {
|
||||
h.filters = append(h.filters, f)
|
||||
}
|
||||
}
|
||||
|
||||
type event int
|
||||
|
||||
// Different types of events that can be recorded, see WithMessageEvents
|
||||
@@ -141,6 +158,14 @@ func NewHandler(handler http.Handler, operation string, opts ...Option) http.Han
|
||||
|
||||
// ServeHTTP serves HTTP requests (http.Handler)
|
||||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
for _, f := range h.filters {
|
||||
if !f(r) {
|
||||
// Simply pass through to the handler if a filter rejects the request
|
||||
h.handler.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
opts := append([]trace.StartOption{}, h.spanStartOptions...) // start with the configured options
|
||||
|
||||
ctx := propagation.ExtractHTTP(r.Context(), h.props, r.Header)
|
||||
|
||||
Reference in New Issue
Block a user