mirror of
https://github.com/prometheus/prometheus.git
synced 2025-01-29 06:30:51 -08:00
Add version 0.0.2 processor
This commit is contained in:
parent
76731c80c6
commit
7c3e04c546
|
@ -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
|
||||
|
|
155
retrieval/format/processor0_0_2.go
Normal file
155
retrieval/format/processor0_0_2.go
Normal file
|
@ -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
|
||||
}
|
263
retrieval/format/processor0_0_2_test.go
Normal file
263
retrieval/format/processor0_0_2_test.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue