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 1 commit
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
2 changes: 1 addition & 1 deletion api/grpc/mpi/v1/command.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/grpc/mpi/v1/common.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/grpc/mpi/v1/files.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

533 changes: 511 additions & 22 deletions go.mod

Large diffs are not rendered by default.

2,918 changes: 2,871 additions & 47 deletions go.sum

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions internal/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/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/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/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
os.Setenv("OPENTELEMETRY_COLLECTOR_CONFIG_FILE", "/path/to/config.yaml")
oliveromahony marked this conversation as resolved.
Show resolved Hide resolved
defer os.Unsetenv("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