diff --git a/docs/modules/fxmetrics.md b/docs/modules/fxmetrics.md index bac8885..3e123b4 100644 --- a/docs/modules/fxmetrics.md +++ b/docs/modules/fxmetrics.md @@ -108,4 +108,17 @@ example_total 1 You can also get, real time, the status of your metrics on the [fxcore](https://github.com/ankorstore/yokai/tree/main/fxcore) dashboard: ![](../../assets/images/dash-metrics-light.png#only-light) -![](../../assets/images/dash-metrics-dark.png#only-dark) \ No newline at end of file +![](../../assets/images/dash-metrics-dark.png#only-dark) + +## Configuration + +The following metrics collectors can be enabled: + +```yaml title="configs/config.yaml" +modules: + metrics: + collect: + build: true # to collect build infos metrics (disabled by default) + go: true # to collect go metrics (disabled by default) + process: true # to collect process metrics (disabled by default) +``` diff --git a/fxmetrics/README.md b/fxmetrics/README.md index 9df45db..b67301d 100644 --- a/fxmetrics/README.md +++ b/fxmetrics/README.md @@ -60,6 +60,25 @@ func main() { } ``` +### Configuration + +Configuration reference: + +```yaml +# ./configs/config.yaml +app: + name: app + env: dev + version: 0.1.0 + debug: true +modules: + metrics: + collect: + build: true # to collect build infos metrics (disabled by default) + go: true # to collect go metrics (disabled by default) + process: true # to collect process metrics (disabled by default) +``` + ### Registration This module provides the possibility to register your metrics [collectors](https://github.com/prometheus/client_golang/blob/main/prometheus/collector.go) in a common `*prometheus.Registry` via `AsMetricsCollector()`: diff --git a/fxmetrics/module.go b/fxmetrics/module.go index 9733084..3698d5f 100644 --- a/fxmetrics/module.go +++ b/fxmetrics/module.go @@ -1,8 +1,10 @@ package fxmetrics import ( + "github.com/ankorstore/yokai/config" "github.com/ankorstore/yokai/log" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/collectors" "go.uber.org/fx" ) @@ -24,6 +26,7 @@ var FxMetricsModule = fx.Module( type FxMetricsRegistryParam struct { fx.In Factory MetricsRegistryFactory + Config *config.Config Logger *log.Logger Collectors []prometheus.Collector `group:"metrics-collectors"` } @@ -37,7 +40,23 @@ func NewFxMetricsRegistry(p FxMetricsRegistryParam) (*prometheus.Registry, error return nil, err } - for _, collector := range p.Collectors { + var registrableCollectors []prometheus.Collector + + if p.Config.GetBool("modules.metrics.collect.build") { + registrableCollectors = append(registrableCollectors, collectors.NewBuildInfoCollector()) + } + + if p.Config.GetBool("modules.metrics.collect.process") { + registrableCollectors = append(registrableCollectors, collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})) + } + + if p.Config.GetBool("modules.metrics.collect.go") { + registrableCollectors = append(registrableCollectors, collectors.NewGoCollector()) + } + + registrableCollectors = append(registrableCollectors, p.Collectors...) + + for _, collector := range registrableCollectors { err = registry.Register(collector) if err != nil { p.Logger.Error().Err(err).Msgf("failed to register metrics collector %+T", collector) diff --git a/fxmetrics/module_test.go b/fxmetrics/module_test.go index 09a9aac..d7b0c38 100644 --- a/fxmetrics/module_test.go +++ b/fxmetrics/module_test.go @@ -44,17 +44,30 @@ func TestModule(t *testing.T) { "message": "registered metrics collector *prometheus.counter", }) - expectedHelp := ` + // go metric + expectedMetric := ` + # HELP go_memstats_lookups_total Total number of pointer lookups. + # TYPE go_memstats_lookups_total counter + go_memstats_lookups_total 0 + ` + + err := testutil.GatherAndCompare( + registry, + strings.NewReader(expectedMetric), + "go_memstats_lookups_total", + ) + assert.NoError(t, err) + + // custom metric + expectedMetric = ` # HELP test_total test help # TYPE test_total counter - ` - expectedMetric := ` test_total 9 ` - err := testutil.GatherAndCompare( + err = testutil.GatherAndCompare( registry, - strings.NewReader(expectedHelp+expectedMetric), + strings.NewReader(expectedMetric), "test_total", ) assert.NoError(t, err) diff --git a/fxmetrics/testdata/config/config.yaml b/fxmetrics/testdata/config/config.yaml index 0ca0a25..0d80907 100644 --- a/fxmetrics/testdata/config/config.yaml +++ b/fxmetrics/testdata/config/config.yaml @@ -4,3 +4,8 @@ modules: log: level: debug output: test + metrics: + collect: + build: true + go: true + process: true