Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 745a06b

Browse filesBrowse files
committed
add support for prometheus metrics
Signed-off-by: yeya24 <yb532204897@gmail.com>
1 parent 21d2390 commit 745a06b
Copy full SHA for 745a06b

File tree

Expand file treeCollapse file tree

9 files changed

+134
-9
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

9 files changed

+134
-9
lines changed
Open diff view settings
Collapse file

‎dfdaemon/constant/constant.go‎

Copy file name to clipboardExpand all lines: dfdaemon/constant/constant.go
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,10 @@ const (
4444
// DefaultConfigPath the default path of dfdaemon configuration file.
4545
DefaultConfigPath = "/etc/dragonfly/dfdaemon.yml"
4646
)
47+
48+
const (
49+
// Namespace is the prefix of the metrics' name of dragonfly
50+
Namespace = "dragonfly"
51+
// Subsystem represents metrics for dfdaemon
52+
Subsystem = "dfdaemon"
53+
)
Collapse file

‎dfdaemon/handler/root_handler.go‎

Copy file name to clipboardExpand all lines: dfdaemon/handler/root_handler.go
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
_ "net/http/pprof"
2323

2424
"github.com/dragonflyoss/Dragonfly/version"
25+
26+
"github.com/prometheus/client_golang/prometheus/promhttp"
2527
)
2628

2729
// New returns a new http mux for dfdaemon
@@ -30,5 +32,6 @@ func New() *http.ServeMux {
3032
s.HandleFunc("/args", getArgs)
3133
s.HandleFunc("/env", getEnv)
3234
s.HandleFunc("/debug/version", version.Handler)
35+
s.HandleFunc("/metrics", promhttp.Handler().ServeHTTP)
3336
return s
3437
}
Collapse file

‎go.mod‎

