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

Commit 505d10b

Browse files
committed
add how to add metrics and refact metrics to package common
Signed-off-by: yeya24 <[email protected]>
1 parent ae9e9a3 commit 505d10b

File tree

6 files changed

+109
-58
lines changed

6 files changed

+109
-58
lines changed

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: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,22 @@ Finally you can start Prometheus in the same directory. If Prometheus works well
111111

112112
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.
113113

114-
![dragonfly_metrics.png](../images/dragonfly_metrics.png)
114+
![dragonfly_metrics.png](../images/dragonfly_metrics.png)
115+
116+
### Add your own metrics
117+
118+
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/).
119+
120+
We provide several functions to add metrics easily. Here is an example to add a Counter type metric.
121+
122+
``` go
123+
import "github.com/dragonflyoss/Dragonfly/common/util"
124+
125+
requestCounter := util.NewCounter("supernode", "http_requests_total",
126+
"Counter of HTTP requests.", []string{"code"})
127+
requestCounter.WithLabelValues("200").Inc()
128+
```
129+
130+
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).
131+
132+
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: 4 additions & 13 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 (
@@ -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)