Skip to content

Commit

Permalink
feat: improved tracing for courier
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr committed Dec 23, 2024
1 parent 4ca4d79 commit 85a7071
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
16 changes: 15 additions & 1 deletion courier/courier_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"context"

"github.com/pkg/errors"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"

"github.com/ory/x/otelx"
)

func (c *courier) channels(ctx context.Context, id string) (Channel, error) {
Expand Down Expand Up @@ -36,7 +40,16 @@ func (c *courier) channels(ctx context.Context, id string) (Channel, error) {
return nil, errors.Errorf("no courier channels configured")
}

func (c *courier) DispatchMessage(ctx context.Context, msg Message) error {
func (c *courier) DispatchMessage(ctx context.Context, msg Message) (err error) {
ctx, span := c.deps.Tracer(ctx).Tracer().Start(ctx, "courier.DispatchMessage", trace.WithAttributes(
attribute.Stringer("message.id", msg.ID),
attribute.Stringer("message.nid", msg.NID),
attribute.Stringer("message.type", msg.Type),
attribute.String("message.template_type", string(msg.TemplateType)),
attribute.Int("message.send_count", msg.SendCount),
))
defer otelx.End(span, &err)

logger := c.deps.Logger().
WithField("message_id", msg.ID).
WithField("message_nid", msg.NID).
Expand All @@ -56,6 +69,7 @@ func (c *courier) DispatchMessage(ctx context.Context, msg Message) error {
return err
}

span.SetAttributes(attribute.String("channel.id", channel.ID()))
logger = logger.
WithField("channel", channel.ID())

Expand Down
36 changes: 32 additions & 4 deletions courier/smtp_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ package courier

import (
"context"
"fmt"
"net"
"net/textproto"
"strconv"

"github.com/pkg/errors"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
"go.opentelemetry.io/otel/trace"

"github.com/ory/herodot"
"github.com/ory/kratos/courier/template"
"github.com/ory/kratos/driver/config"
"github.com/ory/mail/v3"
"github.com/ory/x/otelx"
)

type (
Expand Down Expand Up @@ -47,7 +51,10 @@ func (c *SMTPChannel) ID() string {
return "email"
}

func (c *SMTPChannel) Dispatch(ctx context.Context, msg Message) error {
func (c *SMTPChannel) Dispatch(ctx context.Context, msg Message) (err error) {
ctx, span := c.d.Tracer(ctx).Tracer().Start(ctx, "courier.SMTPChannel.Dispatch")
defer otelx.End(span, &err)

if c.smtpClient.Host == "" {
return errors.WithStack(herodot.ErrInternalServerError.WithErrorf("Courier tried to deliver an email but %s is not set!", config.ViperKeyCourierSMTPURL))
}
Expand Down Expand Up @@ -87,7 +94,7 @@ func (c *SMTPChannel) Dispatch(ctx context.Context, msg Message) error {
gm.SetBody("text/plain", msg.Body)

logger := c.d.Logger().
WithField("smtp_server", fmt.Sprintf("%s:%d", c.smtpClient.Host, c.smtpClient.Port)).
WithField("smtp_server", net.JoinHostPort(c.smtpClient.Host, strconv.Itoa(c.smtpClient.Port))).
WithField("smtp_ssl_enabled", c.smtpClient.SSL).
WithField("message_from", cfg.FromAddress).
WithField("message_id", msg.ID).
Expand All @@ -107,7 +114,28 @@ func (c *SMTPChannel) Dispatch(ctx context.Context, msg Message) error {
gm.AddAlternative("text/html", htmlBody)
}

if err := errors.WithStack(c.smtpClient.DialAndSend(ctx, gm)); err != nil {
dialCtx, dialSpan := c.d.Tracer(ctx).Tracer().Start(ctx, "courier.SMTPChannel.Dispatch.Dial", trace.WithAttributes(
semconv.NetPeerName(c.smtpClient.Host),
semconv.NetPeerPort(c.smtpClient.Port),
semconv.NetProtocolName("smtp"),
))
snd, err := c.smtpClient.Dial(dialCtx)
otelx.End(dialSpan, &err)

if err != nil {
logger.
WithError(err).
Error("Unable to dial SMTP connection.")
return errors.WithStack(herodot.ErrInternalServerError.
WithError(err.Error()).WithReason("failed to send email via smtp"))
}
defer snd.Close()

sendCtx, sendSpan := c.d.Tracer(ctx).Tracer().Start(ctx, "courier.SMTPChannel.Dispatch.Send")
err = mail.Send(sendCtx, snd, gm)
otelx.End(sendSpan, &err)

if err != nil {
logger.
WithError(err).
Error("Unable to send email using SMTP connection.")
Expand Down

0 comments on commit 85a7071

Please sign in to comment.