7
7
"context"
8
8
"fmt"
9
9
"os"
10
+ "strings"
10
11
11
12
"github.com/go-viper/mapstructure/v2"
12
13
"github.com/observeinc/observe-agent/internal/commands/start"
@@ -25,67 +26,68 @@ var configCmd = &cobra.Command{
25
26
Use : "config" ,
26
27
Short : "Prints the full configuration for this agent." ,
27
28
Long : `This command prints all configuration for this agent including any additional
28
- OTEL configuration.` ,
29
+ bundled OTel configuration.` ,
29
30
RunE : func (cmd * cobra.Command , args []string ) error {
30
- ctx := logger .WithCtx (context .Background (), logger .GetNop ())
31
- configFilePaths , cleanup , err := start .SetupAndGetConfigFiles (ctx )
32
- if err != nil {
33
- return err
34
- }
35
- if cleanup != nil {
36
- defer cleanup ()
37
- }
38
- agentConfig , err := config .AgentConfigFromViper (viper .GetViper ())
31
+ detailedOtel , err := cmd .Flags ().GetBool ("render-otel-details" )
39
32
if err != nil {
40
33
return err
41
34
}
42
- agentConfigYaml , err := yaml . Marshal ( agentConfig )
35
+ singleOtel , err := cmd . Flags (). GetBool ( "render-otel" )
43
36
if err != nil {
44
37
return err
45
38
}
46
- fmt .Printf ("# ======== computed agent config\n " )
47
- fmt .Println (string (agentConfigYaml ) + "\n " )
48
- agentConfigFile := viper .ConfigFileUsed ()
49
- if agentConfigFile != "" {
50
- configFilePaths = append ([]string {agentConfigFile }, configFilePaths ... )
39
+ if singleOtel && detailedOtel {
40
+ return fmt .Errorf ("cannot specify both --render-otel and --render-otel-details" )
51
41
}
52
- for _ , filePath := range configFilePaths {
53
- file , err := os .ReadFile (filePath )
54
- if err != nil {
55
- fmt .Fprintf (os .Stderr , "error reading config file %s: %s" , filePath , err .Error ())
56
- } else {
57
- fmt .Printf ("# ======== config file %s\n " , filePath )
58
- fmt .Println (string (file ))
59
- }
60
- }
61
- return nil
62
- },
63
- }
64
42
65
- var otelConfigSubCmd = & cobra.Command {
66
- Use : "export-otel" ,
67
- Short : "Prints a single otel config file containing the full configuration that would run." ,
68
- Long : `This command prints a single otel config file containing all bundled configuration.
69
- Features that are enabled or disabled in the observe-agent config will be reflected in the output accordingly.` ,
70
- RunE : func (cmd * cobra.Command , args []string ) error {
71
- configFilePaths , cleanup , err := start .SetupAndGetConfigFiles (logger .WithCtx (context .Background (), logger .GetNop ()))
43
+ ctx := logger .WithCtx (context .Background (), logger .GetNop ())
44
+ configFilePaths , cleanup , err := start .SetupAndGetConfigFiles (ctx )
72
45
if cleanup != nil {
73
46
defer cleanup ()
74
47
}
75
48
if err != nil {
76
49
return err
77
50
}
78
- fullVersion , err := cmd .Flags ().GetBool ("full" )
79
- if err != nil {
80
- return err
81
- }
82
- if fullVersion {
51
+ if singleOtel {
52
+ return printShortOtelConfig (ctx , configFilePaths )
53
+ } else if detailedOtel {
83
54
return printFullOtelConfig (configFilePaths )
84
55
}
85
- return printShortOtelConfig ( cmd . Context (), configFilePaths )
56
+ return printAllConfigsIndividually ( configFilePaths )
86
57
},
87
58
}
88
59
60
+ func printAllConfigsIndividually (configFilePaths []string ) error {
61
+ printConfig := func (comment string , data []byte ) {
62
+ fmt .Printf ("# ======== %s\n " , comment )
63
+ fmt .Println (strings .Trim (string (data ), "\n \t " ))
64
+ fmt .Println ("---" )
65
+ }
66
+
67
+ agentConfig , err := config .AgentConfigFromViper (viper .GetViper ())
68
+ if err != nil {
69
+ return err
70
+ }
71
+ agentConfigYaml , err := yaml .Marshal (agentConfig )
72
+ if err != nil {
73
+ return err
74
+ }
75
+ printConfig ("computed agent config" , agentConfigYaml )
76
+ agentConfigFile := viper .ConfigFileUsed ()
77
+ if agentConfigFile != "" {
78
+ configFilePaths = append ([]string {agentConfigFile }, configFilePaths ... )
79
+ }
80
+ for _ , filePath := range configFilePaths {
81
+ file , err := os .ReadFile (filePath )
82
+ if err != nil {
83
+ fmt .Fprintf (os .Stderr , "error reading config file %s: %s" , filePath , err .Error ())
84
+ } else {
85
+ printConfig ("config file " + filePath , file )
86
+ }
87
+ }
88
+ return nil
89
+ }
90
+
89
91
func printShortOtelConfig (ctx context.Context , configFilePaths []string ) error {
90
92
settings := observecol .ConfigProviderSettings (configFilePaths )
91
93
resolver , err := confmap .NewResolver (settings .ResolverSettings )
@@ -132,7 +134,7 @@ func printFullOtelConfig(configFilePaths []string) error {
132
134
}
133
135
134
136
func init () {
135
- otelConfigSubCmd .Flags ().Bool ("full " , false , "Print the full resolved configuration including default values instead of the pre-processed configuration ." )
136
- configCmd .AddCommand ( otelConfigSubCmd )
137
+ configCmd .Flags ().Bool ("render-otel-details " , false , "Print the full resolved otel configuration including default values after the otel components perform their semantic processing ." )
138
+ configCmd .Flags (). Bool ( "render-otel" , false , "Print a single rendered otel configuration file. This file is equivalent to the bundled configuration enabled in the observe-agent config." )
137
139
root .RootCmd .AddCommand (configCmd )
138
140
}
0 commit comments