/
metricsutil.go
131 lines (114 loc) · 3.41 KB
/
metricsutil.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
package metricsutil
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/armon/go-metrics"
"github.com/hashicorp/vault/sdk/logical"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
)
const (
OpenMetricsMIMEType = "application/openmetrics-text"
PrometheusSchemaMIMEType = "prometheus/telemetry"
// ErrorContentType is the content type returned by an error response.
ErrorContentType = "text/plain"
)
const (
PrometheusMetricFormat = "prometheus"
)
type MetricsHelper struct {
inMemSink *metrics.InmemSink
PrometheusEnabled bool
}
func NewMetricsHelper(inMem *metrics.InmemSink, enablePrometheus bool) *MetricsHelper {
return &MetricsHelper{inMem, enablePrometheus}
}
func FormatFromRequest(req *logical.Request) string {
acceptHeaders := req.Headers["Accept"]
if len(acceptHeaders) > 0 {
acceptHeader := acceptHeaders[0]
if strings.HasPrefix(acceptHeader, OpenMetricsMIMEType) {
return PrometheusMetricFormat
}
// Look for prometheus accept header
for _, header := range acceptHeaders {
if strings.Contains(header, PrometheusSchemaMIMEType) {
return PrometheusMetricFormat
}
}
}
return ""
}
func (m *MetricsHelper) ResponseForFormat(format string) *logical.Response {
switch format {
case PrometheusMetricFormat:
return m.PrometheusResponse()
case "":
return m.GenericResponse()
default:
return &logical.Response{
Data: map[string]interface{}{
logical.HTTPContentType: ErrorContentType,
logical.HTTPRawBody: fmt.Sprintf("metric response format \"%s\" unknown", format),
logical.HTTPStatusCode: http.StatusBadRequest,
},
}
}
}
func (m *MetricsHelper) PrometheusResponse() *logical.Response {
resp := &logical.Response{
Data: map[string]interface{}{
logical.HTTPContentType: ErrorContentType,
logical.HTTPStatusCode: http.StatusBadRequest,
},
}
if !m.PrometheusEnabled {
resp.Data[logical.HTTPRawBody] = "prometheus is not enabled"
return resp
}
metricsFamilies, err := prometheus.DefaultGatherer.Gather()
if err != nil && len(metricsFamilies) == 0 {
resp.Data[logical.HTTPRawBody] = fmt.Sprintf("no prometheus metrics could be decoded: %s", err)
return resp
}
// Initialize a byte buffer.
buf := &bytes.Buffer{}
defer buf.Reset()
e := expfmt.NewEncoder(buf, expfmt.FmtText)
for _, mf := range metricsFamilies {
err := e.Encode(mf)
if err != nil {
resp.Data[logical.HTTPRawBody] = fmt.Sprintf("error during the encoding of metrics: %s", err)
return resp
}
}
resp.Data[logical.HTTPContentType] = string(expfmt.FmtText)
resp.Data[logical.HTTPRawBody] = buf.Bytes()
resp.Data[logical.HTTPStatusCode] = http.StatusOK
return resp
}
func (m *MetricsHelper) GenericResponse() *logical.Response {
resp := &logical.Response{
Data: map[string]interface{}{
logical.HTTPContentType: ErrorContentType,
logical.HTTPStatusCode: http.StatusBadRequest,
},
}
summary, err := m.inMemSink.DisplayMetrics(nil, nil)
if err != nil {
resp.Data[logical.HTTPRawBody] = fmt.Sprintf("error while fetching the in-memory metrics: %s", err)
return resp
}
content, err := json.Marshal(summary)
if err != nil {
resp.Data[logical.HTTPRawBody] = fmt.Sprintf("error while marshalling the in-memory metrics: %s", err)
return resp
}
resp.Data[logical.HTTPContentType] = "application/json"
resp.Data[logical.HTTPRawBody] = content
resp.Data[logical.HTTPStatusCode] = http.StatusOK
return resp
}