Skip to content

Commit

Permalink
allow overriding container operator & disallow target path
Browse files Browse the repository at this point in the history
Signed-off-by: ChrsMark <[email protected]>
  • Loading branch information
ChrsMark committed Dec 18, 2024
1 parent 515fd76 commit 64dd656
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
5 changes: 4 additions & 1 deletion receiver/receivercreator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,10 @@ io.opentelemetry.discovery.logs/config: |
```

Note that individual settings are overridden by the configuration provided by the hints while the operators list
is extended keeping first the `container` parser.
is extended keeping first the `container` parser. If `container` parser is explicitly set by the user the default
will be overridden.

`include` cannot be overridden and is fixed to discovered container's log file path.


#### Support multiple target containers
Expand Down
19 changes: 18 additions & 1 deletion receiver/receivercreator/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,31 @@ func createLogsConfig(
logger.Debug("could not unmarshal configuration from hint", zap.Error(err))
}

containerOpFound := false
for k, v := range userConf {
if k == filelogOperatorsConfigKey {
vlist, ok := v.([]any)
if !ok {
logger.Debug("could not parse operators configuration from hint", zap.Any("config", userConf))
}
vlist = append(cont, vlist...)
for _, op := range vlist {
operator, ok := op.(map[string]any)
if !ok {
logger.Debug("could not parse operator configuration from hint", zap.Any("operator", op))
}
if operator["type"] == "container" {
containerOpFound = true
}
}
if !containerOpFound {
// if no container operator found then just extend the list
// otherwise we just use the user provided operators as-is
vlist = append(cont, vlist...)
}
defaultConfMap[k] = vlist
} else if k == "include" {
// path cannot be other than the one of the target container
continue
} else {
defaultConfMap[k] = v
}
Expand Down
74 changes: 74 additions & 0 deletions receiver/receivercreator/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ operators:
- type: add
field: attributes.tag
value: beta`
configReplaceContainer := `
operators:
- type: container
id: replaced
- type: regex_parser
regex: "^(?P<time>\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}) (?P<sev>[A-Z]*) (?P<msg>.*)$"`
configReplaceInclude := `
include:
- /var/log/pod/x/foo.log`

tests := map[string]struct {
inputEndpoint observer.Endpoint
Expand Down Expand Up @@ -421,6 +430,71 @@ operators:
expectedReceiver: receiverTemplate{},
wantError: true,
ignoreReceivers: []string{},
}, `logs_pod_level_hints_replace_container_operator`: {
inputEndpoint: observer.Endpoint{
ID: "namespace/pod-2-UID/filelog(redis)",
Target: "1.2.3.4:",
Details: &observer.PodContainer{
Name: "redis", Pod: observer.Pod{
Name: "pod-2",
Namespace: "default",
UID: "pod-2-UID",
Labels: map[string]string{"env": "prod"},
Annotations: map[string]string{
otelLogsHints + "/enabled": "true",
otelLogsHints + "/config": configReplaceContainer,
},
},
},
},
expectedReceiver: receiverTemplate{
receiverConfig: receiverConfig{
id: id,
config: userConfigMap{
"include": []string{"/var/log/pods/default_pod-2_pod-2-UID/redis/*.log"},
"include_file_name": false,
"include_file_path": true,
"operators": []any{
map[string]any{"id": "replaced", "type": "container"},
map[string]any{"type": "regex_parser", "regex": "^(?P<time>\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}) (?P<sev>[A-Z]*) (?P<msg>.*)$"},
},
},
}, signals: receiverSignals{metrics: false, logs: true, traces: false},
},
wantError: false,
ignoreReceivers: []string{},
}, `logs_pod_level_hints_do_not_replace_path`: {
inputEndpoint: observer.Endpoint{
ID: "namespace/pod-2-UID/filelog(redis)",
Target: "1.2.3.4:",
Details: &observer.PodContainer{
Name: "redis", Pod: observer.Pod{
Name: "pod-2",
Namespace: "default",
UID: "pod-2-UID",
Labels: map[string]string{"env": "prod"},
Annotations: map[string]string{
otelLogsHints + "/enabled": "true",
otelLogsHints + "/config": configReplaceInclude,
},
},
},
},
expectedReceiver: receiverTemplate{
receiverConfig: receiverConfig{
id: id,
config: userConfigMap{
"include": []string{"/var/log/pods/default_pod-2_pod-2-UID/redis/*.log"},
"include_file_name": false,
"include_file_path": true,
"operators": []any{
map[string]any{"id": "container-parser", "type": "container"},
},
},
}, signals: receiverSignals{metrics: false, logs: true, traces: false},
},
wantError: false,
ignoreReceivers: []string{},
},
}

Expand Down

0 comments on commit 64dd656

Please sign in to comment.