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

feat(sourceprocessor): make container name attribute configurable #950

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This release introduces the following breaking changes:
- feat(sourceprocessor): add debug logs for source category filler [#944]
- feat(snmpreceiver): add SNMP receiver to distro [#945]
- feat(syslogexporter): add syslog exporter [#936]
- feat(sourceprocessor): make container name attribute configurable [#950]

### Fixed

Expand All @@ -26,6 +27,7 @@ This release introduces the following breaking changes:
[#944]: https://github.com/SumoLogic/sumologic-otel-collector/pull/944
[#945]: https://github.com/SumoLogic/sumologic-otel-collector/pull/945
[#936]: https://github.com/SumoLogic/sumologic-otel-collector/pull/936
[#950]: https://github.com/SumoLogic/sumologic-otel-collector/pull/950
[Unreleased]: https://github.com/SumoLogic/sumologic-otel-collector/compare/v0.70.0-sumo-0...main

## [v0.70.0-sumo-2]
Expand Down
3 changes: 3 additions & 0 deletions pkg/processor/sourceprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ processors:
# Specifies whether container-level annotations are enabled.
# default: false
enabled: {true, false}
# Name of the attribute that contains the container name.
# default: "k8s.container.name"
container_name_key: <container_name_key>
# List of prefixes for container-level pod annotations.
# default: ["sumologic.com/"]
prefixes:
Expand Down
5 changes: 3 additions & 2 deletions pkg/processor/sourceprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Config struct {
}

type ContainerAnnotationsConfig struct {
Enabled bool `mapstructure:"enabled"`
Prefixes []string `mapstructure:"prefixes"`
Enabled bool `mapstructure:"enabled"`
ContainerNameKey string `mapstructure:"container_name_key"`
Prefixes []string `mapstructure:"prefixes"`
}
3 changes: 2 additions & 1 deletion pkg/processor/sourceprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func TestLoadConfig(t *testing.T) {
PodTemplateHashKey: "pod_labels_pod-template-hash",

ContainerAnnotations: ContainerAnnotationsConfig{
Enabled: false,
Enabled: false,
ContainerNameKey: "k8s.container.name",
Prefixes: []string{
"sumologic.com/",
},
Expand Down
4 changes: 3 additions & 1 deletion pkg/processor/sourceprocessor/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
defaultPodKey = "k8s.pod.name"
defaultPodNameKey = "k8s.pod.pod_name"
defaultPodTemplateHashKey = "k8s.pod.label.pod-template-hash"
defaultContainerNameKey = "k8s.container.name"

stabilityLevel = component.StabilityLevelBeta
)
Expand Down Expand Up @@ -72,7 +73,8 @@ func createDefaultConfig() component.Config {
PodTemplateHashKey: defaultPodTemplateHashKey,

ContainerAnnotations: ContainerAnnotationsConfig{
Enabled: false,
Enabled: false,
ContainerNameKey: defaultContainerNameKey,
Prefixes: []string{
"sumologic.com/",
},
Expand Down
7 changes: 5 additions & 2 deletions pkg/processor/sourceprocessor/source_category_filler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type sourceCategoryFiller struct {
dashReplacement string
annotationPrefix string
containerAnnotationsEnabled bool
containerNameKey string
containerAnnotationsPrefixes []string
}

Expand All @@ -48,6 +49,7 @@ func newSourceCategoryFiller(cfg *Config, logger *zap.Logger) sourceCategoryFill
dashReplacement: cfg.SourceCategoryReplaceDash,
annotationPrefix: cfg.AnnotationPrefix,
containerAnnotationsEnabled: cfg.ContainerAnnotations.Enabled,
containerNameKey: cfg.ContainerAnnotations.ContainerNameKey,
containerAnnotationsPrefixes: cfg.ContainerAnnotations.Prefixes,
}
}
Expand Down Expand Up @@ -104,9 +106,10 @@ func (f *sourceCategoryFiller) getSourceCategoryFromContainerAnnotation(attribut
return ""
}

containerName, found := attributes.Get("k8s.container.name")
containerName, found := attributes.Get(f.containerNameKey)
if !found || containerName.Str() == "" {
f.logger.Debug("Couldn't fill source category from container annotation: k8s.container.name attribute not found.")
f.logger.Debug("Couldn't fill source category from container annotation: container name attribute not found.",
zap.String("container_name_key", f.containerNameKey))
return ""
}

Expand Down
19 changes: 18 additions & 1 deletion pkg/processor/sourceprocessor/source_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func TestSourceCategoryAnnotations(t *testing.T) {
assertAttribute(t, processedAttributes, "_sourceCategory", "annot>sc^namespace^1")
})

t.Run("container-level annotations", func(t *testing.T) {
t.Run("container-level annotations with default settings", func(t *testing.T) {
inputAttributes := createK8sLabels()
inputAttributes["pod_annotation_sumologic.com/container-1.sourceCategory"] = "container-sc"
inputTraces := newTraceData(inputAttributes)
Expand All @@ -453,6 +453,23 @@ func TestSourceCategoryAnnotations(t *testing.T) {
processedAttributes := processedTraces.ResourceSpans().At(0).Resource().Attributes()
assertAttribute(t, processedAttributes, "_sourceCategory", "container-sc")
})

t.Run("container-level annotations with custom settings", func(t *testing.T) {
inputAttributes := createK8sLabels()
delete(inputAttributes, "k8s.container.name")
inputAttributes["containername"] = "container-2"
inputAttributes["pod_annotation_custom.prefix/container-2.sourceCategory"] = "container-sc"
inputTraces := newTraceData(inputAttributes)

cfg.ContainerAnnotations.Enabled = true
cfg.ContainerAnnotations.ContainerNameKey = "containername"
cfg.ContainerAnnotations.Prefixes = []string{"custom.prefix/"}
processedTraces, err := newSourceProcessor(newProcessorCreateSettings(), cfg).ProcessTraces(context.Background(), inputTraces)
assert.NoError(t, err)

processedAttributes := processedTraces.ResourceSpans().At(0).Resource().Attributes()
assertAttribute(t, processedAttributes, "_sourceCategory", "container-sc")
})
}

func TestSourceCategoryTemplateWithCustomAttribute(t *testing.T) {
Expand Down