Skip to content

Commit

Permalink
Allow to hide go runtime metrics.
Browse files Browse the repository at this point in the history
  • Loading branch information
chr-fritz committed May 25, 2024
1 parent 9adc7ce commit 1f02e77
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
7 changes: 6 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
const RunPortParm = "exporter.port"
const RunConfigFileParm = "exporter.configFile"
const RunRestartParm = "exporter.restart"
const WithGoMetricsParamName = "exporter.goMetrics"

type RunOptions struct {
aliveCheckInterval time.Duration
Expand All @@ -57,9 +58,13 @@ func NewRunCommand() *cobra.Command {
cmd.Flags().Uint16P("port", "p", 8080, "The port where all metrics should be exported.")
cmd.Flags().StringP("configFile", "f", "config.yaml", "The knx configuration file.")
cmd.Flags().StringP("restart", "r", "health", "The restart behaviour. Can be health or exit")
cmd.Flags().BoolP("withGoMetrics", "g", true, "Should the go metrics also be exported?")

_ = viper.BindPFlag(RunPortParm, cmd.Flags().Lookup("port"))
_ = viper.BindPFlag(RunConfigFileParm, cmd.Flags().Lookup("configFile"))
_ = viper.BindPFlag(RunRestartParm, cmd.Flags().Lookup("restart"))
_ = viper.BindPFlag(WithGoMetricsParamName, cmd.Flags().Lookup("withGoMetrics"))

_ = cmd.RegisterFlagCompletionFunc("configFile", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"yaml", "yml"}, cobra.ShellCompDirectiveFilterFileExt
})
Expand All @@ -73,7 +78,7 @@ func NewRunCommand() *cobra.Command {
}

func (i *RunOptions) run(_ *cobra.Command, _ []string) error {
exporter := metrics.NewExporter(uint16(viper.GetUint(RunPortParm)))
exporter := metrics.NewExporter(uint16(viper.GetUint(RunPortParm)), viper.GetBool(WithGoMetricsParamName))

exporter.AddLivenessCheck("goroutine-threshold", healthcheck.GoroutineCountCheck(100))
metricsExporter, err := i.initAndRunMetricsExporter(exporter)
Expand Down
10 changes: 7 additions & 3 deletions pkg/metrics/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
type exporter struct {
Port uint16
health healthcheck.Handler
meterRegistry prometheus.Registerer
meterRegistry *prometheus.Registry
server *http.Server
}

Expand All @@ -44,11 +44,15 @@ type Exporter interface {
AddReadinessCheck(name string, check healthcheck.Check)
}

func NewExporter(port uint16) Exporter {
func NewExporter(port uint16, withGoMetrics bool) Exporter {
registry := prometheus.DefaultRegisterer.(*prometheus.Registry)
if !withGoMetrics {
registry = prometheus.NewPedanticRegistry()
}
return &exporter{
Port: port,
health: healthcheck.NewHandler(),
meterRegistry: prometheus.DefaultRegisterer,
meterRegistry: registry,
server: &http.Server{Addr: fmt.Sprintf("0.0.0.0:%d", port)},
}
}
Expand Down

0 comments on commit 1f02e77

Please sign in to comment.