Skip to content

Commit 2bcd1c1

Browse files
feat: add new config command to print out all configuration (#119)
### Description OB-37220 add a new config command to print out all configuration This naming matches the `datadog-agent config` command. Having this will facilitate debugging as we start to add templated data to our OTEL config fragments. ### Checklist - [ ] Created tests which fail without the change (if possible) - [ ] Extended the README / documentation, if necessary
1 parent 7cd4fbf commit 2bcd1c1

File tree

9 files changed

+75
-27
lines changed

9 files changed

+75
-27
lines changed

internal/commands/config/config.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package config
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"os"
10+
11+
"github.com/observeinc/observe-agent/internal/commands/start"
12+
logger "github.com/observeinc/observe-agent/internal/commands/util"
13+
"github.com/observeinc/observe-agent/internal/root"
14+
"github.com/spf13/cobra"
15+
"github.com/spf13/viper"
16+
)
17+
18+
var configCmd = &cobra.Command{
19+
Use: "config",
20+
Short: "Prints the full configuration for this agent.",
21+
Long: `This command prints all configuration for this agent including any additional
22+
OTEL configuration.`,
23+
RunE: func(cmd *cobra.Command, args []string) error {
24+
ctx := logger.WithCtx(context.Background(), logger.GetNop())
25+
configFilePaths, cleanup, err := start.SetupAndGetConfigFiles(ctx)
26+
if err != nil {
27+
return err
28+
}
29+
if cleanup != nil {
30+
defer cleanup()
31+
}
32+
agentConfig := viper.ConfigFileUsed()
33+
configFilePaths = append([]string{agentConfig}, configFilePaths...)
34+
for _, filePath := range configFilePaths {
35+
file, err := os.ReadFile(filePath)
36+
if err != nil {
37+
fmt.Fprintf(os.Stderr, "error reading config file %s: %s", filePath, err.Error())
38+
} else {
39+
fmt.Printf("# ======== config file %s\n", filePath)
40+
fmt.Println(string(file))
41+
}
42+
}
43+
return nil
44+
},
45+
}
46+
47+
func init() {
48+
root.RootCmd.AddCommand(configCmd)
49+
}

internal/commands/start/start.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os"
99

1010
logger "github.com/observeinc/observe-agent/internal/commands/util"
11-
"github.com/observeinc/observe-agent/internal/config"
1211
"github.com/observeinc/observe-agent/internal/connections"
1312
"github.com/observeinc/observe-agent/internal/root"
1413
"github.com/observeinc/observe-agent/observecol"
@@ -17,10 +16,9 @@ import (
1716
collector "go.opentelemetry.io/collector/otelcol"
1817
)
1918

