Skip to content

Commit

Permalink
feat: add new config command to print out all configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
obs-gh-mattcotter committed Oct 29, 2024
1 parent 7cd4fbf commit 8342ccd
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 27 deletions.
49 changes: 49 additions & 0 deletions internal/commands/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package config

import (
"context"
"fmt"
"os"

"github.com/observeinc/observe-agent/internal/commands/start"
logger "github.com/observeinc/observe-agent/internal/commands/util"
"github.com/observeinc/observe-agent/internal/root"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var configCmd = &cobra.Command{
Use: "config",
Short: "Prints the full configuration for this agent.",
Long: `This command prints all configuration for this agent including any additional
OTEL configuration.`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := logger.WithCtx(context.Background(), logger.GetNop())
configFilePaths, cleanup, err := start.SetupAndGetConfigFiles(ctx)
if err != nil {
return err
}
if cleanup != nil {
defer cleanup()
}
agentConfig := viper.ConfigFileUsed()
configFilePaths = append([]string{agentConfig}, configFilePaths...)
for _, filePath := range configFilePaths {
file, err := os.ReadFile(filePath)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading config file %s: %s", filePath, err.Error())
} else {
fmt.Printf("# ======== config file %s\n", filePath)
fmt.Println(string(file))
}
}
return nil
},
}

func init() {
root.RootCmd.AddCommand(configCmd)
}
17 changes: 12 additions & 5 deletions internal/commands/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"

logger "github.com/observeinc/observe-agent/internal/commands/util"
"github.com/observeinc/observe-agent/internal/config"
"github.com/observeinc/observe-agent/internal/connections"
"github.com/observeinc/observe-agent/internal/root"
"github.com/observeinc/observe-agent/observecol"
Expand All @@ -17,10 +16,9 @@ import (
collector "go.opentelemetry.io/collector/otelcol"
)

