Skip to content

Commit 81a52e6

Browse files
authored
Merge pull request #163 from HileQAQ/prometheus
Add support to Prometheus
2 parents c1cf5a1 + 717486a commit 81a52e6

File tree

8 files changed

+338
-30
lines changed

8 files changed

+338
-30
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ Accelerated Container Image is a __non-core__ sub-project of containerd.
7272

7373
* See how to use overlaybd writable layer at [WRITABLE](docs/WRITABLE.md).
7474

75+
* See how to use Prometheus to monitor metrics like latency/error count of snapshotter GRPC APIs at [PROMETHEUS](docs/PROMETHEUS.md).
76+
7577
* Welcome to contribute! [CONTRIBUTING](docs/CONTRIBUTING.md)
7678

7779
## Overview

cmd/overlaybd-snapshotter/main.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"runtime"
2727
"strings"
2828

29+
"github.com/containerd/accelerated-container-image/pkg/metrics"
2930
overlaybd "github.com/containerd/accelerated-container-image/pkg/snapshot"
3031

3132
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
@@ -53,7 +54,6 @@ func parseConfig(fpath string) error {
5354

5455
// TODO: use github.com/urfave/cli
5556
func main() {
56-
5757
pconfig = overlaybd.DefaultBootConfig()
5858
if err := parseConfig(defaultConfigPath); err != nil {
5959
logrus.Error(err)
@@ -62,7 +62,12 @@ func main() {
6262
if pconfig.LogReportCaller {
6363
logrus.SetReportCaller(true)
6464
}
65-
logrus.Infof("%+v", *pconfig)
65+
66+
metrics.Config = pconfig.ExporterConfig
67+
if pconfig.ExporterConfig.Enable {
68+
go metrics.Init()
69+
logrus.Infof("set Prometheus metrics exporter in http://localhost:%d%s", metrics.Config.Port, metrics.Config.UriPrefix)
70+
}
6671

6772
if err := setLogLevel(pconfig.LogLevel); err != nil {
6873
logrus.Errorf("failed to set log level: %v", err)
@@ -117,6 +122,10 @@ func main() {
117122
signal.Notify(signals, unix.SIGTERM, unix.SIGINT, unix.SIGPIPE)
118123

119124
<-handleSignals(context.TODO(), signals, srv)
125+
126+
if pconfig.ExporterConfig.Enable {
127+
metrics.IsAlive.Set(0)
128+
}
120129
}
121130

122131
func handleSignals(ctx context.Context, signals chan os.Signal, server *grpc.Server) chan struct{} {

docs/PROMETHEUS.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Collect Overlaybd metrics with Prometheus
2+
3+
Prometheus is an open-source systems monitoring and alerting toolkit. You can configure overlaybd as a Prometheus target. This topic shows you how to configure overlaybd to use Prometheus.
4+
5+
## Configure Overlaybd
6+
7+
To configure overlaybd as a Prometheus target, you need to specify `uriPrefix` and `port` of the exporter, they can be set in `/etc/overlaybd-snapshotter/config.json` (overlaybd-snapshotter config path), this is an example.
8+
9+
```json
10+
{
11+
"root": "/var/lib/containerd/io.containerd.snapshotter.v1.overlaybd",
12+
"address": "/run/overlaybd-snapshotter/overlaybd.sock",
13+
"verbose": "info",
14+
"rwMode": "overlayfs",
15+
"logReportCaller": false,
16+
"autoRemoveDev": false,
17+
"exporterConfig": {
18+
"enable": true,
19+
"uriPrefix": "/metrics",
20+
"port": 9863
21+
}
22+
}
23+
```
24+
25+
After configuration, you can run overlaybd-snapshotter and curl `http://localhost:9863/metrics` to get Prometheus metrics.
26+
27+
## Configure Prometheus
28+
29+
To use Prometheus to monitor overlaybd, you should modify the `scrape_configs`, and add a `job_name` for overlaybd in `.../prometheus/prometheus.yml`.
30+
31+
```toml
32+
# my global config
33+
global:
34+
scrape_interval: 15s # Set the scrape interval to every 15 seconds. The default is every 1 minute.
35+
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
36+
# scrape_timeout is set to the global default (10s).
37+
38+
# Alertmanager configuration
39+
alerting:
40+
alertmanagers:
41+
- static_configs:
42+
- targets:
43+
# - alertmanager:9093
44+
45+
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
46+
rule_files:
47+
# - "first_rules.yml"
48+
# - "second_rules.yml"
49+
50+
# A scrape configuration containing exactly one endpoint to scrape:
51+
# Here it's Prometheus itself.
52+
scrape_configs:
53+
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
54+
- job_name: "prometheus"
55+
56+
# metrics_path defaults to '/metrics'
57+
# scheme defaults to 'http'.
58+
59+
static_configs:
60+
- targets: ["localhost:9090"]
61+
- job_name: "overlaybd"
62+
metrics_path: "/metrics"
63+
static_configs:
64+
- targets: ["localhost:9863"]
65+
```
66+
67+
In this configuration, you set a job named `overlaybd`, and set `metrics_path` equal to `uriPrefix` in the overlaybd configuration. Next, you can start a Prometheus service using this configuration to monitor overlaybd.

docs/QUICKSTART.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ The config file is `/etc/overlaybd-snapshotter/config.json`. Please create the f
6262
"verbose": "info",
6363
"rwMode": "overlayfs",
6464
"logReportCaller": false,
65-
"autoRemoveDev": false
65+
"autoRemoveDev": false,
66+
"exporterConfig": {
67+
"enable": false,
68+
"uriPrefix": "/metrics",
69+
"port": 9863
70+
}
6671
}
6772
```
6873
| Field | Description |
@@ -73,6 +78,9 @@ The config file is `/etc/overlaybd-snapshotter/config.json`. Please create the f
7378
| `rwMode` | rootfs mode about wether to use native writable layer. See [Native Support for Writable](docs/WRITABLE.md) for detail. |
7479
| `logReportCaller` | enable/disable the calling method |
7580
| `autoRemoveDev` | enable/disable auto clean-up overlaybd device after container removed |
81+
| `exporterConfig.enable` | whether or not create a server to show Prometheus metrics |
82+
| `exporterConfig.uriPrefix` | URI prefix for export metrics, default `/metrics` |
83+
| `exporterConfig.port` | port for http server to show metrics, default `9863` |
7684

7785

7886
#### Start service

go.mod

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,26 @@ require (
77
github.com/containerd/continuity v0.2.2
88
github.com/containerd/go-cni v1.1.5
99
github.com/go-sql-driver/mysql v1.6.0
10+
github.com/moby/locker v1.0.1
1011
github.com/moby/sys/mountinfo v0.5.0
1112
github.com/opencontainers/go-digest v1.0.0
1213
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799
1314
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417
1415
github.com/pkg/errors v0.9.1
16+
github.com/prometheus/client_golang v1.14.0
1517
github.com/sirupsen/logrus v1.8.1
1618
github.com/spf13/cobra v1.1.3
1719
github.com/urfave/cli v1.22.2
18-
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
19-
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e
20+
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f
21+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a
2022
google.golang.org/grpc v1.43.0
2123
)
2224

2325
require (
2426
github.com/Microsoft/go-winio v0.5.1 // indirect
2527
github.com/Microsoft/hcsshim v0.9.2 // indirect
28+
github.com/beorn7/perks v1.0.1 // indirect
29+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
2630
github.com/cilium/ebpf v0.7.0 // indirect
2731
github.com/containerd/cgroups v1.0.3 // indirect
2832
github.com/containerd/console v1.0.3 // indirect
@@ -41,25 +45,28 @@ require (
4145
github.com/gogo/protobuf v1.3.2 // indirect
4246
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
4347
github.com/golang/protobuf v1.5.2 // indirect
44-
github.com/google/go-cmp v0.5.6 // indirect
48+
github.com/google/go-cmp v0.5.8 // indirect
4549
github.com/google/uuid v1.2.0 // indirect
4650
github.com/inconshreveable/mousetrap v1.0.0 // indirect
4751
github.com/klauspost/compress v1.15.9 // indirect
48-
github.com/moby/locker v1.0.1 // indirect
52+
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
4953
github.com/moby/sys/signal v0.6.0 // indirect
5054
github.com/moby/sys/symlink v0.2.0 // indirect
5155
github.com/opencontainers/runc v1.1.1 // indirect
5256
github.com/opencontainers/selinux v1.10.1 // indirect
5357
github.com/pelletier/go-toml v1.9.3 // indirect
58+
github.com/prometheus/client_model v0.3.0 // indirect
59+
github.com/prometheus/common v0.37.0 // indirect
60+
github.com/prometheus/procfs v0.8.0 // indirect
5461
github.com/russross/blackfriday/v2 v2.0.1 // indirect
5562
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
5663
github.com/spf13/pflag v1.0.5 // indirect
5764
go.etcd.io/bbolt v1.3.6 // indirect
5865
go.opencensus.io v0.23.0 // indirect
59-
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
66+
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
6067
golang.org/x/text v0.3.7 // indirect
6168
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
62-
google.golang.org/protobuf v1.27.1 // indirect
69+
google.golang.org/protobuf v1.28.1 // indirect
6370
gotest.tools/v3 v3.0.3 // indirect
6471
)
6572

0 commit comments

Comments
 (0)