[core] Clean IPs from socket errors

in order not to log IPs (GDPR) and also to prevent spread of errors in
Sentry logging as IP/Port combinations are unique per connection and
therefore will spam with many unique errors. By removing the unique
information from the error the errors should be groupable again.

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-06-24 14:59:36 +02:00
parent c311370d1c
commit 49aba55cfe
Signed by: luzifer
GPG Key ID: D91C3E91E4CAD6F5
5 changed files with 44 additions and 9 deletions

View File

@ -0,0 +1,2 @@
// Package helpers contains helpers to assist in other parts of the code
package helpers

35
internal/helpers/operr.go Normal file
View File

@ -0,0 +1,35 @@
package helpers
import (
"errors"
"net"
"regexp"
)
// CleanOpError checks whether a *net.OpError is included in the error
// and if so removes the included address information. This can happen
// in two ways: If the passed error is indeed an OpError the address
// info is just patched out. If the OpError is buried deeper inside
// the wrapped error stack, a new error with patched message is created
// sacrificing the wrapping and possible included stacktrace.
//
// As of the loss of information this is only intended to clean up
// logging and not be used in error returns.
func CleanOpError(err error) error {
if opE, ok := err.(*net.OpError); ok {
// Error in the outmost position is an OpError, lets just patch it
opE.Source = nil
opE.Addr = nil
return opE
}
var opE *net.OpError
if !errors.As(err, &opE) {
// There is no OpError somewhere inside, keep the error as is
return err
}
// Patch out IP information and create an new error with its message
return errors.New(regexp.MustCompile(` (?:(?:[0-9]+\.){3}[0-9]+:[0-9]+(?:->)?)+`).
ReplaceAllString(err.Error(), ""))
}

View File

@ -20,6 +20,7 @@ import (
"github.com/Luzifer/go_helpers/v2/str"
"github.com/Luzifer/rconfig/v2"
"github.com/Luzifer/twitch-bot/v3/internal/helpers"
"github.com/Luzifer/twitch-bot/v3/internal/service/access"
"github.com/Luzifer/twitch-bot/v3/internal/service/timer"
"github.com/Luzifer/twitch-bot/v3/pkg/database"
@ -281,13 +282,7 @@ func main() {
go func() {
log.Info("(re-)connecting IRC client")
if err := ircHdl.Run(); err != nil {
log.WithError(err).Debug("IRC connection failed")
// We don't want to spam Sentry with errors each being unique
// and not groupable as it contains `localip:localport -> remoteip:remoteport`
// therefore we replace it with a generic error and just put
// the underlying error in the debug-level log which doesn't
// get sent to Sentry
log.WithError(errors.New("connection to Twitch IRC servers broke")).Error("IRC run exited unexpectedly")
log.WithError(helpers.CleanOpError(err)).Error("IRC run exited unexpectedly")
}
time.Sleep(ircReconnectDelay)
ircDisconnected <- struct{}{}

View File

@ -12,6 +12,8 @@ import (
"github.com/gorilla/websocket"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/Luzifer/twitch-bot/v3/internal/helpers"
)
const (
@ -173,7 +175,7 @@ func (e *EventSubSocketClient) Run() error {
defer func() {
if err := e.conn.Close(); err != nil {
e.logger.WithError(err).Error("finally closing socket")
e.logger.WithError(helpers.CleanOpError(err)).Error("finally closing socket")
}
}()

View File

@ -7,6 +7,7 @@ import (
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/Luzifer/twitch-bot/v3/internal/helpers"
"github.com/Luzifer/twitch-bot/v3/internal/service/access"
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
"github.com/Luzifer/twitch-bot/v3/plugins"
@ -392,7 +393,7 @@ func (t *twitchWatcher) updateChannelFromAPI(channel string) error {
log.WithField("channel", channel).Info("watching for eventsub events")
go func(storedStatus *twitchChannelState) {
if err := storedStatus.esc.Run(); err != nil {
log.WithField("channel", channel).WithError(err).Error("eventsub client caused error")
log.WithField("channel", channel).WithError(helpers.CleanOpError(err)).Error("eventsub client caused error")
}
storedStatus.CloseESC()
}(storedStatus)