2017-07-06 05:38:40 -07:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
2017-07-10 06:44:29 -07:00
|
|
|
package api_v2
|
2017-07-06 05:38:40 -07:00
|
|
|
|
|
|
|
import (
|
2017-10-24 21:21:42 -07:00
|
|
|
"context"
|
2017-07-06 05:38:40 -07:00
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"math/rand"
|
2018-11-13 05:42:23 -08:00
|
|
|
"net"
|
2017-07-06 05:38:40 -07:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
|
|
|
"github.com/pkg/errors"
|
2019-11-18 11:53:33 -08:00
|
|
|
|
2019-04-04 02:55:32 -07:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2017-10-24 21:21:42 -07:00
|
|
|
|
2019-11-18 11:53:33 -08:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2017-07-06 05:38:40 -07:00
|
|
|
"github.com/prometheus/prometheus/pkg/timestamp"
|
|
|
|
pb "github.com/prometheus/prometheus/prompb"
|
2019-11-18 11:53:33 -08:00
|
|
|
"github.com/prometheus/prometheus/tsdb"
|
2017-07-06 05:38:40 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// API encapsulates all API services.
|
|
|
|
type API struct {
|
2018-08-29 00:25:46 -07:00
|
|
|
enableAdmin bool
|
|
|
|
db func() *tsdb.DB
|
2017-07-06 05:38:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new API object.
|
|
|
|
func New(
|
2017-09-18 03:32:17 -07:00
|
|
|
db func() *tsdb.DB,
|
2017-07-10 00:29:41 -07:00
|
|
|
enableAdmin bool,
|
2017-07-06 05:38:40 -07:00
|
|
|
) *API {
|
|
|
|
return &API{
|
2018-08-29 00:25:46 -07:00
|
|
|
db: db,
|
|
|
|
enableAdmin: enableAdmin,
|
2017-07-06 05:38:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterGRPC registers all API services with the given server.
|
|
|
|
func (api *API) RegisterGRPC(srv *grpc.Server) {
|
2017-07-10 00:29:41 -07:00
|
|
|
if api.enableAdmin {
|
|
|
|
pb.RegisterAdminServer(srv, NewAdmin(api.db))
|
|
|
|
} else {
|
2017-12-02 21:07:05 -08:00
|
|
|
pb.RegisterAdminServer(srv, &AdminDisabled{})
|
2017-07-10 00:29:41 -07:00
|
|
|
}
|
2017-07-06 05:38:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPHandler returns an HTTP handler for a REST API gateway to the given grpc address.
|
2019-01-15 06:32:05 -08:00
|
|
|
func (api *API) HTTPHandler(ctx context.Context, grpcAddr string) (http.Handler, error) {
|
2019-07-09 01:23:44 -07:00
|
|
|
enc := new(runtime.JSONPb)
|
2017-07-06 05:38:40 -07:00
|
|
|
mux := runtime.NewServeMux(runtime.WithMarshalerOption(enc.ContentType(), enc))
|
|
|
|
|
2018-11-13 05:42:23 -08:00
|
|
|
opts := []grpc.DialOption{
|
|
|
|
grpc.WithInsecure(),
|
|
|
|
// Replace the default dialer that connects through proxy when HTTP_PROXY is set.
|
2019-04-04 02:55:32 -07:00
|
|
|
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
|
2018-11-13 05:42:23 -08:00
|
|
|
return (&net.Dialer{}).DialContext(ctx, "tcp", addr)
|
|
|
|
}),
|
|
|
|
}
|
2017-07-06 05:38:40 -07:00
|
|
|
|
|
|
|
err := pb.RegisterAdminHandlerFromEndpoint(ctx, mux, grpcAddr, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return mux, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractTimeRange returns minimum and maximum timestamp in milliseconds as
|
|
|
|
// provided by the time range. It defaults either boundary to the minimum and maximum
|
|
|
|
// possible value.
|
|
|
|
func extractTimeRange(min, max *time.Time) (mint, maxt time.Time, err error) {
|
|
|
|
if min == nil {
|
|
|
|
mint = minTime
|
|
|
|
} else {
|
|
|
|
mint = *min
|
|
|
|
}
|
|
|
|
if max == nil {
|
|
|
|
maxt = maxTime
|
|
|
|
} else {
|
|
|
|
maxt = *max
|
|
|
|
}
|
|
|
|
if mint.After(maxt) {
|
2019-02-03 01:29:23 -08:00
|
|
|
return mint, maxt, errors.Errorf("min time must be before or equal to max time")
|
2017-07-06 05:38:40 -07:00
|
|
|
}
|
|
|
|
return mint, maxt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
minTime = time.Unix(math.MinInt64/1000+62135596801, 0)
|
|
|
|
maxTime = time.Unix(math.MaxInt64/1000-62135596801, 999999999)
|
|
|
|
)
|
|
|
|
|
2019-02-04 04:06:26 -08:00
|
|
|
var (
|
|
|
|
errAdminDisabled = status.Error(codes.Unavailable, "Admin APIs are disabled")
|
|
|
|
errTSDBNotReady = status.Error(codes.Unavailable, "TSDB not ready")
|
|
|
|
)
|
|
|
|
|
2017-12-02 21:07:05 -08:00
|
|
|
// AdminDisabled implements the administration interface that informs
|
2018-04-08 02:51:54 -07:00
|
|
|
// that the API endpoints are disabled.
|
2017-12-02 21:07:05 -08:00
|
|
|
type AdminDisabled struct {
|
2017-07-10 00:29:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// TSDBSnapshot implements pb.AdminServer.
|
2019-04-04 02:55:32 -07:00
|
|
|
func (s *AdminDisabled) TSDBSnapshot(_ context.Context, _ *pb.TSDBSnapshotRequest) (*pb.TSDBSnapshotResponse, error) {
|
2019-02-04 04:06:26 -08:00
|
|
|
return nil, errAdminDisabled
|
2017-07-10 00:29:41 -07:00
|
|
|
}
|
|
|
|
|
2017-11-30 02:35:29 -08:00
|
|
|
// TSDBCleanTombstones implements pb.AdminServer.
|
2019-04-04 02:55:32 -07:00
|
|
|
func (s *AdminDisabled) TSDBCleanTombstones(_ context.Context, _ *pb.TSDBCleanTombstonesRequest) (*pb.TSDBCleanTombstonesResponse, error) {
|
2019-02-04 04:06:26 -08:00
|
|
|
return nil, errAdminDisabled
|
2017-11-30 02:35:29 -08:00
|
|
|
}
|
|
|
|
|
2018-12-18 02:52:17 -08:00
|
|
|
// DeleteSeries implements pb.AdminServer.
|
2019-04-04 02:55:32 -07:00
|
|
|
func (s *AdminDisabled) DeleteSeries(_ context.Context, r *pb.SeriesDeleteRequest) (*pb.SeriesDeleteResponse, error) {
|
2019-02-04 04:06:26 -08:00
|
|
|
return nil, errAdminDisabled
|
2017-07-10 00:29:41 -07:00
|
|
|
}
|
|
|
|
|
2017-07-06 05:38:40 -07:00
|
|
|
// Admin provides an administration interface to Prometheus.
|
|
|
|
type Admin struct {
|
2017-09-18 03:32:17 -07:00
|
|
|
db func() *tsdb.DB
|
2017-07-06 05:38:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAdmin returns a Admin server.
|
2017-09-18 03:32:17 -07:00
|
|
|
func NewAdmin(db func() *tsdb.DB) *Admin {
|
2017-07-06 05:38:40 -07:00
|
|
|
return &Admin{
|
2017-09-18 03:32:17 -07:00
|
|
|
db: db,
|
2017-07-06 05:38:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TSDBSnapshot implements pb.AdminServer.
|
2019-04-04 02:55:32 -07:00
|
|
|
func (s *Admin) TSDBSnapshot(_ context.Context, req *pb.TSDBSnapshotRequest) (*pb.TSDBSnapshotResponse, error) {
|
2017-09-18 03:32:17 -07:00
|
|
|
db := s.db()
|
|
|
|
if db == nil {
|
2019-02-04 04:06:26 -08:00
|
|
|
return nil, errTSDBNotReady
|
2017-09-18 03:32:17 -07:00
|
|
|
}
|
2017-07-06 05:38:40 -07:00
|
|
|
var (
|
2017-09-18 03:32:17 -07:00
|
|
|
snapdir = filepath.Join(db.Dir(), "snapshots")
|
2017-12-10 11:19:34 -08:00
|
|
|
name = fmt.Sprintf("%s-%x",
|
|
|
|
time.Now().UTC().Format("20060102T150405Z0700"),
|
|
|
|
rand.Int())
|
|
|
|
dir = filepath.Join(snapdir, name)
|
2017-07-06 05:38:40 -07:00
|
|
|
)
|
|
|
|
if err := os.MkdirAll(dir, 0777); err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "created snapshot directory: %s", err)
|
|
|
|
}
|
2018-03-08 02:43:41 -08:00
|
|
|
if err := db.Snapshot(dir, !req.SkipHead); err != nil {
|
2017-07-06 05:38:40 -07:00
|
|
|
return nil, status.Errorf(codes.Internal, "create snapshot: %s", err)
|
|
|
|
}
|
|
|
|
return &pb.TSDBSnapshotResponse{Name: name}, nil
|
|
|
|
}
|
|
|
|
|
2017-11-30 02:35:29 -08:00
|
|
|
// TSDBCleanTombstones implements pb.AdminServer.
|
2019-04-04 02:55:32 -07:00
|
|
|
func (s *Admin) TSDBCleanTombstones(_ context.Context, _ *pb.TSDBCleanTombstonesRequest) (*pb.TSDBCleanTombstonesResponse, error) {
|
2017-11-30 02:35:29 -08:00
|
|
|
db := s.db()
|
|
|
|
if db == nil {
|
2019-02-04 04:06:26 -08:00
|
|
|
return nil, errTSDBNotReady
|
2017-11-30 02:35:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.CleanTombstones(); err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "clean tombstones: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.TSDBCleanTombstonesResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2017-08-28 03:03:07 -07:00
|
|
|
// DeleteSeries implements pb.AdminServer.
|
2019-04-04 02:55:32 -07:00
|
|
|
func (s *Admin) DeleteSeries(_ context.Context, r *pb.SeriesDeleteRequest) (*pb.SeriesDeleteResponse, error) {
|
2017-07-06 05:38:40 -07:00
|
|
|
mint, maxt, err := extractTimeRange(r.MinTime, r.MaxTime)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
2019-11-18 11:53:33 -08:00
|
|
|
var matchers []*labels.Matcher
|
2017-07-06 05:38:40 -07:00
|
|
|
|
|
|
|
for _, m := range r.Matchers {
|
2019-11-18 11:53:33 -08:00
|
|
|
var lm *labels.Matcher
|
2017-07-06 05:38:40 -07:00
|
|
|
var err error
|
|
|
|
|
|
|
|
switch m.Type {
|
|
|
|
case pb.LabelMatcher_EQ:
|
2019-11-18 11:53:33 -08:00
|
|
|
lm, err = labels.NewMatcher(labels.MatchEqual, m.Name, m.Value)
|
2017-07-06 05:38:40 -07:00
|
|
|
case pb.LabelMatcher_NEQ:
|
2019-11-18 11:53:33 -08:00
|
|
|
lm, err = labels.NewMatcher(labels.MatchNotEqual, m.Name, m.Value)
|
2017-07-06 05:38:40 -07:00
|
|
|
case pb.LabelMatcher_RE:
|
2019-11-18 11:53:33 -08:00
|
|
|
lm, err = labels.NewMatcher(labels.MatchRegexp, m.Name, m.Value)
|
2017-07-06 05:38:40 -07:00
|
|
|
case pb.LabelMatcher_NRE:
|
2019-11-18 11:53:33 -08:00
|
|
|
lm, err = labels.NewMatcher(labels.MatchNotRegexp, m.Name, m.Value)
|
2017-07-06 05:38:40 -07:00
|
|
|
default:
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "unknown matcher type")
|
|
|
|
}
|
|
|
|
|
2019-11-18 11:53:33 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "bad matcher: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-07-06 05:38:40 -07:00
|
|
|
matchers = append(matchers, lm)
|
|
|
|
}
|
2017-09-18 03:32:17 -07:00
|
|
|
db := s.db()
|
|
|
|
if db == nil {
|
2019-02-04 04:06:26 -08:00
|
|
|
return nil, errTSDBNotReady
|
2017-09-18 03:32:17 -07:00
|
|
|
}
|
|
|
|
if err := db.Delete(timestamp.FromTime(mint), timestamp.FromTime(maxt), matchers...); err != nil {
|
2017-07-06 05:38:40 -07:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
return &pb.SeriesDeleteResponse{}, nil
|
|
|
|
}
|