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 log file #806

Merged
merged 9 commits into from
Sep 10, 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

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

3 changes: 3 additions & 0 deletions internal/collector/otel_collector_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func TestCollector_New(t *testing.T) {

func TestCollector_InitAndClose(t *testing.T) {
conf := types.OTelConfig(t)
conf.Collector.Log.Path = ""

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

Expand Down Expand Up @@ -62,6 +64,7 @@ func TestCollector_Process(t *testing.T) {
defer nginxPlusMock.Close()

conf := types.OTelConfig(t)
conf.Collector.Log.Path = ""

tests := []struct {
name string
Expand Down
13 changes: 13 additions & 0 deletions internal/collector/otelcol.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ extensions:
{{- end }}

service:
telemetry:
logs:
level: {{ .Log.Level -}}
{{- if .Log.Path}}
output_paths: ["{{ .Log.Path -}}"]
{{- else }}
output_paths: ["stderr"]
{{- end }}
{{- if .Log.Path}}
error_output_paths: ["{{ .Log.Path -}}"]
{{- else }}
error_output_paths: ["stderr"]
{{- end }}
extensions:
- health_check
pipelines:
Expand Down
21 changes: 20 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func registerFlags() {
"info",
`The desired verbosity level for logging messages from nginx-agent.
Available options, in order of severity from highest to lowest, are:
panic, fatal, error, info, debug, and trace.`,
panic, fatal, error, info and debug.`,
)
fs.String(
LogPathKey,
Expand Down Expand Up @@ -201,12 +201,28 @@ func registerFlags() {
DefInstanceHealthWatcherMonitoringFrequency,
"How often the NGINX Agent will check for instance health changes.",
)

fs.String(
CollectorConfigPathKey,
DefCollectorConfigPath,
"The path to the Opentelemetry Collector configuration file.",
)

fs.String(
CollectorLogLevelKey,
DefCollectorLogLevel,
`The desired verbosity level for logging messages from nginx-agent OTel collector.
Available options, in order of severity from highest to lowest, are:
ERROR, WARN, INFO and DEBUG.`,
)

fs.String(
CollectorLogPathKey,
DefCollectorLogPath,
`The path to output OTel collector log messages to.
If the default path doesn't exist, log messages are output to stdout/stderr.`,
)

fs.SetNormalizeFunc(normalizeFunc)

fs.VisitAll(func(flag *flag.Flag) {
Expand Down Expand Up @@ -305,6 +321,7 @@ func resolveCollector(allowedDirs []string) (*Collector, error) {
processors []Processor
receivers Receivers
healthCheck ServerConfig
log Log
)

err = errors.Join(
Expand All @@ -313,6 +330,7 @@ func resolveCollector(allowedDirs []string) (*Collector, error) {
resolveMapStructure(CollectorProcessorsKey, &processors),
resolveMapStructure(CollectorReceiversKey, &receivers),
resolveMapStructure(CollectorHealthKey, &healthCheck),
resolveMapStructure(CollectorLogKey, &log),
)
if err != nil {
return nil, fmt.Errorf("unmarshal collector config: %w", err)
Expand All @@ -324,6 +342,7 @@ func resolveCollector(allowedDirs []string) (*Collector, error) {
Processors: processors,
Receivers: receivers,
Health: &healthCheck,
Log: &log,
}

err = col.Validate(allowedDirs)
Expand Down
6 changes: 5 additions & 1 deletion internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@ func TestResolveCollector(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
viperInstance = viper.NewWithOptions(viper.KeyDelimiter(KeyDelimiter))
viperInstance.Set(CollectorRootKey, "set")
viperInstance.Set(CollectorConfigPathKey, test.expected.ConfigPath)
viperInstance.Set(CollectorReceiversKey, test.expected.Receivers)
viperInstance.Set(CollectorProcessorsKey, test.expected.Processors)
viperInstance.Set(CollectorExportersKey, test.expected.Exporters)
viperInstance.Set(CollectorHealthKey, test.expected.Health)
viperInstance.Set(CollectorLogKey, test.expected.Log)

actual, err := resolveCollector(testDefault.AllowedDirectories)
if test.shouldErr {
Expand Down Expand Up @@ -348,6 +348,10 @@ func getAgentConfig() *Config {
Port: 1337,
Type: 0,
},
Log: &Log{
Level: "INFO",
Path: "/var/log/nginx-agent/otelcol.log",
},
},
Command: &Command{
Server: &ServerConfig{
Expand Down
2 changes: 2 additions & 0 deletions internal/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const (
DefTreatErrorsAsWarnings = true

DefCollectorConfigPath = "/var/run/nginx-agent/otelcol.yaml"
DefCollectorLogLevel = "INFO"
DefCollectorLogPath = "/var/log/nginx-agent/otelcol.log"

DefCommandServerHostKey = ""
DefCommandServerPortKey = 0
Expand Down
3 changes: 3 additions & 0 deletions internal/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ var (
CollectorProcessorsKey = pre(CollectorRootKey) + "processors"
CollectorHealthKey = pre(CollectorRootKey) + "health"
CollectorReceiversKey = pre(CollectorRootKey) + "receivers"
CollectorLogKey = pre(CollectorRootKey) + "log"
CollectorLogLevelKey = pre(CollectorLogKey) + "level"
CollectorLogPathKey = pre(CollectorLogKey) + "path"
CommandAuthKey = pre(CommandRootKey) + "auth"
CommandAuthTokenKey = pre(CommandAuthKey) + "token"
CommandServerHostKey = pre(CommandServerKey) + "host"
Expand Down
1 change: 1 addition & 0 deletions internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type (

Collector struct {
ConfigPath string `yaml:"-" mapstructure:"config_path"`
Log *Log `yaml:"-" mapstructure:"log"`
Exporters []Exporter `yaml:"-" mapstructure:"exporters"`
Health *ServerConfig `yaml:"-" mapstructure:"health"`
Processors []Processor `yaml:"-" mapstructure:"processors"`
Expand Down
5 changes: 5 additions & 0 deletions test/config/collector/test-otelcol.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ extensions:
endpoint: "localhost:1337"

service:
telemetry:
logs:
level: INFO
output_paths: ["/var/log/nginx-agent/otelcol.log"]
error_output_paths: ["/var/log/nginx-agent/otelcol.log"]
extensions:
- health_check
pipelines:
Expand Down
4 changes: 4 additions & 0 deletions test/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func AgentConfig() *config.Config {
Port: randomPort3,
Type: 0,
},
Log: &config.Log{
Level: "INFO",
Path: "/var/log/nginx-agent/otelcol.log",
},
},
Command: &config.Command{
Server: &config.ServerConfig{
Expand Down
Loading