Skip to content

Commit

Permalink
update hook with sentry for logger, update message
Browse files Browse the repository at this point in the history
  • Loading branch information
adlandh committed Sep 3, 2023
1 parent 40d7886 commit 7447a51
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
8 changes: 8 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ tasks:
cmds:
- pulumi up -y
registry:
desc: Run registry locally
cmds:
- docker run -d -p 5000:5000 --restart=always --name registry registry:2
- skaffold config set default-repo localhost:5000
send-test-string:
desc: Send test string
dotenv:
- local-secrets.env
cmds:
- http GET http://localhost:8080/api/${AUTH_TOKEN}/test?msg=test --raw="test body"

10 changes: 6 additions & 4 deletions internal/post-forwarder/driven/telegram_message_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/adlandh/post-forwarder/internal/post-forwarder/config"
"github.com/adlandh/post-forwarder/internal/post-forwarder/domain"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)

const MaxMessageLength = 4_096
Expand Down Expand Up @@ -36,14 +37,15 @@ func NewTelegramMessageSender(cfg *config.Config) (*TelegramMessageSender, error
}

func (t TelegramMessageSender) Send(ctx context.Context, service, msg string) error {
fullMessage := fmt.Sprintf("Message from %s: %s", service, msg)
if len(fullMessage) > MaxMessageLength {
fullMessage := fmt.Sprintf("<b>%s</b>:\n%s", service, msg)
if len([]rune(fullMessage)) > MaxMessageLength {
fullMessage = fullMessage[:MaxMessageLength]
}

_, err := t.bot.SendMessage(ctx, &bot.SendMessageParams{
ChatID: t.chatId,
Text: fullMessage,
ChatID: t.chatId,
Text: fullMessage,
ParseMode: models.ParseModeHTML,
})

return err
Expand Down
12 changes: 10 additions & 2 deletions internal/post-forwarder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"context"
"fmt"
"strconv"
"time"

"github.com/adlandh/echo-sentry-middleware"
Expand Down Expand Up @@ -38,7 +38,14 @@ func NewLogger(cfg *config.Config) (*zap.Logger, error) {
func(entry zapcore.Entry) error {
if entry.Level == zapcore.ErrorLevel {
defer sentry.Flush(2 * time.Second)
sentry.CaptureMessage(fmt.Sprintf("%s, Line No: %d :: %s", entry.Caller.File, entry.Caller.Line, entry.Message))
localHub := sentry.CurrentHub().Clone()
localHub.ConfigureScope(func(scope *sentry.Scope) {
scope.SetTag("File", entry.Caller.File)
scope.SetTag("Line", strconv.Itoa(entry.Caller.Line))
scope.SetTag("message", entry.Message)
scope.SetLevel(sentry.LevelError)
})
localHub.CaptureMessage("error")
}
return nil
},
Expand All @@ -58,6 +65,7 @@ func NewSentry(lc fx.Lifecycle, cfg *config.Config) error {
EnableTracing: true,
TracesSampleRate: cfg.SentryTracesSampleRate,
ProfilesSampleRate: cfg.SentryProfilesSampleRate,
MaxErrorDepth: 1,
})
},
OnStop: func(ctx context.Context) error {
Expand Down

0 comments on commit 7447a51

Please sign in to comment.