-
Notifications
You must be signed in to change notification settings - Fork 2
/
metrics.go
76 lines (71 loc) · 3.12 KB
/
metrics.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
package chonker
import (
"fmt"
"github.com/VictoriaMetrics/metrics"
)
// StatsForNerds exposes Prometheus metrics for chonker requests.
// Metric names are prefixed with "chonker_".
// Metrics are labeled with request host URLs.
//
// The following metrics are exposed for a request to https://example.com:
//
// chonker_http_requests_fetching{host="example.com"}
// chonker_http_requests_total{host="example.com"}
// chonker_http_requests_total{host="example.com",range="false"}
// chonker_http_request_chunks_fetching{host="example.com",stage="do"}
// chonker_http_request_chunks_fetching{host="example.com",stage="copy"}
// chonker_http_request_chunks_total{host="example.com"}
// chonker_http_request_chunk_duration_seconds{host="example.com"}
// chonker_http_request_chunk_bytes{host="example.com"}
//
// You can surface these metrics in your application using the
// [metrics.RegisterSet] function.
//
// [metrics.RegisterSet]: https://pkg.go.dev/github.com/VictoriaMetrics/metrics#RegisterSet
var StatsForNerds = metrics.NewSet()
type hostMetrics struct {
// requestsFetching is the number of currently active requests to a host.
requestsFetching *metrics.Gauge
// requestsTotal is the total number of requests completed to a host.
requestsTotal *metrics.Counter
// requestsTotalSansRange is the total number of requests completed to a host
// that did not use range requests.
requestsTotalSansRange *metrics.Counter
// requestChunksFetching is the number of currently active request chunks to a host.
requestChunksFetchingStageDo *metrics.Gauge
requestChunksFetchingStageCopy *metrics.Gauge
// requestChunksTotal is the total number of request chunks completed to a host.
requestChunksTotal *metrics.Counter
// requestChunkDurationSeconds measures the duration of request chunks to a host.
requestChunkDurationSeconds *metrics.Histogram
// requestChunkBytes measures the number of bytes fetched in request chunks to a host.
requestChunkBytes *metrics.Histogram
}
func getHostMetrics(host string) *hostMetrics {
return &hostMetrics{
requestsFetching: StatsForNerds.GetOrCreateGauge(
fmt.Sprintf(`chonker_http_requests_fetching{host="%s"}`, host), nil,
),
requestsTotal: StatsForNerds.GetOrCreateCounter(
fmt.Sprintf(`chonker_http_requests_total{host="%s"}`, host),
),
requestsTotalSansRange: StatsForNerds.GetOrCreateCounter(
fmt.Sprintf(`chonker_http_requests_total{host="%s",range="false"}`, host),
),
requestChunksFetchingStageDo: StatsForNerds.GetOrCreateGauge(
fmt.Sprintf(`chonker_http_request_chunks_fetching{host="%s",stage="do"}`, host), nil,
),
requestChunksFetchingStageCopy: StatsForNerds.GetOrCreateGauge(
fmt.Sprintf(`chonker_http_request_chunks_fetching{host="%s",stage="copy"}`, host), nil,
),
requestChunksTotal: StatsForNerds.GetOrCreateCounter(
fmt.Sprintf(`chonker_http_request_chunks_total{host="%s"}`, host),
),
requestChunkDurationSeconds: StatsForNerds.GetOrCreateHistogram(
fmt.Sprintf(`chonker_http_request_chunk_duration_seconds{host="%s"}`, host),
),
requestChunkBytes: StatsForNerds.GetOrCreateHistogram(
fmt.Sprintf(`chonker_http_request_chunk_bytes{host="%s"}`, host),
),
}
}