Debian Codegraph @

instrumentation/net/http/otelhttp/filters/filters.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Package filters provides a set of filters useful with the
5 // otelhttp.WithFilter() option to control which inbound requests are traced.
6 package filters // import "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/filters"
7
8 import (
9 "net/http"
10 "slices"
11 "strings"
12
13 "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
14 )
15
16 // Any takes a list of Filters and returns a Filter that
17 // returns true if any Filter in the list returns true.
18 func Any(fs ...otelhttp.Filter) otelhttp.Filter {
19 return func(r *http.Request) bool {
20 for _, f := range fs {
21 if f(r) {
22 return true
23 }
24 }
25 return false
26 }
27 }
28
29 // All takes a list of Filters and returns a Filter that
30 // returns true only if all Filters in the list return true.
31 func All(fs ...otelhttp.Filter) otelhttp.Filter {
32 return func(r *http.Request) bool {
33 for _, f := range fs {
34 if !f(r) {
35 return false
36 }
37 }
38 return true
39 }
40 }
41
42 // None takes a list of Filters and returns a Filter that returns
43 // true only if none of the Filters in the list return true.
44 func None(fs ...otelhttp.Filter) otelhttp.Filter {
45 return func(r *http.Request) bool {
46 for _, f := range fs {
47 if f(r) {
48 return false
49 }
50 }
51 return true
52 }
53 }
54
55 // Not provides a convenience mechanism for inverting a Filter.
56 func Not(f otelhttp.Filter) otelhttp.Filter {
57 return func(r *http.Request) bool {
58 return !f(r)
59 }
60 }
61
62 // Hostname returns a Filter that returns true if the request's
63 // hostname matches the provided string.
64 func Hostname(h string) otelhttp.Filter {
65 return func(r *http.Request) bool {
66 return r.URL.Hostname() == h
67 }
68 }
69
70 // Path returns a Filter that returns true if the request's
71 // path matches the provided string.
72 func Path(p string) otelhttp.Filter {
73 return func(r *http.Request) bool {
74 return r.URL.Path == p
75 }
76 }
77
78 // PathPrefix returns a Filter that returns true if the request's
79 // path starts with the provided string.
80 func PathPrefix(p string) otelhttp.Filter {
81 return func(r *http.Request) bool {
82 return strings.HasPrefix(r.URL.Path, p)
83 }
84 }
85
86 // Query returns a Filter that returns true if the request
87 // includes a query parameter k with a value equal to v.
88 func Query(k, v string) otelhttp.Filter {
89 return func(r *http.Request) bool {
90 return slices.Contains(r.URL.Query()[k], v)
91 }
92 }
93
94 // QueryContains returns a Filter that returns true if the request
95 // includes a query parameter k with a value that contains v.
96 func QueryContains(k, v string) otelhttp.Filter {
97 return func(r *http.Request) bool {
98 for _, qv := range r.URL.Query()[k] {
99 if strings.Contains(qv, v) {
100 return true
101 }
102 }
103 return false
104 }
105 }
106
107 // Method returns a Filter that returns true if the request
108 // method is equal to the provided value.
109 func Method(m string) otelhttp.Filter {
110 return func(r *http.Request) bool {
111 return m == r.Method
112 }
113 }