Skip to content

Commit

Permalink
Add metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
teran committed May 12, 2018
1 parent 3409dff commit 2cf3ff1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
44 changes: 36 additions & 8 deletions backend/mailgun/mailgun.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"io"
"io/ioutil"
"log"
"net/http"
"net/mail"

smtp "github.com/emersion/go-smtp"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
mg "gopkg.in/mailgun/mailgun-go.v1"
)

Expand All @@ -16,14 +19,16 @@ var _ smtp.User = &User{}

// Backend type
type Backend struct {
Domain string
privateKey string
publicKey string
Domain string
privateKey string
publicKey string
metricsMailgunMessages *prometheus.CounterVec
}

// User type
type User struct {
mailgunClient mg.Mailgun
mailgunClient mg.Mailgun
metricsMailgunMessages *prometheus.CounterVec
}

// NewBackend returns new instance of backend
Expand All @@ -32,28 +37,49 @@ func NewBackend(domain, privateKey, publicKey string) (smtp.Backend, error) {
return nil, fmt.Errorf("domain, privateKey, publicKey must not be empty")
}

return &Backend{
b := &Backend{
Domain: domain,
privateKey: privateKey,
publicKey: publicKey,
}, nil
metricsMailgunMessages: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "mailgun_messages",
Help: "A counter for messages sent",
},
[]string{"status"},
),
}

prometheus.MustRegister(b.metricsMailgunMessages)

return b, nil
}

// Login is used to authenticate the user
// In relay there's no need for that at the moment
func (b *Backend) Login(username, password string) (smtp.User, error) {
return &User{
mailgunClient: mg.NewMailgun(b.Domain, b.privateKey, b.publicKey),
mailgunClient: mg.NewMailgun(b.Domain, b.privateKey, b.publicKey),
metricsMailgunMessages: b.metricsMailgunMessages,
}, nil
}

// AnonymousLogin returns anonymouse user object
func (b *Backend) AnonymousLogin() (smtp.User, error) {
return &User{
mailgunClient: mg.NewMailgun(b.Domain, b.privateKey, b.publicKey),
mailgunClient: mg.NewMailgun(b.Domain, b.privateKey, b.publicKey),
metricsMailgunMessages: b.metricsMailgunMessages,
}, nil
}

func (b *Backend) ListenAndServeMetrics(addr string) error {
s := http.Server{
Addr: addr,
Handler: promhttp.Handler(),
}
return s.ListenAndServe()
}

// Send will send email synchronously via Mailgun service
func (u *User) Send(from string, to []string, r io.Reader) error {
m, err := mail.ReadMessage(r)
Expand All @@ -70,8 +96,10 @@ func (u *User) Send(from string, to []string, r io.Reader) error {
message := u.mailgunClient.NewMessage(from, m.Header.Get("Subject"), string(mBody), recipient)
resp, id, err := u.mailgunClient.Send(message)
if err != nil {
u.metricsMailgunMessages.WithLabelValues("fail").Inc()
return err
}
u.metricsMailgunMessages.WithLabelValues("success").Inc()
log.Printf("ID: %s Resp: %s", id, resp)
}
return nil
Expand Down
5 changes: 5 additions & 0 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type config struct {
MaxIdleSeconds int `default:"300" envconfig:"MAX_IDLE_SECONDS"`
MaxMessageBytes int `default:"1048576" envconfig:"MAX_MESSAGE_BYTES"`
MaxRecipients int `default:"50" envconfig:"MAX_RECIPIENTS"`
MetricsAddr string `default:":8081" envconfig:"METRICS_ADDR"`
}

func main() {
Expand Down Expand Up @@ -47,6 +48,10 @@ func main() {
s.AuthDisabled = cfg.AuthDisabled
s.AllowInsecureAuth = cfg.AllowInsecureAuth

go func(metricsAddr string) {
log.Fatal(be.(*mailgun.Backend).ListenAndServeMetrics(metricsAddr))
}(cfg.MetricsAddr)

log.Println("Starting server at", s.Addr)
if err := s.ListenAndServe(); err != nil {
log.Fatal(err)
Expand Down

0 comments on commit 2cf3ff1

Please sign in to comment.