Skip to content

Commit

Permalink
[surfacer.cw] Add tests for map values (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
manugarg committed Aug 8, 2023
1 parent 68eab8a commit 33b78ba
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
2 changes: 0 additions & 2 deletions surfacers/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,6 @@ func (cw *CWSurfacer) publishMetrics(ctx context.Context) {
}

cw.metricDatumCache = cw.metricDatumCache[:0] // reset the buffer

return
}

// Create a new cloudwatch metriddatum using the values passed in.
Expand Down
44 changes: 44 additions & 0 deletions surfacers/cloudwatch/cloudwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/cloudprober/cloudprober/logger"
"github.com/cloudprober/cloudprober/metrics"
configpb "github.com/cloudprober/cloudprober/surfacers/cloudwatch/proto"
"github.com/cloudprober/cloudprober/surfacers/common/options"
"github.com/stretchr/testify/assert"
)

func newTestCWSurfacer() CWSurfacer {
Expand Down Expand Up @@ -311,3 +313,45 @@ func ErrorContains(out error, want string) bool {
}
return strings.Contains(out.Error(), want)
}

func TestCWSurfacerRecordEventMetrics(t *testing.T) {
type fields struct {
c *configpb.SurfacerConf
opts *options.Options
writeChan chan *metrics.EventMetrics
}
tests := []struct {
name string
metricName string
metricValue metrics.Value
labels [][2]string
fields fields
}{
{
name: "resp-code",
metricName: "resp-code",
metricValue: metrics.NewMap("code").IncKeyBy("200", 98).IncKeyBy("500", 2),
labels: [][2]string{
{"service", "pagenotes"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ts := time.Now()
em := metrics.NewEventMetrics(ts).AddMetric(tt.metricName, tt.metricValue)
for _, label := range tt.labels {
em.AddLabel(label[0], label[1])
}
publishTimer := time.NewTicker(1 * time.Hour)
defer publishTimer.Stop()
cw := &CWSurfacer{
c: tt.fields.c,
opts: tt.fields.opts,
writeChan: tt.fields.writeChan,
}
cw.recordEventMetrics(context.TODO(), publishTimer, em)
assert.Equal(t, 2, len(cw.metricDatumCache), "cache length should be 2")
})
}
}

0 comments on commit 33b78ba

Please sign in to comment.