Skip to content

Commit 45b1c0f

Browse files
committed
fix: integrate change requests from review
Signed-off-by: Timon Engelke <timon.engelke@inovex.de>
1 parent 75072f6 commit 45b1c0f

4 files changed

Lines changed: 69 additions & 77 deletions

File tree

internal/component/loki/process/stages/json.go

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import (
1515
// Config Errors
1616
const (
1717
ErrExpressionsOrRegexRequired = "JMES expressions or regex is required"
18-
ErrCouldNotCompileJMES = "could not compile JMES expression"
19-
ErrEmptyJSONStageConfig = "empty json stage configuration"
20-
ErrEmptyJSONStageSource = "empty source"
21-
ErrMalformedJSON = "malformed json"
18+
ErrCouldNotCompileJMES = "could not compile JMES expression"
19+
ErrEmptyJSONStageConfig = "empty json stage configuration"
20+
ErrEmptyJSONStageSource = "empty source"
21+
ErrMalformedJSON = "malformed json"
2222
)
2323

2424
// JSONConfig represents a JSON Stage configuration
@@ -143,58 +143,25 @@ func (j *jsonStage) processEntry(extracted map[string]any, entry *string) error
143143
return errors.New(ErrMalformedJSON)
144144
}
145145

146-
for n, e := range j.expressions {
147-
r, err := e.Search(data)
146+
for name, expr := range j.expressions {
147+
rawResult, err := expr.Search(data)
148148
if err != nil {
149149
if Debug {
150150
level.Debug(j.logger).Log("msg", "failed to search JMES expression", "err", err)
151151
}
152152
continue
153153
}
154-
155-
switch r.(type) {
156-
case float64:
157-
// All numbers in JSON are unmarshaled to float64.
158-
extracted[n] = r
159-
case string:
160-
extracted[n] = r
161-
case bool:
162-
extracted[n] = r
163-
case nil:
164-
extracted[n] = nil
165-
default:
166-
// If the value wasn't a string or a number, marshal it back to json
167-
jm, err := json.Marshal(r)
168-
if err != nil {
169-
if Debug {
170-
level.Debug(j.logger).Log("msg", "failed to marshal complex type back to string", "err", err)
171-
}
172-
continue
173-
}
174-
extracted[n] = string(jm)
154+
value, err := j.simplifyType(rawResult)
155+
if err == nil {
156+
extracted[name] = value
175157
}
176158
}
177159
if j.regex.String() != "" {
178-
for key, value := range data {
160+
for key, rawValue := range data {
179161
if j.regex.MatchString(key) {
180-
switch value.(type) {
181-
case float64:
162+
value, err := j.simplifyType(rawValue)
163+
if err == nil {
182164
extracted[key] = value
183-
case string:
184-
extracted[key] = value
185-
case bool:
186-
extracted[key] = value
187-
case nil:
188-
extracted[key] = nil
189-
default:
190-
jm, err := json.Marshal(value)
191-
if err != nil {
192-
if Debug {
193-
level.Debug(j.logger).Log("msg", "failed to marshal complex type back to string", "err", err)
194-
}
195-
continue
196-
}
197-
extracted[key] = string(jm)
198165
}
199166
}
200167
}
@@ -205,6 +172,31 @@ func (j *jsonStage) processEntry(extracted map[string]any, entry *string) error
205172
return nil
206173
}
207174

175+
// extractWithType returns the value if it's a simple type (string, number, bool),
176+
// otherwise, it returns it as a JSON string
177+
func (j *jsonStage) simplifyType(value any) (any, error) {
178+
switch value.(type) {
179+
case float64:
180+
return value, nil
181+
case string:
182+
return value, nil
183+
case bool:
184+
return value, nil
185+
case nil:
186+
return nil, nil
187+
default:
188+
// If the value wasn't a string or a number, marshal it back to json
189+
jm, err := json.Marshal(value)
190+
if err != nil {
191+
if Debug {
192+
level.Debug(j.logger).Log("msg", "failed to marshal complex type back to string", "err", err)
193+
return nil, err
194+
}
195+
}
196+
return string(jm), nil
197+
}
198+
}
199+
208200
// Cleanup implements Stage.
209201
func (*jsonStage) Cleanup() {
210202
// no-op

internal/component/loki/process/stages/json_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,35 +93,35 @@ func TestPipeline_JSON(t *testing.T) {
9393
},
9494
"successfully extract regex values from json": {
9595
testJSONAlloyRegex,
96-
`{"time":"2012-11-01T22:08:41+00:00", "pod_name": "my-pod-123", "pod_label": "my-label"}`,
96+
`{"time":"2012-11-01T22:08:41+00:00", "pod_name": "my-pod-123", "pod_label": "my-label"}`,
9797
map[string]interface{}{
98-
"pod_name": "my-pod-123",
99-
"pod_label": "my-label",
98+
"pod_name": "my-pod-123",
99+
"pod_label": "my-label",
100100
},
101101
},
102102
"successfully extract all values from json via regex": {
103103
testJSONAlloyRegexAll,
104104
testJSONLogLine,
105105
map[string]interface{}{
106-
"time": "2012-11-01T22:08:41+00:00",
107-
"app": "loki",
108-
"component": `["parser","type"]`,
109-
"level": "WARN",
110-
"nested": `{"child":"value"}`,
111-
"duration": float64(125),
112-
"message": "this is a log line",
113-
"extra": "{\"user\":\"marco\"}",
106+
"time": "2012-11-01T22:08:41+00:00",
107+
"app": "loki",
108+
"component": `["parser","type"]`,
109+
"level": "WARN",
110+
"nested": `{"child":"value"}`,
111+
"duration": float64(125),
112+
"message": "this is a log line",
113+
"extra": "{\"user\":\"marco\"}",
114+
},
115+
},
116+
"successfully extract values with expressions and regex from json": {
117+
testJSONAlloyExpressionsAndRegex,
118+
testJSONLogLine,
119+
map[string]interface{}{
120+
"out": "this is a log line",
121+
"app": "loki",
122+
"duration": float64(125),
114123
},
115124
},
116-
"successfully extract values with expressions and regex from json": {
117-
testJSONAlloyExpressionsAndRegex,
118-
testJSONLogLine,
119-
map[string]interface{}{
120-
"out": "this is a log line",
121-
"app": "loki",
122-
"duration": float64(125),
123-
},
124-
},
125125
}
126126

127127
for testName, testData := range tests {

internal/component/loki/process/stages/logfmt.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ func (j *logfmtStage) Process(labels model.LabelSet, extracted map[string]any, t
115115
if ok {
116116
extracted[mapKey] = string(decoder.Value())
117117
mappingExtractedEntriesCount++
118-
} else if j.regex.String() != "" {
119-
// handle "regex"
120-
fmt.Println(j.regex.String(), string(decoder.Key()))
118+
}
119+
// handle "regex"
120+
if j.regex.String() != "" {
121121
if j.regex.MatchString(string(decoder.Key())) {
122122
extracted[string(decoder.Key())] = string(decoder.Value())
123123
regexExtractedEntriesCount++

internal/component/loki/process/stages/logfmt_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ func TestLogfmt(t *testing.T) {
9393
"extra": "user=foo",
9494
},
9595
},
96-
"successfully extract values with expressions and regex from logfmt": {
97-
testLogfmtAlloyRegexAndMapping,
98-
testLogfmtLogLine,
99-
map[string]interface{}{
100-
"out": "this is a log line",
101-
"app": "loki",
102-
"duration": "125",
103-
},
104-
},
96+
"successfully extract values with expressions and regex from logfmt": {
97+
testLogfmtAlloyRegexAndMapping,
98+
testLogfmtLogLine,
99+
map[string]interface{}{
100+
"out": "this is a log line",
101+
"app": "loki",
102+
"duration": "125",
103+
},
104+
},
105105
}
106106

107107
for testName, testData := range tests {

0 commit comments

Comments
 (0)