func SetupAndGenerateCollectorSettings() (*collector.CollectorSettings, func(), error) {
ctx := logger.WithCtx(context.Background(), logger.Get())
func SetupAndGetConfigFiles(ctx context.Context) ([]string, func(), error) {
// Set Env Vars from config
err := config.SetEnvVars()
err := connections.SetEnvVars()
if err != nil {
return nil, nil, err
}
Expand All @@ -29,7 +27,7 @@ func SetupAndGenerateCollectorSettings() (*collector.CollectorSettings, func(),
if err != nil {
return nil, nil, err
}
configFilePaths, overridePath, err := config.GetAllOtelConfigFilePaths(ctx, tmpDir)
configFilePaths, overridePath, err := connections.GetAllOtelConfigFilePaths(ctx, tmpDir)
cleanup := func() {
if overridePath != "" {
os.Remove(overridePath)
Expand All @@ -40,6 +38,15 @@ func SetupAndGenerateCollectorSettings() (*collector.CollectorSettings, func(),
cleanup()
return nil, nil, err
}
return configFilePaths, cleanup, nil
}

func SetupAndGenerateCollectorSettings() (*collector.CollectorSettings, func(), error) {
ctx := logger.WithCtx(context.Background(), logger.Get())
configFilePaths, cleanup, err := SetupAndGetConfigFiles(ctx)
if err != nil {
return nil, cleanup, err
}
// Generate collector settings with all config files
colSettings := observecol.GenerateCollectorSettings(configFilePaths)
return colSettings, cleanup, nil
Expand Down
4 changes: 4 additions & 0 deletions internal/commands/util/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func GetDev() *zap.Logger {
return logger
}

func GetNop() *zap.Logger {
return zap.NewNop()
}

func FromCtx(ctx context.Context) *zap.Logger {
if l, ok := ctx.Value(ctxKey{}).(*zap.Logger); ok {
return l
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package connections

import (
"context"
Expand All @@ -8,7 +8,7 @@ import (
"path/filepath"
"runtime"

"github.com/observeinc/observe-agent/internal/connections"
logger "github.com/observeinc/observe-agent/internal/commands/util"
"github.com/spf13/viper"
)

Expand All @@ -17,7 +17,7 @@ func GetAllOtelConfigFilePaths(ctx context.Context, tmpDir string) ([]string, st
configFilePaths := []string{filepath.Join(GetDefaultConfigFolder(), "otel-collector.yaml")}
var err error
// Get additional config paths based on connection configs
for _, conn := range connections.AllConnectionTypes {
for _, conn := range AllConnectionTypes {
if viper.IsSet(conn.Name) {
connectionPaths, err := conn.GetConfigFilePaths(ctx, tmpDir)
if err != nil {
Expand All @@ -39,7 +39,7 @@ func GetAllOtelConfigFilePaths(ctx context.Context, tmpDir string) ([]string, st
}
configFilePaths = append(configFilePaths, overridePath)
}
fmt.Println("Config file paths:", configFilePaths)
logger.FromCtx(ctx).Info(fmt.Sprint("Config file paths:", configFilePaths))
return configFilePaths, overridePath, nil
}

Expand Down
5 changes: 3 additions & 2 deletions internal/connections/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"text/template"

logger "github.com/observeinc/observe-agent/internal/commands/util"
"github.com/observeinc/observe-agent/internal/config"
"github.com/spf13/viper"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -97,7 +98,7 @@ func (c *ConnectionType) GetConfigFilePaths(ctx context.Context, tmpDir string)
}
switch c.Type {
case SelfMonitoringConnectionTypeName:
conf := &SelfMonitoringConfig{}
conf := &config.SelfMonitoringConfig{}
err := rawConnConfig.Unmarshal(conf)
if err != nil {
logger.FromCtx(ctx).Error("failed to unmarshal config", zap.String("connection", c.Name))
Expand All @@ -108,7 +109,7 @@ func (c *ConnectionType) GetConfigFilePaths(ctx context.Context, tmpDir string)
return nil, err
}
case HostMonitoringConnectionTypeName:
conf := &HostMonitoringConfig{}
conf := &config.HostMonitoringConfig{}
err := rawConnConfig.Unmarshal(conf)
if err != nil {
logger.FromCtx(ctx).Error("failed to unmarshal config", zap.String("connection", c.Name))
Expand Down
10 changes: 0 additions & 10 deletions internal/connections/hostmonitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@ package connections

var HostMonitoringConnectionTypeName = "host_monitoring"

type HostMonitoringConfig struct {
enabled bool
metrics struct {
enabled bool
}
logs struct {
enabled bool
}
}

var HostMonitoringConnectionType = MakeConnectionType(
"host_monitoring",
[]CollectorConfigFragment{
Expand Down
4 changes: 0 additions & 4 deletions internal/connections/selfmonitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package connections

var SelfMonitoringConnectionTypeName = "self_monitoring"

type SelfMonitoringConfig struct {
enabled bool
}

var SelfMonitoringConnectionType = MakeConnectionType(
"self_monitoring",
[]CollectorConfigFragment{
Expand Down
4 changes: 2 additions & 2 deletions internal/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"os"

"github.com/observeinc/observe-agent/internal/config"
"github.com/observeinc/observe-agent/internal/connections"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -47,7 +47,7 @@ func InitConfig() {
// Use config file from the flag.
viper.SetConfigFile(CfgFile)
} else {
viper.AddConfigPath(config.GetDefaultAgentPath())
viper.AddConfigPath(connections.GetDefaultAgentPath())
viper.SetConfigType("yaml")
viper.SetConfigName("observe-agent")
}
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
package main

import (
_ "github.com/observeinc/observe-agent/internal/commands/config"
_ "github.com/observeinc/observe-agent/internal/commands/diagnose"
_ "github.com/observeinc/observe-agent/internal/commands/initconfig"
_ "github.com/observeinc/observe-agent/internal/commands/start"
Expand Down

0 comments on commit 8342ccd

Please sign in to comment.