2019-02-14 20:46:59 +00:00
|
|
|
package metricsutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-10-08 12:55:25 +00:00
|
|
|
"net/http"
|
2019-07-19 01:04:56 +00:00
|
|
|
"strings"
|
|
|
|
|
2019-02-14 20:46:59 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2019-02-14 20:46:59 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/common/expfmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
OpenMetricsMIMEType = "application/openmetrics-text"
|
2019-10-08 12:55:25 +00:00
|
|
|
|
2019-12-11 19:59:19 +00:00
|
|
|
PrometheusSchemaMIMEType = "prometheus/telemetry"
|
|
|
|
|
2019-10-08 12:55:25 +00:00
|
|
|
// ErrorContentType is the content type returned by an error response.
|
|
|
|
ErrorContentType = "text/plain"
|
2019-02-14 20:46:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
PrometheusMetricFormat = "prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MetricsHelper struct {
|
2019-02-20 20:12:21 +00:00
|
|
|
inMemSink *metrics.InmemSink
|
2019-02-14 20:46:59 +00:00
|
|
|
PrometheusEnabled bool
|
|
|
|
}
|
|
|
|
|
2019-02-20 20:12:21 +00:00
|
|
|
func NewMetricsHelper(inMem *metrics.InmemSink, enablePrometheus bool) *MetricsHelper {
|
2019-02-14 20:46:59 +00:00
|
|
|
return &MetricsHelper{inMem, enablePrometheus}
|
|
|
|
}
|
|
|
|
|
2019-02-20 20:12:21 +00:00
|
|
|
func FormatFromRequest(req *logical.Request) string {
|
2019-02-14 20:46:59 +00:00
|
|
|
acceptHeaders := req.Headers["Accept"]
|
|
|
|
if len(acceptHeaders) > 0 {
|
|
|
|
acceptHeader := acceptHeaders[0]
|
|
|
|
if strings.HasPrefix(acceptHeader, OpenMetricsMIMEType) {
|
2019-12-11 19:59:19 +00:00
|
|
|
return PrometheusMetricFormat
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for prometheus accept header
|
|
|
|
for _, header := range acceptHeaders {
|
|
|
|
if strings.Contains(header, PrometheusSchemaMIMEType) {
|
|
|
|
return PrometheusMetricFormat
|
|
|
|
}
|
2019-02-14 20:46:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-10-08 12:55:25 +00:00
|
|
|
func (m *MetricsHelper) ResponseForFormat(format string) *logical.Response {
|
2019-02-14 20:46:59 +00:00
|
|
|
switch format {
|
|
|
|
case PrometheusMetricFormat:
|
|
|
|
return m.PrometheusResponse()
|
|
|
|
case "":
|
|
|
|
return m.GenericResponse()
|
|
|
|
default:
|
2019-10-08 12:55:25 +00:00
|
|
|
return &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
logical.HTTPContentType: ErrorContentType,
|
|
|
|
logical.HTTPRawBody: fmt.Sprintf("metric response format \"%s\" unknown", format),
|
|
|
|
logical.HTTPStatusCode: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
}
|
2019-02-14 20:46:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 12:55:25 +00:00
|
|
|
func (m *MetricsHelper) PrometheusResponse() *logical.Response {
|
|
|
|
resp := &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
logical.HTTPContentType: ErrorContentType,
|
|
|
|
logical.HTTPStatusCode: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-02-14 20:46:59 +00:00
|
|
|
if !m.PrometheusEnabled {
|
2019-10-08 12:55:25 +00:00
|
|
|
resp.Data[logical.HTTPRawBody] = "prometheus is not enabled"
|
|
|
|
return resp
|
2019-02-14 20:46:59 +00:00
|
|
|
}
|
|
|
|
metricsFamilies, err := prometheus.DefaultGatherer.Gather()
|
|
|
|
if err != nil && len(metricsFamilies) == 0 {
|
2019-10-08 12:55:25 +00:00
|
|
|
resp.Data[logical.HTTPRawBody] = fmt.Sprintf("no prometheus metrics could be decoded: %s", err)
|
|
|
|
return resp
|
2019-02-14 20:46:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2019-10-08 12:55:25 +00:00
|
|
|
resp.Data[logical.HTTPRawBody] = fmt.Sprintf("error during the encoding of metrics: %s", err)
|
|
|
|
return resp
|
2019-02-14 20:46:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-08 12:55:25 +00:00
|
|
|
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{
|
2019-02-14 20:46:59 +00:00
|
|
|
Data: map[string]interface{}{
|
2019-10-08 12:55:25 +00:00
|
|
|
logical.HTTPContentType: ErrorContentType,
|
|
|
|
logical.HTTPStatusCode: http.StatusBadRequest,
|
2019-02-14 20:46:59 +00:00
|
|
|
},
|
2019-10-08 12:55:25 +00:00
|
|
|
}
|
2019-02-14 20:46:59 +00:00
|
|
|
|
2019-02-20 20:12:21 +00:00
|
|
|
summary, err := m.inMemSink.DisplayMetrics(nil, nil)
|
2019-02-14 20:46:59 +00:00
|
|
|
if err != nil {
|
2019-10-08 12:55:25 +00:00
|
|
|
resp.Data[logical.HTTPRawBody] = fmt.Sprintf("error while fetching the in-memory metrics: %s", err)
|
|
|
|
return resp
|
2019-02-14 20:46:59 +00:00
|
|
|
}
|
|
|
|
content, err := json.Marshal(summary)
|
|
|
|
if err != nil {
|
2019-10-08 12:55:25 +00:00
|
|
|
resp.Data[logical.HTTPRawBody] = fmt.Sprintf("error while marshalling the in-memory metrics: %s", err)
|
|
|
|
return resp
|
2019-02-14 20:46:59 +00:00
|
|
|
}
|
2019-10-08 12:55:25 +00:00
|
|
|
resp.Data[logical.HTTPContentType] = "application/json"
|
|
|
|
resp.Data[logical.HTTPRawBody] = content
|
|
|
|
resp.Data[logical.HTTPStatusCode] = http.StatusOK
|
|
|
|
return resp
|
2019-02-14 20:46:59 +00:00
|
|
|
}
|