From 76731c80c62b656c72010f4b3aa6f550e4e54ae1 Mon Sep 17 00:00:00 2001 From: Bernerd Schaefer Date: Thu, 25 Apr 2013 16:05:37 +0200 Subject: [PATCH] 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`. --- retrieval/format/discriminator.go | 21 ++++++++++++++++++++- retrieval/format/discriminator_test.go | 14 ++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) 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, },