diff --git a/internal/collector/otelcol.tmpl b/internal/collector/otelcol.tmpl index 821a34a656..de2f49d832 100644 --- a/internal/collector/otelcol.tmpl +++ b/internal/collector/otelcol.tmpl @@ -64,10 +64,9 @@ exporters: {{ if gt (len .TLS.Key) 0 -}}key_file: "{{- .TLS.Key -}}"{{- end }} {{- end }} {{- end }} -{{- range $index, $prometheusExporter := .Exporters.PrometheusExporters }} - prometheus/{{$index}}: - endpoint: "{{ .Server.Host -}}:{{- .Server.Port }}" - namespace: "nginx-agent" +{{- if ne .Exporters.PrometheusExporter nil }} + prometheus: + endpoint: "{{ .Exporters.PrometheusExporter.Server.Host -}}:{{- .Exporters.PrometheusExporter.Server.Port }}" {{- end }} {{- if ne .Exporters.Debug nil }} debug: @@ -117,8 +116,8 @@ service: {{- range $index, $otlpExporter := .Exporters.OtlpExporters }} - otlp/{{$index}} {{- end }} - {{- range $index, $prometheusExporter := .Exporters.PrometheusExporters }} - - prometheus/{{$index}} + {{- if ne .Exporters.PrometheusExporter nil }} + - prometheus {{- end }} {{- if ne .Exporters.Debug nil }} - debug diff --git a/internal/collector/settings_test.go b/internal/collector/settings_test.go index addd1c7c23..ef7fa3b7d8 100644 --- a/internal/collector/settings_test.go +++ b/internal/collector/settings_test.go @@ -53,16 +53,14 @@ func TestTemplateWrite(t *testing.T) { actualConfPath := filepath.Join("/tmp/", "nginx-agent-otelcol-test.yaml") cfg.Collector.ConfigPath = actualConfPath - cfg.Collector.Exporters.PrometheusExporters = append( - cfg.Collector.Exporters.PrometheusExporters, - config.PrometheusExporter{ - Server: &config.ServerConfig{ - Host: "localhost", - Port: 9876, - Type: 0, - }, - TLS: nil, - }) + cfg.Collector.Exporters.PrometheusExporter = &config.PrometheusExporter{ + Server: &config.ServerConfig{ + Host: "localhost", + Port: 9876, + Type: 0, + }, + TLS: nil, + } cfg.Collector.Exporters.Debug = &config.DebugExporter{} diff --git a/internal/config/types.go b/internal/config/types.go index 73287621a4..06738d5f00 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -82,9 +82,9 @@ type ( } Exporters struct { - OtlpExporters []OtlpExporter `yaml:"-" mapstructure:"otlp_exporters"` - Debug *DebugExporter `yaml:"-" mapstructure:"debug"` - PrometheusExporters []PrometheusExporter `yaml:"-" mapstructure:"prometheus_exporters"` + Debug *DebugExporter `yaml:"-" mapstructure:"debug"` + PrometheusExporter *PrometheusExporter `yaml:"-" mapstructure:"prometheus_exporter"` + OtlpExporters []OtlpExporter `yaml:"-" mapstructure:"otlp_exporters"` } OtlpExporter struct { diff --git a/test/config/collector/test-opentelemetry-collector-agent.yaml b/test/config/collector/test-opentelemetry-collector-agent.yaml index f5a23e23cd..cc75d7ff46 100644 --- a/test/config/collector/test-opentelemetry-collector-agent.yaml +++ b/test/config/collector/test-opentelemetry-collector-agent.yaml @@ -36,9 +36,8 @@ exporters: max_elapsed_time: 10m tls: insecure: true - prometheus/0: + prometheus: endpoint: "localhost:9876" - namespace: "nginx-agent" debug: verbosity: detailed sampling_initial: 5 @@ -66,5 +65,5 @@ service: - batch exporters: - otlp/0 - - prometheus/0 + - prometheus - debug