2021-04-29 18:32:41 +00:00
|
|
|
package diagnose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-11 00:55:43 +00:00
|
|
|
"fmt"
|
2021-05-22 02:21:11 +00:00
|
|
|
"io"
|
2021-05-11 00:55:43 +00:00
|
|
|
"time"
|
|
|
|
|
2021-04-29 18:32:41 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
warningEventName = "warning"
|
2021-05-12 17:54:40 +00:00
|
|
|
skippedEventName = "skipped"
|
2021-04-29 18:32:41 +00:00
|
|
|
actionKey = "actionKey"
|
|
|
|
spotCheckOkEventName = "spot-check-ok"
|
|
|
|
spotCheckWarnEventName = "spot-check-warn"
|
|
|
|
spotCheckErrorEventName = "spot-check-error"
|
|
|
|
errorMessageKey = attribute.Key("error.message")
|
|
|
|
nameKey = attribute.Key("name")
|
|
|
|
messageKey = attribute.Key("message")
|
|
|
|
)
|
|
|
|
|
2021-05-22 02:21:11 +00:00
|
|
|
var (
|
|
|
|
MainSection = trace.WithAttributes(attribute.Key("diagnose").String("main-section"))
|
|
|
|
)
|
|
|
|
|
2021-04-29 18:32:41 +00:00
|
|
|
var diagnoseSession = struct{}{}
|
|
|
|
var noopTracer = trace.NewNoopTracerProvider().Tracer("vault-diagnose")
|
|
|
|
|
2021-05-12 17:54:40 +00:00
|
|
|
type testFunction func(context.Context) error
|
|
|
|
|
2021-04-29 18:32:41 +00:00
|
|
|
type Session struct {
|
|
|
|
tc *TelemetryCollector
|
|
|
|
tracer trace.Tracer
|
|
|
|
tp *sdktrace.TracerProvider
|
2021-05-12 17:54:40 +00:00
|
|
|
skip map[string]bool
|
2021-04-29 18:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New initializes a Diagnose tracing session. In particular this wires a TelemetryCollector, which
|
|
|
|
// synchronously receives and tracks OpenTelemetry spans in order to provide a tree structure of results
|
|
|
|
// when the outermost span ends.
|
2021-05-22 02:21:11 +00:00
|
|
|
func New(w io.Writer) *Session {
|
|
|
|
tc := NewTelemetryCollector(w)
|
2021-04-29 18:32:41 +00:00
|
|
|
//so, _ := stdout.NewExporter(stdout.WithPrettyPrint())
|
|
|
|
tp := sdktrace.NewTracerProvider(
|
|
|
|
sdktrace.WithSampler(sdktrace.AlwaysSample()),
|
|
|
|
//sdktrace.WithSpanProcessor(sdktrace.NewSimpleSpanProcessor(so)),
|
|
|
|
sdktrace.WithSpanProcessor(tc),
|
|
|
|
)
|
|
|
|
tracer := tp.Tracer("vault-diagnose")
|
|
|
|
sess := &Session{
|
|
|
|
tp: tp,
|
|
|
|
tc: tc,
|
|
|
|
tracer: tracer,
|
2021-05-12 17:54:40 +00:00
|
|
|
skip: make(map[string]bool),
|
2021-04-29 18:32:41 +00:00
|
|
|
}
|
|
|
|
return sess
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:54:40 +00:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2021-04-29 18:32:41 +00:00
|
|
|
// Context returns a new context with a defined diagnose session
|
|
|
|
func Context(ctx context.Context, sess *Session) context.Context {
|
|
|
|
return context.WithValue(ctx, diagnoseSession, sess)
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:54:40 +00:00
|
|
|
// CurrentSession retrieves the active diagnose session from the context, or nil if none.
|
|
|
|
func CurrentSession(ctx context.Context) *Session {
|
|
|
|
sessionCtxVal := ctx.Value(diagnoseSession)
|
|
|
|
if sessionCtxVal != nil {
|
|
|
|
|
|
|
|
return sessionCtxVal.(*Session)
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-29 18:32:41 +00:00
|
|
|
// Finalize ends the Diagnose session, returning the root of the result tree. This will be empty until
|
|
|
|
// the outermost span ends.
|
|
|
|
func (s *Session) Finalize(ctx context.Context) *Result {
|
|
|
|
s.tp.ForceFlush(ctx)
|
|
|
|
return s.tc.RootResult
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartSpan starts a "diagnose" span, which is really just an OpenTelemetry Tracing span.
|
|
|
|
func StartSpan(ctx context.Context, spanName string, options ...trace.SpanOption) (context.Context, trace.Span) {
|
2021-05-12 17:54:40 +00:00
|
|
|
session := CurrentSession(ctx)
|
|
|
|
if session != nil {
|
2021-04-29 18:32:41 +00:00
|
|
|
return session.tracer.Start(ctx, spanName, options...)
|
|
|
|
} else {
|
|
|
|
return noopTracer.Start(ctx, spanName, options...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 02:21:23 +00:00
|
|
|
// Success sets the span to Successful (overriding any previous status) and sets the message to the input.
|
|
|
|
func Success(ctx context.Context, message string) {
|
|
|
|
span := trace.SpanFromContext(ctx)
|
|
|
|
span.SetStatus(codes.Ok, message)
|
|
|
|
}
|
|
|
|
|
2021-04-29 18:32:41 +00:00
|
|
|
// Fail records a failure in the current span
|
|
|
|
func Fail(ctx context.Context, message string) {
|
|
|
|
span := trace.SpanFromContext(ctx)
|
|
|
|
span.SetStatus(codes.Error, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error records an error in the current span (but unlike Fail, doesn't set the overall span status to Error)
|
|
|
|
func Error(ctx context.Context, err error, options ...trace.EventOption) error {
|
|
|
|
span := trace.SpanFromContext(ctx)
|
|
|
|
span.RecordError(err, options...)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:54:40 +00:00
|
|
|
// Skipped marks the current span skipped
|
|
|
|
func Skipped(ctx context.Context) {
|
|
|
|
span := trace.SpanFromContext(ctx)
|
|
|
|
span.AddEvent(skippedEventName)
|
|
|
|
}
|
|
|
|
|
2021-04-29 18:32:41 +00:00
|
|
|
// Warn records a warning on the current span
|
|
|
|
func Warn(ctx context.Context, msg string) {
|
|
|
|
span := trace.SpanFromContext(ctx)
|
|
|
|
span.AddEvent(warningEventName, trace.WithAttributes(messageKey.String(msg)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SpotOk adds an Ok result without adding a new Span. This should be used for instantaneous checks with no
|
|
|
|
// possible sub-spans
|
2021-05-22 02:21:11 +00:00
|
|
|
func SpotOk(ctx context.Context, checkName, message string, options ...trace.EventOption) {
|
|
|
|
addSpotCheckResult(ctx, spotCheckOkEventName, checkName, message, options...)
|
2021-04-29 18:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SpotWarn adds a Warning result without adding a new Span. This should be used for instantaneous checks with no
|
|
|
|
// possible sub-spans
|
2021-05-22 02:21:11 +00:00
|
|
|
func SpotWarn(ctx context.Context, checkName, message string, options ...trace.EventOption) {
|
|
|
|
addSpotCheckResult(ctx, spotCheckWarnEventName, checkName, message, options...)
|
2021-04-29 18:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SpotError adds an Error result without adding a new Span. This should be used for instantaneous checks with no
|
|
|
|
// possible sub-spans
|
2021-05-22 02:21:11 +00:00
|
|
|
func SpotError(ctx context.Context, checkName string, err error, options ...trace.EventOption) error {
|
2021-04-29 18:32:41 +00:00
|
|
|
var message string
|
|
|
|
if err != nil {
|
|
|
|
message = err.Error()
|
|
|
|
}
|
2021-05-22 02:21:11 +00:00
|
|
|
addSpotCheckResult(ctx, spotCheckErrorEventName, checkName, message, options...)
|
2021-04-29 18:32:41 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-22 02:21:11 +00:00
|
|
|
func addSpotCheckResult(ctx context.Context, eventName, checkName, message string, options ...trace.EventOption) {
|
2021-04-29 18:32:41 +00:00
|
|
|
span := trace.SpanFromContext(ctx)
|
2021-05-22 02:21:11 +00:00
|
|
|
attrs := append(options, trace.WithAttributes(nameKey.String(checkName)))
|
2021-04-29 18:32:41 +00:00
|
|
|
if message != "" {
|
|
|
|
attrs = append(attrs, trace.WithAttributes(messageKey.String(message)))
|
|
|
|
}
|
|
|
|
span.AddEvent(eventName, attrs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SpotCheck(ctx context.Context, checkName string, f func() error) error {
|
|
|
|
err := f()
|
|
|
|
if err != nil {
|
|
|
|
SpotError(ctx, checkName, err)
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
SpotOk(ctx, checkName, "")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test creates a new named span, and executes the provided function within it. If the function returns an error,
|
|
|
|
// the span is considered to have failed.
|
2021-05-12 17:54:40 +00:00
|
|
|
func Test(ctx context.Context, spanName string, function testFunction, options ...trace.SpanOption) error {
|
2021-04-29 18:32:41 +00:00
|
|
|
ctx, span := StartSpan(ctx, spanName, options...)
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
err := function(ctx)
|
|
|
|
if err != nil {
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2021-05-11 00:55:43 +00:00
|
|
|
|
|
|
|
// WithTimeout wraps a context consuming function, and when called, returns an error if the sub-function does not
|
|
|
|
// complete within the timeout, e.g.
|
|
|
|
//
|
|
|
|
// diagnose.Test(ctx, "my-span", diagnose.WithTimeout(5 * time.Second, myTestFunc))
|
2021-05-12 17:54:40 +00:00
|
|
|
func WithTimeout(d time.Duration, f testFunction) testFunction {
|
2021-05-11 00:55:43 +00:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
rch := make(chan error)
|
|
|
|
t := time.NewTimer(d)
|
|
|
|
defer t.Stop()
|
2021-05-25 22:23:20 +00:00
|
|
|
go func() { rch <- f(ctx) }()
|
2021-05-11 00:55:43 +00:00
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
return fmt.Errorf("timed out after %s", d.String())
|
|
|
|
case err := <-rch:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 17:54:40 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|