Skip to content

Commit

Permalink
[processor/transform] Added nil to grammar (#11150)
Browse files Browse the repository at this point in the history
* Added nil to grammar

* Updated changelog
  • Loading branch information
TylerHelmuth authored Jun 20, 2022
1 parent 2a2d4ab commit 02f416b
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- `metricstransformprocessor`: Migrate the processor from OC to pdata (#10817)
- This behavior can be reverted by disabling the `processor.metricstransformprocessor.UseOTLPDataModel` feature gate.
- `transformprocessor`: Add byte slice literal to the grammar. Add new SpanID and TraceID functions that take a byte slice and return a Span/Trace ID. (#10487)
- `transformprocessor`: Add nil literal to the grammar. (#11150)
- `elasticsearchreceiver`: Add integration test for elasticsearch receiver (#10165)
- `tailsamplingprocessor`: New sampler added that allows to sample based on minimum number of spans
- `datadogexporter`: Some config validation and unmarshaling steps are now done on `Validate` and `Unmarshal` instead of `Sanitize` (#8829)
Expand Down
4 changes: 2 additions & 2 deletions processor/transformprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ in the OTLP protobuf definition. e.g., `status.code`, `attributes["http.method"]
- Metric data types are `None`, `Gauge`, `Sum`, `Histogram`, `ExponentialHistogram`, and `Summary`
- `aggregation_temporality` is converted to and from the [protobuf's numeric definition](https://github.com/open-telemetry/opentelemetry-proto/blob/main/opentelemetry/proto/metrics/v1/metrics.proto#L291). Interact with this field using 0, 1, or 2.
- Until the grammar can handle booleans, `is_monotic` is handled via strings the strings `"true"` and `"false"`.
- Literals: Strings, ints, floats, and bools can be referenced as literal values. Byte slices can be references as a literal value via a hex string prefaced with `0x`, such as `0x0001`.
- Function invocations: Functions can be invoked with arguments matching the function's expected arguments
- Literals: Strings, ints, floats, bools, and nil can be referenced as literal values. Byte slices can be references as a literal value via a hex string prefaced with `0x`, such as `0x0001`.
- Function invocations: Functions can be invoked with arguments matching the function's expected arguments. The literal nil cannot be used as a replacement for maps or slices in function calls.
- Where clause: Telemetry to modify can be filtered by appending `where a <op> b`, with `a` and `b` being any of the above.

Supported functions:
Expand Down
4 changes: 4 additions & 0 deletions processor/transformprocessor/internal/common/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (g exprGetter) Get(ctx TransformContext) interface{} {
}

func NewGetter(val Value, functions map[string]interface{}, pathParser PathExpressionParser) (Getter, error) {
if val.IsNil != nil && *val.IsNil {
return &literal{value: nil}, nil
}

if s := val.String; s != nil {
return &literal{value: *s}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ func Test_newGetter(t *testing.T) {
},
want: []byte{1, 2, 3, 4, 5, 6, 7, 8},
},
{
name: "nil literal",
val: Value{
IsNil: (*IsNil)(testhelper.Boolp(true)),
},
want: nil,
},
{
name: "bool literal",
val: Value{
Expand Down
11 changes: 11 additions & 0 deletions processor/transformprocessor/internal/common/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,17 @@ func Test_NewFunctionCall(t *testing.T) {
},
},
},
{
name: "getter arg with nil literal",
inv: Invocation{
Function: "testing_getter",
Arguments: []Value{
{
IsNil: (*IsNil)(testhelper.Boolp(true)),
},
},
},
},
{
name: "string arg",
inv: Invocation{
Expand Down
8 changes: 8 additions & 0 deletions processor/transformprocessor/internal/common/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Value struct {
Float *float64 `| @Float`
Int *int64 `| @Int`
Bool *Boolean `| @("true" | "false")`
IsNil *IsNil `| @"nil"`
Path *Path `| @@ )`
}

Expand Down Expand Up @@ -101,6 +102,13 @@ func (b *Boolean) Capture(values []string) error {
return nil
}

type IsNil bool

func (n *IsNil) Capture(_ []string) error {
*n = true
return nil
}

func ParseQueries(statements []string, functions map[string]interface{}, pathParser PathExpressionParser) ([]Query, error) {
queries := make([]Query, 0)
var errors error
Expand Down
24 changes: 24 additions & 0 deletions processor/transformprocessor/internal/common/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,30 @@ func Test_parse(t *testing.T) {
Condition: nil,
},
},
{
query: `set(attributes["test"], nil)`,
expected: &ParsedQuery{
Invocation: Invocation{
Function: "set",
Arguments: []Value{
{
Path: &Path{
Fields: []Field{
{
Name: "attributes",
MapKey: testhelper.Strp("test"),
},
},
},
},
{
IsNil: (*IsNil)(testhelper.Boolp(true)),
},
},
},
Condition: nil,
},
},
}

for _, tt := range tests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ func TestProcess(t *testing.T) {
td.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Attributes().InsertString("test", "pass")
},
},
{
query: `set(attributes["test"], "pass") where attributes["doesnt exist"] == nil`,
want: func(td ptrace.Traces) {
td.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Attributes().InsertString("test", "pass")
td.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(1).Attributes().InsertString("test", "pass")
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 02f416b

Please sign in to comment.