Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OTel collector on Agent Startup based on configuration #681

Merged
merged 8 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/agent/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ package main
import (
"testing"

"github.com/nginx/agent/v3/test/helpers"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
goleak.VerifyTestMain(m, helpers.GoLeakOptions...)
}
310 changes: 289 additions & 21 deletions go.mod

Large diffs are not rendered by default.

1,224 changes: 1,170 additions & 54 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (a *App) Run(ctx context.Context) error {
slog.InfoContext(ctx, "Starting NGINX Agent")

messagePipe := bus.NewMessagePipe(defaultMessagePipeChannelSize)
err = messagePipe.Register(defaultQueueSize, plugin.LoadPlugins(agentConfig, slogger))
err = messagePipe.Register(defaultQueueSize, plugin.LoadPlugins(agentConfig))
if err != nil {
slog.ErrorContext(ctx, "Failed to register plugins", "error", err)
return
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ func getMetrics() *Metrics {
ProduceInterval: viperInstance.GetDuration(MetricsProduceIntervalKey),
OTelExporter: nil,
PrometheusSource: nil,
Collector: viperInstance.GetBool(MetricsCollectorKey),
}

if viperInstance.IsSet(MetricsOTelExporterKey) && viperInstance.IsSet(OTelGRPCKey) {
Expand Down
1 change: 1 addition & 0 deletions internal/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
// child flags saved as vars to enable easier prefixing.
MetricsProduceIntervalKey = pre(MetricsRootKey) + "produce_interval"
MetricsOTelExporterKey = pre(MetricsRootKey) + OTelExporterRoot
MetricsCollectorKey = pre(MetricsRootKey) + "collector"
OTelExporterBufferLengthKey = pre(MetricsOTelExporterKey) + "buffer_length"
OTelExporterExportRetryCountKey = pre(MetricsOTelExporterKey) + "export_retry_count"
OTelExporterExportIntervalKey = pre(MetricsOTelExporterKey) + "export_interval"
Expand Down
2 changes: 2 additions & 0 deletions internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type Metrics struct {
ProduceInterval time.Duration `yaml:"-" mapstructure:"produce_interval"`
OTelExporter *OTelExporter `yaml:"-" mapstructure:"otel_exporter"`
PrometheusSource *PrometheusSource `yaml:"-" mapstructure:"prometheus_source"`
// temporary setting to enable the collector
Collector bool `yaml:"-" mapstructure:"collector"`
}

// PrometheusSource is a DataSources implementation
Expand Down
20 changes: 20 additions & 0 deletions internal/metrics/collector/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) F5, Inc.
//
// This source code is licensed under the Apache License, Version 2.0 license found in the
// LICENSE file in the root directory of this source tree.
package collector

import (
"github.com/nginx/agent/v3/internal/config"
"go.opentelemetry.io/collector/component"
)

// BuildInfo returns all the OTel collector BuildInfo supported
// based on https://github.com/DataDog/datadog-agent/blob/main/comp/otelcol/collector-contrib/impl/collectorcontrib.go
func BuildInfo(cfg *config.Config) component.BuildInfo {
return component.BuildInfo{
Version: cfg.Version,
Command: "otel-nginx-agent",
Description: "NGINX Agent OpenTelemetry Collector",
}
}
21 changes: 21 additions & 0 deletions internal/metrics/collector/build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) F5, Inc.
//
// This source code is licensed under the Apache License, Version 2.0 license found in the
// LICENSE file in the root directory of this source tree.
package collector

import (
"testing"

"github.com/nginx/agent/v3/test/types"
"github.com/stretchr/testify/assert"
)

func TestBuildInfo(t *testing.T) {
agentConfig := types.GetAgentConfig()
info := BuildInfo(agentConfig)

assert.Equal(t, agentConfig.Version, info.Version)
assert.NotEmpty(t, info.Description)
assert.NotNil(t, info.Command)
}
76 changes: 76 additions & 0 deletions internal/metrics/collector/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) F5, Inc.
//
// This source code is licensed under the Apache License, Version 2.0 license found in the
// LICENSE file in the root directory of this source tree.
package collector

