Skip to content

feat: add agent config options for internal telemetry and health check, enable use of status cmd in kubernetes #182

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 2 commits into from
Apr 15, 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: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ vendor:
build:
go build ./...

docker-image:
env GOOS=linux GOARCH=arm64 go build -o observe-agent
docker build -f packaging/docker/Dockerfile -t observe-agent:dev .

## test: Runs Go tests across all packages
go-test: build
go list -f '{{.Dir}}' -m | xargs go test -v ./...
Expand Down
100 changes: 5 additions & 95 deletions internal/commands/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,12 @@ package config
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/commands/util"
"github.com/observeinc/observe-agent/internal/commands/util/logger"
"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{
Expand Down Expand Up @@ -49,96 +41,14 @@ bundled OTel configuration.`,
return err
}
if singleOtel {
return printShortOtelConfig(ctx, configFilePaths)
return util.PrintShortOtelConfig(ctx, configFilePaths)
} else if detailedOtel {
return printFullOtelConfig(configFilePaths)
return util.PrintFullOtelConfig(configFilePaths)
}
return printAllConfigsIndividually(configFilePaths)
return util.PrintAllConfigsIndividually(configFilePaths)
},
}

func printAllConfigsIndividually(configFilePaths []string) error {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all code motion; I put these methods in util since the config package depends on the start package, and having these methods available in start for debugging is very useful.

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 {
if len(configFilePaths) == 0 {
return nil
}
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 {
if len(configFilePaths) == 0 {
return nil
}
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.")
Expand Down
9 changes: 7 additions & 2 deletions internal/commands/diagnose/agentstatuscheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"embed"

"github.com/observeinc/observe-agent/internal/commands/status"
"github.com/observeinc/observe-agent/internal/config"
"github.com/spf13/viper"
)

Expand All @@ -13,8 +14,12 @@ type StatusTestResult struct {
Error string
}

func checkStatus(_ *viper.Viper) (bool, any, error) {
data, err := status.GetStatusData()
func checkStatus(v *viper.Viper) (bool, any, error) {
conf, err := config.AgentConfigFromViper(v)
if err != nil {
return false, StatusTestResult{Error: err.Error()}, err
}
data, err := status.GetStatusData(conf)
if err != nil {
return false, StatusTestResult{
Passed: false,
Expand Down
72 changes: 37 additions & 35 deletions internal/commands/diagnose/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,48 @@ var diagnostics = []Diagnostic{
authDiagnostic(),
}

// diagnoseCmd represents the diagnose command
var diagnoseCmd = &cobra.Command{
Use: "diagnose",
Short: "Run diagnostic checks.",
Long: `This command runs diagnostic checks for various settings and configurations
func NewDiagnoseCmd(v *viper.Viper) *cobra.Command {
return &cobra.Command{
Use: "diagnose",
Short: "Run diagnostic checks.",
Long: `This command runs diagnostic checks for various settings and configurations
to attempt to identify issues that could cause the agent to function improperly.`,
Run: func(cmd *cobra.Command, args []string) {
v := viper.GetViper()
fmt.Print("Running diagnosis checks...")
var failedChecks []string
for _, diagnostic := range diagnostics {
fmt.Printf("\n\n\n%s\n==================\n", diagnostic.checkName)
success, data, err := diagnostic.check(v)
if !success {
failedChecks = append(failedChecks, diagnostic.checkName)
Run: func(cmd *cobra.Command, args []string) {
fmt.Print("Running diagnosis checks...")
var failedChecks []string
for _, diagnostic := range diagnostics {
fmt.Printf("\n\n\n%s\n==================\n", diagnostic.checkName)
success, data, err := diagnostic.check(v)
if !success {
failedChecks = append(failedChecks, diagnostic.checkName)
}
if err != nil {
fmt.Printf("⚠️ Failed to run check: %s\n", err.Error())
continue
}
t := template.Must(template.
New(diagnostic.templateName).
ParseFS(diagnostic.templateFS, diagnostic.templateName))
if err := t.ExecuteTemplate(os.Stdout, diagnostic.templateName, data); err != nil {
fmt.Printf("⚠️ Failed to print output for check: %s\n", err.Error())
continue
}
}
if err != nil {
fmt.Printf("⚠️ Failed to run check: %s\n", err.Error())
continue
if len(failedChecks) > 0 {
fmt.Printf("\n\n\n❌ %d out of %d checks failed:\n", len(failedChecks), len(diagnostics))
for _, check := range failedChecks {
fmt.Printf(" - %s\n", check)
}
os.Exit(1)
} else {
fmt.Printf("\n✅ All %d checks passed!\n", len(diagnostics))
}
t := template.Must(template.
New(diagnostic.templateName).
ParseFS(diagnostic.templateFS, diagnostic.templateName))
if err := t.ExecuteTemplate(os.Stdout, diagnostic.templateName, data); err != nil {
fmt.Printf("⚠️ Failed to print output for check: %s\n", err.Error())
continue
}
}
if len(failedChecks) > 0 {
fmt.Printf("\n\n\n❌ %d out of %d checks failed:\n", len(failedChecks), len(diagnostics))
for _, check := range failedChecks {
fmt.Printf(" - %s\n", check)
}
os.Exit(1)
} else {
fmt.Printf("\n✅ All %d checks passed!\n", len(diagnostics))
}
},
},
}
}

func init() {
v := viper.GetViper()
diagnoseCmd := NewDiagnoseCmd(v)
root.RootCmd.AddCommand(diagnoseCmd)
}
2 changes: 1 addition & 1 deletion internal/commands/diagnose/otelconfigcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"embed"

"github.com/observeinc/observe-agent/internal/commands/start"
logger "github.com/observeinc/observe-agent/internal/commands/util"
"github.com/observeinc/observe-agent/internal/commands/util/logger"
"github.com/observeinc/observe-agent/observecol"
"github.com/spf13/viper"
"go.opentelemetry.io/collector/otelcol"
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os"
"strings"

logger "github.com/observeinc/observe-agent/internal/commands/util"
"github.com/observeinc/observe-agent/internal/commands/util/logger"
"github.com/observeinc/observe-agent/internal/connections"
"github.com/observeinc/observe-agent/internal/root"
"github.com/observeinc/observe-agent/observecol"
Expand Down
29 changes: 19 additions & 10 deletions internal/commands/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"html/template"
"os"

"github.com/observeinc/observe-agent/internal/config"
"github.com/observeinc/observe-agent/internal/connections"
"github.com/observeinc/observe-agent/internal/root"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

const statusTemplate = "status.tmpl"
Expand All @@ -20,22 +22,29 @@ var (
statusTemplateFS embed.FS
)

// statusCmd represents the status command
var statusCmd = &cobra.Command{
Use: "status",
Short: "Display status of agent",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
getStatusFromTemplate()
},
func NewStatusCmd(v *viper.Viper) *cobra.Command {
return &cobra.Command{
Use: "status",
Short: "Display status of agent",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
getStatusFromTemplate(v)
},
}
}

func init() {
v := viper.GetViper()
statusCmd := NewStatusCmd(v)
root.RootCmd.AddCommand(statusCmd)
}

func getStatusFromTemplate() error {
data, err := GetStatusData()
func getStatusFromTemplate(v *viper.Viper) error {
conf, err := config.AgentConfigFromViper(v)
if err != nil {
return err
}
data, err := GetStatusData(conf)
if err != nil {
return err
}
Expand Down
Loading
Loading