go.temporal.io/server@v1.23.0/common/metrics/otel_options_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 metrics
    26  
    27  import (
    28  	"testing"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  	"github.com/stretchr/testify/require"
    32  	"go.opentelemetry.io/otel/metric"
    33  )
    34  
    35  type testCase struct {
    36  	name         string
    37  	catalog      map[string]metricDefinition
    38  	expectedOpts []metric.InstrumentOption
    39  }
    40  
    41  func TestAddOptions(t *testing.T) {
    42  	t.Parallel()
    43  
    44  	metricName := "foo"
    45  	inputOpts := []metric.InstrumentOption{
    46  		metric.WithDescription("foo description"),
    47  		metric.WithUnit(Milliseconds),
    48  	}
    49  	for _, c := range []testCase{
    50  		{
    51  			name:         "missing metric",
    52  			catalog:      map[string]metricDefinition{},
    53  			expectedOpts: inputOpts,
    54  		},
    55  		{
    56  			name: "empty metric definition",
    57  			catalog: map[string]metricDefinition{
    58  				metricName: {},
    59  			},
    60  			expectedOpts: inputOpts,
    61  		},
    62  		{
    63  			name: "opts overwritten",
    64  			catalog: map[string]metricDefinition{
    65  				metricName: {
    66  					description: "bar description",
    67  					unit:        Bytes,
    68  				},
    69  			},
    70  			expectedOpts: []metric.InstrumentOption{
    71  				metric.WithDescription("foo description"),
    72  				metric.WithUnit(Milliseconds),
    73  				metric.WithDescription("bar description"),
    74  				metric.WithUnit(Bytes),
    75  			},
    76  		},
    77  	} {
    78  		c := c
    79  		t.Run(c.name, func(t *testing.T) {
    80  			t.Parallel()
    81  
    82  			handler := &otelMetricsHandler{catalog: c.catalog}
    83  			var (
    84  				counter counterOptions
    85  				gauge   gaugeOptions
    86  				hist    histogramOptions
    87  			)
    88  			for _, opt := range inputOpts {
    89  				counter = append(counter, opt.(metric.Int64CounterOption))
    90  				gauge = append(gauge, opt.(metric.Float64ObservableGaugeOption))
    91  				hist = append(hist, opt.(metric.Int64HistogramOption))
    92  			}
    93  			counter = addOptions(handler, counter, metricName)
    94  			gauge = addOptions(handler, gauge, metricName)
    95  			hist = addOptions(handler, hist, metricName)
    96  			require.Len(t, counter, len(c.expectedOpts))
    97  			require.Len(t, gauge, len(c.expectedOpts))
    98  			require.Len(t, hist, len(c.expectedOpts))
    99  			for i, opt := range c.expectedOpts {
   100  				assert.Equal(t, opt, counter[i])
   101  				assert.Equal(t, opt, gauge[i])
   102  				assert.Equal(t, opt, hist[i])
   103  			}
   104  		})
   105  	}
   106  }