Skip to content

Commit bf92d2f

Browse files
authored
Change rule expression to match endpoint types for receiver creator (#2661)
Change the rule expression in receiver creator for matching endpoints types port, hostport and pod from type.port, type.hostport and type.pod respectively, to type == "port", type == "hostport" and type == "pod". Testing: Fixed unit tests failing due to change Documentation: Updated documentation to reflect change.
1 parent fef5054 commit bf92d2f

File tree

10 files changed

+47
-54
lines changed

10 files changed

+47
-54
lines changed

extension/observer/endpoints.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ const (
3838
)
3939

4040
var (
41-
// EndpointTypes is a map of all known endpoint types.
42-
EndpointTypes = map[EndpointType]bool{
43-
PortType: true,
44-
PodType: true,
45-
HostPortType: true,
46-
}
47-
4841
_ EndpointDetails = (*Pod)(nil)
4942
_ EndpointDetails = (*Port)(nil)
5043
_ EndpointDetails = (*HostPort)(nil)
@@ -71,17 +64,11 @@ func (e *Endpoint) Env() (EndpointEnv, error) {
7164
if e.Details == nil {
7265
return nil, errors.New("endpoint is missing details")
7366
}
67+
7468
env := e.Details.Env()
7569
env["endpoint"] = e.Target
70+
env["type"] = string(e.Details.Type())
7671

77-
// Populate type field for evaluating rules with `type.port && ...`.
78-
// Use string instead of EndpointType for rule evaluation.
79-
types := map[string]bool{}
80-
for endpointType := range EndpointTypes {
81-
types[string(endpointType)] = false
82-
}
83-
types[string(e.Details.Type())] = true
84-
env["type"] = types
8572
return env, nil
8673
}
8774

extension/observer/endpoints_test.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ func TestEndpointEnv(t *testing.T) {
4444
},
4545
},
4646
want: EndpointEnv{
47-
"type": map[string]bool{
48-
"pod": true,
49-
"hostport": false,
50-
"port": false,
51-
},
47+
"type": "pod",
5248
"endpoint": "192.68.73.2",
5349
"name": "pod_name",
5450
"labels": map[string]string{
@@ -85,11 +81,7 @@ func TestEndpointEnv(t *testing.T) {
8581
},
8682
},
8783
want: EndpointEnv{
88-
"type": map[string]bool{
89-
"pod": false,
90-
"hostport": false,
91-
"port": true,
92-
},
84+
"type": "port",
9385
"endpoint": "192.68.73.2",
9486
"name": "port_name",
9587
"port": uint16(2379),
@@ -122,11 +114,7 @@ func TestEndpointEnv(t *testing.T) {
122114
},
123115
},
124116
want: EndpointEnv{
125-
"type": map[string]bool{
126-
"hostport": true,
127-
"pod": false,
128-
"port": false,
129-
},
117+
"type": "hostport",
130118
"endpoint": "127.0.0.1",
131119
"process_name": "process_name",
132120
"command": "./cmd --config config.yaml",

extension/observer/hostobserver/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Endpoint variables exposed by this observer are as follows.
2222

2323
| Variable | Description |
2424
|-----------|--------------------------------------------------------------------------------------------|
25-
| type.port | `true` |
25+
| type | `"port"` |
2626
| name | name of the process associated to the port |
2727
| port | port number |
2828
| command | full command used to invoke this process, including the executable itself at the beginning |

receiver/receivercreator/README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,39 +62,39 @@ This setting controls what resource attributes are set on metrics emitted from t
6262

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

65-
`type.pod`
65+
`type == "pod"`
6666

6767
| Resource Attribute | Default |
6868
|--------------------|---------------|
6969
| k8s.pod.name | \`name\` |
7070
| k8s.pod.uid | \`uid\` |
7171
| k8s.namespace.name | \`namespace\` |
7272

73-
`type.port`
73+
`type == "port"`
7474

7575
| Resource Attribute | Default |
7676
|--------------------|-------------------|
7777
| k8s.pod.name | \`pod.name\` |
7878
| k8s.pod.uid | \`pod.uid\` |
7979
| k8s.namespace.name | \`pod.namespace\` |
8080

81-
`type.hostport`
81+
`type == "hostport"`
8282

8383
None
8484

8585
See `redis/2` in [examples](#examples).
8686

8787
## Rule Expressions
8888

89-
Each rule must start with `type.(pod|port|hostport) &&` such that the rule matches
89+
Each rule must start with `type == ("pod"|"port"|"hostport") &&` such that the rule matches
9090
only one endpoint type. Depending on the type of endpoint the rule is
9191
targeting it will have different variables available.
9292

9393
### Pod
9494

9595
| Variable | Description |
9696
|-------------|-----------------------------------|
97-
| type.pod | `true` |
97+
| type | `"pod"` |
9898
| name | name of the pod |
9999
| namespace | namespace of the pod |
100100
| uid | unique id of the pod |
@@ -105,7 +105,7 @@ targeting it will have different variables available.
105105

106106
| Variable | Description |
107107
|-----------------|-----------------------------------------|
108-
| type.port | `true` |
108+
| type | `"port"` |
109109
| name | container port name |
110110
| port | port number |
111111
| protocol | The transport protocol ("TCP" or "UDP") |
@@ -119,7 +119,7 @@ targeting it will have different variables available.
119119

120120
| Variable | Description |
121121
|---------------|--------------------------------------------------|
122-
| type.hostport | `true` |
122+
| type | `"hostport"` |
123123
| process_name | Name of the process |
124124
| command | Command line with the used to invoke the process |
125125
| is_ipv6 | true if endpoint is IPv6, otherwise false |
@@ -141,14 +141,14 @@ receivers:
141141
receivers:
142142
prometheus_simple:
143143
# Configure prometheus scraping if standard prometheus annotations are set on the pod.
144-
rule: type.pod && annotations["prometheus.io/scrape"] == "true"
144+
rule: type == "pod" && annotations["prometheus.io/scrape"] == "true"
145145
config:
146146
metrics_path: '`"prometheus.io/path" in annotations ? annotations["prometheus.io/path"] : "/metrics"`'
147147
endpoint: '`endpoint`:`"prometheus.io/port" in annotations ? annotations["prometheus.io/port"] : 9090`'
148148

149149
redis/1:
150150
# If this rule matches an instance of this receiver will be started.
151-
rule: type.port && port == 6379
151+
rule: type == "port" && port == 6379
152152
config:
153153
# Static receiver-specific config.
154154
password: secret
@@ -157,7 +157,7 @@ receivers:
157157

158158
redis/2:
159159
# Set a resource attribute based on endpoint value.
160-
rule: type.port && port == 6379
160+
rule: type == "port" && port == 6379
161161
resource_attributes:
162162
# Dynamic value.
163163
app: `pod.labels["app"]`
@@ -169,7 +169,7 @@ receivers:
169169
receivers:
170170
redis/on_host:
171171
# If this rule matches an instance of this receiver will be started.
172-
rule: type.port && port == 6379 && is_ipv6 == true
172+
rule: type == "port" && port == 6379 && is_ipv6 == true
173173
config:
174174
service_name: redis_on_host
175175

receiver/receivercreator/config_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ func TestLoadConfig(t *testing.T) {
8484
r1 := cfg.Receivers["receiver_creator/1"].(*Config)
8585

8686
assert.NotNil(t, r1)
87-
assert.Len(t, r1.receiverTemplates, 1)
87+
assert.Len(t, r1.receiverTemplates, 2)
88+
assert.Contains(t, r1.receiverTemplates, "examplereceiver/1")
89+
assert.Equal(t, `type == "port"`, r1.receiverTemplates["examplereceiver/1"].Rule)
8890
assert.Contains(t, r1.receiverTemplates, "nop/1")
89-
assert.Equal(t, `type.port`, r1.receiverTemplates["nop/1"].Rule)
91+
assert.Equal(t, `type == "port"`, r1.receiverTemplates["nop/1"].Rule)
9092
assert.Equal(t, userConfigMap{
9193
endpointConfigKey: "localhost:12345",
9294
}, r1.receiverTemplates["nop/1"].config)

receiver/receivercreator/fixtures_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ var portEndpoint = observer.Endpoint{
4848
},
4949
}
5050

51+
var hostportEndpoint = observer.Endpoint{
52+
ID: "port-1",
53+
Target: "localhost:1234",
54+
Details: &observer.HostPort{
55+
ProcessName: "splunk",
56+
Command: "./splunk",
57+
Port: 1234,
58+
Transport: observer.ProtocolTCP,
59+
},
60+
}
61+
5162
var unsupportedEndpoint = observer.Endpoint{
5263
ID: "endpoint-1",
5364
Target: "localhost:1234",

receiver/receivercreator/observerhandler_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestOnAdd(t *testing.T) {
5252
rcvrCfg := receiverConfig{typeStr: configmodels.Type("name"), config: userConfigMap{"foo": "bar"}, fullName: "name/1"}
5353
cfg := createDefaultConfig().(*Config)
5454
cfg.receiverTemplates = map[string]receiverTemplate{
55-
"name/1": {rcvrCfg, "", newRuleOrPanic(`type.port`)},
55+
"name/1": {rcvrCfg, "", newRuleOrPanic(`type == "port"`)},
5656
}
5757
handler := &observerHandler{
5858
config: cfg,
@@ -104,7 +104,7 @@ func TestOnChange(t *testing.T) {
104104
newRcvr := &nopWithEndpointReceiver{}
105105
cfg := createDefaultConfig().(*Config)
106106
cfg.receiverTemplates = map[string]receiverTemplate{
107-
"name/1": {rcvrCfg, "", newRuleOrPanic(`type.port`)},
107+
"name/1": {rcvrCfg, "", newRuleOrPanic(`type == "port"`)},
108108
}
109109
handler := &observerHandler{
110110
config: cfg,
@@ -136,8 +136,8 @@ func TestDynamicConfig(t *testing.T) {
136136
cfg.receiverTemplates = map[string]receiverTemplate{
137137
"name/1": {
138138
receiverConfig: receiverConfig{typeStr: configmodels.Type("name"), config: userConfigMap{"endpoint": "`endpoint`:6379"}, fullName: "name/1"},
139-
Rule: "type.pod",
140-
rule: newRuleOrPanic("type.pod"),
139+
Rule: `type == "pod"`,
140+
rule: newRuleOrPanic("type == \"pod\""),
141141
},
142142
}
143143
handler := &observerHandler{

receiver/receivercreator/rules.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type rule struct {
3030
}
3131

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

3535
// newRule creates a new rule instance.
3636
func newRule(ruleStr string) (rule, error) {

receiver/receivercreator/rules_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ func Test_ruleEval(t *testing.T) {
4444
}{
4545
// Doesn't work yet. See comment in newRule.
4646
// {"unknown variable", args{`type == "port" && unknown_var == 1`, portEndpoint}, false, true},
47-
{"basic port", args{`type.port && name == "http" && pod.labels["app"] == "redis"`, portEndpoint}, true, false},
48-
{"basic pod", args{`type.pod && labels["region"] == "west-1"`, podEndpoint}, true, false},
49-
{"annotations", args{`type.pod && annotations["scrape"] == "true"`, podEndpoint}, true, false},
47+
{"basic port", args{`type == "port" && name == "http" && pod.labels["app"] == "redis"`, portEndpoint}, true, false},
48+
{"basic hostport", args{`type == "hostport" && port == 1234 && process_name == "splunk"`, hostportEndpoint}, true, false},
49+
{"basic pod", args{`type == "pod" && labels["region"] == "west-1"`, podEndpoint}, true, false},
50+
{"annotations", args{`type == "pod" && annotations["scrape"] == "true"`, podEndpoint}, true, false},
5051
}
5152
for _, tt := range tests {
5253
t.Run(tt.name, func(t *testing.T) {
@@ -81,7 +82,9 @@ func Test_newRule(t *testing.T) {
8182
{"empty rule", args{""}, true},
8283
{"does not start with type", args{"port == 1234"}, true},
8384
{"invalid syntax", args{"port =="}, true},
84-
{"valid", args{`type.port && port_name == "http"`}, false},
85+
{"valid port", args{`type == "port" && port_name == "http"`}, false},
86+
{"valid pod", args{`type=="pod" && port_name == "http"`}, false},
87+
{"valid hostport", args{`type == "hostport" && port_name == "http"`}, false},
8588
}
8689
for _, tt := range tests {
8790
t.Run(tt.name, func(t *testing.T) {

receiver/receivercreator/testdata/config.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ receivers:
33
receiver_creator/1:
44
watch_observers: [mock_observer]
55
receivers:
6+
examplereceiver/1:
7+
rule: type == "port"
68
nop/1:
7-
rule: type.port
9+
rule: type == "port"
810
config:
911
endpoint: localhost:12345
1012

0 commit comments

Comments
 (0)