-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
149 lines (133 loc) · 3.47 KB
/
api.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/prometheus/alertmanager/template"
"github.com/prometheus/client_golang/prometheus"
)
type notificationAPI struct {
store *store
incidentsCreatedTotal prometheus.Counter
incidentsResolvedTotal prometheus.Counter
incidentsDuration prometheus.Histogram
}
func newNotificationAPI(store *store) *notificationAPI {
var (
incidentsCreatedTotal = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "crochet_incidents_total",
Help: "Total number of incidents",
},
)
incidentsDuration = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "crochet_incidents_duration_seconds",
Help: "Duration of incidents",
Buckets: []float64{10, 60, 120, 300, 600, 1800, 3600, 7200},
},
)
)
prometheus.MustRegister(
incidentsCreatedTotal,
incidentsDuration,
)
return ¬ificationAPI{
store: store,
incidentsCreatedTotal: incidentsCreatedTotal,
incidentsDuration: incidentsDuration,
}
}
func (a *notificationAPI) post(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Content-Type") != "application/json" {
logger.Printf("Invalid Content-Type: %q", r.Header.Get("Content-Type"))
w.WriteHeader(http.StatusBadRequest)
return
}
var p webhookPayload
err := json.NewDecoder(r.Body).Decode(&p)
if err != nil {
logger.Println("Failed to decode payload:", err)
w.WriteHeader(http.StatusBadRequest)
return
}
n := ¬ification{
Remote: r.RemoteAddr,
Timestamp: time.Now(),
webhookPayload: &p,
}
a.store.addNotification(n)
if a.store.getIncident(n.Key()) == nil {
// This is a new incident.
a.incidentsCreatedTotal.Inc()
}
i := a.store.updateIncident(n)
if !i.IsResolved() {
return
}
// Record metrics about incident resolution.
a.store.deleteIncident(i)
a.incidentsDuration.Observe(i.Duration().Seconds())
}
func (a *notificationAPI) list(w http.ResponseWriter, r *http.Request) {
enc := json.NewEncoder(w)
err := enc.Encode(a.store.listNotifications())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
}
func (a *notificationAPI) Handle(w http.ResponseWriter, r *http.Request) {
logger.Printf("Processing %q notification API request from %s", r.Method, r.RemoteAddr)
switch r.Method {
case "GET":
a.list(w, r)
case "POST":
a.post(w, r)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
type incidentAPI struct {
store *store
}
func newIncidentAPI(store *store) *incidentAPI {
return &incidentAPI{
store: store,
}
}
func (a *incidentAPI) Handle(w http.ResponseWriter, r *http.Request) {
logger.Printf("Processing %q incident API request from %s", r.Method, r.RemoteAddr)
switch r.Method {
case "GET":
a.list(w, r)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func (a *incidentAPI) list(w http.ResponseWriter, r *http.Request) {
type incident struct {
Duration float64 `json:"duration"`
Key string `json:"key"`
Alerts template.Alerts
}
var incidents []incident
for _, i := range a.store.listIncidents() {
incidents = append(incidents, incident{
Duration: i.Duration().Seconds(),
Key: i.Key(),
Alerts: i.Alerts(),
})
}
enc := json.NewEncoder(w)
err := enc.Encode(incidents)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
}