GODT-1376: Add first userID to sentry scope.

This commit is contained in:
Jakub
2022-01-17 17:51:31 +01:00
parent f9f4ce996d
commit a3d2df9d38
5 changed files with 33 additions and 10 deletions

View File

@ -189,6 +189,8 @@ func New( // nolint[funlen]
cm := pmapi.New(cfg)
sentryReporter.SetClientFromManager(cm)
cm.AddConnectionObserver(pmapi.NewConnectionObserver(
func() { listener.Emit(events.InternetOffEvent, "") },
func() { listener.Emit(events.InternetOnEvent, "") },

View File

@ -20,11 +20,13 @@ package sentry
import (
"errors"
"fmt"
"log"
"os"
"runtime"
"time"
"github.com/ProtonMail/proton-bridge/internal/constants"
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
"github.com/getsentry/sentry-go"
"github.com/sirupsen/logrus"
)
@ -42,7 +44,13 @@ func init() { //nolint[noinit, gochecknoinits]
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetFingerprint([]string{"{{ default }}"})
scope.SetTag("UserID", "not-defined")
})
sentry.Logger = log.New(
logrus.WithField("pkg", "sentry-go").WriterLevel(logrus.WarnLevel),
"", 0,
)
}
type Reporter struct {
@ -111,7 +119,6 @@ func (r *Reporter) scopedReport(context map[string]interface{}, doReport func())
"Client": r.appName,
"Version": r.appVersion,
"UserAgent": r.userAgent.String(),
"UserID": "",
"HostArch": r.hostArch,
}
@ -182,3 +189,6 @@ func isFunctionFilteredOut(function string) bool {
func Flush(maxWaiTime time.Duration) {
sentry.Flush(maxWaiTime)
}
func (r *Reporter) SetClientFromManager(cm pmapi.Manager) {
}

View File

@ -82,4 +82,5 @@ type AuthRefreshHandler func(*AuthRefresh)
type clientManager interface {
r(context.Context) *resty.Request
authRefresh(context.Context, string, string) (*AuthRefresh, error)
setSentryUserID(userID string)
}

View File

@ -23,6 +23,7 @@ import (
"sync"
"time"
"github.com/getsentry/sentry-go"
"github.com/go-resty/resty/v2"
)
@ -35,8 +36,9 @@ type manager struct {
connectionObservers []ConnectionObserver
proxyDialer *ProxyTLSDialer
pingMutex *sync.RWMutex
isPinging bool
pingMutex *sync.RWMutex
isPinging bool
setSentryUserIDOnce sync.Once
}
func New(cfg Config) Manager {
@ -45,11 +47,12 @@ func New(cfg Config) Manager {
func newManager(cfg Config) *manager {
m := &manager{
cfg: cfg,
rc: resty.New().EnableTrace(),
locker: &sync.Mutex{},
pingMutex: &sync.RWMutex{},
isPinging: false,
cfg: cfg,
rc: resty.New().EnableTrace(),
locker: &sync.Mutex{},
pingMutex: &sync.RWMutex{},
isPinging: false,
setSentryUserIDOnce: sync.Once{},
}
proxyDialer, transport := newProxyDialerAndTransport(cfg)
@ -158,3 +161,11 @@ func (m *manager) handleRequestFailure(req *resty.Request, err error) {
go m.pingUntilSuccess()
}
func (m *manager) setSentryUserID(userID string) {
m.setSentryUserIDOnce.Do(func() {
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetTag("UserID", userID)
})
})
}

View File

@ -21,7 +21,6 @@ import (
"context"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/getsentry/sentry-go"
"github.com/go-resty/resty/v2"
"github.com/pkg/errors"
)
@ -126,7 +125,7 @@ func (c *client) UpdateUser(ctx context.Context) (*User, error) {
c.user = user
c.addresses = addresses
sentry.ConfigureScope(func(scope *sentry.Scope) { scope.SetUser(sentry.User{ID: user.ID}) })
c.manager.setSentryUserID(user.ID)
return user, err
}