...

Source file src/github.com/palantir/go-baseapp/baseapp/datadog/datadog_test.go

Documentation: github.com/palantir/go-baseapp/baseapp/datadog

     1  // Copyright 2019 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package datadog
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/DataDog/datadog-go/statsd"
    22  	"github.com/rcrowley/go-metrics"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestTagsFromName(t *testing.T) {
    27  	t.Run("noTags", func(t *testing.T) {
    28  		name, tags := tagsFromName("notags")
    29  		assert.Equal(t, "notags", name)
    30  		assert.Empty(t, tags)
    31  	})
    32  
    33  	t.Run("singleTag", func(t *testing.T) {
    34  		name, tags := tagsFromName("single[tag1]")
    35  		assert.Equal(t, "single", name)
    36  		assert.Equal(t, []string{"tag1"}, tags)
    37  	})
    38  
    39  	t.Run("multipleTags", func(t *testing.T) {
    40  		name, tags := tagsFromName("multiple[tag2:value,tag1]")
    41  		assert.Equal(t, "multiple", name)
    42  		assert.Equal(t, []string{"tag1", "tag2:value"}, tags)
    43  	})
    44  
    45  	t.Run("invalidFormat", func(t *testing.T) {
    46  		name, tags := tagsFromName("invalid[tag1")
    47  		assert.Equal(t, "invalid[tag1", name)
    48  		assert.Empty(t, tags)
    49  	})
    50  }
    51  
    52  func TestEmitCounts(t *testing.T) {
    53  	initialize := func() (*Emitter, *MemoryWriter, metrics.Registry) {
    54  		w := &MemoryWriter{}
    55  		c, _ := statsd.NewWithWriter(w)
    56  		r := metrics.NewRegistry()
    57  		return NewEmitter(c, r), w, r
    58  	}
    59  
    60  	t.Run("single", func(t *testing.T) {
    61  		e, w, r := initialize()
    62  		c := metrics.NewRegisteredCounter("counter", r)
    63  
    64  		c.Inc(1)
    65  		e.EmitOnce()
    66  
    67  		assert.Equal(t, int64(1), c.Count())
    68  		assert.Equal(t, []string{"counter:1|c"}, w.Messages)
    69  	})
    70  
    71  	t.Run("difference", func(t *testing.T) {
    72  		e, w, r := initialize()
    73  		c := metrics.NewRegisteredCounter("counter", r)
    74  
    75  		c.Inc(1)
    76  		e.EmitOnce()
    77  		c.Inc(2)
    78  		e.EmitOnce()
    79  
    80  		assert.Equal(t, int64(3), c.Count())
    81  		assert.Equal(t, []string{"counter:1|c", "counter:2|c"}, w.Messages)
    82  	})
    83  }
    84  
    85  type MemoryWriter struct {
    86  	Messages []string
    87  }
    88  
    89  func (mw *MemoryWriter) Write(b []byte) (int, error) {
    90  	mw.Messages = append(mw.Messages, string(b))
    91  	return len(b), nil
    92  }
    93  
    94  func (mw *MemoryWriter) Close() error { return nil }
    95  
    96  func (mw *MemoryWriter) SetWriteTimeout(t time.Duration) error { return nil }
    97  

View as plain text