mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-26 06:04:05 -08:00
Remove Prometheus internal metric type from interface
This commit is contained in:
parent
40c27a1c2f
commit
3ad4db56aa
70
querier.go
70
querier.go
|
@ -2,26 +2,24 @@ package tsdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/fabxc/tsdb/chunks"
|
"github.com/fabxc/tsdb/chunks"
|
||||||
"github.com/fabxc/tsdb/index"
|
"github.com/fabxc/tsdb/index"
|
||||||
"github.com/prometheus/common/model"
|
"github.com/prometheus/common/model"
|
||||||
"github.com/prometheus/prometheus/storage/metric"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// SeriesIterator provides iteration over a time series associated with a metric.
|
// SeriesIterator provides iteration over a time series associated with a metric.
|
||||||
type SeriesIterator interface {
|
type SeriesIterator interface {
|
||||||
Metric() metric.Metric
|
Metric() map[string]string
|
||||||
Seek(model.Time) (model.SamplePair, bool)
|
Seek(model.Time) (model.SamplePair, bool)
|
||||||
Next() (model.SamplePair, bool)
|
Next() (model.SamplePair, bool)
|
||||||
Err() error
|
Err() error
|
||||||
}
|
}
|
||||||
|
|
||||||
type chunkSeriesIterator struct {
|
type chunkSeriesIterator struct {
|
||||||
m metric.Metric
|
m map[string]string
|
||||||
chunks []chunks.Chunk
|
chunks []chunks.Chunk
|
||||||
|
|
||||||
err error
|
err error
|
||||||
|
@ -29,14 +27,14 @@ type chunkSeriesIterator struct {
|
||||||
curPos int
|
curPos int
|
||||||
}
|
}
|
||||||
|
|
||||||
func newChunkSeriesIterator(m metric.Metric, chunks []chunks.Chunk) *chunkSeriesIterator {
|
func newChunkSeriesIterator(m map[string]string, chunks []chunks.Chunk) *chunkSeriesIterator {
|
||||||
return &chunkSeriesIterator{
|
return &chunkSeriesIterator{
|
||||||
m: m,
|
m: m,
|
||||||
chunks: chunks,
|
chunks: chunks,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it *chunkSeriesIterator) Metric() metric.Metric {
|
func (it *chunkSeriesIterator) Metric() map[string]string {
|
||||||
return it.m
|
return it.m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,34 +104,8 @@ func (q *Querier) Close() error {
|
||||||
|
|
||||||
// Iterator returns an iterator over all chunks that match all given
|
// Iterator returns an iterator over all chunks that match all given
|
||||||
// label matchers. The iterator is only valid until the Querier is closed.
|
// label matchers. The iterator is only valid until the Querier is closed.
|
||||||
func (q *Querier) Iterator(matchers ...*metric.LabelMatcher) (index.Iterator, error) {
|
func (q *Querier) Iterator(key string, matcher index.Matcher) (index.Iterator, error) {
|
||||||
var its []index.Iterator
|
return q.iq.Search(key, matcher)
|
||||||
for _, m := range matchers {
|
|
||||||
var matcher index.Matcher
|
|
||||||
switch m.Type {
|
|
||||||
case metric.Equal:
|
|
||||||
matcher = index.NewEqualMatcher(string(m.Value))
|
|
||||||
case metric.RegexMatch:
|
|
||||||
var err error
|
|
||||||
matcher, err = index.NewRegexpMatcher(string(m.Value))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("matcher type %q not supported", m.Type)
|
|
||||||
}
|
|
||||||
it, err := q.iq.Search(string(m.Name), matcher)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if it != nil {
|
|
||||||
its = append(its, it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(its) == 0 {
|
|
||||||
return nil, errors.New("not found")
|
|
||||||
}
|
|
||||||
return index.Intersect(its...), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RangeIterator returns an iterator over chunks that are present in the given time range.
|
// RangeIterator returns an iterator over chunks that are present in the given time range.
|
||||||
|
@ -148,11 +120,15 @@ func (q *Querier) InstantIterator(at model.Time) (index.Iterator, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hash(m map[string]string) uint64 {
|
||||||
|
return model.LabelsToSignature(m)
|
||||||
|
}
|
||||||
|
|
||||||
// Series returns a list of series iterators over all chunks in the given iterator.
|
// Series returns a list of series iterators over all chunks in the given iterator.
|
||||||
// The returned series iterators are only valid until the querier is closed.
|
// The returned series iterators are only valid until the querier is closed.
|
||||||
func (q *Querier) Series(it index.Iterator) ([]SeriesIterator, error) {
|
func (q *Querier) Series(it index.Iterator) ([]SeriesIterator, error) {
|
||||||
mets := map[model.Fingerprint]metric.Metric{}
|
mets := map[uint64]map[string]string{}
|
||||||
its := map[model.Fingerprint][]chunks.Chunk{}
|
its := map[uint64][]chunks.Chunk{}
|
||||||
|
|
||||||
id, err := it.Seek(0)
|
id, err := it.Seek(0)
|
||||||
for ; err == nil; id, err = it.Next() {
|
for ; err == nil; id, err = it.Next() {
|
||||||
|
@ -160,11 +136,11 @@ func (q *Querier) Series(it index.Iterator) ([]SeriesIterator, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
met := make(model.Metric, len(terms))
|
met := make(map[string]string, len(terms))
|
||||||
for _, t := range terms {
|
for _, t := range terms {
|
||||||
met[model.LabelName(t.Field)] = model.LabelValue(t.Val)
|
met[t.Field] = t.Val
|
||||||
}
|
}
|
||||||
fp := met.Fingerprint()
|
fp := hash(met)
|
||||||
|
|
||||||
chunk, err := q.chunk(ChunkID(id))
|
chunk, err := q.chunk(ChunkID(id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -175,7 +151,7 @@ func (q *Querier) Series(it index.Iterator) ([]SeriesIterator, error) {
|
||||||
if _, ok := mets[fp]; ok {
|
if _, ok := mets[fp]; ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
mets[fp] = metric.Metric{Metric: met, Copied: true}
|
mets[fp] = met
|
||||||
}
|
}
|
||||||
if err != io.EOF {
|
if err != io.EOF {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -220,9 +196,9 @@ func (q *Querier) chunk(id ChunkID) (chunks.Chunk, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Metrics returns the unique metrics found across all chunks in the provided iterator.
|
// Metrics returns the unique metrics found across all chunks in the provided iterator.
|
||||||
func (q *Querier) Metrics(it index.Iterator) ([]metric.Metric, error) {
|
func (q *Querier) Metrics(it index.Iterator) ([]map[string]string, error) {
|
||||||
m := []metric.Metric{}
|
m := []map[string]string{}
|
||||||
fps := map[model.Fingerprint]struct{}{}
|
fps := map[uint64]struct{}{}
|
||||||
|
|
||||||
id, err := it.Seek(0)
|
id, err := it.Seek(0)
|
||||||
for ; err == nil; id, err = it.Next() {
|
for ; err == nil; id, err = it.Next() {
|
||||||
|
@ -230,16 +206,16 @@ func (q *Querier) Metrics(it index.Iterator) ([]metric.Metric, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
met := make(model.Metric, len(terms))
|
met := make(map[string]string, len(terms))
|
||||||
for _, t := range terms {
|
for _, t := range terms {
|
||||||
met[model.LabelName(t.Field)] = model.LabelValue(t.Val)
|
met[t.Field] = t.Val
|
||||||
}
|
}
|
||||||
fp := met.Fingerprint()
|
fp := hash(met)
|
||||||
if _, ok := fps[fp]; ok {
|
if _, ok := fps[fp]; ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fps[fp] = struct{}{}
|
fps[fp] = struct{}{}
|
||||||
m = append(m, metric.Metric{Metric: met, Copied: true})
|
m = append(m, met)
|
||||||
}
|
}
|
||||||
if err != io.EOF {
|
if err != io.EOF {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in a new issue