Skip to content

Commit

Permalink
create logger & load cfg in main, fix ci, add chglog
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaasman00 committed Sep 27, 2024
1 parent 4aa3ba6 commit 83eed76
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 16 deletions.
27 changes: 27 additions & 0 deletions .chloggen/configurable-supervisor-logging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: opampsupervisor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add configurable logging for the supervisor.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [35466]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
6 changes: 3 additions & 3 deletions cmd/opampsupervisor/e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build e2e

package main

import (
Expand Down Expand Up @@ -154,7 +152,9 @@ func newUnstartedOpAMPServer(t *testing.T, connectingCallback onConnectingFuncFa

func newSupervisor(t *testing.T, configType string, extraConfigData map[string]string) *supervisor.Supervisor {
cfgFile := getSupervisorConfig(t, configType, extraConfigData)
s, err := supervisor.NewSupervisor(zap.NewNop(), cfgFile.Name())
cfg, err := config.LoadConfig(cfgFile.Name())
require.NoError(t, err)
s, err := supervisor.NewSupervisor(zap.NewNop(), cfg)
require.NoError(t, err)

return s
Expand Down
14 changes: 13 additions & 1 deletion cmd/opampsupervisor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,25 @@ import (
"os/signal"

"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()

supervisor, err := supervisor.NewSupervisor(*configFlag)
cfg, err := config.LoadConfig(*configFlag)
if err != nil {
log.Fatal("failed to load config: %w", err)
}

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 {
log.Fatal("failed to create new supervisor: %w", err)
}
Expand Down
25 changes: 25 additions & 0 deletions cmd/opampsupervisor/supervisor/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"runtime"
"time"

"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
"github.com/open-telemetry/opamp-go/protobufs"
"go.opentelemetry.io/collector/config/configtls"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -201,3 +204,25 @@ func DefaultSupervisor() Supervisor {
},
}
}

func LoadConfig(configFile string) (Supervisor, error) {
if configFile == "" {
return 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 Supervisor{}, err
}

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

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

return cfg, nil
}
15 changes: 3 additions & 12 deletions cmd/opampsupervisor/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ 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 @@ -137,8 +136,10 @@ type Supervisor struct {
opampServerPort int
}

func NewSupervisor(configFile string) (*Supervisor, error) {
func NewSupervisor(logger *zap.Logger, cfg config.Supervisor) (*Supervisor, error) {
s := &Supervisor{
config: cfg,
logger: logger,
pidProvider: defaultPIDProvider{},
hasNewConfig: make(chan struct{}, 1),
agentConfigOwnMetricsSection: &atomic.Value{},
Expand All @@ -153,10 +154,6 @@ func NewSupervisor(configFile string) (*Supervisor, error) {
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)
}
Expand All @@ -165,12 +162,6 @@ func NewSupervisor(configFile string) (*Supervisor, error) {
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
3 changes: 3 additions & 0 deletions cmd/opampsupervisor/supervisor/telemetry/logger.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package telemetry

import (
Expand Down

0 comments on commit 83eed76

Please sign in to comment.