go.temporal.io/server@v1.23.0/common/rpc/interceptor/telemetry_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 interceptor
    26  
    27  import (
    28  	"testing"
    29  
    30  	"github.com/golang/mock/gomock"
    31  	"github.com/stretchr/testify/assert"
    32  
    33  	"go.temporal.io/server/common/api"
    34  	"go.temporal.io/server/common/log"
    35  	"go.temporal.io/server/common/metrics"
    36  	"go.temporal.io/server/common/namespace"
    37  )
    38  
    39  const (
    40  	startWorkflow = "StartWorkflowExecution"
    41  	queryWorkflow = "QueryWorkflow"
    42  )
    43  
    44  func TestEmitActionMetric(t *testing.T) {
    45  	controller := gomock.NewController(t)
    46  	register := namespace.NewMockRegistry(controller)
    47  	metricsHandler := metrics.NewMockHandler(controller)
    48  	telemetry := NewTelemetryInterceptor(register, metricsHandler, log.NewNoopLogger())
    49  
    50  	testCases := []struct {
    51  		methodName        string
    52  		fullName          string
    53  		expectEmitMetrics bool
    54  	}{
    55  		{
    56  			queryWorkflow,
    57  			api.WorkflowServicePrefix + queryWorkflow,
    58  			true,
    59  		},
    60  		{
    61  			queryWorkflow,
    62  			api.AdminServicePrefix + queryWorkflow,
    63  			false,
    64  		},
    65  		{
    66  			metrics.MatchingClientAddWorkflowTaskScope,
    67  			api.WorkflowServicePrefix + queryWorkflow,
    68  			false,
    69  		},
    70  	}
    71  
    72  	for _, tt := range testCases {
    73  		t.Run(tt.methodName, func(t *testing.T) {
    74  			if tt.expectEmitMetrics {
    75  				metricsHandler.EXPECT().Counter(gomock.Any()).Return(metrics.NoopCounterMetricFunc).Times(1)
    76  			} else {
    77  				metricsHandler.EXPECT().Counter(gomock.Any()).Return(metrics.NoopCounterMetricFunc).Times(0)
    78  			}
    79  			telemetry.emitActionMetric(tt.methodName, tt.fullName, nil, metricsHandler, nil)
    80  		})
    81  	}
    82  }
    83  
    84  func TestOperationOverwrite(t *testing.T) {
    85  	controller := gomock.NewController(t)
    86  	register := namespace.NewMockRegistry(controller)
    87  	metricsHandler := metrics.NewMockHandler(controller)
    88  	telemetry := NewTelemetryInterceptor(register, metricsHandler, log.NewNoopLogger())
    89  
    90  	testCases := []struct {
    91  		methodName        string
    92  		fullName          string
    93  		expectedOperation string
    94  	}{
    95  		{
    96  			"DeleteWorkflowExecution",
    97  			api.AdminServicePrefix + "DeleteWorkflowExecution",
    98  			"AdminDeleteWorkflowExecution",
    99  		},
   100  		{
   101  			"DeleteNamespace",
   102  			api.OperatorServicePrefix + "DeleteNamespace",
   103  			"OperatorDeleteNamespace",
   104  		},
   105  		{
   106  			startWorkflow,
   107  			api.WorkflowServicePrefix + startWorkflow,
   108  			startWorkflow,
   109  		},
   110  	}
   111  
   112  	for _, tt := range testCases {
   113  		t.Run(tt.methodName, func(t *testing.T) {
   114  			operation := telemetry.overrideOperationTag(tt.fullName, tt.methodName)
   115  			assert.Equal(t, tt.expectedOperation, operation)
   116  		})
   117  	}
   118  
   119  }