Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added IGNORE_ERROR mode for context missing strategy #253

Merged
merged 6 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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