-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
HttpFilterOut
sampler to filter traces based on request p…
…ath (#454) Add a new built-in sampler to filter out traces with attribute `http.target` matching a given filter. The filter could be provided either using the `WithPathFilter` method or using the environment variable `OTEL_GO_AUTO_HTTP_INSTRUMENTATION_FILTER_PATH`. Signed-off-by: thomasgouveia <[email protected]>
- Loading branch information
1 parent
68220b0
commit b88895c
Showing
5 changed files
with
187 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
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,83 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package builtin | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
otelsdk "go.opentelemetry.io/otel/sdk/trace" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
// httpFilterOutPathSampler is a custom implementation of OpenTelemetry sampler | ||
// used to filter out traces based on their HTTP request path. | ||
type httpFilterOutPathSampler struct { | ||
filter *regexp.Regexp | ||
} | ||
|
||
// Ensure that the implementation satisfies the interface. | ||
var _ otelsdk.Sampler = (*httpFilterOutPathSampler)(nil) | ||
|
||
// HttpFilterOut returns a Sampler that filter out samples with HTTP path | ||
// matching the given filter. Useful for filter HTTP requests with | ||
// path like /health, /metrics or whatever. | ||
func HttpFilterOut(filter *regexp.Regexp) otelsdk.Sampler { | ||
return &httpFilterOutPathSampler{filter} | ||
} | ||
|
||
// ShouldSample implements the interface [otelsdk.Sampler]. | ||
func (f httpFilterOutPathSampler) ShouldSample(p otelsdk.SamplingParameters) otelsdk.SamplingResult { | ||
if f.shouldDrop(p) { | ||
return otelsdk.SamplingResult{ | ||
Decision: otelsdk.Drop, | ||
Tracestate: trace.SpanContextFromContext(p.ParentContext).TraceState(), | ||
} | ||
} | ||
|
||
return otelsdk.SamplingResult{ | ||
Decision: otelsdk.RecordAndSample, | ||
Tracestate: trace.SpanContextFromContext(p.ParentContext).TraceState(), | ||
} | ||
} | ||
|
||
// Description implements the interface [otelsdk.Sampler]. | ||
func (f httpFilterOutPathSampler) Description() string { | ||
return "HttpFilterOut" | ||
} | ||
|
||
// shouldDrop determines if the given sample should be dropped or not. | ||
func (f httpFilterOutPathSampler) shouldDrop(p otelsdk.SamplingParameters) bool { | ||
attr, err := getAttribute(p, "http.target") | ||
if err != nil { | ||
// If we have an error here, it is because the attribute is not | ||
// found on the given parameters. We can safely return false. | ||
return false | ||
} | ||
httpPath := attr.Value.AsString() | ||
|
||
return f.filter.MatchString(httpPath) | ||
} | ||
|
||
// getAttribute returns the attribute at the given key in the given sampling parameters or an error if the key has no value. | ||
func getAttribute(p otelsdk.SamplingParameters, key string) (attribute.KeyValue, error) { | ||
for _, attr := range p.Attributes { | ||
if attr.Key == attribute.Key(key) { | ||
return attr, nil | ||
} | ||
} | ||
return attribute.KeyValue{}, fmt.Errorf("no attribute with key %s", key) | ||
} |
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,62 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package builtin | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/otel/attribute" | ||
otelsdk "go.opentelemetry.io/otel/sdk/trace" | ||
) | ||
|
||
func TestHttpFilterOut(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
httpPath string | ||
filter *regexp.Regexp | ||
expectedDecision otelsdk.SamplingDecision | ||
}{ | ||
{ | ||
"traceNotDropped", | ||
"/api/v1/something", | ||
regexp.MustCompile("/health.+"), | ||
otelsdk.RecordAndSample, | ||
}, | ||
{ | ||
"traceDropped", | ||
"/health/liveness", | ||
regexp.MustCompile("/health.+"), | ||
otelsdk.Drop, | ||
}, | ||
{ | ||
"traceNotDroppedIfPatternIsUsedAnywhereInPath", | ||
"/v1/deployments/health", | ||
regexp.MustCompile("/health.+"), | ||
otelsdk.RecordAndSample, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
params := otelsdk.SamplingParameters{ | ||
Attributes: []attribute.KeyValue{attribute.String("http.target", tc.httpPath)}, | ||
} | ||
sampler := HttpFilterOut(tc.filter) | ||
assert.Equal(t, tc.expectedDecision, sampler.ShouldSample(params).Decision) | ||
}) | ||
} | ||
} |
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