Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit f47f02a

Browse files
committed
WIP
1 parent 5b118a9 commit f47f02a

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

platform/logger/log_sender.go

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"errors"
88
"fmt"
99
"github.com/QuesmaOrg/quesma/platform/telemetry/headers"
10+
"github.com/goccy/go-json"
11+
"log"
1012
"net/http"
1113
"net/url"
1214
"strconv"
@@ -22,6 +24,45 @@ type LogSender struct {
2224
httpClient *http.Client
2325
}
2426

27+
func (logSender *LogSender) cutMessage(msg []byte) ([]byte, bool) {
28+
29+
var msgMap map[string]any
30+
if err := json.Unmarshal(msg, &msgMap); err != nil {
31+
32+
return nil, false // if not a valid JSON, return as is
33+
}
34+
35+
message, ok := msgMap["message"].(string)
36+
37+
if !ok {
38+
return nil, false // if "message" key is not present or not a string, return as is
39+
}
40+
41+
cutMark := "..."
42+
newLine := "\n"
43+
charToCut := len(message) + len(cutMark) + len(newLine) - cap(logSender.LogBuffer)
44+
45+
log.Println("XXXX LogSender: cutting message to fit buffer, charToCut:", charToCut, "message length:", len(message), "buffer capacity:", cap(logSender.LogBuffer))
46+
47+
// cutting the message will not help, let's drop it
48+
if charToCut < 0 {
49+
return nil, false // if buffer has enough space, return original message
50+
}
51+
52+
if charToCut < len(message) {
53+
msgCut := message[:len(message)-charToCut]
54+
msgMap["message"] = msgCut + cutMark
55+
} else {
56+
msgMap["message"] = message
57+
}
58+
59+
trimmedMsg, err := json.Marshal(msgMap)
60+
if err != nil {
61+
return nil, false // if marshalling fails, return as is
62+
}
63+
return append(trimmedMsg, newLine...), ok // append newline to maintain log format
64+
}
65+
2566
func (logSender *LogSender) EatLogMessage(msg []byte) struct {
2667
bufferLengthCondition bool
2768
timeCondition bool
@@ -38,14 +79,17 @@ func (logSender *LogSender) EatLogMessage(msg []byte) struct {
3879
} else {
3980
addedBefore := false
4081
if !bufferLengthCondition && len(logSender.LogBuffer) == 0 { // msg longer than buffer, let's cut it
41-
cutMark := []byte("...\n")
42-
charToCut := len(msg) + len(cutMark) - cap(logSender.LogBuffer)
43-
if charToCut < len(msg) {
44-
msgCut := msg[:len(msg)-charToCut]
45-
logSender.LogBuffer = append(logSender.LogBuffer, msgCut...)
46-
logSender.LogBuffer = append(logSender.LogBuffer, cutMark...)
82+
83+
// we can the message part
84+
trimmedMsg, ok := logSender.cutMessage(msg)
85+
if ok {
86+
log.Println("XXXX LogSender: message was too long, cutting it to fit buffer")
87+
logSender.LogBuffer = append(logSender.LogBuffer, trimmedMsg...)
4788
addedBefore = true
89+
} else {
90+
log.Println("XXXX LogSender: message was too long, but could not cut it, dropping it")
4891
}
92+
4993
} else if len(logSender.LogBuffer)+len(msg) <= cap(logSender.LogBuffer) { // still fits in buffer
5094
logSender.LogBuffer = append(logSender.LogBuffer, msg...)
5195
addedBefore = true

0 commit comments

Comments
 (0)