forked from grafana-cold-storage/metrictank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster.go
More file actions
120 lines (104 loc) · 3.55 KB
/
Copy pathcluster.go
File metadata and controls
120 lines (104 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package api
import (
"errors"
"fmt"
"net/http"
"strconv"
"github.com/raintank/metrictank/api/middleware"
"github.com/raintank/metrictank/api/models"
"github.com/raintank/metrictank/api/response"
"github.com/raintank/metrictank/cluster"
"github.com/raintank/worldping-api/pkg/log"
"github.com/tinylib/msgp/msgp"
)
var NotFoundErr = errors.New("not found")
func (s *Server) getNodeStatus(ctx *middleware.Context) {
response.Write(ctx, response.NewJson(200, cluster.Manager.ThisNode(), ""))
}
func (s *Server) setNodeStatus(ctx *middleware.Context, status models.NodeStatus) {
primary, err := strconv.ParseBool(status.Primary)
if err != nil {
response.Write(ctx, response.NewError(http.StatusBadRequest, fmt.Sprintf(
"could not parse status to bool. %s",
err.Error())),
)
return
}
cluster.Manager.SetPrimary(primary)
ctx.PlainText(200, []byte("OK"))
}
func (s *Server) appStatus(ctx *middleware.Context) {
if cluster.Manager.IsReady() {
ctx.PlainText(200, []byte("OK"))
return
}
response.Write(ctx, response.NewError(http.StatusServiceUnavailable, "node not ready"))
}
func (s *Server) getClusterStatus(ctx *middleware.Context) {
status := models.ClusterStatus{
NodeName: cluster.Manager.ThisNode().Name,
Members: cluster.Manager.MemberList(),
}
response.Write(ctx, response.NewJson(200, status, ""))
}
// IndexFind returns a sequence of msgp encoded idx.Node's
func (s *Server) indexFind(ctx *middleware.Context, req models.IndexFind) {
// metricDefs only get updated periodically (when using CassandraIdx), so we add a 1day (86400seconds) buffer when
// filtering by our From timestamp. This should be moved to a configuration option
if req.From != 0 {
req.From -= 86400
}
resp := models.NewIndexFindResp()
for _, pattern := range req.Patterns {
nodes, err := s.MetricIndex.Find(req.OrgId, pattern, req.From)
if err != nil {
response.Write(ctx, response.NewError(http.StatusBadRequest, err.Error()))
return
}
resp.Nodes[pattern] = nodes
}
response.Write(ctx, response.NewMsgp(200, resp))
}
// IndexGet returns a msgp encoded schema.MetricDefinition
func (s *Server) indexGet(ctx *middleware.Context, req models.IndexGet) {
def, err := s.MetricIndex.Get(req.Id)
if err != nil {
} else { // currently this can only be notFound
response.Write(ctx, response.NewError(http.StatusNotFound, "Not Found"))
return
}
response.Write(ctx, response.NewMsgp(200, &def))
}
// IndexList returns msgp encoded schema.MetricDefinition's
func (s *Server) indexList(ctx *middleware.Context, req models.IndexList) {
defs := s.MetricIndex.List(req.OrgId)
resp := make([]msgp.Marshaler, len(defs))
for i := range defs {
d := defs[i]
resp[i] = &d
}
response.Write(ctx, response.NewMsgpArray(200, resp))
}
func (s *Server) getData(ctx *middleware.Context, request models.GetData) {
series, err := s.getTargetsLocal(request.Requests)
if err != nil {
// the only errors returned are from us catching panics, so we should treat them
// all as internalServerErrors
log.Error(3, "HTTP getData() %s", err.Error())
response.Write(ctx, response.WrapError(err))
return
}
response.Write(ctx, response.NewMsgp(200, &models.GetDataResp{Series: series}))
}
func (s *Server) indexDelete(ctx *middleware.Context, req models.IndexDelete) {
defs, err := s.MetricIndex.Delete(req.OrgId, req.Query)
if err != nil {
// errors can only be caused by bad request.
response.Write(ctx, response.NewError(http.StatusBadRequest, err.Error()))
return
}
resp := models.MetricsDeleteResp{
DeletedDefs: len(defs),
}
response.Write(ctx, response.NewMsgp(200, &resp))
}