Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
add how to add metrics and refact metrics to package common
Browse files Browse the repository at this point in the history
Signed-off-by: yeya24 <[email protected]>
  • Loading branch information
yeya24 committed Jul 19, 2019
1 parent ae9e9a3 commit 505d10b
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 58 deletions.
64 changes: 64 additions & 0 deletions common/util/metrics_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package util

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

const (
namespace = "dragonfly"
)

// NewCounter will auto-register a Counter metric to prometheus default registry and return it.
func NewCounter(subsystem, name, help string, labels []string) *prometheus.CounterVec {
return promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: help,
},
labels,
)
}

// NewGauge will auto-register a Gauge metric to prometheus default registry and return it.
func NewGauge(subsystem, name, help string, labels []string) *prometheus.GaugeVec {
return promauto.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: help,
},
labels,
)
}

// NewSummary will auto-register a Summary metric to prometheus default registry and return it.
func NewSummary(subsystem, name, help string, labels []string, objectives map[float64]float64) *prometheus.SummaryVec {
return promauto.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: help,
Objectives: objectives,
},
labels,
)
}

// NewHistogram will auto-register a Histogram metric to prometheus default registry and return it.
func NewHistogram(subsystem, name, help string, labels []string, buckets []float64) *prometheus.HistogramVec {
return promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: help,
Buckets: buckets,
},
labels,
)
}
20 changes: 19 additions & 1 deletion docs/user_guide/monitoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,22 @@ Finally you can start Prometheus in the same directory. If Prometheus works well

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.

![dragonfly_metrics.png](../images/dragonfly_metrics.png)
![dragonfly_metrics.png](../images/dragonfly_metrics.png)

### Add your own metrics

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/).

We provide several functions to add metrics easily. Here is an example to add a Counter type metric.

``` go
import "github.com/dragonflyoss/Dragonfly/common/util"

requestCounter := util.NewCounter("supernode", "http_requests_total",
"Counter of HTTP requests.", []string{"code"})
requestCounter.WithLabelValues("200").Inc()
```

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).

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.
8 changes: 4 additions & 4 deletions supernode/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ const (
)

const (
// Namespace is the prefix of the metrics' name of dragonfly
Namespace = "dragonfly"
// Subsystem represents metrics for supernode
Subsystem = "supernode"
// SubsystemSupernode represents metrics from supernode
SubsystemSupernode = "supernode"
// SubsystemDfget represents metrics from dfget
SubsystemDfget = "dfget"
)
52 changes: 13 additions & 39 deletions supernode/server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package server
import (
"net/http"

"github.com/dragonflyoss/Dragonfly/common/util"
"github.com/dragonflyoss/Dragonfly/supernode/config"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

Expand All @@ -19,49 +19,23 @@ type metrics struct {
}

func newMetrics() *metrics {
m := &metrics{
requestCounter: promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: "http_requests_total",
Help: "Counter of HTTP requests.",
},
[]string{"code", "handler", "method"},
return &metrics{
requestCounter: util.NewCounter(config.SubsystemSupernode, "http_requests_total",
"Counter of HTTP requests.", []string{"code", "handler", "method"},
),
requestDuration: promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: "http_request_duration_seconds",
Help: "Histogram of latencies for HTTP requests.",
Buckets: []float64{.1, .2, .4, 1, 3, 8, 20, 60, 120},
},
[]string{"code", "handler", "method"},
requestDuration: util.NewHistogram(config.SubsystemSupernode, "http_request_duration_seconds",
"Histogram of latencies for HTTP requests.", []string{"code", "handler", "method"},
[]float64{.1, .2, .4, 1, 3, 8, 20, 60, 120},
),
requestSize: promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: "http_request_size_bytes",
Help: "Histogram of request size for HTTP requests.",
Buckets: prometheus.ExponentialBuckets(100, 10, 8),
},
[]string{"code", "handler", "method"},
requestSize: util.NewHistogram(config.SubsystemSupernode, "http_request_size_bytes",
"Histogram of request size for HTTP requests.", []string{"code", "handler", "method"},
prometheus.ExponentialBuckets(100, 10, 8),
),
responseSize: promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Name: "http_response_size_bytes",
Help: "Histogram of response size for HTTP requests.",
Buckets: prometheus.ExponentialBuckets(100, 10, 8),
},
[]string{"code", "handler", "method"},
responseSize: util.NewHistogram(config.SubsystemSupernode, "http_response_size_bytes",
"Histogram of response size for HTTP requests.", []string{"code", "handler", "method"},
prometheus.ExponentialBuckets(100, 10, 8),
),
}

return m
}

// instrumentHandler will update metrics for every http request
Expand Down
6 changes: 5 additions & 1 deletion test/util_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ func CheckMetric(c *check.C, metric string, value float64) {
lines := strings.Split(string(data), "\n")
for _, line := range lines {
if strings.Contains(line, metric) {
val, err = strconv.ParseFloat(strings.Split(line, " ")[1], 64)
vals := strings.Split(line, " ")
if len(vals) != 2 {
c.Errorf("bad metrics format")
}
val, err = strconv.ParseFloat(vals[1], 64)
c.Assert(err, check.IsNil)
c.Assert(val, check.Equals, value)
return
Expand Down
17 changes: 4 additions & 13 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ import (
"strings"
"text/template"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

"github.com/dragonflyoss/Dragonfly/apis/types"
"github.com/dragonflyoss/Dragonfly/common/util"
)

var (
Expand Down Expand Up @@ -109,16 +107,9 @@ func Print(program string) string {

// NewBuildInfo register a collector which exports metrics about version and build information.
func NewBuildInfo(program string) {
buildInfo := promauto.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "dragonfly",
Subsystem: program,
Name: "build_info",
Help: fmt.Sprintf(
"A metric with a constant '1' value labeled by version, revision, os, arch and goversion from which %s was built.",
program,
),
},
buildInfo := util.NewGauge(program, "build_info",
fmt.Sprintf("A metric with a constant '1' value labeled by version, revision, os, "+
"arch and goversion from which %s was built.", program),
[]string{"version", "revision", "os", "arch", "goversion"},
)
buildInfo.WithLabelValues(version, revision, os, arch, goVersion).Set(1)
Expand Down

0 comments on commit 505d10b

Please sign in to comment.