feat(GODT-2552): Init telemetry heartbeat.

This commit is contained in:
Romain LE JEUNE
2023-04-18 08:49:47 +02:00
committed by Romain Le Jeune
parent 6ddaa94bc0
commit 0f621d0aad
10 changed files with 330 additions and 24 deletions

View File

@ -41,8 +41,10 @@ import (
"github.com/ProtonMail/proton-bridge/v3/internal/focus"
"github.com/ProtonMail/proton-bridge/v3/internal/safe"
"github.com/ProtonMail/proton-bridge/v3/internal/sentry"
"github.com/ProtonMail/proton-bridge/v3/internal/telemetry"
"github.com/ProtonMail/proton-bridge/v3/internal/user"
"github.com/ProtonMail/proton-bridge/v3/internal/vault"
"github.com/ProtonMail/proton-bridge/v3/pkg/keychain"
"github.com/bradenaw/juniper/xslices"
"github.com/emersion/go-smtp"
"github.com/go-resty/resty/v2"
@ -78,6 +80,9 @@ type Bridge struct {
updater Updater
installCh chan installJob
// heartbeat is the telemetry heartbeat for metrics.
heartbeat telemetry.Heartbeat
// curVersion is the current version of the bridge,
// newVersion is the version that was installed by the updater.
curVersion *semver.Version
@ -278,6 +283,8 @@ func newBridge(
updater: updater,
installCh: make(chan installJob),
heartbeat: telemetry.NewHeartbeat(1143, 1025, gluonCacheDir, keychain.DefaultHelper),
curVersion: curVersion,
newVersion: curVersion,
newVersionLock: safe.NewRWMutex(),
@ -423,6 +430,8 @@ func (bridge *Bridge) init(tlsReporter TLSReporter) error {
})
})
// init telemetry
bridge.initHeartbeat()
return nil
}
@ -479,22 +488,6 @@ func (bridge *Bridge) Close(ctx context.Context) {
bridge.watchers = nil
}
func (bridge *Bridge) ComputeTelemetry() bool {
if bridge.GetTelemetryDisabled() {
return false
}
var telemetry = true
safe.RLock(func() {
for _, user := range bridge.users {
telemetry = telemetry && user.IsTelemetryEnabled(context.Background())
}
}, bridge.usersLock)
return telemetry
}
func (bridge *Bridge) publish(event events.Event) {
bridge.watchersLock.RLock()
defer bridge.watchersLock.RUnlock()

View File

@ -0,0 +1,67 @@
// Copyright (c) 2023 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 bridge
import (
"context"
"github.com/ProtonMail/proton-bridge/v3/internal/safe"
"github.com/ProtonMail/proton-bridge/v3/internal/vault"
)
func (bridge *Bridge) ComputeTelemetry() bool {
var telemetry = true
safe.RLock(func() {
for _, user := range bridge.users {
telemetry = telemetry && user.IsTelemetryEnabled(context.Background())
}
}, bridge.usersLock)
return telemetry
}
func (bridge *Bridge) initHeartbeat() {
safe.RLock(func() {
var splitMode = false
for _, user := range bridge.users {
if user.GetAddressMode() == vault.SplitMode {
splitMode = true
break
}
}
bridge.heartbeat.SetNbAccount(len(bridge.users))
bridge.heartbeat.SetSplitMode(splitMode)
}, bridge.usersLock)
bridge.heartbeat.SetRollout(bridge.GetUpdateRollout())
bridge.heartbeat.SetAutoStart(bridge.GetAutostart())
bridge.heartbeat.SetAutoUpdate(bridge.GetAutoUpdate())
bridge.heartbeat.SetBeta(bridge.GetUpdateChannel())
bridge.heartbeat.SetDoh(bridge.GetProxyAllowed())
bridge.heartbeat.SetShowAllMail(bridge.GetShowAllMail())
bridge.heartbeat.SetIMAPConnectionMode(bridge.GetIMAPSSL())
bridge.heartbeat.SetSMTPConnectionMode(bridge.GetSMTPSSL())
bridge.heartbeat.SetIMAPPort(bridge.GetIMAPPort())
bridge.heartbeat.SetSMTPPort(bridge.GetSMTPPort())
bridge.heartbeat.SetCacheLocation(bridge.GetGluonCacheDir())
if val, err := bridge.GetKeychainApp(); err != nil {
bridge.heartbeat.SetKeyChainPref(val)
}
bridge.heartbeat.SetPrevVersion(bridge.GetLastVersion().String())
}

View File

@ -46,6 +46,8 @@ func (bridge *Bridge) SetKeychainApp(helper string) error {
return err
}
bridge.heartbeat.SetKeyChainPref(helper)
return vault.SetHelper(vaultDir, helper)
}
@ -62,6 +64,8 @@ func (bridge *Bridge) SetIMAPPort(newPort int) error {
return err
}
bridge.heartbeat.SetIMAPPort(newPort)
return bridge.restartIMAP()
}
@ -78,6 +82,8 @@ func (bridge *Bridge) SetIMAPSSL(newSSL bool) error {
return err
}
bridge.heartbeat.SetIMAPConnectionMode(newSSL)
return bridge.restartIMAP()
}
@ -94,6 +100,8 @@ func (bridge *Bridge) SetSMTPPort(newPort int) error {
return err
}
bridge.heartbeat.SetSMTPPort(newPort)
return bridge.restartSMTP()
}
@ -110,6 +118,8 @@ func (bridge *Bridge) SetSMTPSSL(newSSL bool) error {
return err
}
bridge.heartbeat.SetSMTPConnectionMode(newSSL)
return bridge.restartSMTP()
}
@ -141,6 +151,8 @@ func (bridge *Bridge) SetGluonDir(ctx context.Context, newGluonDir string) error
}
}
bridge.heartbeat.SetCacheLocation(newGluonDir)
gluonDataDir, err := bridge.GetGluonDataDir()
if err != nil {
return fmt.Errorf("failed to get Gluon Database directory: %w", err)
@ -207,6 +219,8 @@ func (bridge *Bridge) SetProxyAllowed(allowed bool) error {
bridge.proxyCtl.DisallowProxy()
}
bridge.heartbeat.SetDoh(allowed)
return bridge.vault.SetProxyAllowed(allowed)
}
@ -220,6 +234,8 @@ func (bridge *Bridge) SetShowAllMail(show bool) error {
user.SetShowAllMail(show)
}
bridge.heartbeat.SetShowAllMail(show)
return bridge.vault.SetShowAllMail(show)
}, bridge.usersLock)
}
@ -233,6 +249,8 @@ func (bridge *Bridge) SetAutostart(autostart bool) error {
if err := bridge.vault.SetAutostart(autostart); err != nil {
return err
}
bridge.heartbeat.SetAutoStart(autostart)
}
var err error
@ -253,6 +271,10 @@ func (bridge *Bridge) SetAutostart(autostart bool) error {
return err
}
func (bridge *Bridge) GetUpdateRollout() float64 {
return bridge.vault.GetUpdateRollout()
}
func (bridge *Bridge) GetAutoUpdate() bool {
return bridge.vault.GetAutoUpdate()
}
@ -266,6 +288,8 @@ func (bridge *Bridge) SetAutoUpdate(autoUpdate bool) error {
return err
}
bridge.heartbeat.SetAutoUpdate(autoUpdate)
bridge.goUpdate()
return nil
@ -292,6 +316,8 @@ func (bridge *Bridge) SetUpdateChannel(channel updater.Channel) error {
return err
}
bridge.heartbeat.SetBeta(channel)
bridge.goUpdate()
return nil

View File

@ -295,6 +295,15 @@ func (bridge *Bridge) SetAddressMode(ctx context.Context, userID string, mode va
AddressMode: mode,
})
var splitMode = false
for _, user := range bridge.users {
if user.GetAddressMode() == vault.SplitMode {
splitMode = true
break
}
}
bridge.heartbeat.SetSplitMode(splitMode)
return nil
}, bridge.usersLock)
}
@ -559,6 +568,7 @@ func (bridge *Bridge) addUserWithVault(
// Finally, save the user in the bridge.
safe.Lock(func() {
bridge.users[apiUser.ID] = user
bridge.heartbeat.SetNbAccount(len(bridge.users))
}, bridge.usersLock)
return nil
@ -614,6 +624,8 @@ func (bridge *Bridge) logoutUser(ctx context.Context, user *user.User, withAPI,
logrus.WithError(err).Error("Failed to logout user")
}
bridge.heartbeat.SetNbAccount(len(bridge.users))
user.Close()
}