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/transform] Added nil to grammar #11150

Merged
merged 2 commits into from
Jun 20, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

- `cmd/mdatagen`: Allow attribute values of any types (#9245)
- `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)
- `datadogexporter`: Some config validation and unmarshaling steps are now done on `Validate` and `Unmarshal` instead of `Sanitize` (#8829)
- `examples`: Add an example for scraping Couchbase metrics (#10894)
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 @@ -77,6 +77,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