Skip to content

feat: add config command flag --render-otel to print full otel config #184

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

Merged
merged 3 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/observeinc/observe-agent
go 1.23.7

require (
github.com/go-viper/mapstructure/v2 v2.2.1
github.com/jarcoal/httpmock v1.3.1
github.com/observeinc/observe-agent/observecol v0.0.0-00010101000000-000000000000
github.com/prometheus/client_model v0.6.1
Expand All @@ -11,6 +12,7 @@ require (
github.com/spf13/cobra v1.9.1
github.com/spf13/viper v1.20.0-alpha.6
github.com/stretchr/testify v1.10.0
go.opentelemetry.io/collector/confmap v1.27.0
go.opentelemetry.io/collector/otelcol v0.121.0
go.uber.org/zap v1.27.0
golang.org/x/sys v0.30.0
Expand Down Expand Up @@ -94,7 +96,6 @@ require (
github.com/go-openapi/jsonreference v0.20.4 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-resty/resty/v2 v2.13.1 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/go-zookeeper/zk v1.0.4 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/goccy/go-json v0.10.5 // indirect
Expand Down Expand Up @@ -314,7 +315,6 @@ require (
go.opentelemetry.io/collector/config/configretry v1.27.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.121.0 // indirect
go.opentelemetry.io/collector/config/configtls v1.27.0 // indirect
go.opentelemetry.io/collector/confmap v1.27.0 // indirect
go.opentelemetry.io/collector/confmap/provider/envprovider v1.27.0 // indirect
go.opentelemetry.io/collector/confmap/provider/fileprovider v1.27.0 // indirect
go.opentelemetry.io/collector/confmap/provider/httpprovider v1.27.0 // indirect
Expand Down
123 changes: 100 additions & 23 deletions internal/commands/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,134 @@ import (
"context"
"fmt"
"os"
"strings"

"github.com/go-viper/mapstructure/v2"
"github.com/observeinc/observe-agent/internal/commands/start"
logger "github.com/observeinc/observe-agent/internal/commands/util"
"github.com/observeinc/observe-agent/internal/config"
"github.com/observeinc/observe-agent/internal/root"
"github.com/observeinc/observe-agent/observecol"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/otelcol"
"gopkg.in/yaml.v3"
)

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.`,
bundled OTel configuration.`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := logger.WithCtx(context.Background(), logger.GetNop())
configFilePaths, cleanup, err := start.SetupAndGetConfigFiles(ctx)
detailedOtel, err := cmd.Flags().GetBool("render-otel-details")
if err != nil {
return err
}
if cleanup != nil {
defer cleanup()
}
agentConfig, err := config.AgentConfigFromViper(viper.GetViper())
singleOtel, err := cmd.Flags().GetBool("render-otel")
if err != nil {
return err
}
agentConfigYaml, err := yaml.Marshal(agentConfig)
if singleOtel && detailedOtel {
return fmt.Errorf("cannot specify both --render-otel and --render-otel-details")
}

ctx := logger.WithCtx(context.Background(), logger.GetNop())
configFilePaths, cleanup, err := start.SetupAndGetConfigFiles(ctx)
if cleanup != nil {
defer cleanup()
}
if err != nil {
return err
}
fmt.Printf("# ======== computed agent config\n")
fmt.Println(string(agentConfigYaml) + "\n")
agentConfigFile := viper.ConfigFileUsed()
if agentConfigFile != "" {
configFilePaths = append([]string{agentConfigFile}, 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))
}
if singleOtel {
return printShortOtelConfig(ctx, configFilePaths)
} else if detailedOtel {
return printFullOtelConfig(configFilePaths)
}
return nil
return printAllConfigsIndividually(configFilePaths)
},
}

func printAllConfigsIndividually(configFilePaths []string) error {
printConfig := func(comment string, data []byte) {
fmt.Printf("# ======== %s\n", comment)
fmt.Println(strings.Trim(string(data), "\n\t "))
fmt.Println("---")
}

agentConfig, err := config.AgentConfigFromViper(viper.GetViper())
if err != nil {
return err
}
agentConfigYaml, err := yaml.Marshal(agentConfig)
if err != nil {
return err
}
printConfig("computed agent config", agentConfigYaml)
agentConfigFile := viper.ConfigFileUsed()
if agentConfigFile != "" {
configFilePaths = append([]string{agentConfigFile}, 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 {
printConfig("config file "+filePath, file)
}
}
return nil
}

func printShortOtelConfig(ctx context.Context, configFilePaths []string) error {
settings := observecol.ConfigProviderSettings(configFilePaths)
resolver, err := confmap.NewResolver(settings.ResolverSettings)
if err != nil {
return fmt.Errorf("failed to create new resolver: %w", err)
}
conf, err := resolver.Resolve(ctx)
if err != nil {
return fmt.Errorf("error while resolving config: %w", err)
}
b, err := yaml.Marshal(conf.ToStringMap())
if err != nil {
return fmt.Errorf("error while marshaling to YAML: %w", err)
}
fmt.Printf("%s\n", b)
return nil
}

func printFullOtelConfig(configFilePaths []string) error {
colSettings := observecol.GenerateCollectorSettingsWithConfigFiles(configFilePaths)
factories, err := colSettings.Factories()
if err != nil {
return fmt.Errorf("failed to create component factory maps: %w", err)
}
provider, err := otelcol.NewConfigProvider(colSettings.ConfigProviderSettings)
if err != nil {
return fmt.Errorf("failed to create config provider: %w", err)
}
cfg, err := provider.Get(context.Background(), factories)
if err != nil {
return fmt.Errorf("failed to get config: %w", err)
}
var cfgMap map[string]any
err = mapstructure.Decode(cfg, &cfgMap)
if err != nil {
return fmt.Errorf("failed to marshall config to map: %w", err)
}
cfgYaml, err := yaml.Marshal(cfgMap)
if err != nil {
return fmt.Errorf("failed to marshall config to yaml: %w", err)
}
fmt.Printf("%s\n", cfgYaml)
return nil
}

func init() {
configCmd.Flags().Bool("render-otel-details", false, "Print the full resolved otel configuration including default values after the otel components perform their semantic processing.")
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.")
root.RootCmd.AddCommand(configCmd)
}
31 changes: 18 additions & 13 deletions observecol/otelcollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,31 @@ import (
"go.opentelemetry.io/collector/otelcol"
)

func ConfigProviderSettings(URIs []string) otelcol.ConfigProviderSettings {
return otelcol.ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: URIs,
ProviderFactories: []confmap.ProviderFactory{
fileprovider.NewFactory(),
envprovider.NewFactory(),
yamlprovider.NewFactory(),
httpprovider.NewFactory(),
httpsprovider.NewFactory(),
},
},
}
}

func GenerateCollectorSettings() *otelcol.CollectorSettings {
buildInfo := component.BuildInfo{
Command: "observe-agent",
Description: "Observe Distribution of Opentelemetry Collector",
Version: build.Version,
}
set := &otelcol.CollectorSettings{
BuildInfo: buildInfo,
Factories: components,
ConfigProviderSettings: otelcol.ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
ProviderFactories: []confmap.ProviderFactory{
fileprovider.NewFactory(),
envprovider.NewFactory(),
yamlprovider.NewFactory(),
httpprovider.NewFactory(),
httpsprovider.NewFactory(),
},
},
},
BuildInfo: buildInfo,
Factories: components,
ConfigProviderSettings: ConfigProviderSettings([]string{}),
}
return set
}
Expand Down
Loading