Skip to content

Commit

Permalink
Enh/advanced logging (#1247)
Browse files Browse the repository at this point in the history
* Minor changes in user-settings, still some errors in removing parts

* Added custom setting to add custom speeds for playback and implemented these into video player

* Full rewrite of the logging in the api folder

* Fixed a bug in statistics.go

* Changed logging in dao and tumlive

* Finished rewriting logs everywhere but in the worker

* Minor changes in user-settings, still some errors in removing parts

* Added custom setting to add custom speeds for playback and implemented these into video player

* Full rewrite of the logging in the api folder

* Fixed a bug in statistics.go

* Changed logging in dao and tumlive

* Finished rewriting logs everywhere but in the worker

* Added json logging to dao and in email.go, go upgraded to 1.21, added slogGorm package

* Merged locally

* Changed Dockerfile to work with multiple files in cmd/tumlive

* Fixed a bug

* Fixed a bug

* Fixed some version things

* Fixed build errors

* Fixed bug in worker

* Fixed another bug

* Testing another change in go.work

* Updated go.sum

* Fixed go.work

* Updated to go 1.21.5

* Removed autobuild

* Added correct make targets to build

* Fixed some more bugs

* Changed everything to 1.21 instead of 1.21.5

* Removed toolchain directive

* Added toolchain directive with new version

* Last try with toolchain directive

* Fixed pipeline
  • Loading branch information
SebiWrn committed May 7, 2024
1 parent 66eb273 commit f5d370c
Show file tree
Hide file tree
Showing 78 changed files with 693 additions and 502 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
# uses a compiled language

#- run: |
# make bootstrap
# make all
# make release

- name: Perform CodeQL Analysis
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
test:
strategy:
matrix:
go-version: [ 1.20.x ]
go-version: [ 1.21.x ]
os: [ ubuntu-latest ]
runs-on: ${{ matrix.os }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
steps:
- uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: 1.21
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
Expand Down
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ linters:
fast: false

run:
go: '1.19'
go: '1.21'
timeout: 10m
skip-dirs:
- node_modules
Expand Down Expand Up @@ -67,7 +67,7 @@ linters-settings:
- name: modifies-value-receiver
gofumpt:
extra-rules: false
lang-version: "1.19"
lang-version: "1.21"

issues:
max-issues-per-linter: 0
Expand Down
8 changes: 0 additions & 8 deletions .idea/runConfigurations/all.xml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ docker run --detach \

### Install go

- Install **go >=1.18** by following the steps [here](https://go.dev/doc/install)
- Install **go >=1.21** by following the steps [here](https://go.dev/doc/install)
- Preferably use [JetBrains GoLand](https://youtu.be/vetAfxQxyJE) and open this project as it simplifies this entire process
- Go to File -> Settings -> Go -> Go Modules and enable go modules integration.
- Run `npm i` in the `./web` directory to install the required node modules
Expand Down
10 changes: 10 additions & 0 deletions api/api_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package api

import (
"log/slog"
"os"
)

var logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
})).With("service", "api")
65 changes: 32 additions & 33 deletions api/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

"github.com/getsentry/sentry-go"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -65,7 +64,7 @@ func RegisterRealtimeChatChannel() {
req, err := parseChatPayload(message)

if err != nil {
log.WithError(err).Warn("could not unmarshal request")
logger.Warn("could not unmarshal request", "err", err)
return
}

Expand All @@ -89,7 +88,7 @@ func RegisterRealtimeChatChannel() {
case "react_to":
routes.handleReactTo(tumLiveContext, message.Payload)
default:
log.WithField("type", req.Type).Warn("unknown websocket request type")
logger.Warn("unknown websocket request type", "type", req.Type)
}
},
})
Expand Down Expand Up @@ -121,15 +120,15 @@ type chatRoutes struct {
func (r chatRoutes) handleSubmitPollOptionVote(ctx tools.TUMLiveContext, msg []byte) {
var req submitPollOptionVote
if err := json.Unmarshal(msg, &req); err != nil {
log.WithError(err).Warn("could not unmarshal submit poll answer request")
logger.Warn("could not unmarshal submit poll answer request", "err", err)
return
}
if ctx.User == nil {
return
}

if err := r.ChatDao.AddChatPollOptionVote(req.PollOptionId, ctx.User.ID); err != nil {
log.WithError(err).Warn("could not add poll option vote")
logger.Warn("could not add poll option vote", "err", err)
return
}

Expand All @@ -144,7 +143,7 @@ func (r chatRoutes) handleSubmitPollOptionVote(ctx tools.TUMLiveContext, msg []b
if voteUpdateJson, err := json.Marshal(voteUpdateMap); err == nil {
broadcastStreamToAdmins(ctx.Stream.ID, voteUpdateJson)
} else {
log.WithError(err).Warn("could not marshal vote update map")
logger.Warn("could not marshal vote update map", "err", err)
return
}
}
Expand All @@ -158,22 +157,22 @@ func (r chatRoutes) handleStartPoll(ctx tools.TUMLiveContext, msg []byte) {

var req startPollReq
if err := json.Unmarshal(msg, &req); err != nil {
log.WithError(err).Warn("could not unmarshal start poll request")
logger.Warn("could not unmarshal start poll request", "err", err)
return
}
if ctx.User == nil || !ctx.User.IsAdminOfCourse(*ctx.Course) {
return
}

if len(req.Question) == 0 {
log.Warn("could not create poll with empty question")
logger.Warn("could not create poll with empty question")
return
}

var pollOptions []model.PollOption
for _, answer := range req.PollAnswers {
if len(answer) == 0 {
log.Warn("could not create poll with empty answer")
logger.Warn("could not create poll with empty answer")
return
}
pollOptions = append(pollOptions, model.PollOption{
Expand Down Expand Up @@ -244,7 +243,7 @@ func (r chatRoutes) handleResolve(ctx tools.TUMLiveContext, msg []byte) {
var req wsIdReq
err := json.Unmarshal(msg, &req)
if err != nil {
log.WithError(err).Warn("could not unmarshal message delete request")
logger.Warn("could not unmarshal message delete request", "err", err)
return
}
if ctx.User == nil || !ctx.User.IsAdminOfCourse(*ctx.Course) {
Expand All @@ -253,15 +252,15 @@ func (r chatRoutes) handleResolve(ctx tools.TUMLiveContext, msg []byte) {

err = r.ChatDao.ResolveChat(req.Id)
if err != nil {
log.WithError(err).Error("could not delete chat")
logger.Error("could not delete chat", "err", err)
}

broadcast := gin.H{
"resolve": req.Id,
}
broadcastBytes, err := json.Marshal(broadcast)
if err != nil {
log.WithError(err).Error("could not marshal delete message")
logger.Error("could not marshal delete message", "err", err)
return
}
broadcastStream(ctx.Stream.ID, broadcastBytes)
Expand All @@ -271,22 +270,22 @@ func (r chatRoutes) handleDelete(ctx tools.TUMLiveContext, msg []byte) {
var req wsIdReq
err := json.Unmarshal(msg, &req)
if err != nil {
log.WithError(err).Warn("could not unmarshal message delete request")
logger.Warn("could not unmarshal message delete request", "err", err)
return
}
if ctx.User == nil || !ctx.User.IsAdminOfCourse(*ctx.Course) {
return
}
err = r.ChatDao.DeleteChat(req.Id)
if err != nil {
log.WithError(err).Error("could not delete chat")
logger.Error("could not delete chat", "err", err)
}
broadcast := gin.H{
"delete": req.Id,
}
broadcastBytes, err := json.Marshal(broadcast)
if err != nil {
log.WithError(err).Error("could not marshal delete message")
logger.Error("could not marshal delete message", "err", err)
return
}
broadcastStream(ctx.Stream.ID, broadcastBytes)
Expand All @@ -296,7 +295,7 @@ func (r chatRoutes) handleApprove(ctx tools.TUMLiveContext, msg []byte) {
var req wsIdReq
err := json.Unmarshal(msg, &req)
if err != nil {
log.WithError(err).Warn("could not unmarshal message approve request")
logger.Warn("could not unmarshal message approve request", "err", err)
return
}
if ctx.User == nil || !ctx.User.IsAdminOfCourse(*ctx.Course) {
Expand All @@ -305,7 +304,7 @@ func (r chatRoutes) handleApprove(ctx tools.TUMLiveContext, msg []byte) {

err = r.ChatDao.ApproveChat(req.Id)
if err != nil {
log.WithError(err).Error("could not approve chat")
logger.Error("could not approve chat", "err", err)
return
}

Expand All @@ -315,15 +314,15 @@ func (r chatRoutes) handleApprove(ctx tools.TUMLiveContext, msg []byte) {
*/
chat, err := r.ChatDao.GetChat(req.Id, 0)
if err != nil {
log.WithError(err).Error("could not get chat")
logger.Error("could not get chat", "err", err)
}
broadcast := gin.H{
"approve": req.Id,
"chat": chat,
}
broadcastBytes, err := json.Marshal(broadcast)
if err != nil {
log.WithError(err).Error("could not marshal approve message")
logger.Error("could not marshal approve message", "err", err)
return
}
broadcastStream(ctx.Stream.ID, broadcastBytes)
Expand All @@ -333,23 +332,23 @@ func (r chatRoutes) handleReactTo(ctx tools.TUMLiveContext, msg []byte) {
var req wsReactToReq
err := json.Unmarshal(msg, &req)
if err != nil {
log.WithError(err).Warn("could not unmarshal reactTo request")
logger.Warn("could not unmarshal reactTo request", "err", err)
return
}

if _, isAllowed := allowedReactions[req.Reaction]; !isAllowed {
log.Warn("user tried to add illegal reaction")
logger.Warn("user tried to add illegal reaction")
return
}

err = r.ChatDao.ToggleReaction(ctx.User.ID, req.wsIdReq.Id, ctx.User.Name, req.Reaction)
if err != nil {
log.WithError(err).Error("error reacting to message")
logger.Error("error reacting to message", "err", err)
return
}
reactions, err := r.ChatDao.GetReactions(req.Id)
if err != nil {
log.WithError(err).Error("error getting num of chat reactions")
logger.Error("error getting num of chat reactions", "err", err)
return
}
broadcast := gin.H{
Expand All @@ -358,7 +357,7 @@ func (r chatRoutes) handleReactTo(ctx tools.TUMLiveContext, msg []byte) {
}
broadcastBytes, err := json.Marshal(broadcast)
if err != nil {
log.WithError(err).Error("Can't marshal reactions message")
logger.Error("Can't marshal reactions message", "err", err)
return
}
broadcastStream(ctx.Stream.ID, broadcastBytes)
Expand All @@ -368,7 +367,7 @@ func (r chatRoutes) handleRetract(ctx tools.TUMLiveContext, msg []byte) {
var req wsIdReq
err := json.Unmarshal(msg, &req)
if err != nil {
log.WithError(err).Warn("could not unmarshal message retract request")
logger.Warn("could not unmarshal message retract request", "err", err)
return
}
if ctx.User == nil || !ctx.User.IsAdminOfCourse(*ctx.Course) {
Expand All @@ -377,27 +376,27 @@ func (r chatRoutes) handleRetract(ctx tools.TUMLiveContext, msg []byte) {

err = r.ChatDao.RetractChat(req.Id)
if err != nil {
log.WithError(err).Error("could not retract chat")
logger.Error("could not retract chat", "err", err)
return
}

err = r.ChatDao.RemoveReactions(req.Id)
if err != nil {
log.WithError(err).Error("could not remove reactions from chat")
logger.Error("could not remove reactions from chat", "err", err)
return
}

chat, err := r.ChatDao.GetChat(req.Id, 0)
if err != nil {
log.WithError(err).Error("could not get chat")
logger.Error("could not get chat", "err", err)
}
broadcast := gin.H{
"retract": req.Id,
"chat": chat,
}
broadcastBytes, err := json.Marshal(broadcast)
if err != nil {
log.WithError(err).Error("could not marshal retract message")
logger.Error("could not marshal retract message", "err", err)
return
}
broadcastStream(ctx.Stream.ID, broadcastBytes)
Expand All @@ -406,7 +405,7 @@ func (r chatRoutes) handleRetract(ctx tools.TUMLiveContext, msg []byte) {
func (r chatRoutes) handleMessage(ctx tools.TUMLiveContext, context *realtime.Context, msg []byte) {
var chat chatReq
if err := json.Unmarshal(msg, &chat); err != nil {
log.WithError(err).Error("error unmarshalling chat message")
logger.Error("error unmarshalling chat message", "err", err)
return
}
if !ctx.Course.ChatEnabled && !ctx.Stream.ChatEnabled {
Expand Down Expand Up @@ -578,7 +577,7 @@ func (r chatRoutes) getActivePoll(c *gin.Context) {
if isAdminOfCourse {
voteCount, err = r.ChatDao.GetPollOptionVoteCount(option.ID)
if err != nil {
log.WithError(err).Warn("could not get poll option vote count")
logger.Warn("could not get poll option vote count", "err", err)
}
}

Expand Down Expand Up @@ -681,7 +680,7 @@ func CollectStats(daoWrapper dao.DaoWrapper) func() {
if s.LiveNow { // store stats for livestreams only
s.Stats = append(s.Stats, stat)
if err := daoWrapper.AddStat(stat); err != nil {
log.WithError(err).Error("Saving stat failed")
logger.Error("Saving stat failed", "err", err)
}
}
}
Expand Down Expand Up @@ -734,7 +733,7 @@ func afterUnsubscribe(id string, joinTime time.Time, recording bool, daoWrapper
if recording && joinTime.Before(time.Now().Add(time.Minute*-5)) {
err := daoWrapper.AddVodView(id)
if err != nil {
log.WithError(err).Error("Can't save vod view")
logger.Error("Can't save vod view", "err", err)
}
}
}
Loading

0 comments on commit f5d370c

Please sign in to comment.