Skip to content

Commit

Permalink
Add accept header check for prometheus mime type (#7958)
Browse files Browse the repository at this point in the history
* Add accept header check for prometheus mime type

* Fix small header filter bug. Add test
  • Loading branch information
michelvocks authored and briankassouf committed Dec 11, 2019
1 parent 73fd8f3 commit e1b6971
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
11 changes: 10 additions & 1 deletion helper/metricsutil/metricsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
const (
OpenMetricsMIMEType = "application/openmetrics-text"

PrometheusSchemaMIMEType = "prometheus/telemetry"

// ErrorContentType is the content type returned by an error response.
ErrorContentType = "text/plain"
)
Expand All @@ -38,7 +40,14 @@ func FormatFromRequest(req *logical.Request) string {
if len(acceptHeaders) > 0 {
acceptHeader := acceptHeaders[0]
if strings.HasPrefix(acceptHeader, OpenMetricsMIMEType) {
return "prometheus"
return PrometheusMetricFormat
}

// Look for prometheus accept header
for _, header := range acceptHeaders {
if strings.Contains(header, PrometheusSchemaMIMEType) {
return PrometheusMetricFormat
}
}
}
return ""
Expand Down
45 changes: 45 additions & 0 deletions helper/metricsutil/metricsutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package metricsutil

import (
"github.com/hashicorp/vault/sdk/logical"
"testing"
)

func TestFormatFromRequest(t *testing.T) {
testCases := []struct {
original *logical.Request
expected string
}{
{
original: &logical.Request{Headers: map[string][]string{
"Accept": {
"application/vnd.google.protobuf",
"schema=\"prometheus/telemetry\"",
},
}},
expected: "prometheus",
},
{
original: &logical.Request{Headers: map[string][]string{
"Accept": {
"schema=\"prometheus\"",
},
}},
expected: "",
},
{
original: &logical.Request{Headers: map[string][]string{
"Accept": {
"application/openmetrics-text",
},
}},
expected: "prometheus",
},
}

for _, tCase := range testCases {
if metricsType := FormatFromRequest(tCase.original); metricsType != tCase.expected {
t.Fatalf("expected %s but got %s", tCase.expected, metricsType)
}
}
}

0 comments on commit e1b6971

Please sign in to comment.