Skip to content

Commit

Permalink
Make any section skippable via string contains skip checking. (#12055)
Browse files Browse the repository at this point in the history
* Make any section skippable via string contains skip checking

* Fix unit test
  • Loading branch information
sgmiller committed Jul 13, 2021
1 parent 55eb96b commit f6e3589
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 35 deletions.
2 changes: 1 addition & 1 deletion command/operator_diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (c *OperatorDiagnoseCommand) RunWithParsedFlags() int {
}
}
ctx := diagnose.Context(context.Background(), c.diagnose)
c.diagnose.SetSkipList(c.flagSkips)
c.diagnose.SkipFilters = c.flagSkips
err := c.offlineDiagnostics(ctx)

results := c.diagnose.Finalize(ctx)
Expand Down
51 changes: 20 additions & 31 deletions vault/diagnose/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"time"

"github.com/hashicorp/vault/sdk/helper/strutil"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
Expand Down Expand Up @@ -38,10 +39,10 @@ var noopTracer = trace.NewNoopTracerProvider().Tracer("vault-diagnose")
type testFunction func(context.Context) error

type Session struct {
tc *TelemetryCollector
tracer trace.Tracer
tp *sdktrace.TracerProvider
skip map[string]bool
tc *TelemetryCollector
tracer trace.Tracer
tp *sdktrace.TracerProvider
SkipFilters []string
}

// New initializes a Diagnose tracing session. In particular this wires a TelemetryCollector, which
Expand All @@ -60,21 +61,14 @@ func New(w io.Writer) *Session {
tp: tp,
tc: tc,
tracer: tracer,
skip: make(map[string]bool),
}
return sess
}

func (s *Session) SetSkipList(ls []string) {
for _, e := range ls {
s.skip[e] = true
}
}

// IsSkipped returns true if skipName is present in the skip list. Can be used in combination with Skip to mark a
// span skipped and conditionally skip some logic.
func (s *Session) IsSkipped(skipName string) bool {
return s.skip[skipName]
// IsSkipped returns true if skipName is present in the SkipFilters list. Can be used in combination with Skip to mark a
// span skipped and conditionally skips some logic.
func (s *Session) IsSkipped(spanName string) bool {
return strutil.StrListContainsCaseInsensitive(s.SkipFilters, spanName)
}

// Context returns a new context with a defined diagnose session
Expand Down Expand Up @@ -191,6 +185,12 @@ func addSpotCheckResult(ctx context.Context, eventName, checkName, message strin
}

func SpotCheck(ctx context.Context, checkName string, f func() error) error {
sess := CurrentSession(ctx)
if sess.IsSkipped(checkName) {
SpotSkipped(ctx, checkName, "skipped as requested")
return nil
}

err := f()
if err != nil {
SpotError(ctx, checkName, err)
Expand All @@ -206,6 +206,11 @@ func SpotCheck(ctx context.Context, checkName string, f func() error) error {
func Test(ctx context.Context, spanName string, function testFunction, options ...trace.SpanOption) error {
ctx, span := StartSpan(ctx, spanName, options...)
defer span.End()
sess := CurrentSession(ctx)
if sess.IsSkipped(spanName) {
Skipped(ctx, "skipped as requested")
return nil
}

err := function(ctx)
if err != nil {
Expand Down Expand Up @@ -233,22 +238,6 @@ func WithTimeout(d time.Duration, f testFunction) testFunction {
}
}

// Skippable wraps a Test function with logic that will not run the test if the skipName
// was in the session's skip list
func Skippable(skipName string, f testFunction) testFunction {
return func(ctx context.Context) error {
session := CurrentSession(ctx)
if session != nil {
if !session.IsSkipped(skipName) {
return f(ctx)
} else {
Skipped(ctx, "skipped as requested")
}
}
return nil
}
}

// CapitalizeFirstLetter returns a string with the first letter capitalized
func CapitalizeFirstLetter(msg string) string {
words := strings.Split(msg, " ")
Expand Down
4 changes: 2 additions & 2 deletions vault/diagnose/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestDiagnoseOtelResults(t *testing.T) {
},
}
sess := New(os.Stdout)
sess.SetSkipList([]string{"dispose-grounds"})
sess.SkipFilters = []string{"dispose-grounds"}
ctx := Context(context.Background(), sess)

func() {
Expand Down Expand Up @@ -107,7 +107,7 @@ func makeCoffee(ctx context.Context) error {

SpotCheck(ctx, "pick-scone", pickScone)

Test(ctx, "dispose-grounds", Skippable("dispose-grounds", disposeGrounds))
Test(ctx, "dispose-grounds", disposeGrounds)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion vault/diagnose/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
status_ok = "\u001b[32m[ success ]\u001b[0m "
status_failed = "\u001b[31m[ failure ]\u001b[0m "
status_warn = "\u001b[33m[ warning ]\u001b[0m "
status_skipped = "\u001b[90m[ skip ]\u001b[0m "
status_skipped = "\u001b[90m[ skipped ]\u001b[0m "
same_line = "\x0d"
ErrorStatus = 2
WarningStatus = 1
Expand Down

0 comments on commit f6e3589

Please sign in to comment.