Skip to content

Commit

Permalink
Handle Prom/OpenMetrics upgrade
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Baillie <[email protected]>
  • Loading branch information
martinbaillie committed Mar 21, 2024
1 parent a7eec52 commit 40320c3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions github/path_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/hashicorp/vault/sdk/logical"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/version"
)

const prefixMetrics = "vault_github_token"
Expand Down Expand Up @@ -96,17 +96,18 @@ func (b *backend) pathMetricsRead(
buf := new(bytes.Buffer)
defer buf.Reset()

text := expfmt.NewFormat(expfmt.TypeTextPlain)
// Write metrics as Prometheus exposition format.
for _, mf := range metricsFamilies {
if err = expfmt.NewEncoder(buf, expfmt.FmtText).Encode(mf); err != nil {
if err = expfmt.NewEncoder(buf, text).Encode(mf); err != nil {
res.Data[logical.HTTPRawBody] = fmt.Sprintf("%s: %s", errFailedMetricsEncoding, err)

return res, fmt.Errorf("%s: %w", errFailedMetricsEncoding, err)
}
}

res.Data[logical.HTTPStatusCode] = http.StatusOK
res.Data[logical.HTTPContentType] = string(expfmt.FmtText)
res.Data[logical.HTTPContentType] = string(text)
res.Data[logical.HTTPRawBody] = buf.Bytes()

return res, nil
Expand Down
2 changes: 1 addition & 1 deletion github/path_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestBackend_PathMetricsRead(t *testing.T) {

assert.NilError(t, err)
assert.Assert(t, statusCode(res.Data[logical.HTTPStatusCode].(int)).Successful())
assert.Equal(t, res.Data[logical.HTTPContentType], string(expfmt.FmtText))
assert.Equal(t, res.Data[logical.HTTPContentType], string(expfmt.NewFormat(expfmt.TypeTextPlain)))
assert.Assert(t, strings.Contains(
string(res.Data[logical.HTTPRawBody].([]byte)),
fmt.Sprintf("%s_build_info", prefixMetrics)),
Expand Down

0 comments on commit 40320c3

Please sign in to comment.