Skip to content

Commit 982195a

Browse files
committed
prometheus metrics
1 parent 1638ea6 commit 982195a

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

handlers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ func ConfirmHandler(w http.ResponseWriter, r *http.Request) {
3333
Email: user.Email,
3434
Message: "email address is now confirmed",
3535
}
36+
37+
confirmations++
3638
sendJSON(w, response)
3739
}
3840

@@ -53,6 +55,7 @@ func UnsubscribeHandler(w http.ResponseWriter, r *http.Request) {
5355
Message: "email has been unsubscribed",
5456
}
5557

58+
unsubscribed++
5659
sendJSON(w, status)
5760
}
5861

@@ -107,9 +110,11 @@ func SendHandler(w http.ResponseWriter, r *http.Request) {
107110

108111
err = SendEmail(user)
109112
if err != nil {
113+
emailErrors++
110114
sendError(w, err)
111115
return
112116
}
117+
emailsSent++
113118
}
114119

115120
func RequestHandler(w http.ResponseWriter, r *http.Request) {
@@ -149,6 +154,7 @@ func RequestHandler(w http.ResponseWriter, r *http.Request) {
149154
Message: "check email",
150155
}
151156

157+
requests++
152158
sendJSON(w, response)
153159
}
154160

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func main() {
3333
r.HandleFunc("/resend", ResendHandler).Methods("GET")
3434
r.HandleFunc("/unsubscribe", UnsubscribeHandler).Methods("GET")
3535
r.HandleFunc("/send", SendHandler).Methods("POST")
36+
r.HandleFunc("/metrics", MetricsHandler)
3637
http.ListenAndServe(":8000", r)
3738
}
3839

metrics.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"strings"
7+
)
8+
9+
var (
10+
emailsSent int
11+
emailErrors int
12+
requests int
13+
confirmations int
14+
unsubscribed int
15+
)
16+
17+
func MetricsHandler(w http.ResponseWriter, r *http.Request) {
18+
var out []string
19+
out = append(out, fmt.Sprintf("email_sent %d", emailsSent))
20+
out = append(out, fmt.Sprintf("email_errors %d", emailErrors))
21+
out = append(out, fmt.Sprintf("requests %d", requests))
22+
out = append(out, fmt.Sprintf("confirmations %d", confirmations))
23+
out = append(out, fmt.Sprintf("unsubscribed %d", unsubscribed))
24+
w.Write([]byte(strings.Join(out, "\n")))
25+
w.WriteHeader(http.StatusOK)
26+
}

0 commit comments

Comments
 (0)