Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[processor/filter] Add new metric-only functions #16798

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/fp-drop-metric-based-on-attribute.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: filterprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add new functions `HasAttrKeyOnDatapoint` and `HasAttrOnDatapoint` to allow existing expr matching logic with OTTL.

# One or more tracking issues related to the change
issues: [16798]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
34 changes: 32 additions & 2 deletions processor/filterprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,45 @@ See the table below for details on each context and the fields it exposes.
The OTTL allows the use of `and`, `or`, and `()` in conditions.
See [OTTL Boolean Expressions](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/pkg/ottl/README.md#boolean-expressions) for more details.

The filter processor has access to all the [factory functions of the OTTL](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/pkg/ottl/ottlfuncs#ottl-functions)

For conditions that apply to the same signal, such as spans and span events, if the "higher" level telemetry matches a condition and is dropped, the "lower" level condition will not be checked.
This means that if a span is dropped but a span event condition was defined, the span event condition will not be checked.
The same relationship applies to metrics and datapoints.

If all span events for a span are dropped, the span will be left intact.
If all datapoints for a metric are dropped, the metric will also be dropped.

### OTTL Functions

The filter processor has access to all the [factory functions of the OTTL](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/pkg/ottl/ottlfuncs#ottl-functions)

In addition, the processor defines a few of its own functions:

**Metrics only functions**
- [HasAttrKeyOnDatapoint](#HasAttrKeyOnDatapoint)
- [HasAttrOnDatapoint](#HasAttrOnDatapoint)

#### HasAttrKeyOnDatapoint

`HasAttrKeyOnDatapoint(key)`

Returns `true` if the given key appears in the attribute map of any datapoint on a metric.
`key` must be a string.

Examples:

- `HasAttrKeyOnDatapoint("http.method")`

#### HasAttrOnDatapoint

`HasAttrOnDatapoint(key, value)`

Returns `true` if the given key and value appears in the attribute map of any datapoint on a metric.
`key` and `value` must both be strings.

Examples:

- `HasAttrOnDatapoint("http.method", "GET")`

### OTTL Examples

```yaml
Expand Down
97 changes: 96 additions & 1 deletion processor/filterprocessor/internal/common/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package common // import "github.com/open-telemetry/opentelemetry-collector-cont

import (
"context"
"fmt"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pmetric"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/expr"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
Expand Down Expand Up @@ -61,7 +63,7 @@ func ParseLog(conditions []string, set component.TelemetrySettings) (expr.BoolEx

func ParseMetric(conditions []string, set component.TelemetrySettings) (expr.BoolExpr[ottlmetric.TransformContext], error) {
statmentsStr := conditionsToStatements(conditions)
parser := ottlmetric.NewParser(functions[ottlmetric.TransformContext](), set)
parser := ottlmetric.NewParser(metricFunctions(), set)
statements, err := parser.ParseStatements(statmentsStr)
if err != nil {
return nil, err
Expand Down Expand Up @@ -120,3 +122,96 @@ func functions[K any]() map[string]interface{} {
},
}
}

func metricFunctions() map[string]interface{} {
funcs := functions[ottlmetric.TransformContext]()
funcs["HasAttrKeyOnDatapoint"] = hasAttributeKeyOnDatapoint
funcs["HasAttrOnDatapoint"] = hasAttributeOnDatapoint

return funcs
}

func hasAttributeOnDatapoint(key string, expectedVal string) (ottl.ExprFunc[ottlmetric.TransformContext], error) {
return func(ctx context.Context, tCtx ottlmetric.TransformContext) (interface{}, error) {
return checkDataPoints(tCtx, key, &expectedVal)
}, nil
}

func hasAttributeKeyOnDatapoint(key string) (ottl.ExprFunc[ottlmetric.TransformContext], error) {
return func(ctx context.Context, tCtx ottlmetric.TransformContext) (interface{}, error) {
return checkDataPoints(tCtx, key, nil)
}, nil
}

func checkDataPoints(tCtx ottlmetric.TransformContext, key string, expectedVal *string) (interface{}, error) {
metric := tCtx.GetMetric()
switch metric.Type() {
case pmetric.MetricTypeSum:
return checkNumberDataPointSlice(metric.Sum().DataPoints(), key, expectedVal), nil
case pmetric.MetricTypeGauge:
return checkNumberDataPointSlice(metric.Gauge().DataPoints(), key, expectedVal), nil
case pmetric.MetricTypeHistogram:
return checkHistogramDataPointSlice(metric.Histogram().DataPoints(), key, expectedVal), nil
case pmetric.MetricTypeExponentialHistogram:
return checkExponentialHistogramDataPointSlice(metric.ExponentialHistogram().DataPoints(), key, expectedVal), nil
case pmetric.MetricTypeSummary:
return checkSummaryDataPointSlice(metric.Summary().DataPoints(), key, expectedVal), nil
}
return nil, fmt.Errorf("unknown metric type")
}

func checkNumberDataPointSlice(dps pmetric.NumberDataPointSlice, key string, expectedVal *string) bool {
for i := 0; i < dps.Len(); i++ {
dp := dps.At(i)
value, ok := dp.Attributes().Get(key)
if ok {
if expectedVal != nil {
return value.Str() == *expectedVal
}
return true
}
}
return false
}

func checkHistogramDataPointSlice(dps pmetric.HistogramDataPointSlice, key string, expectedVal *string) bool {
for i := 0; i < dps.Len(); i++ {
dp := dps.At(i)
value, ok := dp.Attributes().Get(key)
if ok {
if expectedVal != nil {
return value.Str() == *expectedVal
}
return true
}
}
return false
}

func checkExponentialHistogramDataPointSlice(dps pmetric.ExponentialHistogramDataPointSlice, key string, expectedVal *string) bool {
for i := 0; i < dps.Len(); i++ {
dp := dps.At(i)
value, ok := dp.Attributes().Get(key)
if ok {
if expectedVal != nil {
return value.Str() == *expectedVal
}
return true
}
}
return false
}

func checkSummaryDataPointSlice(dps pmetric.SummaryDataPointSlice, key string, expectedVal *string) bool {
for i := 0; i < dps.Len(); i++ {
dp := dps.At(i)
value, ok := dp.Attributes().Get(key)
if ok {
if expectedVal != nil {
return value.Str() == *expectedVal
}
return true
}
}
return false
}
Loading