Skip to content

Commit

Permalink
receiver_creator: update expr with breaking type fn (#26064)
Browse files Browse the repository at this point in the history
Fixing a bug - These changes adopt the latest version of expr and
incorporate a breaking built-in `type` function addition by relocating
its implementation to `typeOf`.

Fixes #26038
  • Loading branch information
rmfitzpatrick authored Aug 23, 2023
1 parent 7669ba0 commit 3a0c2d1
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 13 deletions.
27 changes: 27 additions & 0 deletions .chloggen/bumpexpr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Update expr and relocate breaking `type` function to `typeOf`

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [26038]

# (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:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
2 changes: 1 addition & 1 deletion cmd/configschema/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ require (
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/aliyun/aliyun-log-go-sdk v0.1.53 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/antonmedv/expr v1.13.0 // indirect
github.com/antonmedv/expr v1.14.3 // indirect
github.com/apache/arrow/go/v12 v12.0.1 // indirect
github.com/apache/pulsar-client-go v0.8.1 // indirect
github.com/apache/pulsar-client-go/oauth2 v0.0.0-20220120090717-25e59572242e // indirect
Expand Down
4 changes: 2 additions & 2 deletions cmd/configschema/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/otelcontribcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ require (
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/aliyun/aliyun-log-go-sdk v0.1.53 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/antonmedv/expr v1.13.0 // indirect
github.com/antonmedv/expr v1.14.3 // indirect
github.com/apache/arrow/go/v12 v12.0.1 // indirect
github.com/apache/pulsar-client-go v0.8.1 // indirect
github.com/apache/pulsar-client-go/oauth2 v0.0.0-20220120090717-25e59572242e // indirect
Expand Down
4 changes: 2 additions & 2 deletions cmd/otelcontribcol/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ require (
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/aliyun/aliyun-log-go-sdk v0.1.53 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/antonmedv/expr v1.13.0 // indirect
github.com/antonmedv/expr v1.14.3 // indirect
github.com/apache/arrow/go/v12 v12.0.1 // indirect
github.com/apache/pulsar-client-go v0.8.1 // indirect
github.com/apache/pulsar-client-go/oauth2 v0.0.0-20220120090717-25e59572242e // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions receiver/receivercreator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ syntax](https://github.com/antonmedv/expr/blob/master/docs/Language-Definition.m
Variables available are detailed below in [Rule
Expressions](#rule-expressions).

Note: The built-in `type` function introduced in v1.14.1 has been relocated to `typeOf`.

**receivers.<receiver_type/id>.config**

This is configuration that will be used when creating the receiver at
Expand Down
2 changes: 1 addition & 1 deletion receiver/receivercreator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/receiver/receiv
go 1.20

require (
github.com/antonmedv/expr v1.13.0
github.com/antonmedv/expr v1.14.3
github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer v0.83.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/sharedcomponent v0.83.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.83.0
Expand Down
4 changes: 2 additions & 2 deletions receiver/receivercreator/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion receiver/receivercreator/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"regexp"

"github.com/antonmedv/expr"
"github.com/antonmedv/expr/builtin"
"github.com/antonmedv/expr/vm"

"github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer"
Expand Down Expand Up @@ -36,7 +37,15 @@ func newRule(ruleStr string) (rule, error) {

// TODO: Maybe use https://godoc.org/github.com/antonmedv/expr#Env in type checking
// depending on type == specified.
v, err := expr.Compile(ruleStr)
v, err := expr.Compile(
ruleStr,
// expr v1.14.1 introduced a `type` builtin whose implementation we relocate to `typeOf`
// to avoid collision
expr.DisableBuiltin("type"),
expr.Function("typeOf", func(params ...interface{}) (interface{}, error) {
return builtin.Type(params[0]), nil
}, new(func(interface{}) string)),
)
if err != nil {
return rule{}, err
}
Expand Down
1 change: 1 addition & 0 deletions receiver/receivercreator/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func Test_ruleEval(t *testing.T) {
{"annotations", args{`type == "pod" && annotations["scrape"] == "true"`, podEndpoint}, true, false},
{"basic container", args{`type == "container" && labels["region"] == "east-1"`, containerEndpoint}, true, false},
{"basic k8s.node", args{`type == "k8s.node" && kubelet_endpoint_port == 10250`, k8sNodeEndpoint}, true, false},
{"relocated type builtin", args{`type == "k8s.node" && typeOf("some string") == "string"`, k8sNodeEndpoint}, true, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 3a0c2d1

Please sign in to comment.