Skip to content

Commit

Permalink
Set OpenMetrics content header
Browse files Browse the repository at this point in the history
  • Loading branch information
mrueg committed Feb 5, 2023
1 parent 5da24c2 commit 49af6b3
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/metricshandler/metrics_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"strings"
"sync"

"github.com/prometheus/common/expfmt"

appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
Expand Down Expand Up @@ -183,7 +185,9 @@ func (m *MetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
resHeader := w.Header()
var writer io.Writer = w

resHeader.Set("Content-Type", `text/plain; version=`+"0.0.4")
contentType := expfmt.NegotiateIncludingOpenMetrics(r.Header)

resHeader.Set("Content-Type", string(contentType))

if m.enableGZIPEncoding {
// Gzip response if requested. Taken from
Expand All @@ -206,6 +210,11 @@ func (m *MetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

// If we send openmetrics, we need to include a EOF directive
if contentType == expfmt.FmtOpenMetrics {
w.Write([]byte("# EOF\n"))
}

// In case we gzipped the response, we have to close the writer.
if closer, ok := writer.(io.Closer); ok {
err := closer.Close()
Expand Down

0 comments on commit 49af6b3

Please sign in to comment.