Skip to content

Commit

Permalink
Add default config for collector (#899)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhurley authored Oct 17, 2024
1 parent ea60800 commit 0ea4a5f
Show file tree
Hide file tree
Showing 15 changed files with 352 additions and 123 deletions.
8 changes: 8 additions & 0 deletions internal/collector/otel_collector_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func New(conf *config.Config) (*Collector, error) {
return &Collector{
config: conf,
service: oTelCollector,
stopped: true,
}, nil
}

Expand All @@ -73,6 +74,13 @@ func (oc *Collector) Init(ctx context.Context, mp bus.MessagePipeInterface) erro
var runCtx context.Context
runCtx, oc.cancel = context.WithCancel(ctx)

if !oc.config.AreReceiversConfigured() {
slog.InfoContext(runCtx, "No receivers configured for OTel Collector. "+
"Waiting to discover a receiver before starting OTel collector.")

return nil
}

err := writeCollectorConfig(oc.config.Collector)
if err != nil {
return fmt.Errorf("write OTel Collector config: %w", err)
Expand Down
29 changes: 27 additions & 2 deletions internal/collector/otel_collector_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
package collector

import (
"bytes"
"context"
"fmt"
"strings"
"testing"
"time"

"github.com/nginx/agent/v3/test/stub"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/otelcol"
Expand All @@ -29,6 +33,27 @@ func TestCollector_New(t *testing.T) {
require.NoError(t, err, "NewCollector should not return an error with valid config")
}

func TestCollector_Init(t *testing.T) {
conf := types.OTelConfig(t)
conf.Collector = &config.Collector{}

logBuf := &bytes.Buffer{}
stub.StubLoggerWith(logBuf)

collector, err := New(conf)
require.NoError(t, err, "NewCollector should not return an error with valid config")

initError := collector.Init(context.Background(), nil)
require.NoError(t, initError)

if s := logBuf.String(); !strings.Contains(s, "No receivers configured for OTel Collector. "+
"Waiting to discover a receiver before starting OTel collector.") {
t.Errorf("Unexpected log %s", s)
}

assert.True(t, collector.stopped)
}

func TestCollector_InitAndClose(t *testing.T) {
conf := types.OTelConfig(t)
conf.Collector.Log.Path = ""
Expand Down Expand Up @@ -83,7 +108,7 @@ func TestCollector_Process(t *testing.T) {
},
},
receivers: config.Receivers{
HostMetrics: config.HostMetrics{
HostMetrics: &config.HostMetrics{
CollectionInterval: time.Minute,
InitialDelay: time.Second,
Scrapers: &config.HostMetricsScrapers{
Expand Down Expand Up @@ -119,7 +144,7 @@ func TestCollector_Process(t *testing.T) {
},
},
receivers: config.Receivers{
HostMetrics: config.HostMetrics{
HostMetrics: &config.HostMetrics{
CollectionInterval: time.Minute,
InitialDelay: time.Second,
Scrapers: &config.HostMetricsScrapers{
Expand Down
4 changes: 2 additions & 2 deletions internal/collector/otelcol.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
receivers:
{{- if ne .Receivers.HostMetrics.CollectionInterval 0 }}
{{- if ne .Receivers.HostMetrics nil }}
hostmetrics:
collection_interval: {{ .Receivers.HostMetrics.CollectionInterval }}
initial_delay: {{ .Receivers.HostMetrics.InitialDelay }}
Expand Down Expand Up @@ -148,7 +148,7 @@ service:
pipelines:
metrics:
receivers:
{{- if ne .Receivers.HostMetrics.CollectionInterval 0 }}
{{- if ne .Receivers.HostMetrics nil }}
- hostmetrics
{{- end }}
{{- range $index, $otlpReceiver := .Receivers.OtlpReceivers }}
Expand Down
2 changes: 1 addition & 1 deletion internal/collector/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestTemplateWrite(t *testing.T) {

cfg.Collector.Exporters.Debug = &config.DebugExporter{}

cfg.Collector.Receivers.HostMetrics = config.HostMetrics{
cfg.Collector.Receivers.HostMetrics = &config.HostMetrics{
CollectionInterval: time.Minute,
InitialDelay: time.Second,
Scrapers: &config.HostMetricsScrapers{
Expand Down
Loading

0 comments on commit 0ea4a5f

Please sign in to comment.