diff --git a/retrieval/format/discriminator.go b/retrieval/format/discriminator.go index edcf6a423..f05c5f36c 100644 --- a/retrieval/format/discriminator.go +++ b/retrieval/format/discriminator.go @@ -15,6 +15,7 @@ package format import ( "fmt" + "mime" "net/http" ) @@ -39,7 +40,25 @@ func (r *registry) ProcessorForRequestHeader(header http.Header) (processor Proc 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 { case "0.0.1": diff --git a/retrieval/format/discriminator_test.go b/retrieval/format/discriminator_test.go index e7e48036b..7c1fb92bb 100644 --- a/retrieval/format/discriminator_test.go +++ b/retrieval/format/discriminator_test.go @@ -31,12 +31,22 @@ func testDiscriminatorHttpHeader(t test.Tester) { 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, 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, err: nil, },