From da22a2a137b2b6f0f899fb1f443b255df52d9e62 Mon Sep 17 00:00:00 2001 From: rafal Date: Sun, 7 Jul 2024 18:10:52 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=99=88=20template:=20fix=20tag=20func?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/plugin/builtin/event/sensor.go | 12 ++++++------ pkg/proto/template.go | 2 +- pkg/trace/trace.go | 17 +++++++++-------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/pkg/plugin/builtin/event/sensor.go b/pkg/plugin/builtin/event/sensor.go index 3dcd632..77066c6 100644 --- a/pkg/plugin/builtin/event/sensor.go +++ b/pkg/plugin/builtin/event/sensor.go @@ -21,21 +21,21 @@ func MakeTags() *Tags { } } -func (t *Tags) tag(name string, value ...any) (any, error) { - switch len(value) { +func (t *Tags) tag(name string, values ...any) (any, error) { + switch len(values) { case 0: t.mu.Lock() - value, ok := t.m[name] + v, ok := t.m[name] t.mu.Unlock() if !ok { return nil, errors.New("tag not found: %q", name) } - return value, nil + return v, nil case 1: t.mu.Lock() - t.m[name] = value + t.m[name] = values[0] t.mu.Unlock() - return value, nil + return values[0], nil default: return nil, errors.New("too many arguments for tag: %q", name) } diff --git a/pkg/proto/template.go b/pkg/proto/template.go index 5d7d5d1..98b5ee4 100644 --- a/pkg/proto/template.go +++ b/pkg/proto/template.go @@ -92,7 +92,7 @@ func (t *T) Match(ctx context.Context, data string) func(context.Context, any) ( ) err := tmpl.Execute(&buf, got) - tr.ExecuteMatch(ctx, buf.Bytes(), err) + tr.ExecuteMatch(ctx, []byte(data), buf.Bytes(), err) if err != nil { return false, errors.New("failed to evaluate %q: %w", data, err) } diff --git a/pkg/trace/trace.go b/pkg/trace/trace.go index c2eaef2..34574dc 100644 --- a/pkg/trace/trace.go +++ b/pkg/trace/trace.go @@ -32,7 +32,7 @@ var ( ParseKey: func(context.Context, *gojq.Query, error) {}, UnmarshalValue: func(context.Context, []byte, any, error) {}, TemplateValue: func(context.Context, string, *template.Template, error) {}, - ExecuteMatch: func(context.Context, []byte, error) {}, + ExecuteMatch: func(context.Context, []byte, []byte, error) {}, UnmarshalMatch: func(context.Context, []byte, any, error) {}, EqualMatch: func(context.Context, any, any, bool) {}, MatchTimeout: func(context.Context) {}, @@ -97,9 +97,10 @@ func LogPattern() PatternTrace { slog.Info("trace: TemplateValue", tags...) } }, - ExecuteMatch: func(ctx context.Context, p []byte, err error) { + ExecuteMatch: func(ctx context.Context, data, result []byte, err error) { tags := append(attrs(ctx), - "raw", string(p), + "data", string(data), + "result", string(result), ) if err != nil { tags = append(tags, tint.Err(err)) @@ -356,7 +357,7 @@ type PatternTrace struct { ParseKey func(context.Context, *gojq.Query, error) UnmarshalValue func(context.Context, []byte, any, error) TemplateValue func(context.Context, string, *template.Template, error) - ExecuteMatch func(context.Context, []byte, error) + ExecuteMatch func(context.Context, []byte, []byte, error) UnmarshalMatch func(context.Context, []byte, any, error) EqualMatch func(context.Context, any, any, bool) MatchTimeout func(context.Context) @@ -386,9 +387,9 @@ func (pt PatternTrace) Join(extra PatternTrace) PatternTrace { } if extra.ExecuteMatch != nil { fn := pt.ExecuteMatch - pt.ExecuteMatch = func(ctx context.Context, p []byte, err error) { - fn(ctx, p, err) - extra.ExecuteMatch(ctx, p, err) + pt.ExecuteMatch = func(ctx context.Context, data, result []byte, err error) { + fn(ctx, data, result, err) + extra.ExecuteMatch(ctx, data, result, err) } } if extra.UnmarshalMatch != nil { @@ -400,7 +401,7 @@ func (pt PatternTrace) Join(extra PatternTrace) PatternTrace { } if extra.EqualMatch != nil { fn := pt.EqualMatch - pt.EqualMatch = func(ctx context.Context, want any, got any, ok bool) { + pt.EqualMatch = func(ctx context.Context, want, got any, ok bool) { fn(ctx, want, got, ok) extra.EqualMatch(ctx, want, got, ok) }