Skip to content

Commit

Permalink
feat(fxmetrics): Added build info, process and go metrics collectors (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ekkinox authored Mar 19, 2024
1 parent 7c7fa4d commit 44fba35
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 7 deletions.
15 changes: 14 additions & 1 deletion docs/modules/fxmetrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
![](../../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)
```
19 changes: 19 additions & 0 deletions fxmetrics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`:
Expand Down
21 changes: 20 additions & 1 deletion fxmetrics/module.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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"`
}
Expand All @@ -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)
Expand Down
23 changes: 18 additions & 5 deletions fxmetrics/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions fxmetrics/testdata/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ modules:
log:
level: debug
output: test
metrics:
collect:
build: true
go: true
process: true

0 comments on commit 44fba35

Please sign in to comment.