20-
func SetupAndGenerateCollectorSettings() (*collector.CollectorSettings, func(), error) {
21-
ctx := logger.WithCtx(context.Background(), logger.Get())
19+
func SetupAndGetConfigFiles(ctx context.Context) ([]string, func(), error) {
2220
// Set Env Vars from config
23-
err := config.SetEnvVars()
21+
err := connections.SetEnvVars()
2422
if err != nil {
2523
return nil, nil, err
2624
}
@@ -29,7 +27,7 @@ func SetupAndGenerateCollectorSettings() (*collector.CollectorSettings, func(),
2927
if err != nil {
3028
return nil, nil, err
3129
}
32-
configFilePaths, overridePath, err := config.GetAllOtelConfigFilePaths(ctx, tmpDir)
30+
configFilePaths, overridePath, err := connections.GetAllOtelConfigFilePaths(ctx, tmpDir)
3331
cleanup := func() {
3432
if overridePath != "" {
3533
os.Remove(overridePath)
@@ -40,6 +38,15 @@ func SetupAndGenerateCollectorSettings() (*collector.CollectorSettings, func(),
4038
cleanup()
4139
return nil, nil, err
4240
}
41+
return configFilePaths, cleanup, nil
42+
}
43+
44+
func SetupAndGenerateCollectorSettings() (*collector.CollectorSettings, func(), error) {
45+
ctx := logger.WithCtx(context.Background(), logger.Get())
46+
configFilePaths, cleanup, err := SetupAndGetConfigFiles(ctx)
47+
if err != nil {
48+
return nil, cleanup, err
49+
}
4350
// Generate collector settings with all config files
4451
colSettings := observecol.GenerateCollectorSettings(configFilePaths)
4552
return colSettings, cleanup, nil

internal/commands/util/logger.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ func GetDev() *zap.Logger {
1818
return logger
1919
}
2020

21+
func GetNop() *zap.Logger {
22+
return zap.NewNop()
23+
}
24+
2125
func FromCtx(ctx context.Context) *zap.Logger {
2226
if l, ok := ctx.Value(ctxKey{}).(*zap.Logger); ok {
2327
return l

internal/config/confighandler.go renamed to internal/connections/confighandler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package config
1+
package connections
22

33
import (
44
"context"
@@ -8,7 +8,7 @@ import (
88
"path/filepath"
99
"runtime"
1010

11-
"github.com/observeinc/observe-agent/internal/connections"
11+
logger "github.com/observeinc/observe-agent/internal/commands/util"
1212
"github.com/spf13/viper"
1313
)
1414

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

internal/connections/connections.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"text/template"
1010

1111
logger "github.com/observeinc/observe-agent/internal/commands/util"
12+
"github.com/observeinc/observe-agent/internal/config"
1213
"github.com/spf13/viper"
1314
"go.uber.org/zap"
1415
)
@@ -97,7 +98,7 @@ func (c *ConnectionType) GetConfigFilePaths(ctx context.Context, tmpDir string)
9798
}
9899
switch c.Type {
99100
case SelfMonitoringConnectionTypeName:
100-
conf := &SelfMonitoringConfig{}
101+
conf := &config.SelfMonitoringConfig{}
101102
err := rawConnConfig.Unmarshal(conf)
102103
if err != nil {
103104
logger.FromCtx(ctx).Error("failed to unmarshal config", zap.String("connection", c.Name))
@@ -108,7 +109,7 @@ func (c *ConnectionType) GetConfigFilePaths(ctx context.Context, tmpDir string)
108109
return nil, err
109110
}
110111
case HostMonitoringConnectionTypeName:
111-
conf := &HostMonitoringConfig{}
112+
conf := &config.HostMonitoringConfig{}
112113
err := rawConnConfig.Unmarshal(conf)
113114
if err != nil {
114115
logger.FromCtx(ctx).Error("failed to unmarshal config", zap.String("connection", c.Name))

internal/connections/hostmonitoring.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@ package connections
22

33
var HostMonitoringConnectionTypeName = "host_monitoring"
44

5-
type HostMonitoringConfig struct {
6-
enabled bool
7-
metrics struct {
8-
enabled bool
9-
}
10-
logs struct {
11-
enabled bool
12-
}
13-
}
14-
155
var HostMonitoringConnectionType = MakeConnectionType(
166
"host_monitoring",
177
[]CollectorConfigFragment{

internal/connections/selfmonitoring.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ package connections
22

33
var SelfMonitoringConnectionTypeName = "self_monitoring"
44

5-
type SelfMonitoringConfig struct {
6-
enabled bool
7-
}
8-
95
var SelfMonitoringConnectionType = MakeConnectionType(
106
"self_monitoring",
117
[]CollectorConfigFragment{

internal/root/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"fmt"
88
"os"
99

10-
"github.com/observeinc/observe-agent/internal/config"
10+
"github.com/observeinc/observe-agent/internal/connections"
1111
"github.com/spf13/cobra"
1212
"github.com/spf13/viper"
1313
)
@@ -47,7 +47,7 @@ func InitConfig() {
4747
// Use config file from the flag.
4848
viper.SetConfigFile(CfgFile)
4949
} else {
50-
viper.AddConfigPath(config.GetDefaultAgentPath())
50+
viper.AddConfigPath(connections.GetDefaultAgentPath())
5151
viper.SetConfigType("yaml")
5252
viper.SetConfigName("observe-agent")
5353
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
44
package main
55

66
import (
7+
_ "github.com/observeinc/observe-agent/internal/commands/config"
78
_ "github.com/observeinc/observe-agent/internal/commands/diagnose"
89
_ "github.com/observeinc/observe-agent/internal/commands/initconfig"
910
_ "github.com/observeinc/observe-agent/internal/commands/start"

0 commit comments

Comments
 (0)