Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 9c2fced

Browse files
authored
Merge pull request #696 from yeya24/add-metrics-docs
docs: add a doc about how to add metrics
2 parents 3bee0cf + f142f4e commit 9c2fced

File tree

10 files changed

+127
-63
lines changed

10 files changed

+127
-63
lines changed

apis/swagger.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ paths:
5858
500:
5959
$ref: "#/responses/500ErrorResponse"
6060

61+
/metrics:
62+
get:
63+
summary: "Get Prometheus metrics"
64+
description: "Get Prometheus metrics"
65+
responses:
66+
200:
67+
description: "no error"
68+
schema:
69+
type: "string"
70+
example: "go_goroutines 1"
71+
6172
/peer/registry:
6273
post:
6374
summary: "registry a task"

cmd/dfdaemon/app/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func init() {
3131

3232
// versionExample shows examples in version command, and is used in auto-generated cli docs.
3333
func versionExample() string {
34-
return `dfdaemon, version 0.4.1
34+
return `dfdaemon version 0.4.1
3535
Git commit: 6fd5c8f
3636
Build date: 20190717-15:57:52
3737
Go version: go1.12.6

cmd/dfget/app/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func init() {
4747

4848
// versionExample shows examples in version command, and is used in auto-generated cli docs.
4949
func versionExample() string {
50-
return `dfget, version 0.4.1
50+
return `dfget version 0.4.1
5151
Git commit: 6fd5c8f
5252
Build date: 20190717-15:57:52
5353
Go version: go1.12.6

cmd/supernode/app/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func init() {
3131

3232
// versionExample shows examples in version command, and is used in auto-generated cli docs.
3333
func versionExample() string {
34-
return `supernode, version 0.4.1
34+
return `supernode version 0.4.1
3535
Git commit: 6fd5c8f
3636
Build date: 20190717-15:57:52
3737
Go version: go1.12.6

common/util/metrics_util.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package util
2+
3+
import (
4+
"github.com/prometheus/client_golang/prometheus"
5+
"github.com/prometheus/client_golang/prometheus/promauto"
6+
)
7+
8+
const (
9+
namespace = "dragonfly"
10+
)
11+
12+
// NewCounter will auto-register a Counter metric to prometheus default registry and return it.
13+
func NewCounter(subsystem, name, help string, labels []string) *prometheus.CounterVec {
14+
return promauto.NewCounterVec(
15+
prometheus.CounterOpts{
16+
Namespace: namespace,
17+
Subsystem: subsystem,
18+
Name: name,
19+
Help: help,
20+
},
21+
labels,
22+
)
23+
}
24+
25+
// NewGauge will auto-register a Gauge metric to prometheus default registry and return it.
26+
func NewGauge(subsystem, name, help string, labels []string) *prometheus.GaugeVec {
27+
return promauto.NewGaugeVec(
28+
prometheus.GaugeOpts{
29+
Namespace: namespace,
30+
Subsystem: subsystem,
31+
Name: name,
32+
Help: help,
33+
},
34+
labels,
35+
)
36+
}
37+
38+
// NewSummary will auto-register a Summary metric to prometheus default registry and return it.
39+
func NewSummary(subsystem, name, help string, labels []string, objectives map[float64]float64) *prometheus.SummaryVec {
40+
return promauto.NewSummaryVec(
41+
prometheus.SummaryOpts{
42+
Namespace: namespace,
43+
Subsystem: subsystem,
44+
Name: name,
45+
Help: help,
46+
Objectives: objectives,
47+
},
48+
labels,
49+
)
50+
}
51+
52+
// NewHistogram will auto-register a Histogram metric to prometheus default registry and return it.
53+
func NewHistogram(subsystem, name, help string, labels []string, buckets []float64) *prometheus.HistogramVec {
54+
return promauto.NewHistogramVec(
55+
prometheus.HistogramOpts{
56+
Namespace: namespace,
57+
Subsystem: subsystem,
58+
Name: name,
59+
Help: help,
60+
Buckets: buckets,
61+
},
62+
labels,
63+
)
64+
}

docs/user_guide/monitoring.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ scrape_configs:
9393
- targets: ['localhost:8002', 'localhost:65001']
9494
```
9595

96-
If you are not familiar with Prometheus, you can modify `prometheus.yml` to this configuration above. Here we don't use any alert rules and alertmanager, so these parts is unset. After modifying this file, you can validate it via `promtool`.
96+
If you are not familiar with Prometheus, you can modify `prometheus.yml` to this configuration above. We add `localhost:8002` and `localhost:65001` to targets as an example, it represents the ip address of supernode and dfdaemon, which is accessible by Prometheus server. Most of the cases you may have to change to other ip address rather than using `localhost` because these components run in different machines or in docker containers.
97+
98+
Here we don't use any alert rules and alertmanager, so these parts is unset. After modifying this file, you can validate it via `promtool`.
9799

98100
``` bash
99101
./promtool check config prometheus.yml
@@ -111,4 +113,22 @@ Finally you can start Prometheus in the same directory. If Prometheus works well
111113

112114
In Prometheus web ui, you can search Dragonfly metrics below. If you want to learn more about Prometheus query language, please check [promql](https://prometheus.io/docs/prometheus/latest/querying/basics/) for help.
113115

114-
![dragonfly_metrics.png](../images/dragonfly_metrics.png)
116+
![dragonfly_metrics.png](../images/dragonfly_metrics.png)
117+
118+
### Add your own metrics
119+
120+
Sometimes maybe you want to add your own metrics to Dragonfly. First please ensure you know the basic concepts about Prometheus metrics. If you don't, please check [metrics types](https://prometheus.io/docs/concepts/metric_types/).
121+
122+
We provide several functions to add metrics easily. Here is an example to add a Counter type metric.
123+
124+
``` go
125+
import "github.com/dragonflyoss/Dragonfly/common/util"
126+
127+
requestCounter := util.NewCounter("supernode", "http_requests_total",
128+
"Counter of HTTP requests.", []string{"code"})
129+
requestCounter.WithLabelValues("200").Inc()
130+
```
131+
132+
This function will auto-register metrics to Prometheus default registry and you can get `dragonfly_supernode_http_requests_total{code,handler,method}` in /metrics endpoint. Here we also add prefix `dragonfly` to metrics name by default. If you want to learn more about how to use these metrics after getting them, please check [prometheus/client_golang](https://github.com/prometheus/client_golang).
133+
134+
As for naming of metric and label, it is better to follow the best practice. We suggest you to check this [metric and label naming](https://prometheus.io/docs/practices/naming/) guide for more detailed information.

supernode/config/constants.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ const (
7070
)
7171

7272
const (
73-
// Namespace is the prefix of the metrics' name of dragonfly
74-
Namespace = "dragonfly"
75-
// Subsystem represents metrics for supernode
76-
Subsystem = "supernode"
73+
// SubsystemSupernode represents metrics from supernode
74+
SubsystemSupernode = "supernode"
75+
// SubsystemDfget represents metrics from dfget
76+
SubsystemDfget = "dfget"
7777
)

supernode/server/metrics.go

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package server
33
import (
44
"net/http"
55

6+
"github.com/dragonflyoss/Dragonfly/common/util"
67
"github.com/dragonflyoss/Dragonfly/supernode/config"
78

89
"github.com/prometheus/client_golang/prometheus"
9-
"github.com/prometheus/client_golang/prometheus/promauto"
1010
"github.com/prometheus/client_golang/prometheus/promhttp"
1111
)
1212

@@ -19,49 +19,23 @@ type metrics struct {
1919
}
2020

2121
func newMetrics() *metrics {
22-
m := &metrics{
23-
requestCounter: promauto.NewCounterVec(
24-
prometheus.CounterOpts{
25-
Namespace: config.Namespace,
26-
Subsystem: config.Subsystem,
27-
Name: "http_requests_total",
28-
Help: "Counter of HTTP requests.",
29-
},
30-
[]string{"code", "handler", "method"},
22+
return &metrics{
23+
requestCounter: util.NewCounter(config.SubsystemSupernode, "http_requests_total",
24+
"Counter of HTTP requests.", []string{"code", "handler", "method"},
3125
),
32-
requestDuration: promauto.NewHistogramVec(
33-
prometheus.HistogramOpts{
34-
Namespace: config.Namespace,
35-
Subsystem: config.Subsystem,
36-
Name: "http_request_duration_seconds",
37-
Help: "Histogram of latencies for HTTP requests.",
38-
Buckets: []float64{.1, .2, .4, 1, 3, 8, 20, 60, 120},
39-
},
40-
[]string{"code", "handler", "method"},
26+
requestDuration: util.NewHistogram(config.SubsystemSupernode, "http_request_duration_seconds",
27+
"Histogram of latencies for HTTP requests.", []string{"code", "handler", "method"},
28+
[]float64{.1, .2, .4, 1, 3, 8, 20, 60, 120},
4129
),
42-
requestSize: promauto.NewHistogramVec(
43-
prometheus.HistogramOpts{
44-
Namespace: config.Namespace,
45-
Subsystem: config.Subsystem,
46-
Name: "http_request_size_bytes",
47-
Help: "Histogram of request size for HTTP requests.",
48-
Buckets: prometheus.ExponentialBuckets(100, 10, 8),
49-
},
50-
[]string{"code", "handler", "method"},
30+
requestSize: util.NewHistogram(config.SubsystemSupernode, "http_request_size_bytes",
31+
"Histogram of request size for HTTP requests.", []string{"code", "handler", "method"},
32+
prometheus.ExponentialBuckets(100, 10, 8),
5133
),
52-
responseSize: promauto.NewHistogramVec(
53-
prometheus.HistogramOpts{
54-
Namespace: config.Namespace,
55-
Subsystem: config.Subsystem,
56-
Name: "http_response_size_bytes",
57-
Help: "Histogram of response size for HTTP requests.",
58-
Buckets: prometheus.ExponentialBuckets(100, 10, 8),
59-
},
60-
[]string{"code", "handler", "method"},
34+
responseSize: util.NewHistogram(config.SubsystemSupernode, "http_response_size_bytes",
35+
"Histogram of response size for HTTP requests.", []string{"code", "handler", "method"},
36+
prometheus.ExponentialBuckets(100, 10, 8),
6137
),
6238
}
63-
64-
return m
6539
}
6640

6741
// instrumentHandler will update metrics for every http request

test/util_api.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ func CheckMetric(c *check.C, metric string, value float64) {
3232
lines := strings.Split(string(data), "\n")
3333
for _, line := range lines {
3434
if strings.Contains(line, metric) {
35-
val, err = strconv.ParseFloat(strings.Split(line, " ")[1], 64)
35+
vals := strings.Split(line, " ")
36+
if len(vals) != 2 {
37+
c.Errorf("bad metrics format")
38+
}
39+
val, err = strconv.ParseFloat(vals[1], 64)
3640
c.Assert(err, check.IsNil)
3741
c.Assert(val, check.Equals, value)
3842
return

version/version.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ import (
2727
"strings"
2828
"text/template"
2929

30-
"github.com/prometheus/client_golang/prometheus"
31-
"github.com/prometheus/client_golang/prometheus/promauto"
32-
3330
"github.com/dragonflyoss/Dragonfly/apis/types"
31+
"github.com/dragonflyoss/Dragonfly/common/util"
3432
)
3533

3634
var (
@@ -80,7 +78,7 @@ func init() {
8078

8179
// versionInfoTmpl contains the template used by Info.
8280
var versionInfoTmpl = `
83-
{{.program}}, version {{.version}}
81+
{{.program}} version {{.version}}
8482
Git commit: {{.revision}}
8583
Build date: {{.buildDate}}
8684
Go version: {{.goVersion}}
@@ -109,16 +107,9 @@ func Print(program string) string {
109107

110108
// NewBuildInfo register a collector which exports metrics about version and build information.
111109
func NewBuildInfo(program string) {
112-
buildInfo := promauto.NewGaugeVec(
113-
prometheus.GaugeOpts{
114-
Namespace: "dragonfly",
115-
Subsystem: program,
116-
Name: "build_info",
117-
Help: fmt.Sprintf(
118-
"A metric with a constant '1' value labeled by version, revision, os, arch and goversion from which %s was built.",
119-
program,
120-
),
121-
},
110+
buildInfo := util.NewGauge(program, "build_info",
111+
fmt.Sprintf("A metric with a constant '1' value labeled by version, revision, os, "+
112+
"arch and goversion from which %s was built.", program),
122113
[]string{"version", "revision", "os", "arch", "goversion"},
123114
)
124115
buildInfo.WithLabelValues(version, revision, os, arch, goVersion).Set(1)

0 commit comments

Comments
 (0)