Skip to content

Commit

Permalink
Adds flag to enable status logger (#5)
Browse files Browse the repository at this point in the history
* Adds flag to enable status logger
  • Loading branch information
nickschuch authored Feb 2, 2025
1 parent 2a4cb1c commit 9007960
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
6 changes: 4 additions & 2 deletions cmd/skpr-fpm-metrics-adapter-sidecar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
type Options struct {
ServerConfig sidecar.ServerConfig
LogLevel string
StatusLogger sidecar.LogStatus
}

func main() {
Expand Down Expand Up @@ -62,7 +63,7 @@ func main() {

logger.Info("Booting sidecar")

server, err := sidecar.NewServer(logger, o.ServerConfig)
server, err := sidecar.NewServer(logger, o.ServerConfig, o.StatusLogger)
if err != nil {
return fmt.Errorf("failed to start server: %w", err)
}
Expand All @@ -83,7 +84,8 @@ func main() {
cmd.PersistentFlags().StringVar(&o.ServerConfig.Path, "path", env.String("SKPR_FPM_METRICS_ADAPTER_PATH", "/metrics"), "Path which our metrics endpoint will be served on")
cmd.PersistentFlags().StringVar(&o.ServerConfig.Endpoint, "endpoint", env.String("SKPR_FPM_METRICS_ADAPTER_ENDPOINT", "127.0.0.1:9000"), "Endpoint which we will poll for FPM status information")
cmd.PersistentFlags().DurationVar(&o.ServerConfig.EndpointPoll, "endpoint-poll", env.Duration("SKPR_FPM_METRICS_ADAPTER_ENDPOINT_POLL", 10*time.Second), "How frequently to poll the endpoint for status information")
cmd.PersistentFlags().DurationVar(&o.ServerConfig.LogFrequency, "log-frequency", env.Duration("SKPR_FPM_METRICS_ADAPTER_LOG_FREQUENCY", 30*time.Second), "How frequently to log status information for external systems")
cmd.PersistentFlags().BoolVar(&o.StatusLogger.Enabled, "log-enabled", env.Bool("SKPR_FPM_METRICS_ADAPTER_LOG_STATUS_ENABLED", false), "If the status logger is enabled")
cmd.PersistentFlags().DurationVar(&o.StatusLogger.Frequency, "log-frequency", env.Duration("SKPR_FPM_METRICS_ADAPTER_LOG_STATUS_FREQUENCY", 30*time.Second), "How frequently to log status information for external systems")

err := cmd.Execute()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/sidecar/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// The process which will continually log the current status.
func (s *Server) logStatus(ctx context.Context) error {
ticker := time.NewTicker(s.config.LogFrequency)
ticker := time.NewTicker(s.statusLogger.Frequency)

for {
select {
Expand Down
23 changes: 18 additions & 5 deletions internal/sidecar/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Server struct {
status fpm.Status
// Configuration used by the HTTP server.
config ServerConfig
// Configuration used by the status logger.
statusLogger LogStatus
}

// ServerConfig which is used by the HTTP server.
Expand All @@ -32,15 +34,22 @@ type ServerConfig struct {
Endpoint string
// EndpointPoll frequency for collecting FPM status information.
EndpointPoll time.Duration
// LogFrequency for how often a status is logged for external systems.
LogFrequency time.Duration
}

// LogStatus is used to configure the status logger.
type LogStatus struct {
// If the status logger is enabled.
Enabled bool
// How frequency to log the status.
Frequency time.Duration
}

// NewServer for collecting and responding with the latest FPM status.
func NewServer(logger *slog.Logger, config ServerConfig) (*Server, error) {
func NewServer(logger *slog.Logger, config ServerConfig, statusLogger LogStatus) (*Server, error) {
server := &Server{
logger: logger,
config: config,
logger: logger,
config: config,
statusLogger: statusLogger,
}

return server, nil
Expand Down Expand Up @@ -94,6 +103,10 @@ func (s *Server) Run(ctx context.Context) error {

// Logger for emmit metrics as a log event for external systems.
group.Go(func() error {
if !s.statusLogger.Enabled {
return nil
}

// We want to shutdown all other tasks if this logger exits.
defer cancel()

Expand Down

0 comments on commit 9007960

Please sign in to comment.