feat(BRIDGE-218): observability adapter; gluon observability metrics and tests;

This commit is contained in:
Atanas Janeshliev
2024-10-08 13:13:07 +00:00
parent 3710dff0cd
commit 040d887aae
13 changed files with 281 additions and 26 deletions

View File

@ -36,6 +36,7 @@ import (
"github.com/ProtonMail/proton-bridge/v3/internal/constants"
"github.com/ProtonMail/proton-bridge/v3/internal/files"
"github.com/ProtonMail/proton-bridge/v3/internal/logging"
"github.com/ProtonMail/proton-bridge/v3/internal/services/observability"
"github.com/sirupsen/logrus"
)
@ -77,6 +78,7 @@ func newIMAPServer(
tasks *async.Group,
uidValidityGenerator imap.UIDValidityGenerator,
panicHandler async.PanicHandler,
observabilitySender observability.Sender,
) (*gluon.Server, error) {
gluonCacheDir = ApplyGluonCachePathSuffix(gluonCacheDir)
gluonConfigDir = ApplyGluonConfigPathSuffix(gluonConfigDir)
@ -121,6 +123,7 @@ func newIMAPServer(
gluon.WithReporter(reporter),
gluon.WithUIDValidityGenerator(uidValidityGenerator),
gluon.WithPanicHandler(panicHandler),
gluon.WithObservabilitySender(observability.NewAdapter(observabilitySender), int(observability.GluonImapError), int(observability.GluonMessageError), int(observability.GluonOtherError)),
)
if err != nil {
return nil, err

View File

@ -31,6 +31,7 @@ import (
"github.com/ProtonMail/gluon/reporter"
"github.com/ProtonMail/proton-bridge/v3/internal/events"
"github.com/ProtonMail/proton-bridge/v3/internal/services/imapservice"
"github.com/ProtonMail/proton-bridge/v3/internal/services/observability"
bridgesmtp "github.com/ProtonMail/proton-bridge/v3/internal/services/smtp"
"github.com/ProtonMail/proton-bridge/v3/internal/services/syncservice"
"github.com/ProtonMail/proton-bridge/v3/pkg/cpc"
@ -60,6 +61,8 @@ type Service struct {
uidValidityGenerator imap.UIDValidityGenerator
telemetry Telemetry
observabilitySender observability.Sender
}
func NewService(
@ -71,6 +74,7 @@ func NewService(
reporter reporter.Reporter,
uidValidityGenerator imap.UIDValidityGenerator,
telemetry Telemetry,
observabilitySender observability.Sender,
) *Service {
return &Service{
requests: cpc.NewCPC(),
@ -85,6 +89,8 @@ func NewService(
tasks: async.NewGroup(ctx, panicHandler),
uidValidityGenerator: uidValidityGenerator,
telemetry: telemetry,
observabilitySender: observabilitySender,
}
}
@ -449,6 +455,7 @@ func (sm *Service) createIMAPServer(ctx context.Context) (*gluon.Server, error)
sm.tasks,
sm.uidValidityGenerator,
sm.panicHandler,
sm.observabilitySender,
)
if err == nil {
sm.eventPublisher.PublishEvent(ctx, events.IMAPServerCreated{})

View File

@ -0,0 +1,93 @@
// 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 observability
import (
"github.com/ProtonMail/go-proton-api"
)
type Adapter struct {
sender Sender
}
func NewAdapter(sender Sender) *Adapter {
return &Adapter{sender: sender}
}
// VerifyAndParseGenericMetrics parses a metric provided as an interface into a proton.ObservabilityMetric type.
// It's exported as it is also used in integration tests.
func VerifyAndParseGenericMetrics(metric map[string]interface{}) (bool, proton.ObservabilityMetric) {
name, ok := metric["Name"].(string)
if !ok {
return false, proton.ObservabilityMetric{}
}
version, ok := metric["Version"].(int)
if !ok {
return false, proton.ObservabilityMetric{}
}
timestamp, ok := metric["Timestamp"].(int64)
if !ok {
return false, proton.ObservabilityMetric{}
}
data, ok := metric["Data"]
if !ok {
return false, proton.ObservabilityMetric{}
}
return true, proton.ObservabilityMetric{
Name: name,
Version: version,
Timestamp: timestamp,
Data: data,
}
}
func (adapter *Adapter) AddMetrics(metrics ...map[string]interface{}) {
var typedMetrics []proton.ObservabilityMetric
for _, metric := range metrics {
if ok, m := VerifyAndParseGenericMetrics(metric); ok {
typedMetrics = append(typedMetrics, m)
}
}
if len(typedMetrics) > 0 {
adapter.sender.AddMetrics(typedMetrics...)
}
}
func (adapter *Adapter) AddDistinctMetrics(errType interface{}, metrics ...map[string]interface{}) {
errTypeInt, ok := errType.(int)
if !ok {
return
}
var typedMetrics []proton.ObservabilityMetric
for _, metric := range metrics {
if ok, m := VerifyAndParseGenericMetrics(metric); ok {
typedMetrics = append(typedMetrics, m)
}
}
if len(typedMetrics) > 0 {
adapter.sender.AddDistinctMetrics(DistinctionErrorTypeEnum(errTypeInt), typedMetrics...)
}
}

View File

@ -0,0 +1,58 @@
// 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 observability
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func Test_AdapterCustomMetrics(t *testing.T) {
customMetric := map[string]interface{}{
"Name": "name",
"Version": 1,
"Timestamp": time.Now().Unix(),
"Data": map[string]interface{}{
"Value": 1,
"Labels": map[string]string{
"error": "customError",
},
},
}
ok, metric := VerifyAndParseGenericMetrics(customMetric)
require.True(t, ok)
require.Equal(t, metric.Name, customMetric["Name"])
require.Equal(t, metric.Timestamp, customMetric["Timestamp"])
require.Equal(t, metric.Version, customMetric["Version"])
require.Equal(t, metric.Data, customMetric["Data"])
}
func Test_AdapterGluonMetrics(t *testing.T) {
metrics := GenerateAllGluonMetrics()
for _, metric := range metrics {
ok, m := VerifyAndParseGenericMetrics(metric)
fmt.Println(m)
require.True(t, ok)
}
}

View File

@ -25,13 +25,19 @@ type DistinctionErrorTypeEnum int
const (
SyncError DistinctionErrorTypeEnum = iota
EventLoopError
GluonImapError
GluonMessageError
GluonOtherError
EventLoopError // EventLoopError - should always be kept last when inserting new keys.
)
// errorSchemaMap - maps between the DistinctionErrorTypeEnum and the relevant schema name.
var errorSchemaMap = map[DistinctionErrorTypeEnum]string{ //nolint:gochecknoglobals
SyncError: "bridge_sync_errors_users_total",
EventLoopError: "bridge_event_loop_events_errors_users_total",
SyncError: "bridge_sync_errors_users_total",
EventLoopError: "bridge_event_loop_events_errors_users_total",
GluonImapError: "bridge_gluon_imap_errors_users_total",
GluonMessageError: "bridge_gluon_message_errors_users_total",
GluonOtherError: "bridge_gluon_other_errors_users_total",
}
// createLastSentMap - needs to be updated whenever we make changes to the enum.

View File

@ -26,17 +26,20 @@ import (
)
const genericHeartbeatSchemaName = "bridge_generic_user_heartbeat_total"
const genericHeartbeatVersion = 2
type heartbeatData struct {
receivedSyncError bool
receivedEventLoopError bool
receivedOtherError bool
receivedGluonError bool
}
func (d *distinctionUtility) resetHeartbeatData() {
d.heartbeatData.receivedSyncError = false
d.heartbeatData.receivedOtherError = false
d.heartbeatData.receivedEventLoopError = false
d.heartbeatData.receivedGluonError = false
}
func (d *distinctionUtility) updateHeartbeatData(errType DistinctionErrorTypeEnum) {
@ -46,6 +49,8 @@ func (d *distinctionUtility) updateHeartbeatData(errType DistinctionErrorTypeEnu
d.heartbeatData.receivedSyncError = true
case EventLoopError:
d.heartbeatData.receivedEventLoopError = true
case GluonMessageError, GluonImapError, GluonOtherError:
d.heartbeatData.receivedGluonError = true
}
})
}
@ -95,13 +100,14 @@ func (d *distinctionUtility) generateHeartbeatUserMetric() proton.ObservabilityM
formatBool(d.heartbeatData.receivedOtherError),
formatBool(d.heartbeatData.receivedSyncError),
formatBool(d.heartbeatData.receivedEventLoopError),
formatBool(d.heartbeatData.receivedGluonError),
)
}
func generateHeartbeatMetric(plan, mailClient, dohEnabled, betaAccess, otherError, syncError, eventLoopError string) proton.ObservabilityMetric {
func generateHeartbeatMetric(plan, mailClient, dohEnabled, betaAccess, otherError, syncError, eventLoopError, gluonError string) proton.ObservabilityMetric {
return proton.ObservabilityMetric{
Name: genericHeartbeatSchemaName,
Version: 1,
Version: genericHeartbeatVersion,
Timestamp: time.Now().Unix(),
Data: map[string]interface{}{
"Value": 1,
@ -113,6 +119,7 @@ func generateHeartbeatMetric(plan, mailClient, dohEnabled, betaAccess, otherErro
"receivedOtherError": otherError,
"receivedSyncError": syncError,
"receivedEventLoopError": eventLoopError,
"receivedGluonError": gluonError,
},
},
}

View File

@ -18,6 +18,7 @@
package observability
import (
gluonMetrics "github.com/ProtonMail/gluon/observability/metrics"
"github.com/ProtonMail/go-proton-api"
)
@ -85,16 +86,19 @@ func GenerateAllHeartbeatMetricPermutations() []proton.ObservabilityMetric {
for _, receivedOtherError := range trueFalseValues {
for _, receivedSyncError := range trueFalseValues {
for _, receivedEventLoopError := range trueFalseValues {
metrics = append(metrics,
generateHeartbeatMetric(plan,
mailClient,
dohEnabled,
betaAccess,
receivedOtherError,
receivedSyncError,
receivedEventLoopError,
),
)
for _, receivedGluonError := range trueFalseValues {
metrics = append(metrics,
generateHeartbeatMetric(plan,
mailClient,
dohEnabled,
betaAccess,
receivedOtherError,
receivedSyncError,
receivedEventLoopError,
receivedGluonError,
),
)
}
}
}
}
@ -104,3 +108,19 @@ func GenerateAllHeartbeatMetricPermutations() []proton.ObservabilityMetric {
}
return metrics
}
func GenerateAllGluonMetrics() []map[string]interface{} {
var metrics []map[string]interface{}
metrics = append(metrics,
gluonMetrics.GenerateFailedParseIMAPCommandMetric(),
gluonMetrics.GenerateFailedToCreateMailbox(),
gluonMetrics.GenerateFailedToDeleteMailboxMetric(),
gluonMetrics.GenerateFailedToCopyMessagesMetric(),
gluonMetrics.GenerateFailedToMoveMessagesFromMailboxMetric(),
gluonMetrics.GenerateFailedToRemoveDeletedMessagesMetric(),
gluonMetrics.GenerateFailedToCommitDatabaseTransactionMetric(),
gluonMetrics.GenerateAppendToDraftsMustNotReturnExistingRemoteID(),
gluonMetrics.GenerateDatabaseMigrationFailed(),
)
return metrics
}