From bae46b8296a68124639092e20b88c866ce4340b0 Mon Sep 17 00:00:00 2001 From: Roman Karpenko Date: Thu, 12 Dec 2024 16:20:11 +0200 Subject: [PATCH 1/2] improve regexp performance in `loki.process`: call fmt only if debug is enabled. --- CHANGELOG.md | 1 + internal/component/loki/process/stages/regex.go | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 685f799212..2f6c4a06ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,7 @@ Main (unreleased) - Upgrade `github.com/goccy/go-json` to v0.10.4, which reduces the memory consumption of an Alloy instance by 20MB. If Alloy is running certain otelcol components, this reduction will not apply. (@ptodev) +- improve performance in regexp component: call fmt only if debug is enabled (@r0ka) - Update `prometheus.write.queue` library for performance increases in cpu. (@mattdurham) diff --git a/internal/component/loki/process/stages/regex.go b/internal/component/loki/process/stages/regex.go index 531c230515..42625f2cee 100644 --- a/internal/component/loki/process/stages/regex.go +++ b/internal/component/loki/process/stages/regex.go @@ -112,7 +112,9 @@ func (r *regexStage) Process(labels model.LabelSet, extracted map[string]interfa extracted[name] = match[i] } } - level.Debug(r.logger).Log("msg", "extracted data debug in regex stage", "extracted data", fmt.Sprintf("%v", extracted)) + if Debug { + level.Debug(r.logger).Log("msg", "extracted data debug in regex stage", "extracted data", fmt.Sprintf("%v", extracted)) + } } // Name implements Stage From 9c2d6c4353d4b3a67fb06c94799ab0b5542e711e Mon Sep 17 00:00:00 2001 From: Roman Karpenko Date: Tue, 7 Jan 2025 11:50:24 +0200 Subject: [PATCH 2/2] improve regexp performance in `loki.process`: call fmt only if debug is enabled. surrounding rest of the debug messages with debug check. --- internal/component/loki/process/stages/regex.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/internal/component/loki/process/stages/regex.go b/internal/component/loki/process/stages/regex.go index 42625f2cee..48eba6a80d 100644 --- a/internal/component/loki/process/stages/regex.go +++ b/internal/component/loki/process/stages/regex.go @@ -83,13 +83,17 @@ func (r *regexStage) Process(labels model.LabelSet, extracted map[string]interfa if r.config.Source != nil { if _, ok := extracted[*r.config.Source]; !ok { - level.Debug(r.logger).Log("msg", "source does not exist in the set of extracted values", "source", *r.config.Source) + if Debug { + level.Debug(r.logger).Log("msg", "source does not exist in the set of extracted values", "source", *r.config.Source) + } return } value, err := getString(extracted[*r.config.Source]) if err != nil { - level.Debug(r.logger).Log("msg", "failed to convert source value to string", "source", *r.config.Source, "err", err, "type", reflect.TypeOf(extracted[*r.config.Source])) + if Debug { + level.Debug(r.logger).Log("msg", "failed to convert source value to string", "source", *r.config.Source, "err", err, "type", reflect.TypeOf(extracted[*r.config.Source])) + } return } @@ -97,13 +101,17 @@ func (r *regexStage) Process(labels model.LabelSet, extracted map[string]interfa } if input == nil { - level.Debug(r.logger).Log("msg", "cannot parse a nil entry") + if Debug { + level.Debug(r.logger).Log("msg", "cannot parse a nil entry") + } return } match := r.expression.FindStringSubmatch(*input) if match == nil { - level.Debug(r.logger).Log("msg", "regex did not match", "input", *input, "regex", r.expression) + if Debug { + level.Debug(r.logger).Log("msg", "regex did not match", "input", *input, "regex", r.expression) + } return }