From b60dc21e6ca52d320b584619c25f8c17c0465c34 Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Thu, 11 Aug 2022 10:52:49 -0700 Subject: [PATCH] Fix usage of make to reserve capacity, not values --- p2p/host/resource-manager/obs/stats.go | 4 ++-- p2p/host/resource-manager/obs/stats_test.go | 23 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/p2p/host/resource-manager/obs/stats.go b/p2p/host/resource-manager/obs/stats.go index e2d97f782..6fd488583 100644 --- a/p2p/host/resource-manager/obs/stats.go +++ b/p2p/host/resource-manager/obs/stats.go @@ -322,14 +322,14 @@ func (r StatsTraceReporter) ConsumeEvent(evt rcmgr.TraceEvt) { tags := []tag.Mutator{tag.Upsert(scopeTag, scopeName), tag.Upsert(resourceTag, resource)} if evt.DeltaIn != 0 { - tagsWithDir := make([]tag.Mutator, 3) + tagsWithDir := make([]tag.Mutator, 0, 3) tagsWithDir = append(tagsWithDir, tag.Insert(directionTag, "inbound")) tagsWithDir = append(tagsWithDir, tags...) stats.RecordWithTags(ctx, tagsWithDir[0:], blockedResources.M(int64(1))) } if evt.DeltaOut != 0 { - tagsWithDir := make([]tag.Mutator, 3) + tagsWithDir := make([]tag.Mutator, 0, 3) tagsWithDir = append(tagsWithDir, tag.Insert(directionTag, "outbound")) tagsWithDir = append(tagsWithDir, tags...) stats.RecordWithTags(ctx, tagsWithDir, blockedResources.M(int64(1))) diff --git a/p2p/host/resource-manager/obs/stats_test.go b/p2p/host/resource-manager/obs/stats_test.go index 02363302c..990e216dc 100644 --- a/p2p/host/resource-manager/obs/stats_test.go +++ b/p2p/host/resource-manager/obs/stats_test.go @@ -2,9 +2,11 @@ package obs_test import ( "testing" + "time" rcmgr "github.com/libp2p/go-libp2p-resource-manager" "github.com/libp2p/go-libp2p-resource-manager/obs" + "go.opencensus.io/stats/view" ) func TestTraceReporterStartAndClose(t *testing.T) { @@ -14,3 +16,24 @@ func TestTraceReporterStartAndClose(t *testing.T) { } defer rcmgr.Close() } + +func TestConsumeEvent(t *testing.T) { + evt := rcmgr.TraceEvt{ + Type: rcmgr.TraceBlockAddStreamEvt, + Name: "conn-1", + DeltaOut: 1, + Time: time.Now().Format(time.RFC3339Nano), + } + + err := view.Register(obs.DefaultViews...) + if err != nil { + t.Fatal(err) + } + + str, err := obs.NewStatsTraceReporter() + if err != nil { + t.Fatal(err) + } + + str.ConsumeEvent(evt) +}