2018-10-09 06:09:44 -07:00
|
|
|
// Copyright 2018 The Prometheus Authors
|
2018-10-04 06:52:03 -07:00
|
|
|
// 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 (
|
2018-10-05 09:11:16 -07:00
|
|
|
"mime"
|
|
|
|
|
2023-10-18 11:04:02 -07:00
|
|
|
"github.com/gogo/protobuf/types"
|
|
|
|
|
2021-11-08 06:23:17 -08:00
|
|
|
"github.com/prometheus/prometheus/model/exemplar"
|
Style cleanup of all the changes in sparsehistogram so far
A lot of this code was hacked together, literally during a
hackathon. This commit intends not to change the code substantially,
but just make the code obey the usual style practices.
A (possibly incomplete) list of areas:
* Generally address linter warnings.
* The `pgk` directory is deprecated as per dev-summit. No new packages should
be added to it. I moved the new `pkg/histogram` package to `model`
anticipating what's proposed in #9478.
* Make the naming of the Sparse Histogram more consistent. Including
abbreviations, there were just too many names for it: SparseHistogram,
Histogram, Histo, hist, his, shs, h. The idea is to call it "Histogram" in
general. Only add "Sparse" if it is needed to avoid confusion with
conventional Histograms (which is rare because the TSDB really has no notion
of conventional Histograms). Use abbreviations only in local scope, and then
really abbreviate (not just removing three out of seven letters like in
"Histo"). This is in the spirit of
https://github.com/golang/go/wiki/CodeReviewComments#variable-names
* Several other minor name changes.
* A lot of formatting of doc comments. For one, following
https://github.com/golang/go/wiki/CodeReviewComments#comment-sentences
, but also layout question, anticipating how things will look like
when rendered by `godoc` (even where `godoc` doesn't render them
right now because they are for unexported types or not a doc comment
at all but just a normal code comment - consistency is queen!).
* Re-enabled `TestQueryLog` and `TestEndopints` (they pass now,
leaving them disabled was presumably an oversight).
* Bucket iterator for histogram.Histogram is now created with a
method.
* HistogramChunk.iterator now allows iterator recycling. (I think
@dieterbe only commented it out because he was confused by the
question in the comment.)
* HistogramAppender.Append panics now because we decided to treat
staleness marker differently.
Signed-off-by: beorn7 <beorn@grafana.com>
2021-10-09 06:57:07 -07:00
|
|
|
"github.com/prometheus/prometheus/model/histogram"
|
2021-11-08 06:23:17 -08:00
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
2018-10-04 06:52:03 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Parser parses samples from a byte slice of samples in the official
|
2018-10-04 09:57:47 -07:00
|
|
|
// Prometheus and OpenMetrics text exposition formats.
|
2018-10-04 06:52:03 -07:00
|
|
|
type Parser interface {
|
2021-06-29 14:45:23 -07:00
|
|
|
// 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.
|
2018-10-04 06:52:03 -07:00
|
|
|
Series() ([]byte, *int64, float64)
|
|
|
|
|
2021-06-29 14:45:23 -07:00
|
|
|
// Histogram returns the bytes of a series with a sparse histogram as a
|
Style cleanup of all the changes in sparsehistogram so far
A lot of this code was hacked together, literally during a
hackathon. This commit intends not to change the code substantially,
but just make the code obey the usual style practices.
A (possibly incomplete) list of areas:
* Generally address linter warnings.
* The `pgk` directory is deprecated as per dev-summit. No new packages should
be added to it. I moved the new `pkg/histogram` package to `model`
anticipating what's proposed in #9478.
* Make the naming of the Sparse Histogram more consistent. Including
abbreviations, there were just too many names for it: SparseHistogram,
Histogram, Histo, hist, his, shs, h. The idea is to call it "Histogram" in
general. Only add "Sparse" if it is needed to avoid confusion with
conventional Histograms (which is rare because the TSDB really has no notion
of conventional Histograms). Use abbreviations only in local scope, and then
really abbreviate (not just removing three out of seven letters like in
"Histo"). This is in the spirit of
https://github.com/golang/go/wiki/CodeReviewComments#variable-names
* Several other minor name changes.
* A lot of formatting of doc comments. For one, following
https://github.com/golang/go/wiki/CodeReviewComments#comment-sentences
, but also layout question, anticipating how things will look like
when rendered by `godoc` (even where `godoc` doesn't render them
right now because they are for unexported types or not a doc comment
at all but just a normal code comment - consistency is queen!).
* Re-enabled `TestQueryLog` and `TestEndopints` (they pass now,
leaving them disabled was presumably an oversight).
* Bucket iterator for histogram.Histogram is now created with a
method.
* HistogramChunk.iterator now allows iterator recycling. (I think
@dieterbe only commented it out because he was confused by the
question in the comment.)
* HistogramAppender.Append panics now because we decided to treat
staleness marker differently.
Signed-off-by: beorn7 <beorn@grafana.com>
2021-10-09 06:57:07 -07:00
|
|
|
// value, the timestamp if set, and the histogram in the current sample.
|
2022-08-25 08:07:41 -07:00
|
|
|
// Depending on the parsed input, the function returns an (integer) Histogram
|
|
|
|
// or a FloatHistogram, with the respective other return value being nil.
|
|
|
|
Histogram() ([]byte, *int64, *histogram.Histogram, *histogram.FloatHistogram)
|
2021-06-29 14:45:23 -07:00
|
|
|
|
2018-10-04 06:52:03 -07:00
|
|
|
// 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)
|
|
|
|
|
2018-10-04 09:57:47 -07:00
|
|
|
// 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)
|
|
|
|
|
2018-10-04 06:52:03 -07:00
|
|
|
// 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
|
|
|
|
|
2019-11-19 01:33:30 -08:00
|
|
|
// Exemplar writes the exemplar of the current sample into the passed
|
2023-07-13 05:16:10 -07:00
|
|
|
// exemplar. It can be called repeatedly to retrieve multiple exemplars
|
|
|
|
// for the same sample. It returns false once all exemplars are
|
|
|
|
// retrieved (including the case where no exemplars exist at all).
|
2019-11-19 01:33:30 -08:00
|
|
|
Exemplar(l *exemplar.Exemplar) bool
|
|
|
|
|
2023-10-18 11:04:02 -07:00
|
|
|
// CreatedTimestamp writes the created timestamp of the current sample
|
|
|
|
// into the passed timestamp. It returns false if no created timestamp
|
|
|
|
// exists or if the metric type does not support created timestamps.
|
|
|
|
CreatedTimestamp(ct *types.Timestamp) bool
|
|
|
|
|
2018-10-04 06:52:03 -07:00
|
|
|
// 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.
|
2022-02-08 01:57:56 -08:00
|
|
|
//
|
|
|
|
// This function always returns a valid parser, but might additionally
|
|
|
|
// return an error if the content type cannot be parsed.
|
2023-05-10 16:59:21 -07:00
|
|
|
func New(b []byte, contentType string, parseClassicHistograms bool) (Parser, error) {
|
2022-02-08 01:57:56 -08:00
|
|
|
if contentType == "" {
|
|
|
|
return NewPromParser(b), nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 09:11:16 -07:00
|
|
|
mediaType, _, err := mime.ParseMediaType(contentType)
|
2021-06-29 14:45:23 -07:00
|
|
|
if err != nil {
|
2022-03-22 06:47:42 -07:00
|
|
|
return NewPromParser(b), err
|
2021-06-29 14:45:23 -07:00
|
|
|
}
|
|
|
|
switch mediaType {
|
|
|
|
case "application/openmetrics-text":
|
2022-02-08 01:57:56 -08:00
|
|
|
return NewOpenMetricsParser(b), nil
|
2021-06-29 14:45:23 -07:00
|
|
|
case "application/vnd.google.protobuf":
|
2023-05-10 16:59:21 -07:00
|
|
|
return NewProtobufParser(b, parseClassicHistograms), nil
|
2021-06-29 14:45:23 -07:00
|
|
|
default:
|
2022-03-22 06:47:42 -07:00
|
|
|
return NewPromParser(b), nil
|
2018-10-05 09:11:16 -07:00
|
|
|
}
|
2018-10-04 06:52:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Entry represents the type of a parsed entry.
|
|
|
|
type Entry int
|
|
|
|
|
|
|
|
const (
|
2021-06-29 14:45:23 -07:00
|
|
|
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
|
2023-05-10 16:59:21 -07:00
|
|
|
EntryHistogram Entry = 5 // A series with a native histogram as a value.
|
2018-10-04 06:52:03 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// MetricType represents metric type values.
|
|
|
|
type MetricType string
|
|
|
|
|
|
|
|
const (
|
2020-10-27 03:06:53 -07:00
|
|
|
MetricTypeCounter = MetricType("counter")
|
|
|
|
MetricTypeGauge = MetricType("gauge")
|
|
|
|
MetricTypeHistogram = MetricType("histogram")
|
|
|
|
MetricTypeGaugeHistogram = MetricType("gaugehistogram")
|
|
|
|
MetricTypeSummary = MetricType("summary")
|
|
|
|
MetricTypeInfo = MetricType("info")
|
|
|
|
MetricTypeStateset = MetricType("stateset")
|
|
|
|
MetricTypeUnknown = MetricType("unknown")
|
2018-10-04 06:52:03 -07:00
|
|
|
)
|