cmd: add a diode to avoid log blocking

This commit is contained in:
Vincent Bernat
2025-09-12 14:37:46 +02:00
parent cb6b2bd229
commit 7caae8f991
7 changed files with 35 additions and 8 deletions

View File

@@ -49,7 +49,7 @@ containers started with the label "akvorado.conntrack.fix=1".`,
return fmt.Errorf("unable to initialize conntrack fixer component: %w", err)
}
addCommonHTTPHandlers(r, "conntrack-fixer", httpComponent)
versionMetrics(r)
moreMetrics(r)
components := []any{
httpComponent,

View File

@@ -123,7 +123,7 @@ func consoleStart(r *reporter.Reporter, config ConsoleConfiguration, checkOnly b
// Expose some information and metrics
addCommonHTTPHandlers(r, "console", httpComponent)
versionMetrics(r)
moreMetrics(r)
// If we only asked for a check, stop here.
if checkOnly {

View File

@@ -116,7 +116,7 @@ func demoExporterStart(r *reporter.Reporter, config DemoExporterConfiguration, c
// Expose some information and metrics
addCommonHTTPHandlers(r, "demo-exporter", httpComponent)
versionMetrics(r)
moreMetrics(r)
// If we only asked for a check, stop here.
if checkOnly {

View File

@@ -100,7 +100,7 @@ func inletStart(r *reporter.Reporter, config InletConfiguration, checkOnly bool)
// Expose some information and metrics
addCommonHTTPHandlers(r, "inlet", httpComponent)
versionMetrics(r)
moreMetrics(r)
// If we only asked for a check, stop here.
if checkOnly {

View File

@@ -190,7 +190,7 @@ func orchestratorStart(r *reporter.Reporter, config OrchestratorConfiguration, c
// Expose some information and metrics
addCommonHTTPHandlers(r, "orchestrator", httpComponent)
versionMetrics(r)
moreMetrics(r)
// If we only asked for a check, stop here.
if checkOnly {

View File

@@ -164,7 +164,7 @@ func outletStart(r *reporter.Reporter, config OutletConfiguration, checkOnly boo
// Expose some information and metrics
addCommonHTTPHandlers(r, "outlet", httpComponent)
versionMetrics(r)
moreMetrics(r)
// If we only asked for a check, stop here.
if checkOnly {

View File

@@ -6,14 +6,23 @@ package cmd
import (
"os"
"runtime"
"sync/atomic"
"akvorado/common/helpers"
"akvorado/common/reporter"
"github.com/mattn/go-isatty"
"github.com/rs/zerolog"
"github.com/rs/zerolog/diode"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
var debug bool
var (
debug bool
missedLogs atomic.Uint64
)
// RootCmd is the root for all commands
var RootCmd = &cobra.Command{
@@ -23,7 +32,10 @@ var RootCmd = &cobra.Command{
if isatty.IsTerminal(os.Stdout.Fd()) {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
} else {
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
w := diode.NewWriter(os.Stdout, 1000, 0, func(missed int) {
missedLogs.Add(uint64(missed))
})
log.Logger = zerolog.New(w).With().Timestamp().Logger()
}
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if debug {
@@ -34,6 +46,21 @@ var RootCmd = &cobra.Command{
SilenceUsage: true,
}
func moreMetrics(r *reporter.Reporter) {
versionMetrics(r)
r.GaugeFunc(reporter.GaugeOpts{
Name: "dropped_log_messages",
Help: "Number of log messages dropped.",
}, func() float64 {
return float64(missedLogs.Load())
})
r.GaugeVec(reporter.GaugeOpts{
Name: "info",
Help: "Akvorado build information",
}, []string{"version", "compiler"}).
WithLabelValues(helpers.AkvoradoVersion, runtime.Version()).Set(1)
}
func init() {
RootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false,
"Enable debug logs")