|
| 1 | +// Copyright 2020, OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package filters provides a set of filters useful with the |
| 16 | +// othttp.WithFilter() option to control which inbound requests are traced. |
| 17 | +package filters |
| 18 | + |
| 19 | +import ( |
| 20 | + "net/http" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "go.opentelemetry.io/otel/plugin/othttp" |
| 24 | +) |
| 25 | + |
| 26 | +// Any takes a list of Filters and returns a Filter that |
| 27 | +// returns true if any Filter in the list returns true. |
| 28 | +func Any(fs ...othttp.Filter) othttp.Filter { |
| 29 | + return func(r *http.Request) bool { |
| 30 | + for _, f := range fs { |
| 31 | + if f(r) { |
| 32 | + return true |
| 33 | + } |
| 34 | + } |
| 35 | + return false |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// All takes a list of Filters and returns a Filter that |
| 40 | +// returns true only if all Filters in the list return true. |
| 41 | +func All(fs ...othttp.Filter) othttp.Filter { |
| 42 | + return func(r *http.Request) bool { |
| 43 | + for _, f := range fs { |
| 44 | + if !f(r) { |
| 45 | + return false |
| 46 | + } |
| 47 | + } |
| 48 | + return true |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// None takes a list of Filters and returns a Filter that returns |
| 53 | +// true only if none of the Filters in the list return true. |
| 54 | +func None(fs ...othttp.Filter) othttp.Filter { |
| 55 | + return func(r *http.Request) bool { |
| 56 | + for _, f := range fs { |
| 57 | + if f(r) { |
| 58 | + return false |
| 59 | + } |
| 60 | + } |
| 61 | + return true |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// Not provides a convenience mechanism for inverting a Filter |
| 66 | +func Not(f othttp.Filter) othttp.Filter { |
| 67 | + return func(r *http.Request) bool { |
| 68 | + return !f(r) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// Hostname returns a Filter that returns true if the request's |
| 73 | +// hostname matches the provided string. |
| 74 | +func Hostname(h string) othttp.Filter { |
| 75 | + return func(r *http.Request) bool { |
| 76 | + return r.URL.Hostname() == h |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// Path returns a Filter that returns true if the request's |
| 81 | +// path matches the provided string. |
| 82 | +func Path(p string) othttp.Filter { |
| 83 | + return func(r *http.Request) bool { |
| 84 | + return r.URL.Path == p |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// PathPrefix returns a Filter that returns true if the request's |
| 89 | +// path starts with the provided string. |
| 90 | +func PathPrefix(p string) othttp.Filter { |
| 91 | + return func(r *http.Request) bool { |
| 92 | + return strings.HasPrefix(r.URL.Path, p) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// Query returns a Filter that returns true if the request |
| 97 | +// includes a query parameter k with a value equal to v. |
| 98 | +func Query(k, v string) othttp.Filter { |
| 99 | + return func(r *http.Request) bool { |
| 100 | + for _, qv := range r.URL.Query()[k] { |
| 101 | + if v == qv { |
| 102 | + return true |
| 103 | + } |
| 104 | + } |
| 105 | + return false |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// QueryContains returns a Filter that returns true if the request |
| 110 | +// includes a query parameter k with a value that contains v. |
| 111 | +func QueryContains(k, v string) othttp.Filter { |
| 112 | + return func(r *http.Request) bool { |
| 113 | + for _, qv := range r.URL.Query()[k] { |
| 114 | + if strings.Contains(qv, v) { |
| 115 | + return true |
| 116 | + } |
| 117 | + } |
| 118 | + return false |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// Method returns a Filter that returns true if the request |
| 123 | +// method is equal to the provided value. |
| 124 | +func Method(m string) othttp.Filter { |
| 125 | + return func(r *http.Request) bool { |
| 126 | + return m == r.Method |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// Header returns a Filter that returns true if the request |
| 131 | +// includes a header k with a value equal to v. |
| 132 | +func Header(k, v string) othttp.Filter { |
| 133 | + return func(r *http.Request) bool { |
| 134 | + for _, hv := range r.Header.Values(k) { |
| 135 | + if v == hv { |
| 136 | + return true |
| 137 | + } |
| 138 | + } |
| 139 | + return false |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// HeaderContains returns a Filter that returns true if the request |
| 144 | +// includes a header k with a value that contains v. |
| 145 | +func HeaderContains(k, v string) othttp.Filter { |
| 146 | + return func(r *http.Request) bool { |
| 147 | + for _, hv := range r.Header.Values(k) { |
| 148 | + if strings.Contains(hv, v) { |
| 149 | + return true |
| 150 | + } |
| 151 | + } |
| 152 | + return false |
| 153 | + } |
| 154 | +} |
0 commit comments