Skip to content

Commit

Permalink
create logger in NewSupervisor
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaasman00 committed Sep 27, 2024
1 parent a24c555 commit 4aa3ba6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 54 deletions.
54 changes: 3 additions & 51 deletions cmd/opampsupervisor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,30 @@
package main

import (
"errors"
"flag"
"fmt"
"log"
"os"
"os/signal"

"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/opampsupervisor/supervisor"
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/opampsupervisor/supervisor/config"
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/opampsupervisor/supervisor/telemetry"
)

func main() {
configFlag := flag.String("config", "", "Path to a supervisor configuration file")
flag.Parse()

// load & validate config
cfg, err := loadConfig(*configFlag)
supervisor, err := supervisor.NewSupervisor(*configFlag)
if err != nil {
log.Fatal("failed to load config: %w", err)
}
if err := cfg.Validate(); err != nil {
log.Fatal("failed to validate config: %w", err)
}

// create logger
logger, err := telemetry.NewLogger(cfg.Telemetry.Logs)
if err != nil {
log.Fatal("failed to create logger: %w", err)
}

supervisor, err := supervisor.NewSupervisor(logger, cfg)
if err != nil {
logger.Error(err.Error())
os.Exit(-1)
return
log.Fatal("failed to create new supervisor: %w", err)
}

err = supervisor.Start()
if err != nil {
logger.Error(err.Error())
os.Exit(-1)
return
log.Fatal("failed to start supervisor: %w", err)
}

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
<-interrupt
supervisor.Shutdown()
}

func loadConfig(configFile string) (config.Supervisor, error) {
if configFile == "" {
return config.Supervisor{}, errors.New("path to config file cannot be empty")
}

k := koanf.New("::")
if err := k.Load(file.Provider(configFile), yaml.Parser()); err != nil {
return config.Supervisor{}, err
}

decodeConf := koanf.UnmarshalConf{
Tag: "mapstructure",
}

cfg := config.DefaultSupervisor()
if err := k.UnmarshalWithConf("", &cfg, decodeConf); err != nil {
return config.Supervisor{}, fmt.Errorf("cannot parse %v: %w", configFile, err)
}

return cfg, nil
}
42 changes: 39 additions & 3 deletions cmd/opampsupervisor/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/google/uuid"
"github.com/knadh/koanf/maps"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/rawbytes"
"github.com/knadh/koanf/v2"
"github.com/open-telemetry/opamp-go/client"
Expand All @@ -39,6 +40,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/opampsupervisor/supervisor/commander"
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/opampsupervisor/supervisor/config"
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/opampsupervisor/supervisor/healthchecker"
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/opampsupervisor/supervisor/telemetry"
)

var (
Expand Down Expand Up @@ -135,10 +137,8 @@ type Supervisor struct {
opampServerPort int
}

func NewSupervisor(logger *zap.Logger, cfg config.Supervisor) (*Supervisor, error) {
func NewSupervisor(configFile string) (*Supervisor, error) {
s := &Supervisor{
config: cfg,
logger: logger,
pidProvider: defaultPIDProvider{},
hasNewConfig: make(chan struct{}, 1),
agentConfigOwnMetricsSection: &atomic.Value{},
Expand All @@ -153,10 +153,24 @@ func NewSupervisor(logger *zap.Logger, cfg config.Supervisor) (*Supervisor, erro
return nil, err
}

if err := s.loadConfig(configFile); err != nil {
return nil, fmt.Errorf("error loading config: %w", err)
}

if err := s.config.Validate(); err != nil {
return nil, fmt.Errorf("error validating config: %w", err)
}

if err := os.MkdirAll(s.config.Storage.Directory, 0700); err != nil {
return nil, fmt.Errorf("error creating storage dir: %w", err)
}

logger, err := telemetry.NewLogger(s.config.Telemetry.Logs)
if err != nil {
return nil, fmt.Errorf("error creating logger: %w", err)
}
s.logger = logger

return s, nil
}

Expand Down Expand Up @@ -240,6 +254,28 @@ func (s *Supervisor) createTemplates() error {
return nil
}

func (s *Supervisor) loadConfig(configFile string) error {
if configFile == "" {
return errors.New("path to config file cannot be empty")
}

k := koanf.New("::")
if err := k.Load(file.Provider(configFile), yaml.Parser()); err != nil {
return err
}

decodeConf := koanf.UnmarshalConf{
Tag: "mapstructure",
}

s.config = config.DefaultSupervisor()
if err := k.UnmarshalWithConf("", &s.config, decodeConf); err != nil {
return fmt.Errorf("cannot parse %v: %w", configFile, err)
}

return nil
}

// getBootstrapInfo obtains the Collector's agent description by
// starting a Collector with a specific config that only starts
// an OpAMP extension, obtains the agent description, then
Expand Down

0 comments on commit 4aa3ba6

Please sign in to comment.