Skip to content

Commit

Permalink
changed fmt to logger
Browse files Browse the repository at this point in the history
  • Loading branch information
okankoAMZ committed Nov 12, 2024
1 parent afca216 commit c3a0db2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
3 changes: 1 addition & 2 deletions plugins/inputs/prometheus/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ func Start(configFilePath string, receiver storage.Appendable, shutDownChan chan
//Setup Target Allocator Scrape Post Process Handler
taManager.AttachReloadConfigHandler(
func(prometheusConfig *config.Config) {
level.Info(logger).Log("msg", "ReloadConfigHandler called")
relabelScrapeConfigs(prometheusConfig, logger)
},
)
Expand Down Expand Up @@ -343,7 +342,7 @@ func relabelScrapeConfigs(prometheusConfig *config.Config, logger log.Logger) {
},
}

level.Info(logger).Log("msg", "Add extra relabel_configs and metric_relabel_configs to save job, instance and __name__ before user relabel")
level.Debug(logger).Log("msg", "Add extra relabel_configs and metric_relabel_configs to save job, instance and __name__ before user relabel")

sc.RelabelConfigs = append(relabelConfigs, sc.RelabelConfigs...)
sc.MetricRelabelConfigs = append(metricNameRelabelConfigs, sc.MetricRelabelConfigs...)
Expand Down
33 changes: 20 additions & 13 deletions plugins/inputs/prometheus/target_allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ const DEFAULT_TLS_RELOAD_INTERVAL_SECONDS = 10 * time.Second

type TargetAllocatorManager struct {
enabled bool
manager *tamanager.Manager
config *otelpromreceiver.Config
host component.Host
sm *scrape.Manager
dm *discovery.Manager
shutdownCh chan struct{}
taReadyCh chan struct{}
reloadConfigHandler func(config *promconfig.Config)
manager *tamanager.Manager
config *otelpromreceiver.Config
sm *scrape.Manager
dm *discovery.Manager
logger log.Logger
}

func isPodNameAvailable() bool {
Expand Down Expand Up @@ -76,10 +77,10 @@ func loadConfigFromFilename(filename string) (*otelpromreceiver.Config, error) {
}

// Adapter from go-kit/log to zap.Logger
func createLogger(level *promlog.AllowedLevel) *zap.Logger {
func createLogger(level *promlog.AllowedLevel) (*zap.Logger, error) {
zapLevel, err := zapcore.ParseLevel(level.String())
if err != nil {
fmt.Printf("Error parsing level: %v. Defaulting to info.", err)
err = fmt.Errorf("Error parsing level: %v. Defaulting to info.", err)
zapLevel = zapcore.InfoLevel
}
// Create a base zap logger (you can customize it as needed)
Expand All @@ -90,7 +91,7 @@ func createLogger(level *promlog.AllowedLevel) *zap.Logger {
)
// Create the zap logger
zapLogger := zap.New(zapCore)
return zapLogger
return zapLogger, err
}

func createTargetAllocatorManager(filename string, logger log.Logger, logLevel *promlog.AllowedLevel, sm *scrape.Manager, dm *discovery.Manager) *TargetAllocatorManager {
Expand All @@ -104,6 +105,7 @@ func createTargetAllocatorManager(filename string, logger log.Logger, logLevel *
shutdownCh: make(chan struct{}, 1),
taReadyCh: make(chan struct{}, 1),
reloadConfigHandler: nil,
logger: logger,
}
err := tam.loadConfig(filename)
if err != nil {
Expand All @@ -120,10 +122,14 @@ func createTargetAllocatorManager(filename string, logger log.Logger, logLevel *
return &tam
}
func (tam *TargetAllocatorManager) loadManager(logLevel *promlog.AllowedLevel) {
logger, err := createLogger(logLevel)
if err != nil {
level.Error(tam.logger).Log("msg", "Error creating logger", "err", err)
}
receiverSettings := receiver.Settings{
ID: component.MustNewID(strings.ReplaceAll(tam.config.TargetAllocator.CollectorID, "-", "_")),
TelemetrySettings: component.TelemetrySettings{
Logger: createLogger(logLevel),
Logger: logger,
TracerProvider: nil,
MeterProvider: nil,
MetricsLevel: 0,
Expand Down Expand Up @@ -155,7 +161,7 @@ func (tam *TargetAllocatorManager) Run() error {
}
err = tam.reloadConfigTicker()
if err != nil {
close(tam.shutdownCh)
tam.manager.Shutdown()
return err
}
// go ahead and let dependencies know TA is ready
Expand All @@ -172,13 +178,14 @@ func (tam *TargetAllocatorManager) AttachReloadConfigHandler(handler func(config
tam.reloadConfigHandler = handler
}
func (tam *TargetAllocatorManager) reloadConfigTicker() error {

if tam.config.TargetAllocator == nil {
return fmt.Errorf("target Allocator is not configured properly")
return level.Error(tam.logger).Log("msg", "target Allocator is not configured properly")
}
if tam.reloadConfigHandler == nil {
return fmt.Errorf("target allocator reload config handler is not configured properly")
return level.Error(tam.logger).Log("msg", "target allocator reload config handler is not configured properly")
}
fmt.Printf("Starting Target Allocator Reload Config Ticker with %fs interval\n", tam.config.TargetAllocator.Interval.Seconds())
level.Info(tam.logger).Log("msg", "Starting Target Allocator Reload Config Ticker", "interval", tam.config.TargetAllocator.Interval.Seconds())
ticker := time.NewTicker(tam.config.TargetAllocator.Interval)
go func() {
for {
Expand All @@ -188,7 +195,7 @@ func (tam *TargetAllocatorManager) reloadConfigTicker() error {
case <-tam.shutdownCh:
ticker.Stop()
// Stop the ticker and exit when stop is signaled
fmt.Printf("Stopping Target Allocator Reload Config Ticker \n")
level.Info(tam.logger).Log("msg", "Stopping Target Allocator Reload Config Ticker")
return
}
}
Expand Down

0 comments on commit c3a0db2

Please sign in to comment.