-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
57 lines (45 loc) · 1.29 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (C) 2023 Toitware ApS. All rights reserved.
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.
package service
import (
"context"
"time"
"github.com/toitware/tpkg/config"
"github.com/toitware/tpkg/pkg/service/debug"
"github.com/uber-go/tally"
"github.com/uber-go/tally/prometheus"
"go.uber.org/fx"
"go.uber.org/zap"
)
func provideReporter(cfg *config.Config, logger *zap.Logger, mux debug.Mux) (tally.CachedStatsReporter, error) {
if !cfg.Metrics.Enabled {
return nil, nil
}
r := prometheus.NewReporter(prometheus.Options{})
mux.Handle("/metrics", r.HTTPHandler())
return r, nil
}
func provideTally(lc fx.Lifecycle, reporter tally.CachedStatsReporter, cfg *config.Config) (tally.Scope, error) {
if !cfg.Metrics.Enabled {
return tally.NoopScope, nil
}
scope, closer := tally.NewRootScope(tally.ScopeOptions{
Tags: cfg.Metrics.Tags,
Prefix: cfg.Metrics.Prefix,
CachedReporter: reporter,
Separator: prometheus.DefaultSeparator,
}, time.Second)
lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return closer.Close()
},
OnStart: func(ctx context.Context) error {
return nil
},
})
scope.Counter("boot").Inc(1)
return scope, nil
}
func loadTally(scope tally.Scope) {
}