/
grpc.go
132 lines (112 loc) · 3.99 KB
/
grpc.go
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
121
122
123
124
125
126
127
128
129
130
131
132
package alerts
import (
"github.com/alerting/alerts/pkg/resources"
cap "github.com/alerting/alerts/pkg/cap"
protobuf "github.com/alerting/alerts/pkg/protobuf"
raven "github.com/getsentry/raven-go"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
otlog "github.com/opentracing/opentracing-go/log"
log "github.com/sirupsen/logrus"
context "golang.org/x/net/context"
)
// Server is GRPC server for the alerts microservice.
type Server struct {
Storage Storage
Resources resources.ResourcesServiceClient
}
// NewServer creates a new Server.
func NewServer(storage Storage, resources resources.ResourcesServiceClient) (*Server, error) {
return &Server{
Storage: storage,
Resources: resources,
}, nil
}
// Add adds a new alert.
func (s *Server) Add(ctx context.Context, alert *cap.Alert) (*cap.Alert, error) {
// Start a new span.
span, sctx := opentracing.StartSpanFromContext(ctx, "Server::Add")
defer span.Finish()
log.WithField("alert", alert.ID()).Info("Add alert")
// Run tasks against the incoming alert.
err := cleanupAlert(sctx, alert)
if err != nil {
log.WithError(err).Error("Failed to cleanup alert")
raven.CaptureError(err, nil)
ext.Error.Set(span, true)
span.LogFields(otlog.Error(err))
return nil, err
}
// Process alert resources.
log.Debug("Processing resources")
for _, info := range alert.Infos {
for i, resource := range info.Resources {
log.WithField("resource", resource.Uri).Debug("Adding resource")
newResource, err := s.Resources.Add(sctx, resource)
if err != nil {
log.WithError(err).Error("Failed to add resource")
raven.CaptureError(err, nil)
ext.Error.Set(span, true)
span.LogFields(otlog.Error(err))
return nil, err
}
info.Resources[i] = newResource
}
}
// Figure out if the alert has already been superseded.
alert.Superseded, err = s.Storage.IsSuperseded(sctx, alert.Reference())
if err != nil {
log.WithError(err).Error("Failed to check if alert has been superseded")
raven.CaptureError(err, nil)
ext.Error.Set(span, true)
span.LogFields(otlog.Error(err))
return nil, err
}
// If the alert is an UPDATE or a CANCEL, the supersede the old versions of the alert.
if alert.MessageType == cap.Alert_UPDATE || alert.MessageType == cap.Alert_CANCEL {
log.Debug("Superseding referenced alerts")
for _, reference := range alert.References {
log.WithField("id", reference.ID()).Debug("Superseding")
err := s.Storage.Supersede(sctx, reference)
if err != nil {
log.WithError(err).Error("Failed to supersede alert")
raven.CaptureError(err, nil)
ext.Error.Set(span, true)
span.LogFields(otlog.Error(err))
//return nil, err
}
}
}
// Add the alert to storage.
err = s.Storage.Add(sctx, alert)
return alert, err
}
// Get returns the alert for the given reference.
func (s *Server) Get(ctx context.Context, reference *cap.Reference) (*cap.Alert, error) {
// Start a new span.
span, sctx := opentracing.StartSpanFromContext(ctx, "Server::Get")
defer span.Finish()
// Load the alert from storage.
log.WithField("reference", reference).Info("Get alert")
return s.Storage.Get(sctx, reference)
}
// Has returns whether or not an alert exists for the given reference.
func (s *Server) Has(ctx context.Context, reference *cap.Reference) (*protobuf.BooleanResult, error) {
// Start a new span.
span, sctx := opentracing.StartSpanFromContext(ctx, "Server::Has")
defer span.Finish()
// Check if the alert exists from storage.
log.WithField("reference", reference).Info("Has alert")
exists, err := s.Storage.Has(sctx, reference)
return &protobuf.BooleanResult{
Result: exists,
}, err
}
// Find returns alerts matching the criteria. NOTE: Results are per Info block.
func (s *Server) Find(ctx context.Context, criteria *FindCriteria) (*FindResult, error) {
// Start a new span.
span, sctx := opentracing.StartSpanFromContext(ctx, "Server::Find")
defer span.Finish()
log.WithField("criteria", criteria).Info("Finding")
return s.Storage.Find(sctx, criteria)
}