-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[receiver/receiver_creator] Add support for enabling logs' collecting from K8s hints #36581
Open
ChrsMark
wants to merge
5
commits into
open-telemetry:main
Choose a base branch
from
ChrsMark:f_hints_logs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d573793
[receiver/receiver_creator] Add support for logs' hints
ChrsMark fcacfbe
fix docs
ChrsMark a2a3f77
allow overriding container operator & disallow target path
ChrsMark 1dd06ec
override all configurations
ChrsMark 55d537e
minor docs improvements
ChrsMark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: receivercreator | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add support for starting logs' collection based on provided k8s annotations' hints | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [34427] | ||
|
||
# (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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,11 +21,15 @@ const ( | |
|
||
// hint suffix for metrics | ||
otelMetricsHints = otelHints + ".metrics" | ||
otelLogsHints = otelHints + ".logs" | ||
|
||
// hints definitions | ||
discoveryEnabledHint = "enabled" | ||
scraperHint = "scraper" | ||
configHint = "config" | ||
|
||
logsReceiver = "filelog" | ||
defaultLogPathPattern = "/var/log/pods/%s_%s_%s/%s/*.log" | ||
) | ||
|
||
// k8sHintsBuilder creates configurations from hints provided as Pod's annotations. | ||
|
@@ -57,7 +61,7 @@ func (builder *k8sHintsBuilder) createReceiverTemplateFromHints(env observer.End | |
return nil, fmt.Errorf("could not get endpoint type: %v", zap.Any("env", env)) | ||
} | ||
|
||
if endpointType != string(observer.PortType) { | ||
if endpointType != string(observer.PortType) && endpointType != string(observer.PodContainerType) { | ||
return nil, nil | ||
} | ||
|
||
|
@@ -72,7 +76,14 @@ func (builder *k8sHintsBuilder) createReceiverTemplateFromHints(env observer.End | |
return nil, nil | ||
} | ||
|
||
return builder.createScraper(pod.Annotations, env) | ||
switch endpointType { | ||
case string(observer.PortType): | ||
return builder.createScraper(pod.Annotations, env) | ||
case string(observer.PodContainerType): | ||
return builder.createLogsReceiver(pod.Annotations, env) | ||
default: | ||
return nil, nil | ||
} | ||
} | ||
|
||
func (builder *k8sHintsBuilder) createScraper( | ||
|
@@ -91,7 +102,7 @@ func (builder *k8sHintsBuilder) createScraper( | |
port = p.Port | ||
pod := p.Pod | ||
|
||
if !discoveryMetricsEnabled(annotations, otelMetricsHints, fmt.Sprint(port)) { | ||
if !discoveryEnabled(annotations, otelMetricsHints, fmt.Sprint(port)) { | ||
return nil, nil | ||
} | ||
|
||
|
@@ -118,6 +129,48 @@ func (builder *k8sHintsBuilder) createScraper( | |
return &recTemplate, err | ||
} | ||
|
||
func (builder *k8sHintsBuilder) createLogsReceiver( | ||
annotations map[string]string, | ||
env observer.EndpointEnv, | ||
) (*receiverTemplate, error) { | ||
if _, ok := builder.ignoreReceivers[logsReceiver]; ok { | ||
// receiver is ignored | ||
return nil, nil | ||
} | ||
|
||
var containerName string | ||
var c observer.PodContainer | ||
err := mapstructure.Decode(env, &c) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not extract pod's container: %v", zap.Any("env", env)) | ||
} | ||
if c.Name == "" { | ||
return nil, fmt.Errorf("could not extract container name: %v", zap.Any("container", c)) | ||
} | ||
containerName = c.Name | ||
pod := c.Pod | ||
|
||
if !discoveryEnabled(annotations, otelLogsHints, containerName) { | ||
return nil, nil | ||
} | ||
|
||
subreceiverKey := logsReceiver | ||
builder.logger.Debug("handling added hinted receiver", zap.Any("subreceiverKey", subreceiverKey)) | ||
|
||
userConfMap := createLogsConfig( | ||
annotations, | ||
containerName, | ||
pod.UID, | ||
pod.Name, | ||
pod.Namespace, | ||
builder.logger) | ||
|
||
recTemplate, err := newReceiverTemplate(fmt.Sprintf("%v/%v_%v", subreceiverKey, pod.UID, containerName), userConfMap) | ||
recTemplate.signals = receiverSignals{false, true, false} | ||
|
||
return &recTemplate, err | ||
} | ||
|
||
func getScraperConfFromAnnotations( | ||
annotations map[string]string, | ||
defaultEndpoint, scopeSuffix string, | ||
|
@@ -149,6 +202,43 @@ func getScraperConfFromAnnotations( | |
return conf, nil | ||
} | ||
|
||
func createLogsConfig( | ||
annotations map[string]string, | ||
containerName, podUID, podName, namespace string, | ||
logger *zap.Logger, | ||
) userConfigMap { | ||
scopeSuffix := containerName | ||
logPath := fmt.Sprintf(defaultLogPathPattern, namespace, podName, podUID, containerName) | ||
cont := []any{map[string]any{"id": "container-parser", "type": "container"}} | ||
defaultConfMap := userConfigMap{ | ||
"include": []string{logPath}, | ||
"include_file_path": true, | ||
"include_file_name": false, | ||
"operators": cont, | ||
} | ||
|
||
configStr, found := getHintAnnotation(annotations, otelLogsHints, configHint, scopeSuffix) | ||
if !found || configStr == "" { | ||
return defaultConfMap | ||
} | ||
|
||
userConf := make(map[string]any) | ||
if err := yaml.Unmarshal([]byte(configStr), &userConf); err != nil { | ||
logger.Debug("could not unmarshal configuration from hint", zap.Error(err)) | ||
} | ||
|
||
for k, v := range userConf { | ||
if k == "include" { | ||
// path cannot be other than the one of the target container | ||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we log here to warn users? |
||
} else { | ||
defaultConfMap[k] = v | ||
dmitryax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
return defaultConfMap | ||
} | ||
|
||
func getHintAnnotation(annotations map[string]string, hintBase string, hintKey string, suffix string) (string, bool) { | ||
// try to scope the hint more on container level by suffixing | ||
// with .<port> in case of Port event or # TODO: .<container_name> in case of Pod Container event | ||
|
@@ -162,7 +252,7 @@ func getHintAnnotation(annotations map[string]string, hintBase string, hintKey s | |
return podLevelHint, ok | ||
} | ||
|
||
func discoveryMetricsEnabled(annotations map[string]string, hintBase string, scopeSuffix string) bool { | ||
func discoveryEnabled(annotations map[string]string, hintBase string, scopeSuffix string) bool { | ||
enabledHint, found := getHintAnnotation(annotations, hintBase, discoveryEnabledHint, scopeSuffix) | ||
if !found { | ||
return false | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's avoid using positional values in struct initialization. It's easy to break by changing the struct's fields and unclear what
false, true, false
is. I see it's done for metrics as well. Please file a separate PR to fix metrics