go.temporal.io/server@v1.23.0/common/persistence/visibility/visiblity_manager_metrics.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 visibility
    26  
    27  import (
    28  	"context"
    29  	"time"
    30  
    31  	"go.temporal.io/api/serviceerror"
    32  
    33  	"go.temporal.io/server/common/log"
    34  	"go.temporal.io/server/common/log/tag"
    35  	"go.temporal.io/server/common/metrics"
    36  	"go.temporal.io/server/common/namespace"
    37  	"go.temporal.io/server/common/persistence"
    38  	"go.temporal.io/server/common/persistence/visibility/manager"
    39  )
    40  
    41  var _ manager.VisibilityManager = (*visibilityManagerMetrics)(nil)
    42  
    43  type visibilityManagerMetrics struct {
    44  	metricHandler metrics.Handler
    45  	logger        log.Logger
    46  	delegate      manager.VisibilityManager
    47  
    48  	visibilityPluginNameMetricsTag metrics.Tag
    49  }
    50  
    51  func NewVisibilityManagerMetrics(
    52  	delegate manager.VisibilityManager,
    53  	metricHandler metrics.Handler,
    54  	logger log.Logger,
    55  	visibilityPluginNameMetricsTag metrics.Tag,
    56  ) *visibilityManagerMetrics {
    57  	return &visibilityManagerMetrics{
    58  		metricHandler: metricHandler,
    59  		logger:        logger,
    60  		delegate:      delegate,
    61  
    62  		visibilityPluginNameMetricsTag: visibilityPluginNameMetricsTag,
    63  	}
    64  }
    65  
    66  func (m *visibilityManagerMetrics) Close() {
    67  	m.delegate.Close()
    68  }
    69  
    70  func (m *visibilityManagerMetrics) GetReadStoreName(nsName namespace.Name) string {
    71  	return m.delegate.GetReadStoreName(nsName)
    72  }
    73  
    74  func (m *visibilityManagerMetrics) GetStoreNames() []string {
    75  	return m.delegate.GetStoreNames()
    76  }
    77  
    78  func (m *visibilityManagerMetrics) HasStoreName(stName string) bool {
    79  	return m.delegate.HasStoreName(stName)
    80  }
    81  
    82  func (m *visibilityManagerMetrics) GetIndexName() string {
    83  	return m.delegate.GetIndexName()
    84  }
    85  
    86  func (m *visibilityManagerMetrics) ValidateCustomSearchAttributes(
    87  	searchAttributes map[string]any,
    88  ) (map[string]any, error) {
    89  	return m.delegate.ValidateCustomSearchAttributes(searchAttributes)
    90  }
    91  
    92  func (m *visibilityManagerMetrics) RecordWorkflowExecutionStarted(
    93  	ctx context.Context,
    94  	request *manager.RecordWorkflowExecutionStartedRequest,
    95  ) error {
    96  	handler, startTime := m.tagScope(metrics.VisibilityPersistenceRecordWorkflowExecutionStartedScope)
    97  	err := m.delegate.RecordWorkflowExecutionStarted(ctx, request)
    98  	handler.Timer(metrics.VisibilityPersistenceLatency.Name()).Record(time.Since(startTime))
    99  	return m.updateErrorMetric(handler, err)
   100  }
   101  
   102  func (m *visibilityManagerMetrics) RecordWorkflowExecutionClosed(
   103  	ctx context.Context,
   104  	request *manager.RecordWorkflowExecutionClosedRequest,
   105  ) error {
   106  	handler, startTime := m.tagScope(metrics.VisibilityPersistenceRecordWorkflowExecutionClosedScope)
   107  	err := m.delegate.RecordWorkflowExecutionClosed(ctx, request)
   108  	handler.Timer(metrics.VisibilityPersistenceLatency.Name()).Record(time.Since(startTime))
   109  	return m.updateErrorMetric(handler, err)
   110  }
   111  
   112  func (m *visibilityManagerMetrics) UpsertWorkflowExecution(
   113  	ctx context.Context,
   114  	request *manager.UpsertWorkflowExecutionRequest,
   115  ) error {
   116  	handler, startTime := m.tagScope(metrics.VisibilityPersistenceUpsertWorkflowExecutionScope)
   117  	err := m.delegate.UpsertWorkflowExecution(ctx, request)
   118  	handler.Timer(metrics.VisibilityPersistenceLatency.Name()).Record(time.Since(startTime))
   119  	return m.updateErrorMetric(handler, err)
   120  }
   121  
   122  func (m *visibilityManagerMetrics) DeleteWorkflowExecution(
   123  	ctx context.Context,
   124  	request *manager.VisibilityDeleteWorkflowExecutionRequest,
   125  ) error {
   126  	handler, startTime := m.tagScope(metrics.VisibilityPersistenceDeleteWorkflowExecutionScope)
   127  	err := m.delegate.DeleteWorkflowExecution(ctx, request)
   128  	handler.Timer(metrics.VisibilityPersistenceLatency.Name()).Record(time.Since(startTime))
   129  	return m.updateErrorMetric(handler, err)
   130  }
   131  
   132  func (m *visibilityManagerMetrics) ListOpenWorkflowExecutions(
   133  	ctx context.Context,
   134  	request *manager.ListWorkflowExecutionsRequest,
   135  ) (*manager.ListWorkflowExecutionsResponse, error) {
   136  	handler, startTime := m.tagScope(metrics.VisibilityPersistenceListOpenWorkflowExecutionsScope)
   137  	response, err := m.delegate.ListOpenWorkflowExecutions(ctx, request)
   138  	handler.Timer(metrics.VisibilityPersistenceLatency.Name()).Record(time.Since(startTime))
   139  	return response, m.updateErrorMetric(handler, err)
   140  }
   141  
   142  func (m *visibilityManagerMetrics) ListClosedWorkflowExecutions(
   143  	ctx context.Context,
   144  	request *manager.ListWorkflowExecutionsRequest,
   145  ) (*manager.ListWorkflowExecutionsResponse, error) {
   146  	handler, startTime := m.tagScope(metrics.VisibilityPersistenceListClosedWorkflowExecutionsScope)
   147  	response, err := m.delegate.ListClosedWorkflowExecutions(ctx, request)
   148  	handler.Timer(metrics.VisibilityPersistenceLatency.Name()).Record(time.Since(startTime))
   149  	return response, m.updateErrorMetric(handler, err)
   150  }
   151  
   152  func (m *visibilityManagerMetrics) ListOpenWorkflowExecutionsByType(
   153  	ctx context.Context,
   154  	request *manager.ListWorkflowExecutionsByTypeRequest,
   155  ) (*manager.ListWorkflowExecutionsResponse, error) {
   156  	handler, startTime := m.tagScope(metrics.VisibilityPersistenceListOpenWorkflowExecutionsByTypeScope)
   157  	response, err := m.delegate.ListOpenWorkflowExecutionsByType(ctx, request)
   158  	handler.Timer(metrics.VisibilityPersistenceLatency.Name()).Record(time.Since(startTime))
   159  	return response, m.updateErrorMetric(handler, err)
   160  }
   161  
   162  func (m *visibilityManagerMetrics) ListClosedWorkflowExecutionsByType(
   163  	ctx context.Context,
   164  	request *manager.ListWorkflowExecutionsByTypeRequest,
   165  ) (*manager.ListWorkflowExecutionsResponse, error) {
   166  	handler, startTime := m.tagScope(metrics.VisibilityPersistenceListClosedWorkflowExecutionsByTypeScope)
   167  	response, err := m.delegate.ListClosedWorkflowExecutionsByType(ctx, request)
   168  	handler.Timer(metrics.VisibilityPersistenceLatency.Name()).Record(time.Since(startTime))
   169  	return response, m.updateErrorMetric(handler, err)
   170  }
   171  
   172  func (m *visibilityManagerMetrics) ListOpenWorkflowExecutionsByWorkflowID(
   173  	ctx context.Context,
   174  	request *manager.ListWorkflowExecutionsByWorkflowIDRequest,
   175  ) (*manager.ListWorkflowExecutionsResponse, error) {
   176  	handler, startTime := m.tagScope(metrics.VisibilityPersistenceListOpenWorkflowExecutionsByWorkflowIDScope)
   177  	response, err := m.delegate.ListOpenWorkflowExecutionsByWorkflowID(ctx, request)
   178  	handler.Timer(metrics.VisibilityPersistenceLatency.Name()).Record(time.Since(startTime))
   179  	return response, m.updateErrorMetric(handler, err)
   180  }
   181  
   182  func (m *visibilityManagerMetrics) ListClosedWorkflowExecutionsByWorkflowID(
   183  	ctx context.Context,
   184  	request *manager.ListWorkflowExecutionsByWorkflowIDRequest,
   185  ) (*manager.ListWorkflowExecutionsResponse, error) {
   186  	handler, startTime := m.tagScope(metrics.VisibilityPersistenceListClosedWorkflowExecutionsByWorkflowIDScope)
   187  	response, err := m.delegate.ListClosedWorkflowExecutionsByWorkflowID(ctx, request)
   188  	handler.Timer(metrics.VisibilityPersistenceLatency.Name()).Record(time.Since(startTime))
   189  	return response, m.updateErrorMetric(handler, err)
   190  }
   191  
   192  func (m *visibilityManagerMetrics) ListClosedWorkflowExecutionsByStatus(
   193  	ctx context.Context,
   194  	request *manager.ListClosedWorkflowExecutionsByStatusRequest,
   195  ) (*manager.ListWorkflowExecutionsResponse, error) {
   196  	handler, startTime := m.tagScope(metrics.VisibilityPersistenceListClosedWorkflowExecutionsByStatusScope)
   197  	response, err := m.delegate.ListClosedWorkflowExecutionsByStatus(ctx, request)
   198  	handler.Timer(metrics.VisibilityPersistenceLatency.Name()).Record(time.Since(startTime))
   199  	return response, m.updateErrorMetric(handler, err)
   200  }
   201  
   202  func (m *visibilityManagerMetrics) ListWorkflowExecutions(
   203  	ctx context.Context,
   204  	request *manager.ListWorkflowExecutionsRequestV2,
   205  ) (*manager.ListWorkflowExecutionsResponse, error) {
   206  	handler, startTime := m.tagScope(metrics.VisibilityPersistenceListWorkflowExecutionsScope)
   207  	response, err := m.delegate.ListWorkflowExecutions(ctx, request)
   208  	handler.Timer(metrics.VisibilityPersistenceLatency.Name()).Record(time.Since(startTime))
   209  	return response, m.updateErrorMetric(handler, err)
   210  }
   211  
   212  func (m *visibilityManagerMetrics) ScanWorkflowExecutions(
   213  	ctx context.Context,
   214  	request *manager.ListWorkflowExecutionsRequestV2,
   215  ) (*manager.ListWorkflowExecutionsResponse, error) {
   216  	handler, startTime := m.tagScope(metrics.VisibilityPersistenceScanWorkflowExecutionsScope)
   217  	response, err := m.delegate.ScanWorkflowExecutions(ctx, request)
   218  	handler.Timer(metrics.VisibilityPersistenceLatency.Name()).Record(time.Since(startTime))
   219  	return response, m.updateErrorMetric(handler, err)
   220  }
   221  
   222  func (m *visibilityManagerMetrics) CountWorkflowExecutions(
   223  	ctx context.Context,
   224  	request *manager.CountWorkflowExecutionsRequest,
   225  ) (*manager.CountWorkflowExecutionsResponse, error) {
   226  	handler, startTime := m.tagScope(metrics.VisibilityPersistenceCountWorkflowExecutionsScope)
   227  	response, err := m.delegate.CountWorkflowExecutions(ctx, request)
   228  	handler.Timer(metrics.VisibilityPersistenceLatency.Name()).Record(time.Since(startTime))
   229  	return response, m.updateErrorMetric(handler, err)
   230  }
   231  
   232  func (m *visibilityManagerMetrics) GetWorkflowExecution(
   233  	ctx context.Context,
   234  	request *manager.GetWorkflowExecutionRequest,
   235  ) (*manager.GetWorkflowExecutionResponse, error) {
   236  	handler, startTime := m.tagScope(metrics.VisibilityPersistenceGetWorkflowExecutionScope)
   237  	response, err := m.delegate.GetWorkflowExecution(ctx, request)
   238  	handler.Timer(metrics.VisibilityPersistenceLatency.Name()).Record(time.Since(startTime))
   239  	return response, m.updateErrorMetric(handler, err)
   240  }
   241  
   242  func (m *visibilityManagerMetrics) tagScope(operation string) (metrics.Handler, time.Time) {
   243  	taggedHandler := m.metricHandler.WithTags(metrics.OperationTag(operation), m.visibilityPluginNameMetricsTag)
   244  	taggedHandler.Counter(metrics.VisibilityPersistenceRequests.Name()).Record(1)
   245  	return taggedHandler, time.Now().UTC()
   246  }
   247  
   248  func (m *visibilityManagerMetrics) updateErrorMetric(handler metrics.Handler, err error) error {
   249  	if err == nil {
   250  		return nil
   251  	}
   252  
   253  	handler.Counter(metrics.VisibilityPersistenceErrorWithType.Name()).Record(1, metrics.ServiceErrorTypeTag(err))
   254  
   255  	switch err := err.(type) {
   256  	case *serviceerror.InvalidArgument,
   257  		*persistence.TimeoutError,
   258  		*persistence.ConditionFailedError,
   259  		*serviceerror.NotFound:
   260  		// no-op
   261  
   262  	case *serviceerror.ResourceExhausted:
   263  		handler.Counter(metrics.VisibilityPersistenceResourceExhausted.Name()).Record(1, metrics.ResourceExhaustedCauseTag(err.Cause))
   264  	default:
   265  		m.logger.Error("Operation failed with an error.", tag.Error(err))
   266  		handler.Counter(metrics.VisibilityPersistenceFailures.Name()).Record(1)
   267  	}
   268  
   269  	return err
   270  }