Skip to content

Commit

Permalink
chore: add sentry context on.. context
Browse files Browse the repository at this point in the history
  • Loading branch information
aldy505 committed May 30, 2024
1 parent 945228d commit cc6708f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ RUN go build -o brassite ./cmd/brassite/main.go

FROM alpine:3.20 AS runtime

WORKDIR /usr/local/src/brassite

COPY . .

COPY --from=builder /build/brassite /usr/local/bin/brassite

CMD ["/usr/local/bin/brassite"]
22 changes: 17 additions & 5 deletions cmd/brassite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,30 @@ func main() {
}

<-exitSignal
slog.Info("Shutting down Brassite")
}

func runWorker(feed brassite.Feed) {
for {
slog.Debug("Starting worker", slog.String("feed_name", feed.Name), slog.String("url", feed.URL), slog.Duration("interval", feed.Interval))

ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
hub := sentry.CurrentHub().Clone()
hub.Scope().SetTag("feed_name", feed.Name)
hub.Scope().SetExtras(map[string]interface{}{
"feed_name": feed.Name,
"url": feed.URL,
"interval": feed.Interval.String(),
"without_content": feed.WithoutContent,
})
ctx = sentry.SetHubOnContext(ctx, hub)

// Call the feed parser
request, err := http.NewRequestWithContext(ctx, http.MethodGet, feed.URL, nil)
if err != nil {
slog.Error("Failed to create request", slog.Any("error", err), slog.String("feed_name", feed.Name))
cancel()
sentry.CaptureException(err)
sentry.GetHubFromContext(ctx).CaptureException(err)
time.Sleep(feed.Interval)
continue
}
Expand All @@ -134,7 +144,7 @@ func runWorker(feed brassite.Feed) {
if err != nil {
slog.Error("Failed to send request", slog.Any("error", err), slog.String("feed_name", feed.Name))
cancel()
sentry.CaptureException(err)
sentry.GetHubFromContext(ctx).CaptureException(err)
time.Sleep(feed.Interval)
continue
}
Expand All @@ -145,11 +155,14 @@ func runWorker(feed brassite.Feed) {
slog.Error("Failed to parse feed", slog.Any("error", err), slog.String("feed_name", feed.Name))
_ = response.Body.Close()
cancel()
sentry.CaptureException(err)
sentry.GetHubFromContext(ctx).CaptureException(err)
time.Sleep(feed.Interval)
continue
}

// Don't take too long to close the body
_ = response.Body.Close()

// Only select the new items by using now - interval
var newItems []*gofeed.Item
for _, item := range remoteFeed.Items {
Expand Down Expand Up @@ -201,7 +214,7 @@ func runWorker(feed brassite.Feed) {
if err != nil {
slog.Error("Failed to deliver to Discord", slog.String("feed_name", feed.Name), slog.Any("error", err))

sentry.CaptureException(err)
sentry.GetHubFromContext(ctx).CaptureException(err)
}
}

Expand All @@ -216,7 +229,6 @@ func runWorker(feed brassite.Feed) {
// }
}

_ = response.Body.Close()
cancel()

time.Sleep(feed.Interval)
Expand Down
1 change: 1 addition & 0 deletions delivery_discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func DeliverToDiscord(ctx context.Context, webhookURL string, feedItem FeedItem,
}

request.Header.Set("Content-Type", "application/json")
request.Header.Set("User-Agent", "Brassite/1.0")

response, err := http.DefaultClient.Do(request)
if err != nil {
Expand Down

0 comments on commit cc6708f

Please sign in to comment.