From f0f4078114ecd70db7779743400980ecf95495bc Mon Sep 17 00:00:00 2001 From: "M.S.Srinivas" Date: Wed, 23 Jun 2021 14:21:51 -0700 Subject: [PATCH] Add a no-op WavefrontSender implementation (#65) * Add a no-op WavefrontSender implementation * Added unit tests for noop_sender Co-authored-by: mssrinivas <> --- senders/client_noop.go | 56 +++++++++++++++++++++++++++++++++++ senders/client_noop_test.go | 59 +++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 senders/client_noop.go create mode 100644 senders/client_noop_test.go diff --git a/senders/client_noop.go b/senders/client_noop.go new file mode 100644 index 0000000..70cf30b --- /dev/null +++ b/senders/client_noop.go @@ -0,0 +1,56 @@ +package senders + +import ( + "github.com/wavefronthq/wavefront-sdk-go/event" + "github.com/wavefronthq/wavefront-sdk-go/histogram" +) + +type wavefrontNoOpSender struct { +} + +var ( + defaultNoopClient Sender = &wavefrontNoOpSender{} +) + +// NewWavefrontNoOpClient returns a Wavefront Client instance for which all operations are no-ops. +func NewWavefrontNoOpClient() (Sender, error) { + return defaultNoopClient, nil +} + +func (sender *wavefrontNoOpSender) Start() { + // no-op +} + +func (sender *wavefrontNoOpSender) SendMetric(name string, value float64, ts int64, source string, tags map[string]string) error { + return nil +} + +func (sender *wavefrontNoOpSender) SendDeltaCounter(name string, value float64, source string, tags map[string]string) error { + return nil +} + +func (sender *wavefrontNoOpSender) SendDistribution(name string, centroids []histogram.Centroid, + hgs map[histogram.Granularity]bool, ts int64, source string, tags map[string]string) error { + return nil +} + +func (sender *wavefrontNoOpSender) SendSpan(name string, startMillis, durationMillis int64, source, traceId, spanId string, + parents, followsFrom []string, tags []SpanTag, spanLogs []SpanLog) error { + return nil +} + +func (sender *wavefrontNoOpSender) SendEvent(name string, startMillis, endMillis int64, source string, tags map[string]string, setters ...event.Option) error { + return nil +} + +func (sender *wavefrontNoOpSender) Close() { + // no-op +} + +func (sender *wavefrontNoOpSender) Flush() error { + return nil +} + +func (sender *wavefrontNoOpSender) GetFailureCount() int64 { + return 0 +} diff --git a/senders/client_noop_test.go b/senders/client_noop_test.go new file mode 100644 index 0000000..74a1dfb --- /dev/null +++ b/senders/client_noop_test.go @@ -0,0 +1,59 @@ +package senders_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/wavefronthq/wavefront-sdk-go/histogram" + "github.com/wavefronthq/wavefront-sdk-go/senders" +) + +func TestNoOpSender(t *testing.T) { + wf, err := senders.NewSender("http://DUMMY_TOKEN@localhost:8080") + assert.Nil(t, err) + if wf == nil { + t.Error("Failed Initialize NewSender", err) + } + + if err := wf.SendMetric("new-york.power.usage", 42422.0, 0, "go_test", map[string]string{"env": "test"}); err != nil { + t.Error("Failed SendMetric", err) + } + + centroids := []histogram.Centroid{ + {Value: 30.0, Count: 20}, + {Value: 5.1, Count: 10}, + } + + hgs := map[histogram.Granularity]bool{ + histogram.MINUTE: true, + histogram.HOUR: true, + histogram.DAY: true, + } + + if err := wf.SendDistribution("request.latency", centroids, hgs, 0, "appServer1", map[string]string{"region": "us-west"}); err != nil { + t.Error("Failed SendDistribution", err) + } + + if err := wf.SendDeltaCounter("invocation.count", 0, "appServer2", map[string]string{"region": "us-west"}); err != nil { + t.Error("Failed SendDeltaCounter", err) + } + + if err := wf.SendSpan("getAllUsers", 0, 343500, "localhost", + "7b3bf470-9456-11e8-9eb6-529269fb1459", "0313bafe-9457-11e8-9eb6-529269fb1459", + []string{"2f64e538-9457-11e8-9eb6-529269fb1459"}, nil, + []senders.SpanTag{ + {Key: "application", Value: "Wavefront"}, + {Key: "http.method", Value: "GET"}, + }, + nil); err != nil { + t.Error("Failed SendSpan", err) + } + + if err := wf.SendEvent("updateAllUsers", 0, 37484, "localhost", map[string]string{"region": "us-west"}); err != nil { + t.Error("Failed SendEvent", err) + } + + wf.Flush() + wf.Close() + assert.Equal(t, int64(0), wf.GetFailureCount(), "GetFailureCount") +}