Skip to content

Commit

Permalink
feat(sumologicschemaprocessor): add config for nesting processor
Browse files Browse the repository at this point in the history
  • Loading branch information
aboguszewski-sumo committed Dec 22, 2022
1 parent 9034571 commit 9048ad6
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- feat(sumologicschemaprocessor): add nesting processor [#877]

[Unreleased]: https://github.com/SumoLogic/sumologic-otel-collector/compare/v0.67.0-sumo-0...main
[#877]: https://github.com/SumoLogic/sumologic-otel-collector/pull/877

## [v0.67.0-sumo-0]

Expand Down
80 changes: 80 additions & 0 deletions pkg/processor/sumologicschemaprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ processors:
# See `translate_telegraf_metrics_processor.go` for full list of translations.
# default = true
translate_telegraf_attributes: {true, false}

# Specifies if attributes should be nested, basing on their keys.
# See "Nesting attributes" documentation chapter from this document.
nest_attributes:
# Defines whether attributes should be nested.
# default = false
enabled: {true, false}

# Defines the string used to separate key names in attributes that are to be nested.
# default = "."
separator: <separator>
```
## Features
Expand Down Expand Up @@ -98,3 +109,72 @@ Below is a list of all attribute keys that are being translated.
| `k8s.statefulset.name` | `statefulset` |
| `service.name` | `service` |
| `log.file.path_resolved` | `_sourceName` |

### Nesting attributes

Nesting attributes allows to change the structure of attributes (both resource level and record level attributes)
basing on their keys by nesting attributes with common paths into maps.
Common path is defined as a common prefix that consists of strings separated by a given separator string (by default a dot - `.`)
and end with that separator, for example: if the separator is `.`,
then `xyz` is a common prefix path for `xyz.abc.qwe` and `xyz.foo`,
but `sumo` **is not** a prefix path for neither `sumologic.foo.baz` nor `sumologic.bar.baz`.

#### Example

The following set of attributes:

```json
{
"kubernetes.container_name": "xyz",
"kubernetes.host.name": "the host",
"kubernetes.host.address": "127.0.0.1",
"kubernetes.namespace_name": "sumologic",
"another_attr": "42"
}
```

Should be translated into such set:

```json
{
"kubernetes": {
"container_name": "xyz",
"namespace_name": "sumologic",
"host": {
"name": "the host",
"address": "127.0.0.1"
}
},
"another_attr": "42"
}
```

#### Known issues

This feature has undefined behavior when in input there are various keys that will be mapped to the same nested structure.
For example, given the following attributes:

```json
{
"a.b.c.": "d",
"a": {
"b": {
"c": "e"
}
}
}
```

It is not possible to predict, what will be the result. It will have the following structure:

```json
{
"a": {
"b": {
"c": ...
}
}
}
```

However, it is not possible to know a priori if the value under key `c` will be equal to `d` or `e`.
15 changes: 12 additions & 3 deletions pkg/processor/sumologicschemaprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ import (
type Config struct {
config.ProcessorSettings `mapstructure:",squash"`

AddCloudNamespace bool `mapstructure:"add_cloud_namespace"`
TranslateAttributes bool `mapstructure:"translate_attributes"`
TranslateTelegrafAttributes bool `mapstructure:"translate_telegraf_attributes"`
AddCloudNamespace bool `mapstructure:"add_cloud_namespace"`
TranslateAttributes bool `mapstructure:"translate_attributes"`
TranslateTelegrafAttributes bool `mapstructure:"translate_telegraf_attributes"`
NestAttributes *NestingProcessorConfig `mapstructure:"nest_attributes"`
}

const (
defaultAddCloudNamespace = true
defaultTranslateAttributes = true
defaultTranslateTelegrafAttributes = true

// Nesting processor default config
defaultNestingEnabled = false
defaultNestingSeparator = "."
)

// Ensure the Config struct satisfies the config.Processor interface.
Expand All @@ -42,6 +47,10 @@ func createDefaultConfig() component.Config {
AddCloudNamespace: defaultAddCloudNamespace,
TranslateAttributes: defaultTranslateAttributes,
TranslateTelegrafAttributes: defaultTranslateTelegrafAttributes,
NestAttributes: &NestingProcessorConfig{
Separator: defaultNestingSeparator,
Enabled: defaultNestingEnabled,
},
}
}

Expand Down
29 changes: 29 additions & 0 deletions pkg/processor/sumologicschemaprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func TestLoadConfig(t *testing.T) {
AddCloudNamespace: false,
TranslateAttributes: true,
TranslateTelegrafAttributes: true,
NestAttributes: &NestingProcessorConfig{
Enabled: false,
Separator: ".",
},
})

p2 := cfg.Processors[component.NewIDWithName(typeStr, "disabled-attribute-translation")]
Expand All @@ -64,6 +68,10 @@ func TestLoadConfig(t *testing.T) {
AddCloudNamespace: true,
TranslateAttributes: false,
TranslateTelegrafAttributes: true,
NestAttributes: &NestingProcessorConfig{
Enabled: false,
Separator: ".",
},
})

p3 := cfg.Processors[component.NewIDWithName(typeStr, "disabled-telegraf-attribute-translation")]
Expand All @@ -77,5 +85,26 @@ func TestLoadConfig(t *testing.T) {
AddCloudNamespace: true,
TranslateAttributes: true,
TranslateTelegrafAttributes: false,
NestAttributes: &NestingProcessorConfig{
Enabled: false,
Separator: ".",
},
})

p4 := cfg.Processors[component.NewIDWithName(typeStr, "enabled-nesting")]

assert.Equal(t, p4,
&Config{
ProcessorSettings: config.NewProcessorSettings(component.NewIDWithName(
typeStr,
"enabled-nesting",
)),
AddCloudNamespace: true,
TranslateAttributes: true,
TranslateTelegrafAttributes: true,
NestAttributes: &NestingProcessorConfig{
Enabled: true,
Separator: "!",
},
})
}
11 changes: 8 additions & 3 deletions pkg/processor/sumologicschemaprocessor/nesting_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@ import (
"go.opentelemetry.io/collector/pdata/ptrace"
)

type NestingProcessorConfig struct {
Separator string `mapstructure:"separator"`
Enabled bool `mapstructure:"enabled"`
}

type NestingProcessor struct {
separator string
enabled bool
}

func newNestingProcessor(separator string, enabled bool) (*NestingProcessor, error) {
func newNestingProcessor(config *NestingProcessorConfig) (*NestingProcessor, error) {
proc := &NestingProcessor{
separator: separator,
enabled: enabled,
separator: config.Separator,
enabled: config.Enabled,
}

return proc, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestNestingAttributes(t *testing.T) {

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
proc, err := newNestingProcessor(".", true)
proc, err := newNestingProcessor(&NestingProcessorConfig{Separator: ".", Enabled: true})
require.NoError(t, err)

attrs := mapToPcommonMap(testCase.input)
Expand Down
2 changes: 1 addition & 1 deletion pkg/processor/sumologicschemaprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func newSumologicSchemaProcessor(set component.ProcessorCreateSettings, config *
return nil, err
}

nestingProcessor, err := newNestingProcessor(".", false)
nestingProcessor, err := newNestingProcessor(config.NestAttributes)
if err != nil {
return nil, err
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/processor/sumologicschemaprocessor/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ processors:
translate_attributes: false
sumologic_schema/disabled-telegraf-attribute-translation:
translate_telegraf_attributes: false
sumologic_schema/enabled-nesting:
nest_attributes:
enabled: true
separator: "!"

exporters:
nop:
Expand Down Expand Up @@ -39,6 +43,14 @@ service:
exporters:
- nop

metrics/3:
receivers:
- nop
processors:
- sumologic_schema/enabled-nesting
exporters:
- nop

traces:
receivers:
- nop
Expand Down

0 comments on commit 9048ad6

Please sign in to comment.