Skip to content

Commit

Permalink
Added IGNORE_ERROR mode for context missing strategy (#253)
Browse files Browse the repository at this point in the history
* Added IGNORE_ERROR mode for context missing strategy

* Addressed reviewdog comments

* Added test
  • Loading branch information
bhautikpip authored Oct 13, 2020
1 parent 4137269 commit 95821db
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
9 changes: 9 additions & 0 deletions strategy/ctxmissing/ctxmissing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,12 @@ func TestDefaultLogErrorStrategy(t *testing.T) {
l.ContextMissing("TestLogError")
assert.True(t, strings.Contains(buf.String(), "Suppressing AWS X-Ray context missing panic: TestLogError"))
}

func TestDefaultIgnoreErrorStrategy(t *testing.T) {
defer func() {
p := recover()
assert.Equal(t, p, nil)
}()
r := NewDefaultIgnoreErrorStrategy()
r.ContextMissing("TestIgnoreError")
}
21 changes: 21 additions & 0 deletions strategy/ctxmissing/default_context_missing.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ var RuntimeErrorStrategy = "RUNTIME_ERROR"
// context missing strategy.
var LogErrorStrategy = "LOG_ERROR"

// IgnoreErrorStrategy provides the AWS_XRAY_CONTEXT_MISSING
// environment variable value for enabling the ignore error
// context missing strategy.
var IgnoreErrorStrategy = "IGNORE_ERROR"

// DefaultRuntimeErrorStrategy implements the
// runtime error context missing strategy.
type DefaultRuntimeErrorStrategy struct{}
Expand All @@ -28,6 +33,10 @@ type DefaultRuntimeErrorStrategy struct{}
// log error context missing strategy.
type DefaultLogErrorStrategy struct{}

// DefaultIgnoreErrorStrategy implements the
// ignore error context missing strategy.
type DefaultIgnoreErrorStrategy struct{}

// NewDefaultRuntimeErrorStrategy initializes
// an instance of DefaultRuntimeErrorStrategy.
func NewDefaultRuntimeErrorStrategy() *DefaultRuntimeErrorStrategy {
Expand All @@ -40,6 +49,12 @@ func NewDefaultLogErrorStrategy() *DefaultLogErrorStrategy {
return &DefaultLogErrorStrategy{}
}

// NewDefaultIgnoreErrorStrategy initializes
// an instance of DefaultIgnoreErrorStrategy.
func NewDefaultIgnoreErrorStrategy() *DefaultIgnoreErrorStrategy {
return &DefaultIgnoreErrorStrategy{}
}

// ContextMissing panics when the segment context is missing.
func (dr *DefaultRuntimeErrorStrategy) ContextMissing(v interface{}) {
panic(v)
Expand All @@ -50,3 +65,9 @@ func (dr *DefaultRuntimeErrorStrategy) ContextMissing(v interface{}) {
func (dl *DefaultLogErrorStrategy) ContextMissing(v interface{}) {
logger.Errorf("Suppressing AWS X-Ray context missing panic: %v", v)
}

// ContextMissing ignores an error message when the
// segment context is missing.
func (di *DefaultIgnoreErrorStrategy) ContextMissing(v interface{}) {
// do nothing
}
9 changes: 9 additions & 0 deletions xray/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ func newGlobalConfig() *globalConfig {
} else if cms == ctxmissing.LogErrorStrategy {
cm := ctxmissing.NewDefaultLogErrorStrategy()
ret.contextMissingStrategy = cm
} else if cms == ctxmissing.IgnoreErrorStrategy {
cm := ctxmissing.NewDefaultIgnoreErrorStrategy()
ret.contextMissingStrategy = cm
}
} else {
cm := ctxmissing.NewDefaultRuntimeErrorStrategy()
Expand Down Expand Up @@ -151,6 +154,9 @@ func ContextWithConfig(ctx context.Context, c Config) (context.Context, error) {
} else if cms == ctxmissing.LogErrorStrategy {
cm := ctxmissing.NewDefaultLogErrorStrategy()
c.ContextMissingStrategy = cm
} else if cms == ctxmissing.IgnoreErrorStrategy {
cm := ctxmissing.NewDefaultIgnoreErrorStrategy()
c.ContextMissingStrategy = cm
}
}

Expand Down Expand Up @@ -217,6 +223,9 @@ func Configure(c Config) error {
} else if cms == ctxmissing.LogErrorStrategy {
cm := ctxmissing.NewDefaultLogErrorStrategy()
globalCfg.contextMissingStrategy = cm
} else if cms == ctxmissing.IgnoreErrorStrategy {
cm := ctxmissing.NewDefaultIgnoreErrorStrategy()
globalCfg.contextMissingStrategy = cm
}
} else if c.ContextMissingStrategy != nil {
globalCfg.contextMissingStrategy = c.ContextMissingStrategy
Expand Down

0 comments on commit 95821db

Please sign in to comment.