Skip to content

Commit

Permalink
chore:added a logrus hook for sentry (#201)
Browse files Browse the repository at this point in the history
* added logrus_sentry to collect error messages in sentry

* fixed the dockerfile not having devmode set

* switched to `makasim/sentryhook`, as this does not use the old sentry-sdk for go

* bumped the sentry-sdk

* Improved how we implement the sentry integration

* fixed the go.mod and go.sum files

* added log levels

* merge
  • Loading branch information
CommanderStorm authored Sep 12, 2023
1 parent b80d19b commit d7965bc
Show file tree
Hide file tree
Showing 6 changed files with 804 additions and 38 deletions.
3 changes: 3 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ services:
restart: always
build:
context: server/
args:
version: dev # compiled with the git sha in prod
ports:
- 50051:50051
environment:
- DB_DSN=root:${DB_ROOT_PASSWORD}@tcp(db:${DB_PORT:-3306})/${DB_NAME}?charset=utf8mb4&parseTime=True&loc=Local
- ENVIRONMENT=dev
- SENTRY_DSN=${SENTRY_DSN}
- APNS_KEY_ID=${APNS_KEY_ID}
- APNS_TEAM_ID=${APNS_TEAM_ID}
Expand Down
6 changes: 0 additions & 6 deletions server/backend/cron/fileDownload.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/disintegration/imaging"
"github.com/gabriel-vasile/mimetype"
"github.com/getsentry/sentry-go"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
"image"
Expand Down Expand Up @@ -43,7 +42,6 @@ func (c *CronService) downloadFile(file model.Files) {
resp, err := http.Get(url)
if err != nil {
log.WithError(err).WithField("url", url).Warn("Could not download image")
sentry.CaptureException(err)
return
}
// read body here because we can't exhaust the io.reader twice
Expand All @@ -59,7 +57,6 @@ func (c *CronService) downloadFile(file model.Files) {
downloadedImg, _, err := image.Decode(bytes.NewReader(body))
if err != nil {
log.WithError(err).WithField("url", url).Warn("Couldn't decode source image")
sentry.CaptureException(err)
return
}

Expand All @@ -69,21 +66,18 @@ func (c *CronService) downloadFile(file model.Files) {
err = imaging.Save(dstImage, StorageDir+dstFileName, imaging.JPEGQuality(75))
if err != nil {
log.WithError(err).WithField("url", url).Warn("Could not save image file")
sentry.CaptureException(err)
return
}
} else {
// save without resizing image
err = os.WriteFile(fmt.Sprintf("%s%s", file.Path, file.Name), body, 0644)
if err != nil {
sentry.CaptureException(err)
log.WithError(err).Error("Can't save file to disk")
return
}
}
err = c.db.Model(&model.Files{}).Where("url = ?", url).Update("downloaded", true).Error
if err != nil {
sentry.CaptureException(err)
log.WithError(err).Error("Could not set image to downloaded.")
}
}
14 changes: 4 additions & 10 deletions server/backend/cron/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/getsentry/sentry-go"
"github.com/guregu/null"
"github.com/microcosm-cc/bluemonday"
"github.com/mmcdole/gofeed"
Expand Down Expand Up @@ -44,8 +43,7 @@ func (c *CronService) newsCron(cronjob *model.Crontab) error {
var source model.NewsSource
err := c.db.Find(&source, cronjob.ID.Int64).Error
if err != nil {
log.Printf("error getting news source from database: %v", err)
sentry.CaptureException(err)
log.WithError(err).Error("getting news source from database")
return err
}
// skip sources with null url
Expand All @@ -68,15 +66,13 @@ func (c *CronService) parseNewsFeed(source model.NewsSource) error {
log.WithField("url", source.URL.String).Trace("processing newsfeed")
feed, err := c.gf.ParseURL(source.URL.String)
if err != nil {
log.Printf("error parsing rss: %v", err)
sentry.CaptureException(err)
log.WithError(err).Error("parsing rss")
return err
}
// get all news for this source so we only process new ones, using map for performance reasons
existingNewsLinksForSource := make([]string, 0)
if err := c.db.Table("`news`").Select("`link`").Where("`src` = ?", source.Source).Scan(&existingNewsLinksForSource).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
log.Printf("failed to fetch existing news: %v", err)
sentry.CaptureException(err)
log.WithError(err).Error("failed to fetch existing news")
return err
}
var newNews []model.News
Expand Down Expand Up @@ -108,8 +104,7 @@ func (c *CronService) parseNewsFeed(source model.NewsSource) error {
if pickedEnclosure != nil {
fileId, err = c.saveImage(pickedEnclosure.URL)
if err != nil {
log.WithError(err).Error("error saving image")
sentry.CaptureException(err)
log.WithError(err).Error("can't save news image")
}
enclosureUrl = null.String{NullString: sql.NullString{String: pickedEnclosure.URL, Valid: true}}
}
Expand Down Expand Up @@ -195,7 +190,6 @@ func (c *CronService) cleanOldNewsForSource(source int32) error {
log.WithField("RowsAffected", res.RowsAffected).Info("cleaned up old news")
} else {
log.WithError(res.Error).Error("failed to clean up old news")
sentry.CaptureException(res.Error)
return res.Error
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.17.1
github.com/guregu/null v4.0.0+incompatible
github.com/influxdata/influxdb-client-go/v2 v2.12.3
github.com/makasim/sentryhook v0.4.2
github.com/microcosm-cc/bluemonday v1.0.25
github.com/mmcdole/gofeed v1.2.1
github.com/satori/go.uuid v1.2.0
Expand Down Expand Up @@ -41,7 +42,6 @@ require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mmcdole/goxpp v1.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
Loading

0 comments on commit d7965bc

Please sign in to comment.