onasty/mailer/metrics.go (view raw)
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | |
| 6 | "github.com/prometheus/client_golang/prometheus" |
| 7 | "github.com/prometheus/client_golang/prometheus/promauto" |
| 8 | "github.com/prometheus/client_golang/prometheus/promhttp" |
| 9 | ) |
| 10 | |
| 11 | var ( |
| 12 | emailSentSuccessfully = promauto.NewCounter(prometheus.CounterOpts{ |
| 13 | Name: "mail_sent_total", |
| 14 | Help: "the total number of successfully sent email", |
| 15 | }) |
| 16 | |
| 17 | emailFailedToSend = promauto.NewCounterVec(prometheus.CounterOpts{ |
| 18 | Name: "mail_failed_total", |
| 19 | Help: "the total number of email that failed to send", |
| 20 | }, []string{"request_id"}) |
| 21 | ) |
| 22 | |
| 23 | func MetricsHandler() http.Handler { |
| 24 | mux := http.NewServeMux() |
| 25 | mux.Handle("GET /metrics", promhttp.Handler()) |
| 26 | return mux |
| 27 | } |
| 28 | |
| 29 | func RecordEmailSent() { |
| 30 | go emailSentSuccessfully.Inc() |
| 31 | } |
| 32 | |
| 33 | func RecordEmailFailed(reqid string) { |
| 34 | go emailFailedToSend.With(prometheus.Labels{ |
| 35 | "request_id": reqid, |
| 36 | }).Inc() |
| 37 | } |