Copy file name to clipboardExpand all lines: go.mod
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ require (
2828
github.com/pborman/uuid v0.0.0-20180122190007-c65b2f87fee3
2929
github.com/pkg/errors v0.8.0
3030
github.com/prashantv/gostub v1.0.0
31+
github.com/prometheus/client_golang v0.9.3
3132
github.com/russross/blackfriday v0.0.0-20171011182219-6d1ef893fcb0 // indirect
3233
github.com/sirupsen/logrus v1.2.0
3334
github.com/spf13/afero v1.2.2
Collapse file

‎go.sum‎

Copy file name to clipboardExpand all lines: go.sum
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5
1212
github.com/asaskevich/govalidator v0.0.0-20170903095215-73945b6115bf h1:wXq5VXJjLole37O6oWZwqBRbKZw6VmC+wuAe8j/w2ZA=
1313
github.com/asaskevich/govalidator v0.0.0-20170903095215-73945b6115bf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
1414
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
15+
github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0=
1516
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
1617
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
1718
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
@@ -64,6 +65,7 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb
6465
github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s=
6566
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
6667
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
68+
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
6769
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
6870
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
6971
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
@@ -100,6 +102,7 @@ github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzR
100102
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
101103
github.com/mailru/easyjson v0.0.0-20170902151237-2a92e673c9a6 h1:xhfqLjTK1g6iq92WjkfuaN6bC7Aoxb5//G8IfwyMyYA=
102104
github.com/mailru/easyjson v0.0.0-20170902151237-2a92e673c9a6/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
105+
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
103106
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
104107
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
105108
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
@@ -116,12 +119,16 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
116119
github.com/prashantv/gostub v1.0.0 h1:wTzvgO04xSS3gHuz6Vhuo0/kvWelyJxwNS0IRBPAwGY=
117120
github.com/prashantv/gostub v1.0.0/go.mod h1:dP1v6T1QzyGJJKFocwAU0lSZKpfjstjH8TlhkEU0on0=
118121
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
122+
github.com/prometheus/client_golang v0.9.3 h1:9iH4JKXLzFbOAdtqv/a+j8aewx2Y8lAjAydhbaScPF8=
119123
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
120124
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
125+
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx/RA8rT8tKFRuGUZhuA90OyIBpPCXkcbwU8DE=
121126
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
122127
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
128+
github.com/prometheus/common v0.4.0 h1:7etb9YClo3a6HjLzfl6rIQaU+FDfi0VSX39io3aQ+DM=
123129
github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
124130
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
131+
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084 h1:sofwID9zm4tzrgykg80hfFph1mryUeLRsUfoocVVmRY=
125132
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
126133
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
127134
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
Collapse file

‎supernode/config/constants.go‎

Copy file name to clipboardExpand all lines: supernode/config/constants.go
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,10 @@ const (
6868
// CDNWriterRoutineLimit 4
6969
CDNWriterRoutineLimit = 4
7070
)
71+
72+
const (
73+
// Namespace is the prefix of the metrics' name of dragonfly
74+
Namespace = "dragonfly"
75+
// Subsystem represents metrics for supernode
76+
Subsystem = "supernode"
77+
)
Collapse file

‎supernode/server/metrics.go‎

Copy file name to clipboard
+68Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package server
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/dragonflyoss/Dragonfly/supernode/config"
7+
8+
"github.com/prometheus/client_golang/prometheus"
9+
"github.com/prometheus/client_golang/prometheus/promauto"
10+
"github.com/prometheus/client_golang/prometheus/promhttp"
11+
)
12+
13+
// metrics defines three prometheus metrics for monitoring http handler status
14+
type metrics struct {
15+
requestCounter *prometheus.CounterVec
16+
requestDuration *prometheus.HistogramVec
17+
responseSize *prometheus.HistogramVec
18+
}
19+
20+
func newMetrics() *metrics {
21+
m := &metrics{
22+
requestCounter: promauto.NewCounterVec(
23+
prometheus.CounterOpts{
24+
Namespace: config.Namespace,
25+
Subsystem: config.Subsystem,
26+
Name: "http_requests_total",
27+
Help: "Counter of HTTP requests.",
28+
},
29+
[]string{"handler", "code"},
30+
),
31+
requestDuration: promauto.NewHistogramVec(
32+
prometheus.HistogramOpts{
33+
Namespace: config.Namespace,
34+
Subsystem: config.Subsystem,
35+
Name: "http_request_duration_seconds",
36+
Help: "Histogram of latencies for HTTP requests.",
37+
Buckets: []float64{.1, .2, .4, 1, 3, 8, 20, 60, 120},
38+
},
39+
[]string{"handler"},
40+
),
41+
responseSize: promauto.NewHistogramVec(
42+
prometheus.HistogramOpts{
43+
Namespace: config.Namespace,
44+
Subsystem: config.Subsystem,
45+
Name: "http_response_size_bytes",
46+
Help: "Histogram of response size for HTTP requests.",
47+
Buckets: prometheus.ExponentialBuckets(100, 10, 8),
48+
},
49+
[]string{"handler"},
50+
),
51+
}
52+
53+
return m
54+
}
55+
56+
// instrumentHandler will update metrics for every http request
57+
func (m *metrics) instrumentHandler(handlerName string, handler http.HandlerFunc) http.HandlerFunc {
58+
return promhttp.InstrumentHandlerCounter(
59+
m.requestCounter.MustCurryWith(prometheus.Labels{"handler": handlerName}),
60+
promhttp.InstrumentHandlerDuration(
61+
m.requestDuration.MustCurryWith(prometheus.Labels{"handler": handlerName}),
62+
promhttp.InstrumentHandlerResponseSize(
63+
m.responseSize.MustCurryWith(prometheus.Labels{"handler": handlerName}),
64+
handler,
65+
),
66+
),
67+
)
68+
}
Collapse file

‎supernode/server/router.go‎

Copy file name to clipboardExpand all lines: supernode/server/router.go
+16-6Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,21 @@ import (
1010
"github.com/dragonflyoss/Dragonfly/version"
1111

1212
"github.com/gorilla/mux"
13+
"github.com/prometheus/client_golang/prometheus/promhttp"
1314
)
1415

1516
// versionMatcher defines to parse version url path.
1617
const versionMatcher = "/v{version:[0-9.]+}"
1718

