From 76731c80c62b656c72010f4b3aa6f550e4e54ae1 Mon Sep 17 00:00:00 2001 From: Bernerd Schaefer Date: Thu, 25 Apr 2013 16:05:37 +0200 Subject: [PATCH 1/4] 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 edcf6a4230..f05c5f36c5 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 e7e48036b0..7c1fb92bb1 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, }, From 7c3e04c5469f3ccca96c85ee4a3455d0bc5d235d Mon Sep 17 00:00:00 2001 From: Bernerd Schaefer Date: Thu, 25 Apr 2013 17:36:47 +0200 Subject: [PATCH 2/4] Add version 0.0.2 processor --- retrieval/format/discriminator.go | 3 + retrieval/format/processor0_0_2.go | 155 ++++++++++++++ retrieval/format/processor0_0_2_test.go | 263 ++++++++++++++++++++++++ 3 files changed, 421 insertions(+) create mode 100644 retrieval/format/processor0_0_2.go create mode 100644 retrieval/format/processor0_0_2_test.go diff --git a/retrieval/format/discriminator.go b/retrieval/format/discriminator.go index f05c5f36c5..eabafc0af0 100644 --- a/retrieval/format/discriminator.go +++ b/retrieval/format/discriminator.go @@ -61,6 +61,9 @@ func (r *registry) ProcessorForRequestHeader(header http.Header) (processor Proc } switch prometheusApiVersion { + case "0.0.2": + processor = Processor002 + return case "0.0.1": processor = Processor001 return diff --git a/retrieval/format/processor0_0_2.go b/retrieval/format/processor0_0_2.go new file mode 100644 index 0000000000..704565cf26 --- /dev/null +++ b/retrieval/format/processor0_0_2.go @@ -0,0 +1,155 @@ +// Copyright 2013 Prometheus Team +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package format + +import ( + "encoding/json" + "fmt" + "github.com/prometheus/prometheus/model" + "github.com/prometheus/prometheus/utility" + "io" + "io/ioutil" + "time" +) + +const ( + baseLabels002 = "baseLabels" + counter002 = "counter" + docstring002 = "docstring" + gauge002 = "gauge" + histogram002 = "histogram" + labels002 = "labels" + metric002 = "metric" + type002 = "type" + value002 = "value" + percentile002 = "percentile" +) + +var ( + Processor002 Processor = &processor002{} +) + +// processor002 is responsible for handling API version 0.0.2. +type processor002 struct { + time utility.Time +} + +// entity002 represents a the JSON structure that 0.0.2 uses. +type entity002 []struct { + BaseLabels map[string]string `json:"baseLabels"` + Docstring string `json:"docstring"` + Metric struct { + MetricType string `json:"type"` + Value []struct { + Labels map[string]string `json:"labels"` + Value interface{} `json:"value"` + } `json:"value"` + } `json:"metric"` +} + +func (p *processor002) Process(stream io.ReadCloser, timestamp time.Time, baseLabels model.LabelSet, results chan Result) (err error) { + // TODO(matt): Replace with plain-jane JSON unmarshalling. + defer stream.Close() + + buffer, err := ioutil.ReadAll(stream) + if err != nil { + return + } + + entities := entity002{} + + err = json.Unmarshal(buffer, &entities) + if err != nil { + return + } + + // TODO(matt): This outer loop is a great basis for parallelization. + for _, entity := range entities { + for _, value := range entity.Metric.Value { + metric := model.Metric{} + for label, labelValue := range baseLabels { + metric[label] = labelValue + } + + for label, labelValue := range entity.BaseLabels { + metric[model.LabelName(label)] = model.LabelValue(labelValue) + } + + for label, labelValue := range value.Labels { + metric[model.LabelName(label)] = model.LabelValue(labelValue) + } + + switch entity.Metric.MetricType { + case gauge002, counter002: + sampleValue, ok := value.Value.(float64) + if !ok { + err = fmt.Errorf("Could not convert value from %s %s to float64.", entity, value) + continue + } + + sample := model.Sample{ + Metric: metric, + Timestamp: timestamp, + Value: model.SampleValue(sampleValue), + } + + results <- Result{ + Err: err, + Sample: sample, + } + + break + + case histogram002: + sampleValue, ok := value.Value.(map[string]interface{}) + if !ok { + err = fmt.Errorf("Could not convert value from %q to a map[string]interface{}.", value.Value) + continue + } + + for percentile, percentileValue := range sampleValue { + individualValue, ok := percentileValue.(float64) + if !ok { + err = fmt.Errorf("Could not convert value from %q to a float64.", percentileValue) + continue + } + + childMetric := make(map[model.LabelName]model.LabelValue, len(metric)+1) + + for k, v := range metric { + childMetric[k] = v + } + + childMetric[model.LabelName(percentile002)] = model.LabelValue(percentile) + + sample := model.Sample{ + Metric: childMetric, + Timestamp: timestamp, + Value: model.SampleValue(individualValue), + } + + results <- Result{ + Err: err, + Sample: sample, + } + } + + break + default: + } + } + } + + return +} diff --git a/retrieval/format/processor0_0_2_test.go b/retrieval/format/processor0_0_2_test.go new file mode 100644 index 0000000000..e9d5f92e12 --- /dev/null +++ b/retrieval/format/processor0_0_2_test.go @@ -0,0 +1,263 @@ +// Copyright 2013 Prometheus Team +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package format + +import ( + "container/list" + "fmt" + "github.com/prometheus/prometheus/model" + "github.com/prometheus/prometheus/utility/test" + "io/ioutil" + "strings" + "testing" + "time" +) + +func testProcessor002Process(t test.Tester) { + var scenarios = []struct { + in string + out []Result + err error + }{ + { + err: fmt.Errorf("unexpected end of JSON input"), + }, + { + in: "[{\"baseLabels\":{\"name\":\"rpc_calls_total\"},\"docstring\":\"RPC calls.\",\"metric\":{\"type\":\"counter\",\"value\":[{\"labels\":{\"service\":\"zed\"},\"value\":25},{\"labels\":{\"service\":\"bar\"},\"value\":25},{\"labels\":{\"service\":\"foo\"},\"value\":25}]}},{\"baseLabels\":{\"name\":\"rpc_latency_microseconds\"},\"docstring\":\"RPC latency.\",\"metric\":{\"type\":\"histogram\",\"value\":[{\"labels\":{\"service\":\"foo\"},\"value\":{\"0.010000\":15.890724674774395,\"0.050000\":15.890724674774395,\"0.500000\":84.63044031436561,\"0.900000\":160.21100853053224,\"0.990000\":172.49828748957728}},{\"labels\":{\"service\":\"zed\"},\"value\":{\"0.010000\":0.0459814091918713,\"0.050000\":0.0459814091918713,\"0.500000\":0.6120456642749681,\"0.900000\":1.355915069887731,\"0.990000\":1.772733213161236}},{\"labels\":{\"service\":\"bar\"},\"value\":{\"0.010000\":78.48563317257356,\"0.050000\":78.48563317257356,\"0.500000\":97.31798360385088,\"0.900000\":109.89202084295582,\"0.990000\":109.99626121011262}}]}}]", + out: []Result{ + { + Sample: model.Sample{ + Metric: model.Metric{"service": "zed", model.MetricNameLabel: "rpc_calls_total"}, + Value: 25, + }, + }, + { + Sample: model.Sample{ + Metric: model.Metric{"service": "bar", model.MetricNameLabel: "rpc_calls_total"}, + Value: 25, + }, + }, + { + Sample: model.Sample{ + + Metric: model.Metric{"service": "foo", model.MetricNameLabel: "rpc_calls_total"}, + Value: 25, + }, + }, + { + Sample: model.Sample{ + + Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"}, + Value: 0.0459814091918713, + }, + }, + { + Sample: model.Sample{ + + Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"}, + Value: 78.48563317257356, + }, + }, + { + Sample: model.Sample{ + + Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"}, + Value: 15.890724674774395, + }, + }, + { + Sample: model.Sample{ + + Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"}, + Value: 0.0459814091918713, + }, + }, + { + Sample: model.Sample{ + + Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"}, + Value: 78.48563317257356, + }, + }, + { + Sample: model.Sample{ + + Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"}, + Value: 15.890724674774395, + }, + }, + { + Sample: model.Sample{ + + Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"}, + Value: 0.6120456642749681, + }, + }, + { + Sample: model.Sample{ + + Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"}, + Value: 97.31798360385088, + }, + }, + { + Sample: model.Sample{ + + Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"}, + Value: 84.63044031436561, + }, + }, + { + Sample: model.Sample{ + + Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"}, + Value: 1.355915069887731, + }, + }, + { + Sample: model.Sample{ + + Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"}, + Value: 109.89202084295582, + }, + }, + { + Sample: model.Sample{ + + Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"}, + Value: 160.21100853053224, + }, + }, + { + Sample: model.Sample{ + + Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"}, + Value: 1.772733213161236, + }, + }, + { + Sample: model.Sample{ + + Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"}, + Value: 109.99626121011262, + }, + }, + { + Sample: model.Sample{ + + Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"}, + Value: 172.49828748957728, + }, + }, + }, + }, + } + + for i, scenario := range scenarios { + inputChannel := make(chan Result, 1024) + + defer func(c chan Result) { + close(c) + }(inputChannel) + + reader := strings.NewReader(scenario.in) + + err := Processor002.Process(ioutil.NopCloser(reader), time.Now(), model.LabelSet{}, inputChannel) + if !test.ErrorEqual(scenario.err, err) { + t.Errorf("%d. expected err of %s, got %s", i, scenario.err, err) + continue + } + + if scenario.err != nil && err != nil { + if scenario.err.Error() != err.Error() { + t.Errorf("%d. expected err of %s, got %s", i, scenario.err, err) + } + } else if scenario.err != err { + t.Errorf("%d. expected err of %s, got %s", i, scenario.err, err) + } + + delivered := make([]Result, 0) + + for len(inputChannel) != 0 { + delivered = append(delivered, <-inputChannel) + } + + if len(delivered) != len(scenario.out) { + t.Errorf("%d. expected output length of %d, got %d", i, len(scenario.out), len(delivered)) + + continue + } + + expectedElements := list.New() + for _, j := range scenario.out { + expectedElements.PushBack(j) + } + + for j := 0; j < len(delivered); j++ { + actual := delivered[j] + + found := false + for element := expectedElements.Front(); element != nil && found == false; element = element.Next() { + candidate := element.Value.(Result) + + if !test.ErrorEqual(candidate.Err, actual.Err) { + continue + } + + if candidate.Sample.Value != actual.Sample.Value { + continue + } + + if len(candidate.Sample.Metric) != len(actual.Sample.Metric) { + continue + } + + labelsMatch := false + + for key, value := range candidate.Sample.Metric { + actualValue, ok := actual.Sample.Metric[key] + if !ok { + break + } + if actualValue == value { + labelsMatch = true + break + } + } + + if !labelsMatch { + continue + } + + // XXX: Test time. + found = true + expectedElements.Remove(element) + } + + if !found { + t.Errorf("%d.%d. expected to find %s among candidate, absent", i, j, actual.Sample) + } + } + } +} + +func TestProcessor002Process(t *testing.T) { + testProcessor002Process(t) +} + +func BenchmarkProcessor002Process(b *testing.B) { + for i := 0; i < b.N; i++ { + testProcessor002Process(b) + } +} From dfd5c9ce288f70b8b6e83425361ee1750a4c800d Mon Sep 17 00:00:00 2001 From: Bernerd Schaefer Date: Fri, 26 Apr 2013 11:52:26 +0200 Subject: [PATCH 3/4] Refactor processor for 0.0.2 schema Primary changes: * Strictly typed unmarshalling of metric values * Schema types are contained by the processor (no "type entity002") Minor changes: * Added ProcessorFunc type for expressing processors as simple functions. * Added non-destructive `Merge` method to `model.LabelSet` --- model/metric.go | 16 +++ retrieval/format/processor.go | 7 + retrieval/format/processor0_0_2.go | 172 +++++++++--------------- retrieval/format/processor0_0_2_test.go | 12 +- 4 files changed, 90 insertions(+), 117 deletions(-) diff --git a/model/metric.go b/model/metric.go index 8db9d78029..bc4ad771c1 100644 --- a/model/metric.go +++ b/model/metric.go @@ -34,6 +34,22 @@ const ( // match. type LabelSet map[LabelName]LabelValue +// Helper function to non-destructively merge two label sets. +func (l LabelSet) Merge(other LabelSet) LabelSet { + result := make(LabelSet, len(l)) + + for k, v := range l { + result[k] = v + } + + for k, v := range other { + result[k] = v + } + + return result +} + + func (l LabelSet) String() string { var ( buffer bytes.Buffer diff --git a/retrieval/format/processor.go b/retrieval/format/processor.go index 150838dc91..aee63950ba 100644 --- a/retrieval/format/processor.go +++ b/retrieval/format/processor.go @@ -26,3 +26,10 @@ type Processor interface { // Process performs the work on the input and closes the incoming stream. Process(stream io.ReadCloser, timestamp time.Time, baseLabels model.LabelSet, results chan Result) (err error) } + +// The ProcessorFunc type allows the use of ordinary functions for processors. +type ProcessorFunc func(io.ReadCloser, time.Time, model.LabelSet, chan Result) error + +func (f ProcessorFunc) Process(stream io.ReadCloser, timestamp time.Time, baseLabels model.LabelSet, results chan Result) error { + return f(stream, timestamp, baseLabels, results) +} diff --git a/retrieval/format/processor0_0_2.go b/retrieval/format/processor0_0_2.go index 704565cf26..7f18cb7ddb 100644 --- a/retrieval/format/processor0_0_2.go +++ b/retrieval/format/processor0_0_2.go @@ -17,139 +17,97 @@ import ( "encoding/json" "fmt" "github.com/prometheus/prometheus/model" - "github.com/prometheus/prometheus/utility" "io" - "io/ioutil" "time" ) -const ( - baseLabels002 = "baseLabels" - counter002 = "counter" - docstring002 = "docstring" - gauge002 = "gauge" - histogram002 = "histogram" - labels002 = "labels" - metric002 = "metric" - type002 = "type" - value002 = "value" - percentile002 = "percentile" -) +// Processor for telemetry schema version 0.0.2. +var Processor002 ProcessorFunc = func(stream io.ReadCloser, timestamp time.Time, baseLabels model.LabelSet, results chan Result) error { + // container for telemetry data + var entities []struct { + BaseLabels model.LabelSet `json:"baseLabels"` + Docstring string `json:"docstring"` + Metric struct { + Type string `json:"type"` + Values json.RawMessage `json:"value"` + } `json:"metric"` + } -var ( - Processor002 Processor = &processor002{} -) + // concrete type for histogram values + type histogram struct { + Labels model.LabelSet `json:"labels"` + Values map[string]model.SampleValue `json:"value"` + } -// processor002 is responsible for handling API version 0.0.2. -type processor002 struct { - time utility.Time -} + // concrete type for counter and gauge values + type counter struct { + Labels model.LabelSet `json:"labels"` + Value model.SampleValue `json:"value"` + } -// entity002 represents a the JSON structure that 0.0.2 uses. -type entity002 []struct { - BaseLabels map[string]string `json:"baseLabels"` - Docstring string `json:"docstring"` - Metric struct { - MetricType string `json:"type"` - Value []struct { - Labels map[string]string `json:"labels"` - Value interface{} `json:"value"` - } `json:"value"` - } `json:"metric"` -} - -func (p *processor002) Process(stream io.ReadCloser, timestamp time.Time, baseLabels model.LabelSet, results chan Result) (err error) { - // TODO(matt): Replace with plain-jane JSON unmarshalling. defer stream.Close() - buffer, err := ioutil.ReadAll(stream) - if err != nil { - return + if err := json.NewDecoder(stream).Decode(&entities); err != nil { + return err } - entities := entity002{} - - err = json.Unmarshal(buffer, &entities) - if err != nil { - return - } - - // TODO(matt): This outer loop is a great basis for parallelization. for _, entity := range entities { - for _, value := range entity.Metric.Value { - metric := model.Metric{} - for label, labelValue := range baseLabels { - metric[label] = labelValue - } + entityLabels := baseLabels.Merge(entity.BaseLabels) - for label, labelValue := range entity.BaseLabels { - metric[model.LabelName(label)] = model.LabelValue(labelValue) - } + switch entity.Metric.Type { + case "counter", "gauge": + var values []counter - for label, labelValue := range value.Labels { - metric[model.LabelName(label)] = model.LabelValue(labelValue) - } - - switch entity.Metric.MetricType { - case gauge002, counter002: - sampleValue, ok := value.Value.(float64) - if !ok { - err = fmt.Errorf("Could not convert value from %s %s to float64.", entity, value) - continue + if err := json.Unmarshal(entity.Metric.Values, &values); err != nil { + results <- Result{ + Err: fmt.Errorf("Could not extract %s value: %s", entity.Metric.Type, err), } + continue + } - sample := model.Sample{ - Metric: metric, - Timestamp: timestamp, - Value: model.SampleValue(sampleValue), - } + for _, counter := range values { + labels := entityLabels.Merge(counter.Labels) results <- Result{ - Err: err, - Sample: sample, - } - - break - - case histogram002: - sampleValue, ok := value.Value.(map[string]interface{}) - if !ok { - err = fmt.Errorf("Could not convert value from %q to a map[string]interface{}.", value.Value) - continue - } - - for percentile, percentileValue := range sampleValue { - individualValue, ok := percentileValue.(float64) - if !ok { - err = fmt.Errorf("Could not convert value from %q to a float64.", percentileValue) - continue - } - - childMetric := make(map[model.LabelName]model.LabelValue, len(metric)+1) - - for k, v := range metric { - childMetric[k] = v - } - - childMetric[model.LabelName(percentile002)] = model.LabelValue(percentile) - - sample := model.Sample{ - Metric: childMetric, + Sample: model.Sample{ + Metric: model.Metric(labels), Timestamp: timestamp, - Value: model.SampleValue(individualValue), - } + Value: counter.Value, + }, + } + } + + case "histogram": + var values []histogram + + if err := json.Unmarshal(entity.Metric.Values, &values); err != nil { + results <- Result{ + Err: fmt.Errorf("Could not extract %s value: %s", entity.Metric.Type, err), + } + continue + } + + for _, histogram := range values { + for percentile, value := range histogram.Values { + labels := entityLabels.Merge(histogram.Labels) + labels[model.LabelName("percentile")] = model.LabelValue(percentile) results <- Result{ - Err: err, - Sample: sample, + Sample: model.Sample{ + Metric: model.Metric(labels), + Timestamp: timestamp, + Value: value, + }, } } + } - break - default: + default: + results <- Result{ + Err: fmt.Errorf("Unknown metric type %q", entity.Metric.Type), } } } - return + return nil } diff --git a/retrieval/format/processor0_0_2_test.go b/retrieval/format/processor0_0_2_test.go index e9d5f92e12..ece77c7f07 100644 --- a/retrieval/format/processor0_0_2_test.go +++ b/retrieval/format/processor0_0_2_test.go @@ -31,10 +31,10 @@ func testProcessor002Process(t test.Tester) { err error }{ { - err: fmt.Errorf("unexpected end of JSON input"), + err: fmt.Errorf("EOF"), }, { - in: "[{\"baseLabels\":{\"name\":\"rpc_calls_total\"},\"docstring\":\"RPC calls.\",\"metric\":{\"type\":\"counter\",\"value\":[{\"labels\":{\"service\":\"zed\"},\"value\":25},{\"labels\":{\"service\":\"bar\"},\"value\":25},{\"labels\":{\"service\":\"foo\"},\"value\":25}]}},{\"baseLabels\":{\"name\":\"rpc_latency_microseconds\"},\"docstring\":\"RPC latency.\",\"metric\":{\"type\":\"histogram\",\"value\":[{\"labels\":{\"service\":\"foo\"},\"value\":{\"0.010000\":15.890724674774395,\"0.050000\":15.890724674774395,\"0.500000\":84.63044031436561,\"0.900000\":160.21100853053224,\"0.990000\":172.49828748957728}},{\"labels\":{\"service\":\"zed\"},\"value\":{\"0.010000\":0.0459814091918713,\"0.050000\":0.0459814091918713,\"0.500000\":0.6120456642749681,\"0.900000\":1.355915069887731,\"0.990000\":1.772733213161236}},{\"labels\":{\"service\":\"bar\"},\"value\":{\"0.010000\":78.48563317257356,\"0.050000\":78.48563317257356,\"0.500000\":97.31798360385088,\"0.900000\":109.89202084295582,\"0.990000\":109.99626121011262}}]}}]", + in: `[{"baseLabels":{"name":"rpc_calls_total"},"docstring":"RPC calls.","metric":{"type":"counter","value":[{"labels":{"service":"zed"},"value":25},{"labels":{"service":"bar"},"value":25},{"labels":{"service":"foo"},"value":25}]}},{"baseLabels":{"name":"rpc_latency_microseconds"},"docstring":"RPC latency.","metric":{"type":"histogram","value":[{"labels":{"service":"foo"},"value":{"0.010000":15.890724674774395,"0.050000":15.890724674774395,"0.500000":84.63044031436561,"0.900000":160.21100853053224,"0.990000":172.49828748957728}},{"labels":{"service":"zed"},"value":{"0.010000":0.0459814091918713,"0.050000":0.0459814091918713,"0.500000":0.6120456642749681,"0.900000":1.355915069887731,"0.990000":1.772733213161236}},{"labels":{"service":"bar"},"value":{"0.010000":78.48563317257356,"0.050000":78.48563317257356,"0.500000":97.31798360385088,"0.900000":109.89202084295582,"0.990000":109.99626121011262}}]}}]`, out: []Result{ { Sample: model.Sample{ @@ -179,14 +179,6 @@ func testProcessor002Process(t test.Tester) { continue } - if scenario.err != nil && err != nil { - if scenario.err.Error() != err.Error() { - t.Errorf("%d. expected err of %s, got %s", i, scenario.err, err) - } - } else if scenario.err != err { - t.Errorf("%d. expected err of %s, got %s", i, scenario.err, err) - } - delivered := make([]Result, 0) for len(inputChannel) != 0 { From cf3e6ae084c13e6ce73c881a783815e1b26ec6f2 Mon Sep 17 00:00:00 2001 From: Bernerd Schaefer Date: Fri, 26 Apr 2013 14:27:42 +0200 Subject: [PATCH 4/4] Add LabelSet helper to fix go 1.0.3 build --- retrieval/format/processor.go | 14 ++++++++++++++ retrieval/format/processor0_0_2.go | 14 +++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/retrieval/format/processor.go b/retrieval/format/processor.go index aee63950ba..86c28de320 100644 --- a/retrieval/format/processor.go +++ b/retrieval/format/processor.go @@ -33,3 +33,17 @@ type ProcessorFunc func(io.ReadCloser, time.Time, model.LabelSet, chan Result) e func (f ProcessorFunc) Process(stream io.ReadCloser, timestamp time.Time, baseLabels model.LabelSet, results chan Result) error { return f(stream, timestamp, baseLabels, results) } + +// Helper function to convert map[string]string into model.LabelSet. +// +// NOTE: This should be deleted when support for go 1.0.3 is removed; 1.1 is +// smart enough to unmarshal JSON objects into model.LabelSet directly. +func LabelSet(labels map[string]string) model.LabelSet { + labelset := make(model.LabelSet, len(labels)) + + for k, v := range labels { + labelset[model.LabelName(k)] = model.LabelValue(v) + } + + return labelset +} diff --git a/retrieval/format/processor0_0_2.go b/retrieval/format/processor0_0_2.go index 7f18cb7ddb..207063f61c 100644 --- a/retrieval/format/processor0_0_2.go +++ b/retrieval/format/processor0_0_2.go @@ -25,8 +25,8 @@ import ( var Processor002 ProcessorFunc = func(stream io.ReadCloser, timestamp time.Time, baseLabels model.LabelSet, results chan Result) error { // container for telemetry data var entities []struct { - BaseLabels model.LabelSet `json:"baseLabels"` - Docstring string `json:"docstring"` + BaseLabels map[string]string `json:"baseLabels"` + Docstring string `json:"docstring"` Metric struct { Type string `json:"type"` Values json.RawMessage `json:"value"` @@ -35,13 +35,13 @@ var Processor002 ProcessorFunc = func(stream io.ReadCloser, timestamp time.Time, // concrete type for histogram values type histogram struct { - Labels model.LabelSet `json:"labels"` + Labels map[string]string `json:"labels"` Values map[string]model.SampleValue `json:"value"` } // concrete type for counter and gauge values type counter struct { - Labels model.LabelSet `json:"labels"` + Labels map[string]string `json:"labels"` Value model.SampleValue `json:"value"` } @@ -52,7 +52,7 @@ var Processor002 ProcessorFunc = func(stream io.ReadCloser, timestamp time.Time, } for _, entity := range entities { - entityLabels := baseLabels.Merge(entity.BaseLabels) + entityLabels := baseLabels.Merge(LabelSet(entity.BaseLabels)) switch entity.Metric.Type { case "counter", "gauge": @@ -66,7 +66,7 @@ var Processor002 ProcessorFunc = func(stream io.ReadCloser, timestamp time.Time, } for _, counter := range values { - labels := entityLabels.Merge(counter.Labels) + labels := entityLabels.Merge(LabelSet(counter.Labels)) results <- Result{ Sample: model.Sample{ @@ -89,7 +89,7 @@ var Processor002 ProcessorFunc = func(stream io.ReadCloser, timestamp time.Time, for _, histogram := range values { for percentile, value := range histogram.Values { - labels := entityLabels.Merge(histogram.Labels) + labels := entityLabels.Merge(LabelSet(histogram.Labels)) labels[model.LabelName("percentile")] = model.LabelValue(percentile) results <- Result{