From ad233e83dc764e0fccfbf22db9a7986353cbc811 Mon Sep 17 00:00:00 2001 From: Kristof Gyuracz Date: Wed, 17 Apr 2024 15:59:04 +0200 Subject: [PATCH] chore(otel_conf_gen): improve handling of exporter names Signed-off-by: Kristof Gyuracz --- .../controller/telemetry/otel_conf_gen.go | 20 ++++++------ internal/controller/telemetry/utils.go | 32 +++++++++++++++++++ 2 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 internal/controller/telemetry/utils.go diff --git a/internal/controller/telemetry/otel_conf_gen.go b/internal/controller/telemetry/otel_conf_gen.go index 7d4e1ea..367a066 100644 --- a/internal/controller/telemetry/otel_conf_gen.go +++ b/internal/controller/telemetry/otel_conf_gen.go @@ -244,7 +244,7 @@ func (cfgInput *OtelColConfigInput) generateOTLPExporters(ctx context.Context) m for _, output := range cfgInput.Outputs { if output.Spec.OTLP != nil { - name := fmt.Sprintf("otlp/%s_%s", output.Namespace, output.Name) + name := GetExporterNameForOtelOutput(output) otlpGrpcValuesMarshaled, err := yaml.Marshal(output.Spec.OTLP) if err != nil { logger.Error(errors.New("failed to compile config for output"), "failed to compile config for output %q", output.NamespacedName().String()) @@ -269,7 +269,7 @@ func (cfgInput *OtelColConfigInput) generateLokiExporters(ctx context.Context) m for _, output := range cfgInput.Outputs { if output.Spec.Loki != nil { - name := fmt.Sprintf("loki/%s_%s", output.Namespace, output.Name) + name := GetExporterNameForOtelOutput(output) lokiHTTPValuesMarshaled, err := yaml.Marshal(output.Spec.Loki) if err != nil { logger.Error(errors.New("failed to compile config for output"), "failed to compile config for output %q", output.NamespacedName().String()) @@ -505,17 +505,11 @@ func (cfgInput *OtelColConfigInput) generateProcessors() map[string]any { } func generateOutputExporterNameProcessor(output v1alpha1.OtelOutput) AttributesProcessor { - var exporterName string - if output.Spec.Loki != nil { - exporterName = fmt.Sprintf("loki/%s_%s", output.Namespace, output.Name) - } else if output.Spec.OTLP != nil { - exporterName = fmt.Sprintf("otlp/%s_%s", output.Namespace, output.Name) - } processor := AttributesProcessor{ Actions: []AttributesProcessorAction{{ Action: "insert", Key: "exporter", - Value: exporterName, + Value: GetExporterNameForOtelOutput(output), }}, } @@ -633,11 +627,15 @@ func (cfgInput *OtelColConfigInput) generateNamedPipelines() map[string]Pipeline output := cfgInput.Outputs[idx] if output.Spec.Loki != nil { - namedPipelines[outputPipelineName] = generatePipeline([]string{fmt.Sprintf("routing/subscription_%s_%s_outputs", subscription.Namespace, subscription.Name)}, []string{fmt.Sprintf("attributes/exporter_name_%s", output.Name), fmt.Sprintf("attributes/loki_exporter_%s", output.Name), fmt.Sprintf("resource/loki_exporter_%s", output.Name)}, []string{fmt.Sprintf("loki/%s_%s", output.Namespace, output.Name), outputCountConnectorName}) + namedPipelines[outputPipelineName] = generatePipeline([]string{fmt.Sprintf("routing/subscription_%s_%s_outputs", subscription.Namespace, subscription.Name)}, + []string{fmt.Sprintf("attributes/exporter_name_%s", output.Name), fmt.Sprintf("attributes/loki_exporter_%s", output.Name), fmt.Sprintf("resource/loki_exporter_%s", output.Name)}, + []string{GetExporterNameForOtelOutput(output), outputCountConnectorName}) } if output.Spec.OTLP != nil { - namedPipelines[outputPipelineName] = generatePipeline([]string{fmt.Sprintf("routing/subscription_%s_%s_outputs", subscription.Namespace, subscription.Name)}, []string{fmt.Sprintf("attributes/exporter_name_%s", output.Name)}, []string{fmt.Sprintf("otlp/%s_%s", output.Namespace, output.Name), outputCountConnectorName}) + namedPipelines[outputPipelineName] = generatePipeline([]string{fmt.Sprintf("routing/subscription_%s_%s_outputs", subscription.Namespace, subscription.Name)}, + []string{fmt.Sprintf("attributes/exporter_name_%s", output.Name)}, + []string{GetExporterNameForOtelOutput(output), outputCountConnectorName}) } } } diff --git a/internal/controller/telemetry/utils.go b/internal/controller/telemetry/utils.go new file mode 100644 index 0000000..01af845 --- /dev/null +++ b/internal/controller/telemetry/utils.go @@ -0,0 +1,32 @@ +// Copyright © 2024 Kube logging authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package telemetry + +import ( + "fmt" + + "github.com/kube-logging/telemetry-controller/api/telemetry/v1alpha1" +) + +func GetExporterNameForOtelOutput(output v1alpha1.OtelOutput) string { + var exporterName string + if output.Spec.Loki != nil { + exporterName = fmt.Sprintf("loki/%s_%s", output.Namespace, output.Name) + } else if output.Spec.OTLP != nil { + exporterName = fmt.Sprintf("otlp/%s_%s", output.Namespace, output.Name) + } + + return exporterName +}