Skip to content

Commit

Permalink
Wrapped errors support on metered persistence wrappers (#6035)
Browse files Browse the repository at this point in the history
  • Loading branch information
3vilhamster authored May 21, 2024
1 parent c0b0576 commit cc75f6f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 37 deletions.
53 changes: 27 additions & 26 deletions common/persistence/wrappers/metered/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package metered

import (
"errors"
"time"

"github.com/uber/cadence/common/dynamicconfig"
Expand All @@ -42,32 +43,32 @@ type base struct {
}

func (p *base) updateErrorMetricPerDomain(scope int, err error, scopeWithDomainTag metrics.Scope) {
switch err.(type) {
case *types.DomainAlreadyExistsError:
switch {
case errors.As(err, new(*types.DomainAlreadyExistsError)):
scopeWithDomainTag.IncCounter(metrics.PersistenceErrDomainAlreadyExistsCounterPerDomain)
case *types.BadRequestError:
case errors.As(err, new(*types.BadRequestError)):
scopeWithDomainTag.IncCounter(metrics.PersistenceErrBadRequestCounterPerDomain)
case *persistence.WorkflowExecutionAlreadyStartedError:
case errors.As(err, new(*persistence.WorkflowExecutionAlreadyStartedError)):
scopeWithDomainTag.IncCounter(metrics.PersistenceErrExecutionAlreadyStartedCounterPerDomain)
case *persistence.ConditionFailedError:
case errors.As(err, new(*persistence.ConditionFailedError)):
scopeWithDomainTag.IncCounter(metrics.PersistenceErrConditionFailedCounterPerDomain)
case *persistence.CurrentWorkflowConditionFailedError:
case errors.As(err, new(*persistence.CurrentWorkflowConditionFailedError)):
scopeWithDomainTag.IncCounter(metrics.PersistenceErrCurrentWorkflowConditionFailedCounterPerDomain)
case *persistence.ShardAlreadyExistError:
case errors.As(err, new(*persistence.ShardAlreadyExistError)):
scopeWithDomainTag.IncCounter(metrics.PersistenceErrShardExistsCounterPerDomain)
case *persistence.ShardOwnershipLostError:
case errors.As(err, new(*persistence.ShardOwnershipLostError)):
scopeWithDomainTag.IncCounter(metrics.PersistenceErrShardOwnershipLostCounterPerDomain)
case *types.EntityNotExistsError:
case errors.As(err, new(*types.EntityNotExistsError)):
scopeWithDomainTag.IncCounter(metrics.PersistenceErrEntityNotExistsCounterPerDomain)
case *persistence.DuplicateRequestError:
case errors.As(err, new(*persistence.DuplicateRequestError)):
scopeWithDomainTag.IncCounter(metrics.PersistenceErrDuplicateRequestCounterPerDomain)
case *persistence.TimeoutError:
case errors.As(err, new(*persistence.TimeoutError)):
scopeWithDomainTag.IncCounter(metrics.PersistenceErrTimeoutCounterPerDomain)
scopeWithDomainTag.IncCounter(metrics.PersistenceFailuresPerDomain)
case *types.ServiceBusyError:
case errors.As(err, new(*types.ServiceBusyError)):
scopeWithDomainTag.IncCounter(metrics.PersistenceErrBusyCounterPerDomain)
scopeWithDomainTag.IncCounter(metrics.PersistenceFailuresPerDomain)
case *persistence.DBUnavailableError:
case errors.As(err, new(*persistence.DBUnavailableError)):
scopeWithDomainTag.IncCounter(metrics.PersistenceErrDBUnavailableCounterPerDomain)
scopeWithDomainTag.IncCounter(metrics.PersistenceFailuresPerDomain)
p.logger.Error("DBUnavailable Error:", tag.Error(err), tag.MetricScope(scope))
Expand All @@ -78,32 +79,32 @@ func (p *base) updateErrorMetricPerDomain(scope int, err error, scopeWithDomainT
}

func (p *base) updateErrorMetric(scope int, err error, metricsScope metrics.Scope) {
switch err.(type) {
case *types.DomainAlreadyExistsError:
switch {
case errors.As(err, new(*types.DomainAlreadyExistsError)):
metricsScope.IncCounter(metrics.PersistenceErrDomainAlreadyExistsCounter)
case *types.BadRequestError:
case errors.As(err, new(*types.BadRequestError)):
metricsScope.IncCounter(metrics.PersistenceErrBadRequestCounter)
case *persistence.WorkflowExecutionAlreadyStartedError:
case errors.As(err, new(*persistence.WorkflowExecutionAlreadyStartedError)):
metricsScope.IncCounter(metrics.PersistenceErrExecutionAlreadyStartedCounter)
case *persistence.ConditionFailedError:
case errors.As(err, new(*persistence.ConditionFailedError)):
metricsScope.IncCounter(metrics.PersistenceErrConditionFailedCounter)
case *persistence.CurrentWorkflowConditionFailedError:
case errors.As(err, new(*persistence.CurrentWorkflowConditionFailedError)):
metricsScope.IncCounter(metrics.PersistenceErrCurrentWorkflowConditionFailedCounter)
case *persistence.ShardAlreadyExistError:
case errors.As(err, new(*persistence.ShardAlreadyExistError)):
metricsScope.IncCounter(metrics.PersistenceErrShardExistsCounter)
case *persistence.ShardOwnershipLostError:
case errors.As(err, new(*persistence.ShardOwnershipLostError)):
metricsScope.IncCounter(metrics.PersistenceErrShardOwnershipLostCounter)
case *types.EntityNotExistsError:
case errors.As(err, new(*types.EntityNotExistsError)):
metricsScope.IncCounter(metrics.PersistenceErrEntityNotExistsCounter)
case *persistence.DuplicateRequestError:
case errors.As(err, new(*persistence.DuplicateRequestError)):
metricsScope.IncCounter(metrics.PersistenceErrDuplicateRequestCounter)
case *persistence.TimeoutError:
case errors.As(err, new(*persistence.TimeoutError)):
metricsScope.IncCounter(metrics.PersistenceErrTimeoutCounter)
metricsScope.IncCounter(metrics.PersistenceFailures)
case *types.ServiceBusyError:
case errors.As(err, new(*types.ServiceBusyError)):
metricsScope.IncCounter(metrics.PersistenceErrBusyCounter)
metricsScope.IncCounter(metrics.PersistenceFailures)
case *persistence.DBUnavailableError:
case errors.As(err, new(*persistence.DBUnavailableError)):
metricsScope.IncCounter(metrics.PersistenceErrDBUnavailableCounter)
metricsScope.IncCounter(metrics.PersistenceFailures)
p.logger.Error("DBUnavailable Error:", tag.Error(err), tag.MetricScope(scope))
Expand Down
41 changes: 30 additions & 11 deletions common/persistence/wrappers/metered/metered_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/uber/cadence/common/log/loggerimpl"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/types"
)

var _staticMethods = map[string]bool{
Expand Down Expand Up @@ -152,17 +153,35 @@ func TestWrappersAgainstPreviousImplementation(t *testing.T) {
runScenario(t, wrapper, logs, metricScope)
})
t.Run("with error", func(t *testing.T) {
ctrl := gomock.NewController(t)

zapLogger, logs := setupLogsCapture()
metricScope := tally.NewTestScope("", nil)
metricsClient := metrics.NewClient(metricScope, metrics.ServiceIdx(0))
logger := loggerimpl.NewLogger(zapLogger)

newObj, mocked := tc.prepareMock(t, ctrl, metricsClient, logger)
prepareMockForTest(t, mocked, errors.New("persistence error"))

runScenario(t, newObj, logs, metricScope)
for _, errorType := range []error{
&types.DomainAlreadyExistsError{},
&types.BadRequestError{},
&types.EntityNotExistsError{},
&types.ServiceBusyError{},
&persistence.WorkflowExecutionAlreadyStartedError{},
&persistence.ConditionFailedError{},
&persistence.CurrentWorkflowConditionFailedError{},
&persistence.ShardAlreadyExistError{},
&persistence.ShardOwnershipLostError{},
&persistence.DuplicateRequestError{},
&persistence.TimeoutError{},
&persistence.DBUnavailableError{},
errors.New("persistence error"),
} {
t.Run(reflect.TypeOf(errorType).String(), func(t *testing.T) {
ctrl := gomock.NewController(t)

zapLogger, logs := setupLogsCapture()
metricScope := tally.NewTestScope("", nil)
metricsClient := metrics.NewClient(metricScope, metrics.ServiceIdx(0))
logger := loggerimpl.NewLogger(zapLogger)

newObj, mocked := tc.prepareMock(t, ctrl, metricsClient, logger)
prepareMockForTest(t, mocked, errorType)

runScenario(t, newObj, logs, metricScope)
})
}
})
})
}
Expand Down

0 comments on commit cc75f6f

Please sign in to comment.