prometheus/retrieval/format/processor0_0_2.go

114 lines
2.9 KiB
Go
Raw Normal View History

2013-04-25 15:36:47 +00:00
// 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"
"io"
"time"
)
// 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"`
}
2013-04-25 15:36:47 +00:00
// concrete type for histogram values
type histogram struct {
Labels model.LabelSet `json:"labels"`
Values map[string]model.SampleValue `json:"value"`
}
2013-04-25 15:36:47 +00:00
// concrete type for counter and gauge values
type counter struct {
Labels model.LabelSet `json:"labels"`
Value model.SampleValue `json:"value"`
2013-04-25 15:36:47 +00:00
}
defer stream.Close()
2013-04-25 15:36:47 +00:00
if err := json.NewDecoder(stream).Decode(&entities); err != nil {
return err
2013-04-25 15:36:47 +00:00
}
for _, entity := range entities {
entityLabels := baseLabels.Merge(entity.BaseLabels)
2013-04-25 15:36:47 +00:00
switch entity.Metric.Type {
case "counter", "gauge":
var values []counter
2013-04-25 15:36:47 +00:00
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),
2013-04-25 15:36:47 +00:00
}
continue
}
2013-04-25 15:36:47 +00:00
for _, counter := range values {
labels := entityLabels.Merge(counter.Labels)
2013-04-25 15:36:47 +00:00
results <- Result{
Sample: model.Sample{
Metric: model.Metric(labels),
Timestamp: timestamp,
Value: counter.Value,
},
2013-04-25 15:36:47 +00:00
}
}
2013-04-25 15:36:47 +00:00
case "histogram":
var values []histogram
2013-04-25 15:36:47 +00:00
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),
2013-04-25 15:36:47 +00:00
}
continue
}
2013-04-25 15:36:47 +00:00
for _, histogram := range values {
for percentile, value := range histogram.Values {
labels := entityLabels.Merge(histogram.Labels)
labels[model.LabelName("percentile")] = model.LabelValue(percentile)
2013-04-25 15:36:47 +00:00
results <- Result{
Sample: model.Sample{
Metric: model.Metric(labels),
Timestamp: timestamp,
Value: value,
},
2013-04-25 15:36:47 +00:00
}
}
}
2013-04-25 15:36:47 +00:00
default:
results <- Result{
Err: fmt.Errorf("Unknown metric type %q", entity.Metric.Type),
2013-04-25 15:36:47 +00:00
}
}
}
return nil
2013-04-25 15:36:47 +00:00
}