VictoriaMetrics/lib/logstorage/pipe_extract.go

133 lines
3 KiB
Go
Raw Normal View History

2024-05-19 02:24:32 +00:00
package logstorage
import (
"fmt"
)
2024-05-22 15:17:59 +00:00
// pipeExtract processes '| extract ...' pipe.
2024-05-19 10:56:08 +00:00
//
// See https://docs.victoriametrics.com/victorialogs/logsql/#extract-pipe
type pipeExtract struct {
2024-05-19 11:47:30 +00:00
fromField string
2024-05-22 12:05:32 +00:00
ptn *pattern
2024-05-19 10:56:08 +00:00
2024-05-22 11:04:16 +00:00
patternStr string
2024-05-20 14:09:07 +00:00
// iff is an optional filter for skipping the extract func
2024-05-21 10:55:11 +00:00
iff *ifFilter
2024-05-19 10:56:08 +00:00
}
func (pe *pipeExtract) String() string {
2024-05-19 11:47:30 +00:00
s := "extract"
2024-05-20 14:09:07 +00:00
if pe.iff != nil {
2024-05-21 10:55:11 +00:00
s += " " + pe.iff.String()
2024-05-20 14:09:07 +00:00
}
2024-05-22 15:17:59 +00:00
s += " " + quoteTokenIfNeeded(pe.patternStr)
if !isMsgFieldName(pe.fromField) {
s += " from " + quoteTokenIfNeeded(pe.fromField)
}
2024-05-19 11:47:30 +00:00
return s
2024-05-19 10:56:08 +00:00
}
func (pe *pipeExtract) updateNeededFields(neededFields, unneededFields fieldsSet) {
2024-05-19 13:11:17 +00:00
if neededFields.contains("*") {
2024-05-19 19:25:52 +00:00
unneededFieldsOrig := unneededFields.clone()
2024-05-19 13:11:17 +00:00
needFromField := false
2024-05-22 12:05:32 +00:00
for _, step := range pe.ptn.steps {
2024-05-19 13:11:17 +00:00
if step.field != "" {
2024-05-19 19:25:52 +00:00
if !unneededFieldsOrig.contains(step.field) {
2024-05-19 13:11:17 +00:00
needFromField = true
}
2024-05-19 19:25:52 +00:00
unneededFields.add(step.field)
2024-05-19 13:11:17 +00:00
}
}
if needFromField {
2024-05-22 13:29:18 +00:00
unneededFields.remove(pe.fromField)
2024-05-21 10:55:11 +00:00
if pe.iff != nil {
unneededFields.removeFields(pe.iff.neededFields)
}
2024-05-19 13:11:17 +00:00
} else {
unneededFields.add(pe.fromField)
}
} else {
2024-05-20 14:09:07 +00:00
neededFieldsOrig := neededFields.clone()
2024-05-19 13:11:17 +00:00
needFromField := false
2024-05-22 12:05:32 +00:00
for _, step := range pe.ptn.steps {
2024-05-20 14:09:07 +00:00
if step.field != "" && neededFieldsOrig.contains(step.field) {
2024-05-19 19:25:52 +00:00
needFromField = true
neededFields.remove(step.field)
2024-05-19 13:11:17 +00:00
}
}
if needFromField {
2024-05-22 13:29:18 +00:00
neededFields.add(pe.fromField)
2024-05-21 10:55:11 +00:00
if pe.iff != nil {
neededFields.addFields(pe.iff.neededFields)
}
2024-05-19 10:56:08 +00:00
}
}
}
2024-05-19 19:27:53 +00:00
func (pe *pipeExtract) newPipeProcessor(workersCount int, _ <-chan struct{}, _ func(), ppBase pipeProcessor) pipeProcessor {
2024-05-22 09:38:10 +00:00
patterns := make([]*pattern, workersCount)
for i := range patterns {
2024-05-22 12:05:32 +00:00
patterns[i] = pe.ptn.clone()
2024-05-19 10:56:08 +00:00
}
2024-05-22 11:04:16 +00:00
unpackFunc := func(uctx *fieldsUnpackerContext, s string) {
2024-05-22 09:38:10 +00:00
ptn := patterns[uctx.workerID]
ptn.apply(s)
for _, f := range ptn.fields {
2024-05-22 11:04:16 +00:00
uctx.addField(f.name, *f.value)
2024-05-20 14:09:07 +00:00
}
}
2024-05-19 12:22:09 +00:00
2024-05-22 09:38:10 +00:00
return newPipeUnpackProcessor(workersCount, unpackFunc, ppBase, pe.fromField, "", pe.iff)
2024-05-19 10:56:08 +00:00
}
2024-05-19 11:47:30 +00:00
func parsePipeExtract(lex *lexer) (*pipeExtract, error) {
if !lex.isKeyword("extract") {
return nil, fmt.Errorf("unexpected token: %q; want %q", lex.token, "extract")
}
lex.nextToken()
2024-05-22 15:17:59 +00:00
// parse optional if (...)
var iff *ifFilter
if lex.isKeyword("if") {
f, err := parseIfFilter(lex)
2024-05-19 11:47:30 +00:00
if err != nil {
2024-05-22 15:17:59 +00:00
return nil, err
2024-05-19 11:47:30 +00:00
}
2024-05-22 15:17:59 +00:00
iff = f
2024-05-19 11:47:30 +00:00
}
2024-05-20 14:09:07 +00:00
// parse pattern
2024-05-22 11:04:16 +00:00
patternStr, err := getCompoundToken(lex)
2024-05-19 11:47:30 +00:00
if err != nil {
2024-05-19 13:11:17 +00:00
return nil, fmt.Errorf("cannot read 'pattern': %w", err)
2024-05-19 11:47:30 +00:00
}
2024-05-22 12:05:32 +00:00
ptn, err := parsePattern(patternStr)
2024-05-19 11:47:30 +00:00
if err != nil {
2024-05-22 11:04:16 +00:00
return nil, fmt.Errorf("cannot parse 'pattern' %q: %w", patternStr, err)
2024-05-19 11:47:30 +00:00
}
2024-05-22 15:17:59 +00:00
// parse optional 'from ...' part
fromField := "_msg"
if lex.isKeyword("from") {
lex.nextToken()
f, err := parseFieldName(lex)
if err != nil {
return nil, fmt.Errorf("cannot parse 'from' field name: %w", err)
}
fromField = f
}
2024-05-19 11:47:30 +00:00
pe := &pipeExtract{
2024-05-22 11:04:16 +00:00
fromField: fromField,
2024-05-22 12:05:32 +00:00
ptn: ptn,
2024-05-22 11:04:16 +00:00
patternStr: patternStr,
2024-05-22 15:41:45 +00:00
iff: iff,
2024-05-20 14:09:07 +00:00
}
2024-05-19 11:47:30 +00:00
return pe, nil
}