From 0ed62e5ff24e3c419ad85daf00266c078fb4f165 Mon Sep 17 00:00:00 2001 From: Erik Baranowski <39704712+erikbaranowski@users.noreply.github.com> Date: Thu, 14 Mar 2024 17:17:16 -0400 Subject: [PATCH] =?UTF-8?q?Pass=20in=20a=20label=20prefix=20from=20static?= =?UTF-8?q?=20to=20otelcolconvert=20so=20multiple=20trac=E2=80=A6=20(#6700?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Pass in a label prefix from static to otelcolconvert so multiple trace configs don't bump into each other Signed-off-by: erikbaranowski <39704712+erikbaranowski@users.noreply.github.com> * only add a label prefix if we have more than one trace config Signed-off-by: erikbaranowski <39704712+erikbaranowski@users.noreply.github.com> --------- Signed-off-by: erikbaranowski <39704712+erikbaranowski@users.noreply.github.com> --- .../internal/otelcolconvert/converter.go | 16 ++-- .../internal/otelcolconvert/otelcolconvert.go | 14 ++-- .../staticconvert/internal/build/builder.go | 13 ++- .../staticconvert/testdata/traces_multi.diags | 1 + .../staticconvert/testdata/traces_multi.river | 84 +++++++++++++++++++ .../staticconvert/testdata/traces_multi.yaml | 27 ++++++ 6 files changed, 140 insertions(+), 15 deletions(-) create mode 100644 internal/converter/internal/staticconvert/testdata/traces_multi.diags create mode 100644 internal/converter/internal/staticconvert/testdata/traces_multi.river create mode 100644 internal/converter/internal/staticconvert/testdata/traces_multi.yaml diff --git a/internal/converter/internal/otelcolconvert/converter.go b/internal/converter/internal/otelcolconvert/converter.go index 70f2e4cc72dd..77d74f61c208 100644 --- a/internal/converter/internal/otelcolconvert/converter.go +++ b/internal/converter/internal/otelcolconvert/converter.go @@ -62,8 +62,9 @@ type state struct { // extensionLookup maps OTel extensions to Flow component IDs. extensionLookup map[component.ID]componentID - componentID component.InstanceID // ID of the current component being converted. - componentConfig component.Config // Config of the current component being converted. + componentID component.InstanceID // ID of the current component being converted. + componentConfig component.Config // Config of the current component being converted. + componentLabelPrefix string // Prefix for the label of the current component being converted. } type converterKey struct { @@ -120,9 +121,13 @@ func (state *state) flowLabelForComponent(c component.InstanceID) string { // // Otherwise, we'll replace empty group and component names with "default" // and concatenate them with an underscore. + unsanitizedLabel := state.componentLabelPrefix + if unsanitizedLabel != "" { + unsanitizedLabel += "_" + } switch { case groupName == "" && componentName == "": - return defaultLabel + unsanitizedLabel += defaultLabel default: if groupName == "" { @@ -131,9 +136,10 @@ func (state *state) flowLabelForComponent(c component.InstanceID) string { if componentName == "" { componentName = defaultLabel } - identifier := fmt.Sprintf("%s_%s", groupName, componentName) - return common.SanitizeIdentifierPanics(identifier) + unsanitizedLabel += fmt.Sprintf("%s_%s", groupName, componentName) } + + return common.SanitizeIdentifierPanics(unsanitizedLabel) } // Next returns the set of Flow component IDs for a given data type that the diff --git a/internal/converter/internal/otelcolconvert/otelcolconvert.go b/internal/converter/internal/otelcolconvert/otelcolconvert.go index 78fa32e627f3..8262053c0c90 100644 --- a/internal/converter/internal/otelcolconvert/otelcolconvert.go +++ b/internal/converter/internal/otelcolconvert/otelcolconvert.go @@ -65,7 +65,7 @@ func Convert(in []byte, extraArgs []string) ([]byte, diag.Diagnostics) { f := builder.NewFile() - diags.AddAll(AppendConfig(f, cfg)) + diags.AddAll(AppendConfig(f, cfg, "")) diags.AddAll(common.ValidateNodes(f)) var buf bytes.Buffer @@ -143,7 +143,7 @@ func getFactories() otelcol.Factories { // AppendConfig converts the provided OpenTelemetry config into an equivalent // Flow config and appends the result to the provided file. -func AppendConfig(file *builder.File, cfg *otelcol.Config) diag.Diagnostics { +func AppendConfig(file *builder.File, cfg *otelcol.Config, labelPrefix string) diag.Diagnostics { var diags diag.Diagnostics groups, err := createPipelineGroups(cfg.Service.Pipelines) @@ -198,8 +198,9 @@ func AppendConfig(file *builder.File, cfg *otelcol.Config) diag.Diagnostics { converterLookup: converterTable, - componentConfig: cfg.Extensions, - componentID: cid, + componentConfig: cfg.Extensions, + componentID: cid, + componentLabelPrefix: labelPrefix, } key := converterKey{Kind: component.KindExtension, Type: ext.Type()} @@ -244,8 +245,9 @@ func AppendConfig(file *builder.File, cfg *otelcol.Config) diag.Diagnostics { converterLookup: converterTable, extensionLookup: extensionTable, - componentConfig: componentSet.configLookup[id], - componentID: componentID, + componentConfig: componentSet.configLookup[id], + componentID: componentID, + componentLabelPrefix: labelPrefix, } key := converterKey{Kind: componentSet.kind, Type: id.Type()} diff --git a/internal/converter/internal/staticconvert/internal/build/builder.go b/internal/converter/internal/staticconvert/internal/build/builder.go index c25b33fda1bc..7b23333f86a5 100644 --- a/internal/converter/internal/staticconvert/internal/build/builder.go +++ b/internal/converter/internal/staticconvert/internal/build/builder.go @@ -380,14 +380,19 @@ func (b *ConfigBuilder) appendTraces() { for _, cfg := range b.cfg.Traces.Configs { otelCfg, err := cfg.OtelConfig() - removeReceiver(otelCfg, "traces", "push_receiver") - if err != nil { b.diags.Add(diag.SeverityLevelCritical, fmt.Sprintf("failed to load otelConfig from agent traces config: %s", err)) continue } - // TODO: what prefix should each generated flow label get? each trace instance will need something unique - b.diags.AddAll(otelcolconvert.AppendConfig(b.f, otelCfg)) + + removeReceiver(otelCfg, "traces", "push_receiver") + + // Let's only prefix things if we are doing more than 1 trace config + labelPrefix := "" + if len(b.cfg.Traces.Configs) > 1 { + labelPrefix = cfg.Name + } + b.diags.AddAll(otelcolconvert.AppendConfig(b.f, otelCfg, labelPrefix)) } } diff --git a/internal/converter/internal/staticconvert/testdata/traces_multi.diags b/internal/converter/internal/staticconvert/testdata/traces_multi.diags new file mode 100644 index 000000000000..a4a05d1a3b58 --- /dev/null +++ b/internal/converter/internal/staticconvert/testdata/traces_multi.diags @@ -0,0 +1 @@ +(Warning) Please review your agent command line flags and ensure they are set in your Flow mode config file where necessary. \ No newline at end of file diff --git a/internal/converter/internal/staticconvert/testdata/traces_multi.river b/internal/converter/internal/staticconvert/testdata/traces_multi.river new file mode 100644 index 000000000000..26f204fd8d96 --- /dev/null +++ b/internal/converter/internal/staticconvert/testdata/traces_multi.river @@ -0,0 +1,84 @@ +otelcol.receiver.otlp "trace_config_1_default" { + grpc { + include_metadata = true + } + + http { } + + output { + metrics = [] + logs = [] + traces = [otelcol.processor.attributes.trace_config_1_default.input] + } +} + +otelcol.processor.attributes "trace_config_1_default" { + action { + key = "db.table" + action = "delete" + } + + output { + metrics = [] + logs = [] + traces = [otelcol.exporter.otlp.trace_config_1_default_0.input] + } +} + +otelcol.exporter.otlp "trace_config_1_default_0" { + sending_queue { + queue_size = 1000 + } + + retry_on_failure { + max_elapsed_time = "1m0s" + } + + client { + endpoint = "http://localhost:1234/write" + balancer_name = "" + } +} + +otelcol.receiver.otlp "trace_config_2_default" { + grpc { + include_metadata = true + } + + http { } + + output { + metrics = [] + logs = [] + traces = [otelcol.processor.attributes.trace_config_2_default.input] + } +} + +otelcol.processor.attributes "trace_config_2_default" { + action { + key = "redacted_span" + value = true + action = "upsert" + } + + output { + metrics = [] + logs = [] + traces = [otelcol.exporter.otlp.trace_config_2_default_0.input] + } +} + +otelcol.exporter.otlp "trace_config_2_default_0" { + sending_queue { + queue_size = 1000 + } + + retry_on_failure { + max_elapsed_time = "1m0s" + } + + client { + endpoint = "http://localhost:1234/write" + balancer_name = "" + } +} diff --git a/internal/converter/internal/staticconvert/testdata/traces_multi.yaml b/internal/converter/internal/staticconvert/testdata/traces_multi.yaml new file mode 100644 index 000000000000..4135c94216fd --- /dev/null +++ b/internal/converter/internal/staticconvert/testdata/traces_multi.yaml @@ -0,0 +1,27 @@ +traces: + configs: + - name: trace_config_1 + receivers: + otlp: + protocols: + grpc: + http: + remote_write: + - endpoint: http://localhost:1234/write + attributes: + actions: + - key: db.table + action: delete + - name: trace_config_2 + receivers: + otlp: + protocols: + grpc: + http: + remote_write: + - endpoint: http://localhost:1234/write + attributes: + actions: + - key: redacted_span + value: true + action: upsert \ No newline at end of file