This repository was archived by the owner on Sep 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathserver.go
120 lines (106 loc) · 3.26 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2017 Mark Nevill. All Rights Reserved.
// See LICENSE for licensing terms.
package http_prometheus
import (
"sync"
"net/http"
"strconv"
"time"
"github.com/improbable-eng/go-httpwares/metrics"
"github.com/prometheus/client_golang/prometheus"
)
var (
serverStarted = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_handler_started_requests_total",
Help: "Count of started requests.",
},
[]string{"name", "handler", "host", "path", "method"},
)
serverCompleted = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_handler_completed_requests_total",
Help: "Count of completed requests.",
},
[]string{"name", "handler", "host", "path", "method", "status"},
)
serverLatency = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_handler_completed_latency_seconds",
Help: "Latency of completed requests.",
Buckets: []float64{.01, .03, .1, .3, 1, 3, 10, 30, 100, 300},
},
[]string{"name", "handler", "host", "path", "method", "status"},
)
serverRequestSize = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_handler_request_size_bytes",
Help: "Size of received requests.",
Buckets: prometheus.ExponentialBuckets(32, 32, 6),
},
[]string{"name", "handler", "host", "path", "method"},
)
serverResponseSize = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_handler_response_size_bytes",
Help: "Size of sent responses.",
Buckets: prometheus.ExponentialBuckets(32, 32, 6),
},
[]string{"name", "handler", "host", "path", "method", "status"},
)
serverInit sync.Once
serverHistInit sync.Once
serverSizeInit sync.Once
)
func ServerMetrics(opts ...opt) http_metrics.Reporter {
o := evalOpts(opts)
serverInit.Do(func() {
prometheus.MustRegister(serverStarted)
prometheus.MustRegister(serverCompleted)
})
if o.latency {
serverHistInit.Do(func() {
prometheus.MustRegister(serverLatency)
})
}
if o.sizes {
serverSizeInit.Do(func() {
prometheus.MustRegister(serverRequestSize)
prometheus.MustRegister(serverResponseSize)
})
}
return &serverReporter{opts: o}
}
type serverReporter struct {
opts *options
}
func (r *serverReporter) Track(req *http.Request) http_metrics.Tracker {
return &serverTracker{
opts: r.opts,
meta: reqMeta(req, r.opts, true),
}
}
type serverTracker struct {
opts *options
*meta
}
func (t *serverTracker) RequestStarted() {
serverStarted.WithLabelValues(t.name, t.handler, t.host, t.path, t.method).Inc()
}
func (t *serverTracker) RequestRead(duration time.Duration, size int) {
if t.opts.sizes {
serverRequestSize.WithLabelValues(t.name, t.handler, t.host, t.path, t.method).Observe(float64(size))
}
}
func (t *serverTracker) ResponseStarted(duration time.Duration, code int, header http.Header) {
}
func (t *serverTracker) ResponseDone(duration time.Duration, code int, size int) {
status := strconv.Itoa(code)
serverCompleted.WithLabelValues(t.name, t.handler, t.host, t.path, t.method, status).Inc()
if t.opts.latency {
serverLatency.WithLabelValues(t.name, t.handler, t.host, t.path, t.method, status).Observe(duration.Seconds())
}
if t.opts.sizes {
serverResponseSize.WithLabelValues(t.name, t.handler, t.host, t.path, t.method, strconv.Itoa(code)).Observe(float64(size))
}
}