Skip to content

Commit 27f725a

Browse files
pkg/metrics: introduce v2 (scionproto#4530)
Introduce a v2 version of pkg/metrics. The current metrics package is not optimal: - Using objects that have been called with With allocated on each increment/set value call. - Having the option to call With ties consumer code with initialization code. - There is no way whether a counter is fully initialized with all labels. A full discussion can be found in scionproto#4527. The new v2 version is compatible with prometheus, so no explicit conversion functions are offered, instead the prometheus creation functions can be used directly. So far the new library has been plugged in selected places. The intention is that it will eventually replace the current library completely, however doing so in a single sweep would be too extensive.
1 parent 45053b7 commit 27f725a

27 files changed

+598
-157
lines changed

control/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ go_library(
2828
"//pkg/grpc:go_default_library",
2929
"//pkg/log:go_default_library",
3030
"//pkg/metrics:go_default_library",
31+
"//pkg/metrics/v2:go_default_library",
3132
"//pkg/private/ctrl/path_mgmt:go_default_library",
3233
"//pkg/private/prom:go_default_library",
3334
"//pkg/private/serrors:go_default_library",

control/observability.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
cstrust "github.com/scionproto/scion/control/trust"
2929
"github.com/scionproto/scion/pkg/addr"
3030
"github.com/scionproto/scion/pkg/metrics"
31+
metrics2 "github.com/scionproto/scion/pkg/metrics/v2"
3132
"github.com/scionproto/scion/pkg/private/prom"
3233
"github.com/scionproto/scion/pkg/private/serrors"
3334
"github.com/scionproto/scion/pkg/scrypto"
@@ -82,7 +83,7 @@ type Metrics struct {
8283
TrustTRCFileWritesTotal *prometheus.CounterVec
8384
SCIONNetworkMetrics snet.SCIONNetworkMetrics
8485
SCIONPacketConnMetrics snet.SCIONPacketConnMetrics
85-
SCMPErrors metrics.Counter
86+
SCMPErrors metrics2.Counter
8687
TopoLoader topology.LoaderMetrics
8788
DRKeySecretValueQueriesTotal *prometheus.CounterVec
8889
DRKeyLevel1QueriesTotal *prometheus.CounterVec

doc/dev/style/go.rst

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,29 +113,20 @@ metrics, and for operators to understand where metrics are coming from. As a
113113
bonus, we should leverage the type system to help us spot as many errors as
114114
possible.
115115

116-
To write code that both includes metrics, and is testable, we use the
117-
`metric recommendations from the go-kit project <https://godoc.org/github.com/go-kit/kit/metrics>`__.
116+
To write code that both includes metrics, and is testable, we use the metric
117+
interfaces defined in the ``pkg/metrics/v2`` package.
118118

119-
A simple example with labels (note that ``Foo``'s metrics can be unit tested by mocking the counter):
119+
A simple example with labels (note that ``Giant``'s metrics can be unit tested by
120+
mocking the counter):
120121

121-
.. literalinclude:: /../pkg/metrics/metrics_test.go
122-
:language: Go
123-
:dedent: 1
124-
:start-after: LITERALINCLUDE ExampleCounter_Interface START
125-
:end-before: LITERALINCLUDE ExampleCounter_Interface END
126-
127-
Calling code can later create ``Giant`` objects with Prometheus metric reporting
128-
by plugging a prometheus counter as the ``Counter``. The Prometheus objects can be
129-
obtained from the metrics packages in the following way:
130-
131-
.. literalinclude:: /../pkg/metrics/metrics_test.go
122+
.. literalinclude:: /../pkg/metrics/v2/metrics_test.go
132123
:language: Go
133124
:dedent: 1
134125
:start-after: LITERALINCLUDE ExampleCounter_Implementation START
135126
:end-before: LITERALINCLUDE ExampleCounter_Implementation END
136127

137-
In cases where performance is a concern, consider applying the labels outside of
138-
the performance-critical section.
128+
Calling code can later create ``Giant`` objects with Prometheus metric reporting
129+
by plugging a prometheus counter as the ``Counter`` as shown in the example.
139130

140131
.. note::
141132
Some packages have ``metrics`` packages that define labels and initialize

gateway/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ go_library(
2525
"//pkg/grpc:go_default_library",
2626
"//pkg/log:go_default_library",
2727
"//pkg/metrics:go_default_library",
28+
"//pkg/metrics/v2:go_default_library",
2829
"//pkg/private/serrors:go_default_library",
2930
"//pkg/private/util:go_default_library",
3031
"//pkg/proto/gateway:go_default_library",

gateway/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"github.com/prometheus/client_golang/prometheus/promauto"
2020

2121
"github.com/scionproto/scion/pkg/addr"
22-
"github.com/scionproto/scion/pkg/metrics"
22+
"github.com/scionproto/scion/pkg/metrics/v2"
2323
"github.com/scionproto/scion/pkg/snet"
2424
snetmetrics "github.com/scionproto/scion/pkg/snet/metrics"
2525
)

gateway/pathhealth/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ go_library(
1717
"//pkg/addr:go_default_library",
1818
"//pkg/log:go_default_library",
1919
"//pkg/metrics:go_default_library",
20+
"//pkg/metrics/v2:go_default_library",
2021
"//pkg/private/common:go_default_library",
2122
"//pkg/private/ctrl/path_mgmt:go_default_library",
2223
"//pkg/private/serrors:go_default_library",

gateway/pathhealth/pathwatcher.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/scionproto/scion/pkg/addr"
2626
"github.com/scionproto/scion/pkg/log"
2727
"github.com/scionproto/scion/pkg/metrics"
28+
metrics2 "github.com/scionproto/scion/pkg/metrics/v2"
2829
"github.com/scionproto/scion/pkg/private/common"
2930
"github.com/scionproto/scion/pkg/private/serrors"
3031
"github.com/scionproto/scion/pkg/slayers/path/scion"
@@ -59,8 +60,9 @@ type DefaultPathWatcherFactory struct {
5960
ProbesReceived func(remote addr.IA) metrics.Counter
6061
// ProbesSendErrors keeps track of how many time sending probes failed per
6162
// remote.
62-
ProbesSendErrors func(remote addr.IA) metrics.Counter
63-
SCMPErrors metrics.Counter
63+
ProbesSendErrors func(remote addr.IA) metrics.Counter
64+
65+
SCMPErrors metrics2.Counter
6466
SCIONPacketConnMetrics snet.SCIONPacketConnMetrics
6567
}
6668

pkg/log/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ go_library(
1515
importpath = "github.com/scionproto/scion/pkg/log",
1616
visibility = ["//visibility:public"],
1717
deps = [
18-
"//pkg/metrics:go_default_library",
18+
"//pkg/metrics/v2:go_default_library",
1919
"//pkg/private/common:go_default_library",
2020
"//pkg/private/serrors:go_default_library",
2121
"//private/config:go_default_library",

pkg/log/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"go.uber.org/zap"
1919
"go.uber.org/zap/zapcore"
2020

21-
"github.com/scionproto/scion/pkg/metrics"
21+
"github.com/scionproto/scion/pkg/metrics/v2"
2222
)
2323

2424
type options struct {

pkg/metrics/v2/BUILD.bazel

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
load("//tools/lint:go.bzl", "go_library", "go_test")
2+
3+
go_library(
4+
name = "go_default_library",
5+
srcs = [
6+
"fakes.go",
7+
"metrics.go",
8+
],
9+
importpath = "github.com/scionproto/scion/pkg/metrics/v2",
10+
visibility = ["//visibility:public"],
11+
)
12+
13+
go_test(
14+
name = "go_default_test",
15+
srcs = [
16+
"fakes_test.go",
17+
"metrics_test.go",
18+
],
19+
deps = [
20+
":go_default_library",
21+
"@com_github_prometheus_client_golang//prometheus:go_default_library",
22+
"@com_github_stretchr_testify//assert:go_default_library",
23+
],
24+
)

0 commit comments

Comments
 (0)