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"
|
|
|
|
|
2021-11-08 06:23:17 -08:00
|
|
|
"github.com/prometheus/prometheus/model/exemplar"
|
|
|
|
"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 {
|
|
|
|
// Series returns the bytes of the series, the timestamp if set, and the value
|
|
|
|
// of the current sample.
|
|
|
|
Series() ([]byte, *int64, float64)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// exemplar. It returns if an exemplar exists or not.
|
|
|
|
Exemplar(l *exemplar.Exemplar) 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.
|
|
|
|
func New(b []byte, contentType string) Parser {
|
2018-10-05 09:11:16 -07:00
|
|
|
mediaType, _, err := mime.ParseMediaType(contentType)
|
|
|
|
if err == nil && mediaType == "application/openmetrics-text" {
|
2018-10-09 06:09:44 -07:00
|
|
|
return NewOpenMetricsParser(b)
|
2018-10-05 09:11:16 -07:00
|
|
|
}
|
2018-10-04 06:52:03 -07:00
|
|
|
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
|
|
|
|
EntryComment Entry = 3
|
2018-10-04 09:57:47 -07:00
|
|
|
EntryUnit Entry = 4
|
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
|
|
|
)
|