import (
"log/slog"
"os"

"github.com/nginx/agent/v3/internal/config"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/converter/expandconverter"
"go.opentelemetry.io/collector/confmap/provider/envprovider"
"go.opentelemetry.io/collector/confmap/provider/fileprovider"
"go.opentelemetry.io/collector/confmap/provider/httpprovider"
"go.opentelemetry.io/collector/confmap/provider/httpsprovider"
"go.opentelemetry.io/collector/confmap/provider/yamlprovider"
"go.opentelemetry.io/collector/otelcol"
)

func OTelCollectorSettings(cfg *config.Config) otelcol.CollectorSettings {
return otelcol.CollectorSettings{
Factories: OTelComponentFactories,
BuildInfo: BuildInfo(cfg),
DisableGracefulShutdown: false,
ConfigProviderSettings: ConfigProviderSettings(cfg),
LoggingOptions: nil,
SkipSettingGRPCLogger: false,
}
}

// ConfigProviderSettings are the settings to configure the behavior of the ConfigProvider.
func ConfigProviderSettings(cfg *config.Config) otelcol.ConfigProviderSettings {
return otelcol.ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
ProviderFactories: createProviderFactories(),
ConverterFactories: createConverterFactories(),
URIs: createURIs(cfg),
},
}
}

func createProviderFactories() []confmap.ProviderFactory {
providerConfig := []confmap.ProviderFactory{
envprovider.NewFactory(),
fileprovider.NewFactory(),
httpprovider.NewFactory(),
httpsprovider.NewFactory(),
yamlprovider.NewFactory(),
}

return providerConfig
}

func createConverterFactories() []confmap.ConverterFactory {
converterConfig := []confmap.ConverterFactory{
expandconverter.NewFactory(),
}

return converterConfig
}

func createURIs(cfg *config.Config) []string {
return []string{getConfig(cfg)}
}

func getConfig(_ *config.Config) string {
val, ex := os.LookupEnv("OPENTELEMETRY_COLLECTOR_CONFIG_FILE")
if !ex {
return "/tmp/otel-collector-config.yaml"
}
slog.Info("Using config URI from environment")

return val
}
52 changes: 52 additions & 0 deletions internal/metrics/collector/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) F5, Inc.
//
// This source code is licensed under the Apache License, Version 2.0 license found in the
// LICENSE file in the root directory of this source tree.
package collector

import (
"os"
"testing"

"github.com/nginx/agent/v3/test/types"
"github.com/stretchr/testify/assert"
)

func TestOTelCollectorSettings(t *testing.T) {
cfg := types.GetAgentConfig()

settings := OTelCollectorSettings(cfg)

assert.NotNil(t, settings.Factories, "Factories should not be nil")
assert.Equal(t, "otel-nginx-agent", settings.BuildInfo.Command, "BuildInfo command should match")
assert.False(t, settings.DisableGracefulShutdown, "DisableGracefulShutdown should be false")
assert.NotNil(t, settings.ConfigProviderSettings, "ConfigProviderSettings should not be nil")
assert.Nil(t, settings.LoggingOptions, "LoggingOptions should be nil")
assert.False(t, settings.SkipSettingGRPCLogger, "SkipSettingGRPCLogger should be false")
}

func TestConfigProviderSettings(t *testing.T) {
cfg := types.GetAgentConfig()
settings := ConfigProviderSettings(cfg)

assert.NotNil(t, settings.ResolverSettings, "ResolverSettings should not be nil")
assert.Len(t, settings.ResolverSettings.ProviderFactories, 5, "There should be 5 provider factories")
assert.Len(t, settings.ResolverSettings.ConverterFactories, 1, "There should be 1 converter factory")
assert.NotEmpty(t, settings.ResolverSettings.URIs, "URIs should not be empty")
assert.Equal(t, "/tmp/otel-collector-config.yaml", settings.ResolverSettings.URIs[0], "Default URI should match")
}

func TestGetConfig(t *testing.T) {
// Test with environment variable set
t.Setenv("OPENTELEMETRY_COLLECTOR_CONFIG_FILE", "/path/to/config.yaml")
defer t.Setenv("OPENTELEMETRY_COLLECTOR_CONFIG_FILE", "")

configURI := getConfig(nil)
assert.Equal(t, "/path/to/config.yaml", configURI, "Config URI should match the environment variable")

// Test without environment variable set
os.Unsetenv("OPENTELEMETRY_COLLECTOR_CONFIG_FILE")

configURI = getConfig(nil)
assert.Equal(t, "/tmp/otel-collector-config.yaml", configURI, "Config URI should match the default value")
}
Loading
Loading