Skip to content

Commit

Permalink
Smtp logger (#3339)
Browse files Browse the repository at this point in the history
* allow passing Logger instance in config

* pass main app logger to smtp config

* implement smtp.Logger in smtpsrv to make logs go through app.cfg.Logger #3335

* filter out TCP ECONNRESET error messages

* early return instead of negation; add TODO

---------

Co-authored-by: Jordan Nimlos <[email protected]>
  • Loading branch information
nimjor and Jordan Nimlos authored Oct 5, 2023
1 parent 47df50f commit 51eec74
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/initsmtpsrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (app *App) initSMTPServer(ctx context.Context) error {
TLSConfig: app.cfg.TLSConfigSMTP,
MaxRecipients: app.cfg.SMTPMaxRecipients,
BackgroundContext: app.LogBackgroundContext,
Logger: app.cfg.Logger,
AuthorizeFunc: func(ctx context.Context, id string) (context.Context, error) {
tok, _, err := authtoken.Parse(id, nil)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions smtpsrv/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/tls"

"github.com/target/goalert/alert"
"github.com/target/goalert/util/log"
)

// Config is used to configure the SMTP server.
Expand All @@ -15,6 +16,7 @@ type Config struct {
MaxRecipients int

BackgroundContext func() context.Context
Logger *log.Logger

AuthorizeFunc func(ctx context.Context, id string) (context.Context, error)
CreateAlertFunc func(ctx context.Context, a *alert.Alert) error
Expand Down
32 changes: 32 additions & 0 deletions smtpsrv/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,43 @@ package smtpsrv

import (
"context"
"errors"
"fmt"
"net"
"strings"
"time"

"github.com/emersion/go-smtp"
"github.com/target/goalert/util/log"
)

// SMTPLogger implements the smtp.Logger interface using the main app Logger.
type SMTPLogger struct {
logger *log.Logger
}

// Printf adheres to smtp.Server's Logger interface.
func (l *SMTPLogger) Printf(format string, v ...interface{}) {
s := fmt.Sprintf(format, v...)
// TODO: Uses string compare to filter out errors caused by TCP health checks,
// remove once https://github.com/emersion/go-smtp/issues/236 has been fixed.
if strings.Contains(s, "read: connection reset by peer") {
return
}
l.logger.Error(context.Background(), errors.New(s))
}

// Print adheres to smtp.Server's Logger interface.
func (l *SMTPLogger) Println(v ...interface{}) {
s := fmt.Sprint(v...)
// TODO: Uses string compare to filter out errors caused by TCP health checks,
// remove once https://github.com/emersion/go-smtp/issues/236 has been fixed.
if strings.Contains(s, "read: connection reset by peer") {
return
}
l.logger.Error(context.Background(), errors.New(s))
}

// Server implements an SMTP server that creates alerts.
type Server struct {
cfg Config
Expand All @@ -19,6 +50,7 @@ func NewServer(cfg Config) *Server {
s := &Server{cfg: cfg}

srv := smtp.NewServer(s)
srv.ErrorLog = &SMTPLogger{logger: cfg.Logger}
srv.Domain = cfg.Domain
srv.ReadTimeout = 10 * time.Second
srv.WriteTimeout = 10 * time.Second
Expand Down

0 comments on commit 51eec74

Please sign in to comment.