18-
func initRoute(s *Server) *mux.Router {
19-
r := mux.NewRouter()
19+
// metricsRouter is a wrapper for mux.Router and metrics.
20+
type metricsRouter struct {
21+
router *mux.Router
22+
metrics *metrics
23+
}
2024

25+
func initRoute(s *Server) *metricsRouter {
26+
r := mux.NewRouter()
27+
router := &metricsRouter{r, newMetrics()}
2128
handlers := []*HandlerSpec{
2229
// system
2330
{Method: http.MethodGet, Path: "/_ping", HandlerFunc: s.ping},
@@ -40,18 +47,21 @@ func initRoute(s *Server) *mux.Router {
4047
// register API
4148
for _, h := range handlers {
4249
if h != nil {
43-
r.Path(versionMatcher + h.Path).Methods(h.Method).Handler(filter(h.HandlerFunc, s))
44-
r.Path(h.Path).Methods(h.Method).Handler(filter(h.HandlerFunc, s))
50+
r.Path(versionMatcher + h.Path).Methods(h.Method).Handler(router.metrics.instrumentHandler(versionMatcher+h.Path, filter(h.HandlerFunc)))
51+
r.Path(h.Path).Methods(h.Method).Handler(router.metrics.instrumentHandler(h.Path, filter(h.HandlerFunc)))
4552
}
4653
}
4754

55+
// metrics
56+
r.Handle("/metrics", router.metrics.instrumentHandler("/metrics", promhttp.Handler().ServeHTTP))
57+
4858
if s.Config.Debug || s.Config.EnableProfiler {
4959
r.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index)
5060
}
51-
return r
61+
return router
5262
}
5363

54-
func filter(handler Handler, s *Server) http.HandlerFunc {
64+
func filter(handler Handler) http.HandlerFunc {
5565
pctx := context.Background()
5666

5767
return func(w http.ResponseWriter, req *http.Request) {
Collapse file

‎supernode/server/router_test.go‎

Copy file name to clipboardExpand all lines: supernode/server/router_test.go
+24-2Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/dragonflyoss/Dragonfly/version"
1818

1919
"github.com/go-check/check"
20+
prom_testutil "github.com/prometheus/client_golang/prometheus/testutil"
2021
)
2122

2223
func Test(t *testing.T) {
@@ -31,6 +32,7 @@ func init() {
3132
type RouterTestSuite struct {
3233
addr string
3334
listener net.Listener
35+
router *metricsRouter
3436
}
3537

3638
func (rs *RouterTestSuite) SetUpSuite(c *check.C) {
@@ -57,10 +59,11 @@ func (rs *RouterTestSuite) SetUpSuite(c *check.C) {
5759
OS: runtime.GOOS,
5860
GoVersion: runtime.Version(),
5961
}
60-
router := initRoute(s)
62+
63+
rs.router = initRoute(s)
6164
rs.listener, err = net.Listen("tcp", rs.addr)
6265
c.Check(err, check.IsNil)
63-
go http.Serve(rs.listener, router)
66+
go http.Serve(rs.listener, rs.router.router)
6467
}
6568

6669
func (rs *RouterTestSuite) TearDownSuite(c *check.C) {
@@ -109,3 +112,22 @@ func (rs *RouterTestSuite) TestVersionHandler(c *check.C) {
109112
c.Check(err, check.IsNil)
110113
c.Check(string(expectDFVersion), check.Equals, string(res))
111114
}
115+
116+
func (rs *RouterTestSuite) TestHTTPMetrics(c *check.C) {
117+
// ensure /metrics is accessible
118+
code, _, err := cutil.Get("http://"+rs.addr+"/metrics", 0)
119+
c.Check(err, check.IsNil)
120+
c.Assert(code, check.Equals, 200)
121+
122+
counter := rs.router.metrics.requestCounter
123+
c.Assert(1, check.Equals,
124+
int(prom_testutil.ToFloat64(counter.WithLabelValues("/metrics", strconv.Itoa(http.StatusOK)))))
125+
126+
for i := 0; i < 5; i++ {
127+
code, _, err := cutil.Get("http://"+rs.addr+"/_ping", 0)
128+
c.Check(err, check.IsNil)
129+
c.Assert(code, check.Equals, 200)
130+
c.Assert(i+1, check.Equals,
131+
int(prom_testutil.ToFloat64(counter.WithLabelValues("/_ping", strconv.Itoa(http.StatusOK)))))
132+
}
133+
}
Collapse file

‎supernode/server/server.go‎

Copy file name to clipboardExpand all lines: supernode/server/server.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (s *Server) Start() error {
9191
}
9292

9393
server := &http.Server{
94-
Handler: router,
94+
Handler: router.router,
9595
ReadTimeout: time.Minute * 10,
9696
ReadHeaderTimeout: time.Minute * 10,
9797
IdleTimeout: time.Minute * 10,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.