Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smtp logger #3339

Merged
merged 5 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 26 additions & 0 deletions smtpsrv/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,37 @@ 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 while filtering out ECONNRESET errors caused by TCP health checks
func (l *SMTPLogger) Printf(format string, v ...interface{}) {
nimjor marked this conversation as resolved.
Show resolved Hide resolved
s := fmt.Sprintf(format, v...)
if !strings.Contains(s, "read: connection reset by peer") {
l.logger.Error(context.Background(), errors.New(s))
}
}
nimjor marked this conversation as resolved.
Show resolved Hide resolved

// Print adheres to smtp.Server's Logger interface while filtering out ECONNRESET errors caused by TCP health checks
func (l *SMTPLogger) Println(v ...interface{}) {
s := fmt.Sprint(v...)
if !strings.Contains(s, "read: connection reset by peer") {
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 +44,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