go.temporal.io/server@v1.23.0/common/metrics/metricstest/metricstest_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 metricstest
    26  
    27  import (
    28  	"math"
    29  	"testing"
    30  
    31  	"github.com/stretchr/testify/assert"
    32  	"github.com/stretchr/testify/require"
    33  
    34  	"go.temporal.io/server/common/log"
    35  	"go.temporal.io/server/common/metrics"
    36  )
    37  
    38  func TestBasic(t *testing.T) {
    39  	t.Parallel()
    40  	logger := log.NewTestLogger()
    41  	handler, err := NewHandler(logger, metrics.ClientConfig{})
    42  	require.NoError(t, err)
    43  
    44  	counterName := "counter1"
    45  	counterTags := []metrics.Tag{
    46  		metrics.StringTag("l2", "v2"),
    47  		metrics.StringTag("l1", "v1"),
    48  	}
    49  	expectedSystemTags := []metrics.Tag{
    50  		metrics.StringTag("otel_scope_name", "temporal"),
    51  		metrics.StringTag("otel_scope_version", ""),
    52  	}
    53  	expectedCounterTags := append(expectedSystemTags, counterTags...)
    54  	counter := handler.WithTags(counterTags...).Counter(counterName)
    55  	counter.Record(1)
    56  	counter.Record(1)
    57  
    58  	s1, err := handler.Snapshot()
    59  	require.NoError(t, err)
    60  
    61  	counterVal, err := s1.Counter(counterName+"_total", expectedCounterTags...)
    62  	require.NoError(t, err)
    63  	assert.Equal(t, float64(2), counterVal)
    64  
    65  	gaugeName := "gauge1"
    66  	gaugeTags := []metrics.Tag{
    67  		metrics.StringTag("l3", "v3"),
    68  		metrics.StringTag("l4", "v4"),
    69  	}
    70  	expectedGaugeTags := append(expectedSystemTags, gaugeTags...)
    71  	gauge := handler.WithTags(gaugeTags...).Gauge(gaugeName)
    72  	gauge.Record(-2)
    73  	gauge.Record(10)
    74  
    75  	s2, err := handler.Snapshot()
    76  	require.NoError(t, err)
    77  
    78  	counterVal, err = s2.Counter(counterName+"_total", expectedCounterTags...)
    79  	require.NoError(t, err)
    80  	assert.Equal(t, float64(2), counterVal)
    81  
    82  	gaugeVal, err := s2.Gauge(gaugeName, expectedGaugeTags...)
    83  	require.NoError(t, err)
    84  	assert.Equal(t, float64(10), gaugeVal)
    85  }
    86  
    87  func TestHistogram(t *testing.T) {
    88  	t.Parallel()
    89  	logger := log.NewTestLogger()
    90  	handler, err := NewHandler(logger, metrics.ClientConfig{
    91  		PerUnitHistogramBoundaries: map[string][]float64{
    92  			metrics.Dimensionless: {
    93  				1,
    94  				2,
    95  				5,
    96  			},
    97  		},
    98  	})
    99  	require.NoError(t, err)
   100  
   101  	histogramName := "histogram1"
   102  	histogramTags := []metrics.Tag{
   103  		metrics.StringTag("l2", "v2"),
   104  		metrics.StringTag("l1", "v1"),
   105  	}
   106  	expectedSystemTags := []metrics.Tag{
   107  		metrics.StringTag("otel_scope_name", "temporal"),
   108  		metrics.StringTag("otel_scope_version", ""),
   109  	}
   110  	expectedHistogramTags := append(expectedSystemTags, histogramTags...)
   111  	histogram := handler.WithTags(histogramTags...).Histogram(histogramName, metrics.Dimensionless)
   112  	histogram.Record(1)
   113  	histogram.Record(3)
   114  
   115  	s1, err := handler.Snapshot()
   116  	require.NoError(t, err)
   117  
   118  	expectedBuckets := []HistogramBucket{
   119  		{value: 1, upperBound: 1},
   120  		{value: 1, upperBound: 2},
   121  		{value: 2, upperBound: 5},
   122  		{value: 2, upperBound: math.Inf(1)},
   123  	}
   124  
   125  	histogramVal, err := s1.Histogram(histogramName+"_ratio", expectedHistogramTags...)
   126  	require.NoError(t, err)
   127  	assert.Equal(t, expectedBuckets, histogramVal)
   128  }