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