Use Content-Type data for telemetry versioning

ProcessorForRequestHeader now looks first for a header like
`Content-Type: application/json; schema="prometheus/telemetry";
version="0.0.1"` before falling back to checking
`X-Prometheus-API-Version`.
pull/195/merge
Bernerd Schaefer 12 years ago
parent 19fc094362
commit 76731c80c6

@ -15,6 +15,7 @@ package format
import ( import (
"fmt" "fmt"
"mime"
"net/http" "net/http"
) )
@ -39,7 +40,25 @@ func (r *registry) ProcessorForRequestHeader(header http.Header) (processor Proc
return return
} }
prometheusApiVersion := header.Get("X-Prometheus-API-Version") mediatype, params, err := mime.ParseMediaType(header.Get("Content-Type"))
if err != nil {
err = fmt.Errorf("Invalid Content-Type header %q: %s", header.Get("Content-Type"), err)
return
}
if mediatype != "application/json" {
err = fmt.Errorf("Unsupported media type %q, expected %q", mediatype, "application/json")
return
}
var prometheusApiVersion string
if params["schema"] == "prometheus/telemetry" && params["version"] != "" {
prometheusApiVersion = params["version"]
} else {
prometheusApiVersion = header.Get("X-Prometheus-API-Version")
}
switch prometheusApiVersion { switch prometheusApiVersion {
case "0.0.1": case "0.0.1":

@ -31,12 +31,22 @@ func testDiscriminatorHttpHeader(t test.Tester) {
err: fmt.Errorf("Received illegal and nil header."), err: fmt.Errorf("Received illegal and nil header."),
}, },
{ {
input: map[string]string{"X-Prometheus-API-Version": "0.0.0"}, input: map[string]string{"Content-Type": "application/json", "X-Prometheus-API-Version": "0.0.0"},
output: nil, output: nil,
err: fmt.Errorf("Unrecognized API version 0.0.0"), err: fmt.Errorf("Unrecognized API version 0.0.0"),
}, },
{ {
input: map[string]string{"X-Prometheus-API-Version": "0.0.1"}, input: map[string]string{"Content-Type": "application/json", "X-Prometheus-API-Version": "0.0.1"},
output: Processor001,
err: nil,
},
{
input: map[string]string{"Content-Type": `application/json; schema="prometheus/telemetry"; version=0.0.0`},
output: nil,
err: fmt.Errorf("Unrecognized API version 0.0.0"),
},
{
input: map[string]string{"Content-Type": `application/json; schema="prometheus/telemetry"; version=0.0.1`},
output: Processor001, output: Processor001,
err: nil, err: nil,
}, },

Loading…
Cancel
Save