@@ -13,6 +13,8 @@ import (
13
13
14
14
"github.com/grafana/loki/v3/pkg/loghttp/push"
15
15
"github.com/grafana/loki/v3/pkg/logproto"
16
+ "github.com/grafana/loki/v3/pkg/logql/log"
17
+ "github.com/grafana/loki/v3/pkg/logql/log/jsonexpr"
16
18
"github.com/grafana/loki/v3/pkg/logql/log/logfmt"
17
19
"github.com/grafana/loki/v3/pkg/util/constants"
18
20
)
@@ -141,9 +143,9 @@ func (l *FieldDetector) detectGenericFieldFromLogEntry(entry logproto.Entry, hin
141
143
lineBytes := unsafe .Slice (unsafe .StringData (entry .Line ), len (entry .Line ))
142
144
var v []byte
143
145
if isJSON (entry .Line ) {
144
- v = l . getValueUsingJSONParser (lineBytes , hints )
146
+ v = getValueUsingJSONParser (lineBytes , hints )
145
147
} else if isLogFmt (lineBytes ) {
146
- v = l . getValueUsingLogfmtParser (lineBytes , hints )
148
+ v = getValueUsingLogfmtParser (lineBytes , hints )
147
149
}
148
150
return string (v )
149
151
}
@@ -152,9 +154,9 @@ func (l *FieldDetector) extractLogLevelFromLogLine(log string) string {
152
154
lineBytes := unsafe .Slice (unsafe .StringData (log ), len (log ))
153
155
var v []byte
154
156
if isJSON (log ) {
155
- v = l . getValueUsingJSONParser (lineBytes , l .allowedLevelLabels )
157
+ v = getValueUsingJSONParser (lineBytes , l .allowedLevelLabels )
156
158
} else if isLogFmt (lineBytes ) {
157
- v = l . getValueUsingLogfmtParser (lineBytes , l .allowedLevelLabels )
159
+ v = getValueUsingLogfmtParser (lineBytes , l .allowedLevelLabels )
158
160
} else {
159
161
return detectLevelFromLogLine (log )
160
162
}
@@ -179,7 +181,7 @@ func (l *FieldDetector) extractLogLevelFromLogLine(log string) string {
179
181
}
180
182
}
181
183
182
- func ( l * FieldDetector ) getValueUsingLogfmtParser (line []byte , hints []string ) []byte {
184
+ func getValueUsingLogfmtParser (line []byte , hints []string ) []byte {
183
185
d := logfmt .NewDecoder (line )
184
186
// In order to have the same behaviour as the JSON field extraction,
185
187
// the full line needs to be parsed to extract all possible matching fields.
@@ -201,14 +203,20 @@ func (l *FieldDetector) getValueUsingLogfmtParser(line []byte, hints []string) [
201
203
return res
202
204
}
203
205
204
- func (l * FieldDetector ) getValueUsingJSONParser (log []byte , hints []string ) []byte {
206
+ func getValueUsingJSONParser (line []byte , hints []string ) []byte {
207
+ var res []byte
205
208
for _ , allowedLabel := range hints {
206
- l , _ , _ , err := jsonparser . Get ( log , allowedLabel )
207
- if err = = nil {
208
- return l
209
+ parsed , err := jsonexpr . Parse ( allowedLabel , false )
210
+ if err ! = nil {
211
+ continue
209
212
}
213
+ l , _ , _ , err := jsonparser .Get (line , log .JSONPathsToStrings (parsed )... )
214
+ if err != nil {
215
+ continue
216
+ }
217
+ return l
210
218
}
211
- return nil
219
+ return res
212
220
}
213
221
214
222
func isLogFmt (line []byte ) bool {
0 commit comments