From 0df739934907cd558e132097e1c9644fa888c849 Mon Sep 17 00:00:00 2001 From: Tyler Schade Date: Sat, 25 Feb 2023 08:49:20 -0500 Subject: [PATCH 1/5] test commit --- bee/main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bee/main.go b/bee/main.go index 8d81c96..531aedf 100644 --- a/bee/main.go +++ b/bee/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "github.com/solo-io/bumblebee/pkg/cli" @@ -8,6 +9,7 @@ import ( ) func main() { + fmt.Println("this is my special bee") // Use context with discarded logrus logger so we don't fill our logs unecessarily ctx := context.Background() if err := cli.Bee().ExecuteContext(ctx); err != nil { From ea8fcd6f33bb1b2dff59ae1cac9dd3489a9cd81d Mon Sep 17 00:00:00 2001 From: Tyler Schade Date: Sun, 26 Feb 2023 13:58:30 -0500 Subject: [PATCH 2/5] fix map deletion issue --- bee/main.go | 2 - pkg/loader/loader.go | 6 +++ pkg/stats/stats.go | 89 +++++++++++++++++++++++++++++++++++++++----- 3 files changed, 86 insertions(+), 11 deletions(-) diff --git a/bee/main.go b/bee/main.go index 531aedf..8d81c96 100644 --- a/bee/main.go +++ b/bee/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "log" "github.com/solo-io/bumblebee/pkg/cli" @@ -9,7 +8,6 @@ import ( ) func main() { - fmt.Println("this is my special bee") // Use context with discarded logrus logger so we don't fill our logs unecessarily ctx := context.Background() if err := cli.Bee().ExecuteContext(ctx); err != nil { diff --git a/pkg/loader/loader.go b/pkg/loader/loader.go index 6413283..9b3bb91 100644 --- a/pkg/loader/loader.go +++ b/pkg/loader/loader.go @@ -367,6 +367,7 @@ func (l *loader) startHashMap( select { case <-ticker.C: mapIter := liveMap.Iterate() + labels := make([]map[string]string, 0) for { // Use generic key,value so we can decode ourselves var ( @@ -398,12 +399,15 @@ func (l *loader) startHashMap( } stringLabels := stringify(decodedKey) instrument.Set(ctx, int64(intVal), stringLabels) + labels = append(labels, stringLabels) thisKvPair := KvPair{Key: stringLabels, Value: fmt.Sprint(intVal)} watcher.SendEntry(MapEntry{ Name: name, Entry: thisKvPair, }) } + // remove any old labels that weren't in this last iteration of the HashMap + instrument.Clean(labels) case <-ctx.Done(): // fmt.Println("got done in hashmap loop, returning") @@ -453,6 +457,8 @@ func (n *noop) Set( ) { } +func (n *noop) Clean(_ []map[string]string) {} + func createDir(ctx context.Context, path string, perm os.FileMode) error { file, err := os.Stat(path) if os.IsNotExist(err) { diff --git a/pkg/stats/stats.go b/pkg/stats/stats.go index a2ca759..288d7a3 100644 --- a/pkg/stats/stats.go +++ b/pkg/stats/stats.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "net/http" + "strings" "github.com/mitchellh/hashstructure/v2" "github.com/prometheus/client_golang/prometheus" @@ -20,7 +21,7 @@ const ( type PrometheusOpts struct { Port uint32 MetricsPath string - Registry *prometheus.Registry + Registry *prometheus.Registry } func (p *PrometheusOpts) initDefaults() { @@ -70,13 +71,15 @@ type MetricsProvider interface { type IncrementInstrument interface { Increment(ctx context.Context, labels map[string]string) + Clean(newLabels []map[string]string) } type SetInstrument interface { Set(ctx context.Context, val int64, labels map[string]string) + Clean(newLabels []map[string]string) } -type metricsProvider struct{ +type metricsProvider struct { registry *prometheus.Registry } @@ -88,8 +91,9 @@ func (m *metricsProvider) NewSetCounter(name string, labels []string) SetInstrum m.register(counter) return &setCounter{ - counter: counter, - counterMap: map[uint64]int64{}, + counter: counter, + counterMap: map[uint64]int64{}, + currentLabels: map[string]map[string]string{}, } } @@ -126,8 +130,35 @@ func (m *metricsProvider) register(collectors ...prometheus.Collector) { } type setCounter struct { - counter *prometheus.CounterVec - counterMap map[uint64]int64 + counter *prometheus.CounterVec + counterMap map[uint64]int64 + currentLabels map[string]map[string]string +} + +func makeLabelString(label map[string]string) string { + pairs := make([]string, 0, len(label)) + for k, v := range label { + pairs = append(pairs, fmt.Sprintf("%s|%s", k, v)) + } + return strings.Join(pairs, ",") +} + +func (c *setCounter) trackLabel(decodedKey map[string]string) { + c.currentLabels[makeLabelString(decodedKey)] = decodedKey +} + +func (c *setCounter) Clean(newLabels []map[string]string) { + labelsToKeep := make(map[string]bool) + for _, newLabel := range newLabels { + labelsToKeep[makeLabelString(newLabel)] = true + } + + for oldLabelKey, oldLabel := range c.currentLabels { + if _, ok := labelsToKeep[oldLabelKey]; !ok { + delete(c.currentLabels, oldLabelKey) + c.counter.Delete(oldLabel) + } + } } func (c *setCounter) Set( @@ -135,7 +166,6 @@ func (c *setCounter) Set( intVal int64, decodedKey map[string]string, ) { - keyHash, err := hashstructure.Hash(decodedKey, hashstructure.FormatV2, nil) if err != nil { log.Fatal("This should never happen") @@ -148,10 +178,12 @@ func (c *setCounter) Set( } c.counterMap[keyHash] = intVal c.counter.With(prometheus.Labels(decodedKey)).Add(float64(diff)) + c.trackLabel(decodedKey) } type incrementCounter struct { - counter *prometheus.CounterVec + counter *prometheus.CounterVec + currentLabels map[string]map[string]string } func (i *incrementCounter) Increment( @@ -159,10 +191,30 @@ func (i *incrementCounter) Increment( decodedKey map[string]string, ) { i.counter.With(prometheus.Labels(decodedKey)).Inc() + i.trackLabel(decodedKey) +} + +func (i *incrementCounter) trackLabel(decodedKey map[string]string) { + i.currentLabels[makeLabelString(decodedKey)] = decodedKey +} + +func (i *incrementCounter) Clean(newLabels []map[string]string) { + labelsToKeep := make(map[string]bool) + for _, newLabel := range newLabels { + labelsToKeep[makeLabelString(newLabel)] = true + } + + for oldLabelKey, oldLabel := range i.currentLabels { + if _, ok := labelsToKeep[oldLabelKey]; !ok { + delete(i.currentLabels, oldLabelKey) + i.counter.Delete(oldLabel) + } + } } type gauge struct { - gauge *prometheus.GaugeVec + gauge *prometheus.GaugeVec + currentLabels map[string]map[string]string } func (g *gauge) Set( @@ -171,4 +223,23 @@ func (g *gauge) Set( decodedKey map[string]string, ) { g.gauge.With(prometheus.Labels(decodedKey)).Set(float64(intVal)) + g.trackLabel(decodedKey) +} + +func (g *gauge) trackLabel(decodedKey map[string]string) { + g.currentLabels[makeLabelString(decodedKey)] = decodedKey +} + +func (g *gauge) Clean(newLabels []map[string]string) { + labelsToKeep := make(map[string]bool) + for _, newLabel := range newLabels { + labelsToKeep[makeLabelString(newLabel)] = true + } + + for oldLabelKey, oldLabel := range g.currentLabels { + if _, ok := labelsToKeep[oldLabelKey]; !ok { + delete(g.currentLabels, oldLabelKey) + g.gauge.Delete(oldLabel) + } + } } From dfef77324f2dbd4b6de2fc1eb0442609b97faa0f Mon Sep 17 00:00:00 2001 From: Tyler Schade Date: Sun, 26 Feb 2023 14:04:11 -0500 Subject: [PATCH 3/5] initialize map --- pkg/stats/stats.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/stats/stats.go b/pkg/stats/stats.go index 288d7a3..9837eec 100644 --- a/pkg/stats/stats.go +++ b/pkg/stats/stats.go @@ -105,7 +105,8 @@ func (m *metricsProvider) NewIncrementCounter(name string, labels []string) Incr m.register(counter) return &incrementCounter{ - counter: counter, + counter: counter, + currentLabels: map[string]map[string]string{}, } } @@ -117,7 +118,8 @@ func (m *metricsProvider) NewGauge(name string, labels []string) SetInstrument { m.register(gaugeVec) return &gauge{ - gauge: gaugeVec, + gauge: gaugeVec, + currentLabels: map[string]map[string]string{}, } } From 110ab3b3c1f8c70db9fa4ff887b7e832a5fdf060 Mon Sep 17 00:00:00 2001 From: Tyler Schade Date: Tue, 28 Feb 2023 23:07:10 -0500 Subject: [PATCH 4/5] test --- examples/activeconn/activeconn.c | 7 +++++++ examples/maptest/maptest.c | 0 2 files changed, 7 insertions(+) create mode 100644 examples/maptest/maptest.c diff --git a/examples/activeconn/activeconn.c b/examples/activeconn/activeconn.c index 09589cb..4d7d029 100644 --- a/examples/activeconn/activeconn.c +++ b/examples/activeconn/activeconn.c @@ -3,6 +3,7 @@ #include "bpf/bpf_helpers.h" #include "bpf/bpf_core_read.h" #include "bpf/bpf_tracing.h" +#include "unistd.h" char __license[] SEC("license") = "Dual MIT/GPL"; @@ -49,6 +50,7 @@ record(struct pt_regs *ctx, int ret, int op) __u32 daddr; u64 val; u64 *valp; + __u32 myval; struct dimensions_t key = {}; bpf_printk("exit: getting sk for tid: '%u', ret is: '%d'", tid, ret); @@ -76,6 +78,11 @@ record(struct pt_regs *ctx, int ret, int op) } bpf_map_update_elem(&sockets_ext, &key, &val, 0); bpf_map_delete_elem(&sockets, &tid); + + myval = 1; + bpf_map_update_elem(&sockets_ext, &myval, &myval, 0); + sleep(5); + bpf_map_delete_elem(&sockets_ext, &myval) return 0; } diff --git a/examples/maptest/maptest.c b/examples/maptest/maptest.c new file mode 100644 index 0000000..e69de29 From b09417f79c8c2073308a726b5715fadd7fe1100d Mon Sep 17 00:00:00 2001 From: Tyler Schade Date: Wed, 1 Mar 2023 21:40:19 -0500 Subject: [PATCH 5/5] test with tcpconnect --- examples/activeconn/activeconn.c | 4 ---- examples/maptest/maptest.c | 0 examples/tcpconnect/tcpconnect.c | 1 + 3 files changed, 1 insertion(+), 4 deletions(-) delete mode 100644 examples/maptest/maptest.c diff --git a/examples/activeconn/activeconn.c b/examples/activeconn/activeconn.c index 4d7d029..2f662df 100644 --- a/examples/activeconn/activeconn.c +++ b/examples/activeconn/activeconn.c @@ -79,10 +79,6 @@ record(struct pt_regs *ctx, int ret, int op) bpf_map_update_elem(&sockets_ext, &key, &val, 0); bpf_map_delete_elem(&sockets, &tid); - myval = 1; - bpf_map_update_elem(&sockets_ext, &myval, &myval, 0); - sleep(5); - bpf_map_delete_elem(&sockets_ext, &myval) return 0; } diff --git a/examples/maptest/maptest.c b/examples/maptest/maptest.c deleted file mode 100644 index e69de29..0000000 diff --git a/examples/tcpconnect/tcpconnect.c b/examples/tcpconnect/tcpconnect.c index 2a3ccdb..6bb6fcb 100644 --- a/examples/tcpconnect/tcpconnect.c +++ b/examples/tcpconnect/tcpconnect.c @@ -80,6 +80,7 @@ exit_tcp_connect(struct pt_regs *ctx, int ret) val = 1; } else { + bpf_map_delete_elem(&sockets, &tid); bpf_printk("found existing value '%llu' for {saddr: %u, daddr: %u}", *valp, hash_key.saddr, hash_key.daddr); val = *valp + 1; }