web: predeclare and reuse errors (#5180)
Predeclare and reuse errors to reduce duplicate code Signed-off-by: zhulongcheng <zhulongcheng.me@gmail.com>
This commit is contained in:
parent
a75f8a8e05
commit
fd964426a7
|
@ -111,6 +111,11 @@ var (
|
||||||
maxTime = time.Unix(math.MaxInt64/1000-62135596801, 999999999)
|
maxTime = time.Unix(math.MaxInt64/1000-62135596801, 999999999)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
errAdminDisabled = status.Error(codes.Unavailable, "Admin APIs are disabled")
|
||||||
|
errTSDBNotReady = status.Error(codes.Unavailable, "TSDB not ready")
|
||||||
|
)
|
||||||
|
|
||||||
// AdminDisabled implements the administration interface that informs
|
// AdminDisabled implements the administration interface that informs
|
||||||
// that the API endpoints are disabled.
|
// that the API endpoints are disabled.
|
||||||
type AdminDisabled struct {
|
type AdminDisabled struct {
|
||||||
|
@ -118,17 +123,17 @@ type AdminDisabled struct {
|
||||||
|
|
||||||
// TSDBSnapshot implements pb.AdminServer.
|
// TSDBSnapshot implements pb.AdminServer.
|
||||||
func (s *AdminDisabled) TSDBSnapshot(_ old_ctx.Context, _ *pb.TSDBSnapshotRequest) (*pb.TSDBSnapshotResponse, error) {
|
func (s *AdminDisabled) TSDBSnapshot(_ old_ctx.Context, _ *pb.TSDBSnapshotRequest) (*pb.TSDBSnapshotResponse, error) {
|
||||||
return nil, status.Error(codes.Unavailable, "Admin APIs are disabled")
|
return nil, errAdminDisabled
|
||||||
}
|
}
|
||||||
|
|
||||||
// TSDBCleanTombstones implements pb.AdminServer.
|
// TSDBCleanTombstones implements pb.AdminServer.
|
||||||
func (s *AdminDisabled) TSDBCleanTombstones(_ old_ctx.Context, _ *pb.TSDBCleanTombstonesRequest) (*pb.TSDBCleanTombstonesResponse, error) {
|
func (s *AdminDisabled) TSDBCleanTombstones(_ old_ctx.Context, _ *pb.TSDBCleanTombstonesRequest) (*pb.TSDBCleanTombstonesResponse, error) {
|
||||||
return nil, status.Error(codes.Unavailable, "Admin APIs are disabled")
|
return nil, errAdminDisabled
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteSeries implements pb.AdminServer.
|
// DeleteSeries implements pb.AdminServer.
|
||||||
func (s *AdminDisabled) DeleteSeries(_ old_ctx.Context, r *pb.SeriesDeleteRequest) (*pb.SeriesDeleteResponse, error) {
|
func (s *AdminDisabled) DeleteSeries(_ old_ctx.Context, r *pb.SeriesDeleteRequest) (*pb.SeriesDeleteResponse, error) {
|
||||||
return nil, status.Error(codes.Unavailable, "Admin APIs are disabled")
|
return nil, errAdminDisabled
|
||||||
}
|
}
|
||||||
|
|
||||||
// Admin provides an administration interface to Prometheus.
|
// Admin provides an administration interface to Prometheus.
|
||||||
|
@ -147,7 +152,7 @@ func NewAdmin(db func() *tsdb.DB) *Admin {
|
||||||
func (s *Admin) TSDBSnapshot(_ old_ctx.Context, req *pb.TSDBSnapshotRequest) (*pb.TSDBSnapshotResponse, error) {
|
func (s *Admin) TSDBSnapshot(_ old_ctx.Context, req *pb.TSDBSnapshotRequest) (*pb.TSDBSnapshotResponse, error) {
|
||||||
db := s.db()
|
db := s.db()
|
||||||
if db == nil {
|
if db == nil {
|
||||||
return nil, status.Errorf(codes.Unavailable, "TSDB not ready")
|
return nil, errTSDBNotReady
|
||||||
}
|
}
|
||||||
var (
|
var (
|
||||||
snapdir = filepath.Join(db.Dir(), "snapshots")
|
snapdir = filepath.Join(db.Dir(), "snapshots")
|
||||||
|
@ -169,7 +174,7 @@ func (s *Admin) TSDBSnapshot(_ old_ctx.Context, req *pb.TSDBSnapshotRequest) (*p
|
||||||
func (s *Admin) TSDBCleanTombstones(_ old_ctx.Context, _ *pb.TSDBCleanTombstonesRequest) (*pb.TSDBCleanTombstonesResponse, error) {
|
func (s *Admin) TSDBCleanTombstones(_ old_ctx.Context, _ *pb.TSDBCleanTombstonesRequest) (*pb.TSDBCleanTombstonesResponse, error) {
|
||||||
db := s.db()
|
db := s.db()
|
||||||
if db == nil {
|
if db == nil {
|
||||||
return nil, status.Errorf(codes.Unavailable, "TSDB not ready")
|
return nil, errTSDBNotReady
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.CleanTombstones(); err != nil {
|
if err := db.CleanTombstones(); err != nil {
|
||||||
|
@ -215,7 +220,7 @@ func (s *Admin) DeleteSeries(_ old_ctx.Context, r *pb.SeriesDeleteRequest) (*pb.
|
||||||
}
|
}
|
||||||
db := s.db()
|
db := s.db()
|
||||||
if db == nil {
|
if db == nil {
|
||||||
return nil, status.Errorf(codes.Unavailable, "TSDB not ready")
|
return nil, errTSDBNotReady
|
||||||
}
|
}
|
||||||
if err := db.Delete(timestamp.FromTime(mint), timestamp.FromTime(maxt), matchers...); err != nil {
|
if err := db.Delete(timestamp.FromTime(mint), timestamp.FromTime(maxt), matchers...); err != nil {
|
||||||
return nil, status.Error(codes.Internal, err.Error())
|
return nil, status.Error(codes.Internal, err.Error())
|
||||||
|
|
Loading…
Reference in New Issue