mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-18 11:34:05 -08:00
5de2df752f
This "brings back" protobuf parsing, with the only goal to play with the new sparse histograms. The Prom-2.x style parser is highly adapted to the structure of the Prometheus text format (and later OpenMetrics). Some jumping through hoops is required to feed protobuf into it. This is not meant to be a model for the final implementation. It should just enable sparse histogram ingestion at a reasonable efficiency. Following known shortcomings and flaws: - No tests yet. - Summaries and legacy histograms, i.e. without sparse buckets, are ignored. - Staleness doesn't work (but this could be fixed in the appender, to be discussed). - No tricks have been tried that would be similar to the tricks the text parsers do (like direct pointers into the HTTP response body). That makes things weird here. Tricky optimizations only make sense once the final format is specified, which will almost certainly not be the old protobuf format. (Interestingly, I expect this implementation to be in fact much more efficient than the original protobuf ingestion in Prom-1.x.) - This is using a proto3 version of metrics.proto (mostly to be consistent with the other protobuf uses). However, proto3 sees no difference between an unset field. We depend on that to distinguish between an unset timestamp and the timestamp 0 (1970-01-01, 00:00:00 UTC). In this experimental code, we just assume that timestamp is never specified and therefore a timestamp of 0 always is interpreted as "not set". Signed-off-by: beorn7 <beorn@grafana.com>
111 lines
3.8 KiB
Go
111 lines
3.8 KiB
Go
// Copyright 2018 The Prometheus Authors
|
|
// 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 textparse
|
|
|
|
import (
|
|
"mime"
|
|
|
|
"github.com/prometheus/prometheus/pkg/exemplar"
|
|
"github.com/prometheus/prometheus/pkg/histogram"
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
|
)
|
|
|
|
// Parser parses samples from a byte slice of samples in the official
|
|
// Prometheus and OpenMetrics text exposition formats.
|
|
type Parser interface {
|
|
// Series returns the bytes of a series with a simple float64 as a
|
|
// value, the timestamp if set, and the value of the current sample.
|
|
Series() ([]byte, *int64, float64)
|
|
|
|
// Histogram returns the bytes of a series with a sparse histogram as a
|
|
// value, the timestamp if set, and the sparse histogram in the current
|
|
// sample.
|
|
Histogram() ([]byte, *int64, histogram.SparseHistogram)
|
|
|
|
// Help returns the metric name and help text in the current entry.
|
|
// Must only be called after Next returned a help entry.
|
|
// The returned byte slices become invalid after the next call to Next.
|
|
Help() ([]byte, []byte)
|
|
|
|
// Type returns the metric name and type in the current entry.
|
|
// Must only be called after Next returned a type entry.
|
|
// The returned byte slices become invalid after the next call to Next.
|
|
Type() ([]byte, MetricType)
|
|
|
|
// Unit returns the metric name and unit in the current entry.
|
|
// Must only be called after Next returned a unit entry.
|
|
// The returned byte slices become invalid after the next call to Next.
|
|
Unit() ([]byte, []byte)
|
|
|
|
// Comment returns the text of the current comment.
|
|
// Must only be called after Next returned a comment entry.
|
|
// The returned byte slice becomes invalid after the next call to Next.
|
|
Comment() []byte
|
|
|
|
// Metric writes the labels of the current sample into the passed labels.
|
|
// It returns the string from which the metric was parsed.
|
|
Metric(l *labels.Labels) string
|
|
|
|
// Exemplar writes the exemplar of the current sample into the passed
|
|
// exemplar. It returns if an exemplar exists or not.
|
|
Exemplar(l *exemplar.Exemplar) bool
|
|
|
|
// Next advances the parser to the next sample. It returns false if no
|
|
// more samples were read or an error occurred.
|
|
Next() (Entry, error)
|
|
}
|
|
|
|
// New returns a new parser of the byte slice.
|
|
func New(b []byte, contentType string) Parser {
|
|
mediaType, _, err := mime.ParseMediaType(contentType)
|
|
if err != nil {
|
|
return NewPromParser(b)
|
|
}
|
|
switch mediaType {
|
|
case "application/openmetrics-text":
|
|
return NewOpenMetricsParser(b)
|
|
case "application/vnd.google.protobuf":
|
|
return NewProtobufParser(b)
|
|
default:
|
|
return NewPromParser(b)
|
|
}
|
|
}
|
|
|
|
// Entry represents the type of a parsed entry.
|
|
type Entry int
|
|
|
|
const (
|
|
EntryInvalid Entry = -1
|
|
EntryType Entry = 0
|
|
EntryHelp Entry = 1
|
|
EntrySeries Entry = 2 // A series with a simple float64 as value.
|
|
EntryComment Entry = 3
|
|
EntryUnit Entry = 4
|
|
EntryHistogram Entry = 5 // A series with a sparse histogram as a value.
|
|
)
|
|
|
|
// MetricType represents metric type values.
|
|
type MetricType string
|
|
|
|
const (
|
|
MetricTypeCounter = MetricType("counter")
|
|
MetricTypeGauge = MetricType("gauge")
|
|
MetricTypeHistogram = MetricType("histogram")
|
|
MetricTypeGaugeHistogram = MetricType("gaugehistogram")
|
|
MetricTypeSummary = MetricType("summary")
|
|
MetricTypeInfo = MetricType("info")
|
|
MetricTypeStateset = MetricType("stateset")
|
|
MetricTypeUnknown = MetricType("unknown")
|
|
)
|