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

Change rule expression to match endpoint types for receiver creator #2661

Merged
merged 7 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions extension/observer/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func (e *Endpoint) Env() (EndpointEnv, error) {
env := e.Details.Env()
env["endpoint"] = e.Target

if e.Details.Type() == PodType {
env["type"] = string(PodType)
return env, nil
}

// Populate type field for evaluating rules with `type.port && ...`.
// Use string instead of EndpointType for rule evaluation.
types := map[string]bool{}
Expand All @@ -82,6 +87,7 @@ func (e *Endpoint) Env() (EndpointEnv, error) {
}
types[string(e.Details.Type())] = true
env["type"] = types

return env, nil
}

Expand Down
6 changes: 1 addition & 5 deletions extension/observer/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ func TestEndpointEnv(t *testing.T) {
},
},
want: EndpointEnv{
"type": map[string]bool{
"pod": true,
"hostport": false,
"port": false,
},
"type": "pod",
"endpoint": "192.68.73.2",
"name": "pod_name",
"labels": map[string]string{
Expand Down
6 changes: 3 additions & 3 deletions receiver/receivercreator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ This setting controls what resource attributes are set on metrics emitted from t

Note that the backticks below are not typos--they indicate the value is set dynamically.

`type.pod`
`type`

| Resource Attribute | Default |
|--------------------|---------------|
Expand Down Expand Up @@ -94,7 +94,7 @@ targeting it will have different variables available.

| Variable | Description |
|-------------|-----------------------------------|
| type.pod | `true` |
| type | `pod` |
| name | name of the pod |
| namespace | namespace of the pod |
| uid | unique id of the pod |
Expand Down Expand Up @@ -141,7 +141,7 @@ receivers:
receivers:
prometheus_simple:
# Configure prometheus scraping if standard prometheus annotations are set on the pod.
rule: type.pod && annotations["prometheus.io/scrape"] == "true"
rule: type == "pod" && annotations["prometheus.io/scrape"] == "true"
config:
metrics_path: '`"prometheus.io/path" in annotations ? annotations["prometheus.io/path"] : "/metrics"`'
endpoint: '`endpoint`:`"prometheus.io/port" in annotations ? annotations["prometheus.io/port"] : 9090`'
Expand Down
4 changes: 2 additions & 2 deletions receiver/receivercreator/observerhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ func TestDynamicConfig(t *testing.T) {
cfg.receiverTemplates = map[string]receiverTemplate{
"name/1": {
receiverConfig: receiverConfig{typeStr: configmodels.Type("name"), config: userConfigMap{"endpoint": "`endpoint`:6379"}, fullName: "name/1"},
Rule: "type.pod",
rule: newRuleOrPanic("type.pod"),
Rule: "type == \"pod\"",
rule: newRuleOrPanic("type == \"pod\""),
},
}
handler := &observerHandler{
Expand Down
2 changes: 1 addition & 1 deletion receiver/receivercreator/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type rule struct {
}

// ruleRe is used to verify the rule starts type check.
var ruleRe = regexp.MustCompile(`^type\.(pod|port)`)
var ruleRe = regexp.MustCompile(`^type(\s*==\s*"pod"|\.port)`)

// newRule creates a new rule instance.
func newRule(ruleStr string) (rule, error) {
Expand Down
4 changes: 2 additions & 2 deletions receiver/receivercreator/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func Test_ruleEval(t *testing.T) {
// Doesn't work yet. See comment in newRule.
// {"unknown variable", args{`type == "port" && unknown_var == 1`, portEndpoint}, false, true},
{"basic port", args{`type.port && name == "http" && pod.labels["app"] == "redis"`, portEndpoint}, true, false},
{"basic pod", args{`type.pod && labels["region"] == "west-1"`, podEndpoint}, true, false},
{"annotations", args{`type.pod && annotations["scrape"] == "true"`, podEndpoint}, true, false},
{"basic pod", args{`type == "pod" && labels["region"] == "west-1"`, podEndpoint}, true, false},
{"annotations", args{`type == "pod" && annotations["scrape"] == "true"`, podEndpoint}, true, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down