go.temporal.io/server@v1.23.0/common/metrics/tally/statsd/reporter_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package statsd
    26  
    27  import (
    28  	"strconv"
    29  	"strings"
    30  	"testing"
    31  
    32  	"github.com/stretchr/testify/assert"
    33  )
    34  
    35  func TestMetricNameWithTags(t *testing.T) {
    36  	r := temporalTallyStatsdReporter{
    37  		tallystatsd: nil,
    38  	}
    39  	tags := map[string]string{
    40  		"tag1": "123",
    41  		"tag2": "456",
    42  		"tag3": "789",
    43  	}
    44  	name := "test-metric-name1"
    45  
    46  	assert.Equal(t, "test-metric-name1.tag1.123.tag2.456.tag3.789", r.metricNameWithTags(name, tags))
    47  }
    48  
    49  func TestMetricNameWithTagsStability(t *testing.T) {
    50  	r := temporalTallyStatsdReporter{
    51  		tallystatsd: nil,
    52  	}
    53  	tags := map[string]string{
    54  		"tag1": "123",
    55  		"tag2": "456",
    56  		"tag3": "789",
    57  	}
    58  	name := "test-metric-name2"
    59  
    60  	//test the order is stable
    61  	for i := 1; i <= 16; i++ {
    62  		assert.Equal(t, r.metricNameWithTags(name, tags), r.metricNameWithTags(name, tags))
    63  	}
    64  }
    65  
    66  func TestMetricNameWithSeparatedTags(t *testing.T) {
    67  	testCases := []string{
    68  		"",
    69  		",",
    70  		"__",
    71  		".__",
    72  	}
    73  	for i, tc := range testCases {
    74  		sep := tc
    75  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    76  			r := temporalTallyStatsdReporter{
    77  				tagSeparator: sep,
    78  			}
    79  			tags := map[string]string{
    80  				"tag1": "123",
    81  				"tag2": "456",
    82  				"tag3": "789",
    83  			}
    84  			name := "test-metric-name3"
    85  
    86  			newName := r.metricNameWithTags(name, tags)
    87  
    88  			if sep == "" {
    89  				// Tags should be embedded.
    90  				assert.Equal(t, newName, "test-metric-name3.tag1.123.tag2.456.tag3.789")
    91  			} else {
    92  				// Tags will be appended with the separator.
    93  				assert.True(t, strings.HasPrefix(newName, name))
    94  
    95  				ss := strings.Split(newName, sep)
    96  				assert.Len(t, ss, 1+len(tags))
    97  				assert.Equal(t, ss[0], name)
    98  				assert.Contains(t, ss, "tag1=123")
    99  				assert.Contains(t, ss, "tag2=456")
   100  				assert.Contains(t, ss, "tag3=789")
   101  			}
   102  		})
   103  	}
   104  
   105  }