-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlog_statistics.go
133 lines (123 loc) · 4.75 KB
/
log_statistics.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
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"expvar"
"fmt"
"net/http"
)
type LogStatistics struct {
GetRequests, GetRequestsFoundOnReplica, GetRequestsNotFound PrometheusMetric
PostRequests, PostRequestsNewFileStored, PostRequestsFailed PrometheusMetric
PutRequests, PutRequestsNewFileStored, PutRequestsMissingFileChecks, PutRequestsFailed PrometheusMetric
ReplicationPushAttempts, ReplicationPushAttemptsFailed PrometheusMetric
ConnectionsCurrent PrometheusMetric
}
func NewLogStatistics() *LogStatistics {
return &LogStatistics{
GetRequests: NewPrometheusMetric(&promMetricOptions{
name: "verm_get_requests_total",
metricType: "counter",
description: "GET requests served",
}),
GetRequestsFoundOnReplica: NewPrometheusMetric(&promMetricOptions{
name: "verm_get_requests_found_on_replica_total",
metricType: "counter",
description: "GET requests found on replica",
}),
GetRequestsNotFound: NewPrometheusMetric(&promMetricOptions{
name: "verm_get_requests_not_found_total",
metricType: "counter",
description: "GET requests not found",
}),
PostRequests: NewPrometheusMetric(&promMetricOptions{
name: "verm_post_requests_total",
metricType: "counter",
description: "POST requests served",
}),
PostRequestsNewFileStored: NewPrometheusMetric(&promMetricOptions{
name: "verm_post_requests_new_file_stored_total",
metricType: "counter",
description: "POST requests resulting in a new file stored",
}),
PostRequestsFailed: NewPrometheusMetric(&promMetricOptions{
name: "verm_post_requests_failed_total",
metricType: "counter",
description: "POST requests failed",
}),
PutRequests: NewPrometheusMetric(&promMetricOptions{
name: "verm_put_requests_total",
metricType: "counter",
description: "PUT requests served",
}),
PutRequestsNewFileStored: NewPrometheusMetric(&promMetricOptions{
name: "verm_put_requests_new_file_stored_total",
metricType: "counter",
description: "PUT requests resulting in a new file stored",
}),
PutRequestsMissingFileChecks: NewPrometheusMetric(&promMetricOptions{
name: "verm_put_requests_missing_file_checks_total",
metricType: "counter",
description: "PUT requests checking for missing files",
}),
PutRequestsFailed: NewPrometheusMetric(&promMetricOptions{
name: "verm_put_requests_failed_total",
metricType: "counter",
description: "PUT requests failed",
}),
ReplicationPushAttempts: NewPrometheusMetric(&promMetricOptions{
name: "verm_replication_push_attempts_total",
metricType: "counter",
description: "Replication push attempts",
}),
ReplicationPushAttemptsFailed: NewPrometheusMetric(&promMetricOptions{
name: "verm_replication_push_attempts_failed_total",
metricType: "counter",
description: "Replication push attempts failed",
}),
ConnectionsCurrent: NewPrometheusMetric(&promMetricOptions{
name: "verm_connections_current",
metricType: "gauge",
description: "HTTP connections",
}),
}
}
func (server vermServer) serveStatistics(w http.ResponseWriter, req *http.Request, replicationTargets *ReplicationTargets) {
w.WriteHeader(http.StatusOK)
server.Statistics.GetRequests.PrintStatistics(w)
server.Statistics.GetRequestsFoundOnReplica.PrintStatistics(w)
server.Statistics.GetRequestsNotFound.PrintStatistics(w)
server.Statistics.PostRequests.PrintStatistics(w)
server.Statistics.PostRequestsNewFileStored.PrintStatistics(w)
server.Statistics.PostRequestsFailed.PrintStatistics(w)
server.Statistics.PutRequests.PrintStatistics(w)
server.Statistics.PutRequestsNewFileStored.PrintStatistics(w)
server.Statistics.PutRequestsMissingFileChecks.PrintStatistics(w)
server.Statistics.PutRequestsFailed.PrintStatistics(w)
server.Statistics.ReplicationPushAttempts.PrintStatistics(w)
server.Statistics.ReplicationPushAttemptsFailed.PrintStatistics(w)
server.Statistics.ConnectionsCurrent.PrintStatistics(w)
fmt.Fprintf(w, "%s", replicationTargets.StatisticsString())
}
type PrometheusMetric interface {
Add(int64)
PrintStatistics(w http.ResponseWriter)
}
type promMetricOptions struct {
name string
metricType string
description string
}
type promMetric struct {
metric *expvar.Int
options *promMetricOptions
}
func NewPrometheusMetric(options *promMetricOptions) PrometheusMetric {
return &promMetric{metric: expvar.NewInt(options.name), options: options}
}
func (pi *promMetric) PrintStatistics(w http.ResponseWriter) {
fmt.Fprintf(w, "# HELP %s %s\n", pi.options.name, pi.options.description)
fmt.Fprintf(w, "# TYPE %s %s\n", pi.options.name, pi.options.metricType)
fmt.Fprintf(w, "%s %s\n", pi.options.name, pi.metric.String())
}
func (pi *promMetric) Add(val int64) {
pi.metric.Add(val)
}