Skip to content

Commit ae27a6b

Browse files
nicochecrosbymichael
authored andcommitted
Add metric exposing build version&revision
Signed-off-by: Nicolas Chariglione <[email protected]>
1 parent d58542a commit ae27a6b

3 files changed

Lines changed: 124 additions & 0 deletions

File tree

cmd/containerd/command/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/containerd/containerd/defaults"
3131
"github.com/containerd/containerd/errdefs"
3232
"github.com/containerd/containerd/log"
33+
"github.com/containerd/containerd/metrics"
3334
"github.com/containerd/containerd/mount"
3435
"github.com/containerd/containerd/services/server"
3536
srvconfig "github.com/containerd/containerd/services/server/config"
@@ -218,6 +219,7 @@ can be used and modified as necessary as a custom configuration.`
218219
serve(ctx, l, server.ServeDebug)
219220
}
220221
if config.Metrics.Address != "" {
222+
metrics.Register()
221223
l, err := net.Listen("tcp", config.Metrics.Address)
222224
if err != nil {
223225
return errors.Wrapf(err, "failed to get listener for metrics endpoint")

metrics/buildinfo.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package metrics
18+
19+
import (
20+
"github.com/containerd/containerd/version"
21+
goMetrics "github.com/docker/go-metrics"
22+
"github.com/prometheus/client_golang/prometheus"
23+
)
24+
25+
func newBuildInfoCollector(ns *goMetrics.Namespace) *Collector {
26+
return &Collector{
27+
ns: ns,
28+
m: metric{
29+
name: "build_info",
30+
help: "Build information on containerd",
31+
unit: goMetrics.Total,
32+
vt: prometheus.CounterValue,
33+
labels: []string{
34+
"version",
35+
"revision",
36+
},
37+
getValues: func() []value {
38+
return []value{
39+
{
40+
l: []string{
41+
version.Version,
42+
version.Revision,
43+
},
44+
v: float64(1),
45+
},
46+
}
47+
},
48+
},
49+
}
50+
}

metrics/metrics.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package metrics
18+
19+
import (
20+
goMetrics "github.com/docker/go-metrics"
21+
"github.com/prometheus/client_golang/prometheus"
22+
)
23+
24+
// Collector provides the ability to collect generic metrics and export
25+
// them in the prometheus format
26+
type Collector struct {
27+
ns *goMetrics.Namespace
28+
m metric
29+
}
30+
31+
type metric struct {
32+
name string
33+
help string
34+
unit goMetrics.Unit
35+
vt prometheus.ValueType
36+
labels []string
37+
// getValues returns the value and labels for the data
38+
getValues func() []value
39+
}
40+
41+
type value struct {
42+
v float64
43+
l []string
44+
}
45+
46+
// Describe prometheus metrics
47+
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
48+
m := c.m
49+
ch <- c.ns.NewDesc(m.name, m.help, m.unit, m.labels...)
50+
}
51+
52+
// Collect prometheus metrics
53+
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
54+
m := c.m
55+
for _, v := range m.getValues() {
56+
ch <- prometheus.MustNewConstMetric(
57+
c.ns.NewDesc(m.name, m.help, m.unit, m.labels...),
58+
m.vt,
59+
v.v,
60+
v.l...,
61+
)
62+
}
63+
}
64+
65+
// Register a prometheus namespace for generic metrics
66+
func Register() {
67+
ns := goMetrics.NewNamespace("containerd", "", nil)
68+
69+
c := newBuildInfoCollector(ns)
70+
ns.Add(c)
71+
goMetrics.Register(ns)
72+
}

0 commit comments

Comments
 (0)