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-07-11 22:44:19 +00:00
|
|
|
"strings"
|
2021-05-11 00:55:43 +00:00
|
|
|
"time"
|
|
|
|
|
2021-07-13 23:25:04 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/strutil"
|
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 (
|
2021-06-02 17:17:52 +00:00
|
|
|
warningEventName = "warning"
|
|
|
|
skippedEventName = "skipped"
|
|
|
|
actionKey = "actionKey"
|
|
|
|
spotCheckOkEventName = "spot-check-ok"
|
|
|
|
spotCheckWarnEventName = "spot-check-warn"
|
|
|
|
spotCheckErrorEventName = "spot-check-error"
|
|
|
|
spotCheckSkippedEventName = "spot-check-skipped"
|
2021-06-07 16:29:36 +00:00
|
|
|
adviceEventName = "advice"
|
2021-06-02 17:17:52 +00:00
|
|
|
errorMessageKey = attribute.Key("error.message")
|
|
|
|
nameKey = attribute.Key("name")
|
|
|
|
messageKey = attribute.Key("message")
|
2021-06-07 16:29:36 +00:00
|
|
|
adviceKey = attribute.Key("advice")
|
2021-04-29 18:32:41 +00:00
|
|
|
)
|
|
|
|
|
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 {
|
2021-07-13 23:25:04 +00:00
|
|
|
tc *TelemetryCollector
|
|
|
|
tracer trace.Tracer
|
|
|
|
tp *sdktrace.TracerProvider
|
|
|
|
SkipFilters []string
|
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,
|
|
|
|
}
|
|
|
|
return sess
|
|
|
|
}
|
|
|
|
|
2021-07-13 23:25:04 +00:00
|
|
|
// 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)
|
2021-05-12 17:54:40 +00:00
|
|
|
}
|
|
|
|
|
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
|
2021-06-03 16:01:14 +00:00
|
|
|
func Skipped(ctx context.Context, message string) {
|
2021-05-12 17:54:40 +00:00
|
|
|
span := trace.SpanFromContext(ctx)
|
|
|
|
span.AddEvent(skippedEventName)
|
2021-06-03 16:01:14 +00:00
|
|
|
span.SetStatus(codes.Error, message)
|
2021-05-12 17:54:40 +00:00
|
|
|
}
|
|
|
|
|
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-06-02 17:17:52 +00:00
|
|
|
// SpotSkipped adds a Skipped result without adding a new Span.
|
|
|
|
func SpotSkipped(ctx context.Context, checkName, message string, options ...trace.EventOption) {
|
|
|
|
addSpotCheckResult(ctx, spotCheckSkippedEventName, checkName, message, options...)
|
|
|
|
}
|
|
|
|
|
2021-06-07 16:29:36 +00:00
|
|
|
// Advice builds an EventOption containing advice message. Use to add to spot results.
|
|
|
|
func Advice(message string) trace.EventOption {
|
|
|
|
return trace.WithAttributes(adviceKey.String(message))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advise adds advice to the current diagnose span
|
|
|
|
func Advise(ctx context.Context, message string) {
|
|
|
|
span := trace.SpanFromContext(ctx)
|
|
|
|
span.AddEvent(adviceEventName, Advice(message))
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-07-13 23:25:04 +00:00
|
|
|
sess := CurrentSession(ctx)
|
|
|
|
if sess.IsSkipped(checkName) {
|
|
|
|
SpotSkipped(ctx, checkName, "skipped as requested")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-29 18:32:41 +00:00
|
|
|
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()
|
2021-07-13 23:25:04 +00:00
|
|
|
sess := CurrentSession(ctx)
|
|
|
|
if sess.IsSkipped(spanName) {
|
|
|
|
Skipped(ctx, "skipped as requested")
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-29 18:32:41 +00:00
|
|
|
|
|
|
|
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:
|
2021-07-11 22:44:19 +00:00
|
|
|
return fmt.Errorf("Timeout after %s.", d.String())
|
2021-05-11 00:55:43 +00:00
|
|
|
case err := <-rch:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 17:54:40 +00:00
|
|
|
|
2021-07-11 22:44:19 +00:00
|
|
|
// CapitalizeFirstLetter returns a string with the first letter capitalized
|
|
|
|
func CapitalizeFirstLetter(msg string) string {
|
|
|
|
words := strings.Split(msg, " ")
|
|
|
|
if len(words) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if len(words) > 1 {
|
|
|
|
return strings.Title(words[0]) + " " + strings.Join(words[1:], " ")
|
|
|
|
}
|
|
|
|
return strings.Title(words[0])
|
|
|
|
}
|