interface.go 3.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright 2014 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 storage

import (
17
	"context"
18 19 20 21 22
	"errors"

	"github.com/prometheus/prometheus/pkg/labels"
)

23
// The errors exposed.
24
var (
25
	ErrNotFound                    = errors.New("not found")
26 27
	ErrOutOfOrderSample            = errors.New("out of order sample")
	ErrDuplicateSampleForTimestamp = errors.New("duplicate sample for timestamp")
28
	ErrOutOfBounds                 = errors.New("out of bounds")
29 30 31 32 33
)

// Storage ingests and manages samples, along with various indexes. All methods
// are goroutine-safe. Storage implements storage.SampleAppender.
type Storage interface {
34 35
	Queryable

36 37 38
	// StartTime returns the oldest timestamp stored in the storage.
	StartTime() (int64, error)

39 40 41 42 43 44 45
	// Appender returns a new appender against the storage.
	Appender() (Appender, error)

	// Close closes the storage and all its underlying resources.
	Close() error
}

46 47 48 49 50 51
// A Queryable handles queries against a storage.
type Queryable interface {
	// Querier returns a new Querier on the storage.
	Querier(ctx context.Context, mint, maxt int64) (Querier, error)
}

52 53 54
// Querier provides reading access to time series data.
type Querier interface {
	// Select returns a set of series that matches the given label matchers.
55
	Select(*SelectParams, ...*labels.Matcher) (SeriesSet, error, Warnings)
56 57 58 59

	// LabelValues returns all potential values for a label name.
	LabelValues(name string) ([]string, error)

60 61 62
	// LabelNames returns all the unique label names present in the block in sorted order.
	LabelNames() ([]string, error)

63 64 65 66
	// Close releases the resources of the Querier.
	Close() error
}

67 68
// SelectParams specifies parameters passed to data selections.
type SelectParams struct {
69 70 71
	Start int64 // Start time in milliseconds for this select.
	End   int64 // End time in milliseconds for this select.

72 73 74 75
	Step int64  // Query step size in milliseconds.
	Func string // String representation of surrounding function or aggregation.
}

76 77 78 79 80 81 82 83 84
// QueryableFunc is an adapter to allow the use of ordinary functions as
// Queryables. It follows the idea of http.HandlerFunc.
type QueryableFunc func(ctx context.Context, mint, maxt int64) (Querier, error)

// Querier calls f() with the given parameters.
func (f QueryableFunc) Querier(ctx context.Context, mint, maxt int64) (Querier, error) {
	return f(ctx, mint, maxt)
}

85 86
// Appender provides batched appends against a storage.
type Appender interface {
87
	Add(l labels.Labels, t int64, v float64) (uint64, error)
88

89
	AddFast(l labels.Labels, ref uint64, t int64, v float64) error
90 91 92

	// Commit submits the collected samples and purges the batch.
	Commit() error
93 94

	Rollback() error
95 96 97 98 99
}

// SeriesSet contains a set of series.
type SeriesSet interface {
	Next() bool
100
	At() Series
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	Err() error
}

// Series represents a single time series.
type Series interface {
	// Labels returns the complete set of labels identifying the series.
	Labels() labels.Labels

	// Iterator returns a new iterator of the data of the series.
	Iterator() SeriesIterator
}

// SeriesIterator iterates over the data of a time series.
type SeriesIterator interface {
	// Seek advances the iterator forward to the value at or after
	// the given timestamp.
	Seek(t int64) bool
118 119
	// At returns the current timestamp/value pair.
	At() (t int64, v float64)
120 121 122 123 124
	// Next advances the iterator by one.
	Next() bool
	// Err returns the current error.
	Err() error
}
125 126

type Warnings []error