Skip to content

Commit b2cefd2

Browse files
committed
remove message when empty
1 parent b26935a commit b2cefd2

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

parser/filter_app.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ func (f *AppFilter) parseJsonMapValue(m map[string]interface{}) map[string]inter
2727
m = utils.MergeMap(m, f.filterJson(fmt.Sprint(msgJson)))
2828
delete(m, "@json")
2929
}
30-
if msg, ok := m["@message"]; ok && regexJson.MatchString(fmt.Sprint(msg)) {
30+
if msg, ok := m[MessageKey]; ok && regexJson.MatchString(fmt.Sprint(msg)) {
3131
m = utils.MergeMap(m, f.filterJson(fmt.Sprint(msg)))
32-
delete(m, "@message")
32+
delete(m, MessageKey)
3333
}
3434
return m
3535
}
@@ -62,7 +62,9 @@ func (f *AppFilter) FilterPatterns(pMes *rfc5424.SyslogMessage, patterns []strin
6262

6363
func (f *AppFilter) filterPatternsMsg(message string, patterns []string) map[string]interface{} {
6464
if regexJson.MatchString(message) {
65-
return f.filterJson(message)
65+
m := f.filterJson(message)
66+
m[MessageKey] = ""
67+
return m
6668
}
6769
resultMap := make(map[string]interface{})
6870
for _, pattern := range patterns {
@@ -89,12 +91,12 @@ func (f *AppFilter) filterPatternsMsg(message string, patterns []string) map[str
8991
if textValue != "" {
9092
resultMap = utils.MergeMap(resultMap, f.filterPatternsMsg(textValue, patterns))
9193
}
92-
msg, hasMsg := resultMap["@message"]
94+
msg, hasMsg := resultMap[MessageKey]
9395
if hasMsg && !msgKey.Hide && msgKey.Name != "" {
9496
resultMap = utils.MergeMap(resultMap, utils.CreateMapFromDelim(msgKey.Name, msg))
9597
}
9698
if !hasMsg {
97-
resultMap["@message"] = ""
99+
resultMap[MessageKey] = ""
98100
}
99101
return resultMap
100102
}
@@ -116,7 +118,7 @@ func (f *AppFilter) filterJson(message string) map[string]interface{} {
116118
data := make(map[string]interface{})
117119
err := json.Unmarshal([]byte(message), &data)
118120
if err != nil {
119-
data["@message"] = message
121+
data[MessageKey] = message
120122
data["@exception"] = err.Error()
121123
return data
122124
}

parser/filter_default.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (f DefaultFilter) Filter(pMes *rfc5424.SyslogMessage) map[string]interface{
4646
data["@timestamp"] = *pMes.Timestamp
4747
if pMes.Message != nil && strings.TrimSpace(*pMes.Message) != "" {
4848
data["@level"] = "INFO"
49-
data["@message"] = *pMes.Message
49+
data[MessageKey] = *pMes.Message
5050
}
5151

5252
pData := *pMes.StructuredData

parser/filter_metrics.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func (f MetricsFilter) Filter(pMes *rfc5424.SyslogMessage) map[string]interface{
3131
}
3232
structDataPtr := pMes.StructuredData
3333
*structDataPtr = structData
34+
data[MessageKey] = ""
3435
return data
3536
}
3637

parser/filter_rtr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (f RtrFilter) Filter(pMes *rfc5424.SyslogMessage) map[string]interface{} {
159159
data[k] = v
160160
}
161161

162-
data["@message"] = fmt.Sprintf(
162+
data[MessageKey] = fmt.Sprintf(
163163
"%d %s %s (%d ms)",
164164
dataRtr["status"],
165165
dataRtr["verb"],

parser/parser.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import (
1414
"github.com/orange-cloudfoundry/logs-service-broker/utils"
1515
)
1616

17-
const defCompanyID = "logsbroker@1368"
17+
const (
18+
defCompanyID = "logsbroker@1368"
19+
MessageKey = "@message"
20+
)
1821

1922
// this is from https://github.com/cloudfoundry/cf-syslog-drain-release/blob/f13fd13ec6d08822f261cbb575aa5f357ab32f0d/src/adapter/internal/egress/tcp.go#L21-L24
2023
// gaugeStructuredDataID contains the registered enterprise ID for the Cloud
@@ -48,7 +51,7 @@ type MsgParam map[string]map[string]string
4851

4952
var defaultParsingKeys = []model.ParsingKey{
5053
{
51-
Name: "@message",
54+
Name: MessageKey,
5255
},
5356
{
5457
Name: "@raw",
@@ -146,6 +149,14 @@ func (p Parser) Parse(
146149
}
147150
data = utils.MergeMap(data, values)
148151
}
152+
153+
// clean empty message
154+
if mess, ok := data[MessageKey]; ok {
155+
if str, ok := mess.(string); ok && str == "" {
156+
delete(data, MessageKey)
157+
}
158+
}
159+
149160
if len(logData.InstanceParam.SourceLabels) > 0 {
150161
currentSource := make(map[string]interface{})
151162
if _, ok := data["@source"]; ok {

0 commit comments

Comments
 (0)