2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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"
|
2020-10-27 15:24:43 +00:00
|
|
|
"sync"
|
2019-07-19 01:04:56 +00:00
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2020-10-27 15:24:43 +00:00
|
|
|
// PhysicalTableSizeName is a set of gauge metric keys for physical mount table sizes
|
|
|
|
var PhysicalTableSizeName []string = []string{"core", "mount_table", "size"}
|
|
|
|
|
|
|
|
// LogicalTableSizeName is a set of gauge metric keys for logical mount table sizes
|
|
|
|
var LogicalTableSizeName []string = []string{"core", "mount_table", "num_entries"}
|
|
|
|
|
2019-02-14 20:46:59 +00:00
|
|
|
type MetricsHelper struct {
|
2019-02-20 20:12:21 +00:00
|
|
|
inMemSink *metrics.InmemSink
|
2019-02-14 20:46:59 +00:00
|
|
|
PrometheusEnabled bool
|
2020-10-27 15:24:43 +00:00
|
|
|
LoopMetrics GaugeMetrics
|
|
|
|
}
|
|
|
|
|
|
|
|
type GaugeMetrics struct {
|
|
|
|
// Metrics is a map from keys concatenated by "." to the metric.
|
|
|
|
// It is a map because although we do not care about distinguishing
|
|
|
|
// these loop metrics during emission, we must distinguish them
|
|
|
|
// when we update a metric.
|
|
|
|
Metrics sync.Map
|
|
|
|
}
|
|
|
|
|
|
|
|
type GaugeMetric struct {
|
|
|
|
Value float32
|
|
|
|
Labels []Label
|
|
|
|
Key []string
|
2019-02-14 20:46:59 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 20:12:21 +00:00
|
|
|
func NewMetricsHelper(inMem *metrics.InmemSink, enablePrometheus bool) *MetricsHelper {
|
2020-10-27 15:24:43 +00:00
|
|
|
return &MetricsHelper{inMem, enablePrometheus, GaugeMetrics{Metrics: sync.Map{}}}
|
2019-02-14 20:46:59 +00:00
|
|
|
}
|
|
|
|
|
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 ""
|
|
|
|
}
|
|
|
|
|
2020-10-27 15:24:43 +00:00
|
|
|
func (m *MetricsHelper) AddGaugeLoopMetric(key []string, val float32, labels []Label) {
|
|
|
|
mapKey := m.CreateMetricsCacheKeyName(key, val, labels)
|
|
|
|
m.LoopMetrics.Metrics.Store(mapKey,
|
|
|
|
GaugeMetric{
|
|
|
|
Key: key,
|
|
|
|
Value: val,
|
2021-04-08 16:43:39 +00:00
|
|
|
Labels: labels,
|
|
|
|
})
|
2020-10-27 15:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MetricsHelper) CreateMetricsCacheKeyName(key []string, val float32, labels []Label) string {
|
|
|
|
var keyJoin string = strings.Join(key, ".")
|
2021-04-08 16:43:39 +00:00
|
|
|
labelJoinStr := ""
|
2020-10-27 15:24:43 +00:00
|
|
|
for _, label := range labels {
|
|
|
|
labelJoinStr = labelJoinStr + label.Name + "|" + label.Value + "||"
|
|
|
|
}
|
|
|
|
keyJoin = keyJoin + "." + labelJoinStr
|
|
|
|
return keyJoin
|
|
|
|
}
|
|
|
|
|
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,
|
2022-08-03 18:32:45 +00:00
|
|
|
logical.HTTPRawBody: fmt.Sprintf("metric response format %q unknown", format),
|
2019-10-08 12:55:25 +00:00
|
|
|
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
|
|
|
}
|