Skip to content

Commit

Permalink
Changes Accept header function of api version used.
Browse files Browse the repository at this point in the history
  - v1: `application/openmetrics-text; version=0.0.1,text/plain;version=0.0.4;q=0.5,*/*;q=0.1`
  - v2: `text/plain; version=0.0.4; charset=utf-8`
  • Loading branch information
gmllt committed Jan 12, 2023
1 parent 82206db commit b22754b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 24 deletions.
20 changes: 9 additions & 11 deletions api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,12 @@ import (
"strings"

"github.com/gorilla/mux"
"github.com/orange-cloudfoundry/promfetcher/models"
"github.com/prometheus/common/expfmt"

"github.com/orange-cloudfoundry/promfetcher/errors"
)

func (a Api) metrics(w http.ResponseWriter, req *http.Request) {
// extract the API version from the requested path (ie: /v2)
// and set it to an HTTP header
apiVersion := regexp.MustCompile("/v([0-9]+)(?:/|$)").FindStringSubmatch(req.URL.Path)
if len(apiVersion) == 2 {
req.Header.Set(models.XPromfetcherApiVersion, apiVersion[1])
} else {
// default to v1
req.Header.Set(models.XPromfetcherApiVersion, "1")
}

appIdOrPathOrName, ok := mux.Vars(req)["appIdOrPathOrName"]
if !ok {
appIdOrPathOrName = req.URL.Query().Get("app")
Expand All @@ -47,6 +36,15 @@ func (a Api) metrics(w http.ResponseWriter, req *http.Request) {
_, onlyAppMetrics := req.URL.Query()["only_from_app"]

headersMetrics := make(http.Header)
// extract the API version from the requested path (ie: /v2)
// and set it to an HTTP header
apiVersion := regexp.MustCompile("/v([0-9]+)(?:/|$)").FindStringSubmatch(req.URL.Path)
if len(apiVersion) == 2 && apiVersion[1] != "1" {
headersMetrics.Set("Accept", string(expfmt.FmtText))
} else {
headersMetrics.Set("Accept", `application/openmetrics-text; version=0.0.1,text/plain;version=0.0.4;q=0.5,*/*;q=0.1`)
}

auth := req.Header.Get("Authorization")
if auth != "" {
headersMetrics.Set("Authorization", auth)
Expand Down
4 changes: 0 additions & 4 deletions models/api.go

This file was deleted.

9 changes: 0 additions & 9 deletions scrapers/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/orange-cloudfoundry/promfetcher/clients"
"github.com/orange-cloudfoundry/promfetcher/errors"
"github.com/orange-cloudfoundry/promfetcher/models"
"github.com/prometheus/common/expfmt"
)

type Scraper struct {
Expand Down Expand Up @@ -68,14 +67,6 @@ func (s Scraper) Scrape(route *models.Route, metricPathDefault string, headers h
req.Header[k] = v
}
}
// Prometheus parser is not OpenMetrics compliant
// See: prometheus/common issues: 214, 829
req.Header.Set("Accept", string(expfmt.FmtText))
// keep the OpenMetrics accept HTTP header for the /v1 endpoint
if req.Header.Get(models.XPromfetcherApiVersion) == "1" {
req.Header.Set(models.XPromfetcherApiVersion, "1")
req.Header.Set("Accept", `application/openmetrics-text; version=0.0.1,text/plain;version=0.0.4;q=0.5,*/*;q=0.1`)
}
req.Header.Add("Accept-Encoding", "gzip")
req.Header.Set("X-Prometheus-Scrape-Timeout-Seconds", fmt.Sprintf("%f", (30*time.Second).Seconds()))
req.Header.Set("X-Forwarded-Proto", scheme)
Expand Down

0 comments on commit b22754b

Please sign in to comment.