prometheus/model/textparse/interface.go

198 lines
5.5 KiB
Go
Raw Permalink Normal View History

// 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/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 09:57:07 -04:00
"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels"
)
// Parser parses samples from a byte slice of samples in the official
// Prometheus and OpenMetrics text exposition formats.
type Parser interface {
// Parse returns the next metric with all information collected about it, such as
// metric type, help text, unit, created timestamps, samples, etc.
Next(d DropperCache, keepClassicHistogramSeries bool) (interface{}, error)
2024-09-30 08:05:02 -04:00
}
type DropperCache interface {
Get(rawSeriesId []byte) (isDropped, isKnown bool)
Set(rawSeriesId []byte, lbls labels.Labels) (isDropped bool)
}
// New returns a new parser of the byte slice.
//
// This function always returns a valid parser, but might additionally
// return an error if the content type cannot be parsed.
func New(b []byte, contentType string, parseClassicHistograms bool, st *labels.SymbolTable) (Parser, error) {
if contentType == "" {
return NewPromParser(b, st), nil
}
// mediaType, _, err := mime.ParseMediaType(contentType)
// if err != nil {
// return NewPromParser(b, st), err
// }
// switch mediaType {
// case "application/openmetrics-text":
// return NewOpenMetricsParser(b, st), nil
// case "application/vnd.google.protobuf":
// return NewProtobufParser(b, parseClassicHistograms, st), nil
// default:
// return NewPromParser(b, st), nil
// }
return NewPromParser(b, st), nil
}
type BaseExposedMetric interface {
Name() string
// Labels() labels.Labels
Help() (string, bool)
// Unit() (string, bool)
// Timestamp() (int64, bool)
// CreatedTimestamp() (int64, bool)
// // A uniq identifier for the metric for putting into the dropper cache.
// RawSeriesId() []byte
// IsGauge() bool
// Exemplars() []*exemplar.Exemplar
}
2024-09-30 08:05:02 -04:00
// Untyped float value
type FloatMetric interface {
BaseExposedMetric
Value() float64
}
2024-09-30 08:05:02 -04:00
type FloatCounterMetric interface {
BaseExposedMetric
Value() float64
}
2024-09-30 08:05:02 -04:00
type FloatGaugeMetric interface {
BaseExposedMetric
Value() float64
}
2024-09-30 08:05:02 -04:00
type HistogramCounterMetric interface {
BaseExposedMetric
SumValue() float64
CountValue() float64
CustomBuckets() bool
Buckets() ExposedBucketIterator
Native() (*histogram.Histogram, *histogram.FloatHistogram)
2024-09-30 08:05:02 -04:00
}
type HistogramGaugeMetric interface {
BaseExposedMetric
SumValue() float64
CountValue() float64
CustomBuckets() bool
Buckets() ExposedBucketIterator
Native() (*histogram.Histogram, *histogram.FloatHistogram)
2024-09-30 08:05:02 -04:00
}
type SummaryMetric interface {
BaseExposedMetric
SumValue() float64
CountValue() float64
Quantiles() ExposedQuantileIterator
}
type ExposedBucketIterator interface {
Next() bool
At() ExposedBucket
}
2024-09-30 08:05:02 -04:00
type ExposedBucket struct {
UpperBound float64
Count float64
Exemplar *exemplar.Exemplar
}
2024-09-30 08:05:02 -04:00
type ExposedQuantileIterator interface {
Next() bool
At() ExposedQuantile
2024-09-30 08:05:02 -04:00
}
type ExposedQuantile struct {
Quantile float64
Value float64
2024-09-30 08:05:02 -04:00
}
// // ExposedValues holds the values of a metric, the purpose is to group the values in one place and reuse the memory as much as possible.
// type ExposedValues struct {
// flags ExposureFlags
// timestamp int64
// createdTimestamp int64
// sum float64 // For Counter value, Gauge value, Histogram Sum, Summary Sum and Unknown value.
// count float64 // For Histogram Count, Summary Count.
// counts []ExposedBoundaryCount // For classic histogram bucket counts (cumulative) and summary quantile values.
// h *histogram.Histogram // For native histogram. There is no hasH as this is a pointer that can be nil.
// fh *histogram.FloatHistogram // For native float histogram. There is no hasFH as this is a pointer that can be nil.
// // For native histograms and eventually summaries.
// exemplars []exemplar.Exemplar
// help string
// unit string
// }
// type ExposedBoundaryCount struct {
// store bool // Whether to store this bucket count value (according to relabel drop rules).
// boundary float64
// count uint64
// hasExemplar bool
// exemplar exemplar.Exemplar
// }
// type ExposureFlags uint64
// const (
// ExposureFlagHasTimestamp = 1 << iota
// ExposureFlagHasCreatedTimestamp
// ExposureFlagHasSum
// ExposureFlagStoreSum
// ExposureFlagHasCount
// ExposureFlagStoreCount
// ExposureFlagHasHelp
// ExposureFlagHasUnit
// // No need to have flag for native histograms, we can just check if h or fh is nil.
// )
// // Reset values to zero, reuse memory if possible.
// // For native histograms, the commit will use the value so we need to reset to nil.
// func (v *ExposedValues) Reset() {
// v.flags = 0
// v.h = nil
// v.fh = nil
// v.counts = v.counts[:0]
// v.exemplars = v.exemplars[:0]
// }
// func (v *ExposedValues) SetTimestamp(t int64) {
// v.timestamp = t
// v.flags |= ExposureFlagHasTimestamp
// }
// func (v *ExposedValues) SetCreatedTimestamp(t int64) {
// v.createdTimestamp = t
// v.flags |= ExposureFlagHasCreatedTimestamp
// }