feat(BRIDGE-236): added SMTP observability metrics

This commit is contained in:
Atanas Janeshliev
2024-10-21 11:45:02 +02:00
parent fb523e5573
commit e3d0334b6f
9 changed files with 165 additions and 1 deletions

View File

@ -28,6 +28,7 @@ const (
GluonImapError
GluonMessageError
GluonOtherError
SMTPError
EventLoopError // EventLoopError - should always be kept last when inserting new keys.
)
@ -37,6 +38,7 @@ var errorSchemaMap = map[DistinctionErrorTypeEnum]string{ //nolint:gochecknoglob
EventLoopError: "bridge_event_loop_events_errors_users_total",
GluonImapError: "bridge_gluon_imap_errors_users_total",
GluonMessageError: "bridge_gluon_message_errors_users_total",
SMTPError: "bridge_smtp_errors_users_total",
GluonOtherError: "bridge_gluon_other_errors_users_total",
}

View File

@ -44,6 +44,7 @@ func (d *distinctionUtility) resetHeartbeatData() {
func (d *distinctionUtility) updateHeartbeatData(errType DistinctionErrorTypeEnum) {
d.withUpdateHeartbeatDataLock(func() {
//nolint:exhaustive
switch errType {
case SyncError:
d.heartbeatData.receivedSyncError = true

View File

@ -0,0 +1,90 @@
// Copyright (c) 2024 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
package observabilitymetrics
import (
"time"
"github.com/ProtonMail/go-proton-api"
)
const (
smtpErrorsSchemaName = "bridge_smtp_errors_total"
smtpErrorsSchemaVersion = 1
smtpSendSuccessSchemaName = "bridge_smtp_send_success_total"
smtpSendSuccessSchemaVersion = 1
)
func generateSMTPErrorObservabilityMetric(errorType string) proton.ObservabilityMetric {
return proton.ObservabilityMetric{
Name: smtpErrorsSchemaName,
Version: smtpErrorsSchemaVersion,
Timestamp: time.Now().Unix(),
Data: map[string]interface{}{
"Value": 1,
"Labels": map[string]string{
"errorType": errorType,
},
},
}
}
func GenerateFailedGetParentID() proton.ObservabilityMetric {
return generateSMTPErrorObservabilityMetric("failedGetParentId")
}
func GenerateUnsupportedMIMEType() proton.ObservabilityMetric {
return generateSMTPErrorObservabilityMetric("unsupportedMIMEType")
}
func GenerateFailedCreateDraft() proton.ObservabilityMetric {
return generateSMTPErrorObservabilityMetric("failedToCreateDraft")
}
func GenerateFailedCreateAttachments() proton.ObservabilityMetric {
return generateSMTPErrorObservabilityMetric("failedCreateAttachments")
}
func GenerateFailedToGetRecipients() proton.ObservabilityMetric {
return generateSMTPErrorObservabilityMetric("failedGetRecipients")
}
func GenerateFailedCreatePackages() proton.ObservabilityMetric {
return generateSMTPErrorObservabilityMetric("failedCreatePackages")
}
func GenerateFailedSendDraft() proton.ObservabilityMetric {
return generateSMTPErrorObservabilityMetric("failedSendDraft")
}
func GenerateFailedDeleteFromDrafts() proton.ObservabilityMetric {
return generateSMTPErrorObservabilityMetric("failedDeleteFromDrafts")
}
func GenerateSMTPSendSuccess() proton.ObservabilityMetric {
return proton.ObservabilityMetric{
Name: smtpSendSuccessSchemaName,
Version: smtpSendSuccessSchemaVersion,
Timestamp: time.Now().Unix(),
Data: map[string]interface{}{
"Value": 1,
"Labels": map[string]string{},
},
}
}

View File

@ -29,6 +29,7 @@ import (
"github.com/ProtonMail/gluon/reporter"
"github.com/ProtonMail/go-proton-api"
bridgelogging "github.com/ProtonMail/proton-bridge/v3/internal/logging"
"github.com/ProtonMail/proton-bridge/v3/internal/services/observability"
"github.com/ProtonMail/proton-bridge/v3/internal/services/orderedtasks"
"github.com/ProtonMail/proton-bridge/v3/internal/services/sendrecorder"
"github.com/ProtonMail/proton-bridge/v3/internal/services/userevents"
@ -63,6 +64,8 @@ type Service struct {
addressMode usertypes.AddressMode
serverManager ServerManager
observabilitySender observability.Sender
}
func NewService(
@ -78,6 +81,7 @@ func NewService(
mode usertypes.AddressMode,
identityState *useridentity.State,
serverManager ServerManager,
observabilitySender observability.Sender,
) *Service {
subscriberName := fmt.Sprintf("smpt-%v", userID)
@ -103,6 +107,8 @@ func NewService(
addressMode: mode,
serverManager: serverManager,
observabilitySender: observabilitySender,
}
}

View File

@ -35,7 +35,9 @@ import (
"github.com/ProtonMail/go-proton-api"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/ProtonMail/proton-bridge/v3/internal/logging"
"github.com/ProtonMail/proton-bridge/v3/internal/services/observability"
"github.com/ProtonMail/proton-bridge/v3/internal/services/sendrecorder"
"github.com/ProtonMail/proton-bridge/v3/internal/services/smtp/observabilitymetrics"
"github.com/ProtonMail/proton-bridge/v3/internal/usertypes"
"github.com/ProtonMail/proton-bridge/v3/pkg/message"
"github.com/ProtonMail/proton-bridge/v3/pkg/message/parser"
@ -166,6 +168,10 @@ func (s *Service) smtpSendMail(ctx context.Context, authID string, from string,
// If the message was successfully sent, we can update the message ID in the record.
s.log.Debug("Message sent successfully, signaling recorder")
// Send SMTP success observability metric
s.observabilitySender.AddMetrics(observabilitymetrics.GenerateSMTPSendSuccess())
s.recorder.SignalMessageSent(hash, srID, sent.ID)
return nil
@ -196,7 +202,7 @@ func (s *Service) sendWithKey(
}
parentID, draftsToDelete, err := getParentID(ctx, s.client, authAddrID, addrMode, references)
if err != nil {
// Sentry event has been removed; should be replaced with observability - BRIDGE-206.
s.observabilitySender.AddDistinctMetrics(observability.SMTPError, observabilitymetrics.GenerateFailedGetParentID())
s.log.WithError(err).Warn("Failed to get parent ID")
}
@ -211,6 +217,7 @@ func (s *Service) sendWithKey(
decBody = string(message.PlainBody)
default:
s.observabilitySender.AddDistinctMetrics(observability.SMTPError, observabilitymetrics.GenerateUnsupportedMIMEType())
return proton.Message{}, fmt.Errorf("unsupported MIME type: %v", message.MIMEType)
}
@ -227,32 +234,38 @@ func (s *Service) sendWithKey(
ExternalID: message.ExternalID,
})
if err != nil {
s.observabilitySender.AddDistinctMetrics(observability.SMTPError, observabilitymetrics.GenerateFailedCreateDraft())
return proton.Message{}, fmt.Errorf("failed to create draft: %w", err)
}
attKeys, err := s.createAttachments(ctx, s.client, addrKR, draft.ID, message.Attachments)
if err != nil {
s.observabilitySender.AddDistinctMetrics(observability.SMTPError, observabilitymetrics.GenerateFailedCreateAttachments())
return proton.Message{}, fmt.Errorf("failed to create attachments: %w", err)
}
recipients, err := s.getRecipients(ctx, s.client, userKR, settings, draft)
if err != nil {
s.observabilitySender.AddDistinctMetrics(observability.SMTPError, observabilitymetrics.GenerateFailedToGetRecipients())
return proton.Message{}, fmt.Errorf("failed to get recipients: %w", err)
}
req, err := createSendReq(addrKR, message.MIMEBody, message.RichBody, message.PlainBody, recipients, attKeys)
if err != nil {
s.observabilitySender.AddDistinctMetrics(observability.SMTPError, observabilitymetrics.GenerateFailedCreatePackages())
return proton.Message{}, fmt.Errorf("failed to create packages: %w", err)
}
res, err := s.client.SendDraft(ctx, draft.ID, req)
if err != nil {
s.observabilitySender.AddDistinctMetrics(observability.SMTPError, observabilitymetrics.GenerateFailedSendDraft())
return proton.Message{}, fmt.Errorf("failed to send draft: %w", err)
}
// Only delete the drafts, if any, after message was successfully sent.
if len(draftsToDelete) != 0 {
if err := s.client.DeleteMessage(ctx, draftsToDelete...); err != nil {
s.observabilitySender.AddDistinctMetrics(observability.SMTPError, observabilitymetrics.GenerateFailedDeleteFromDrafts())
s.log.WithField("ids", draftsToDelete).WithError(err).Errorf("Failed to delete requested messages from Drafts")
}
}

View File

@ -265,6 +265,7 @@ func newImpl(
addressMode,
identityState.Clone(),
smtpServerManager,
observabilityService,
)
user.imapService = imapservice.NewService(