2021-04-29 18:32:41 +00:00
|
|
|
package diagnose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2021-05-22 02:21:11 +00:00
|
|
|
"fmt"
|
2021-04-29 18:32:41 +00:00
|
|
|
"io"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-06-24 17:43:49 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
|
2021-06-07 16:29:36 +00:00
|
|
|
wordwrap "github.com/mitchellh/go-wordwrap"
|
2021-04-29 18:32:41 +00:00
|
|
|
"go.opentelemetry.io/otel/codes"
|
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
status_unknown = "[ ] "
|
2021-07-11 22:44:19 +00:00
|
|
|
status_ok = "\u001b[32m[ success ]\u001b[0m "
|
|
|
|
status_failed = "\u001b[31m[ failure ]\u001b[0m "
|
|
|
|
status_warn = "\u001b[33m[ warning ]\u001b[0m "
|
2021-07-13 23:25:04 +00:00
|
|
|
status_skipped = "\u001b[90m[ skipped ]\u001b[0m "
|
2021-05-22 02:21:11 +00:00
|
|
|
same_line = "\x0d"
|
|
|
|
ErrorStatus = 2
|
|
|
|
WarningStatus = 1
|
|
|
|
OkStatus = 0
|
|
|
|
SkippedStatus = -1
|
2021-04-29 18:32:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var errUnimplemented = errors.New("unimplemented")
|
|
|
|
|
2021-05-22 02:21:11 +00:00
|
|
|
type status int
|
|
|
|
|
|
|
|
func (s status) String() string {
|
|
|
|
switch s {
|
|
|
|
case OkStatus:
|
|
|
|
return "ok"
|
|
|
|
case WarningStatus:
|
|
|
|
return "warn"
|
|
|
|
case ErrorStatus:
|
|
|
|
return "fail"
|
|
|
|
}
|
|
|
|
return "invalid"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s status) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprint("\"", s.String(), "\"")), nil
|
|
|
|
}
|
|
|
|
|
2021-04-29 18:32:41 +00:00
|
|
|
type Result struct {
|
2021-05-22 02:21:11 +00:00
|
|
|
Time time.Time `json:"time"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Status status `json:"status"`
|
|
|
|
Warnings []string `json:"warnings,omitempty"`
|
|
|
|
Message string `json:"message,omitempty"`
|
2021-06-07 16:29:36 +00:00
|
|
|
Advice string
|
2021-05-22 02:21:11 +00:00
|
|
|
Children []*Result `json:"children,omitempty"`
|
2021-04-29 18:32:41 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 02:21:11 +00:00
|
|
|
func (r *Result) finalize() status {
|
|
|
|
maxStatus := r.Status
|
2021-04-29 18:32:41 +00:00
|
|
|
if len(r.Children) > 0 {
|
|
|
|
sort.SliceStable(r.Children, func(i, j int) bool {
|
|
|
|
return r.Children[i].Time.Before(r.Children[j].Time)
|
|
|
|
})
|
|
|
|
for _, c := range r.Children {
|
2021-05-22 02:21:11 +00:00
|
|
|
cms := c.finalize()
|
|
|
|
if cms > maxStatus {
|
|
|
|
maxStatus = cms
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if maxStatus > r.Status {
|
|
|
|
r.Status = maxStatus
|
2021-04-29 18:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-22 02:21:11 +00:00
|
|
|
return maxStatus
|
2021-04-29 18:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Result) ZeroTimes() {
|
|
|
|
var zero time.Time
|
|
|
|
r.Time = zero
|
|
|
|
for _, c := range r.Children {
|
|
|
|
c.ZeroTimes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TelemetryCollector is an otel SpanProcessor that gathers spans and once the outermost
|
|
|
|
// span ends, walks the otel traces in order to produce a top-down tree of Diagnose results.
|
|
|
|
type TelemetryCollector struct {
|
2021-05-22 02:21:11 +00:00
|
|
|
ui io.Writer
|
2021-04-29 18:32:41 +00:00
|
|
|
spans map[trace.SpanID]sdktrace.ReadOnlySpan
|
|
|
|
rootSpan sdktrace.ReadOnlySpan
|
|
|
|
results map[trace.SpanID]*Result
|
|
|
|
RootResult *Result
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
2021-05-22 02:21:11 +00:00
|
|
|
// NewTelemetryCollector creates a SpanProcessor that collects OpenTelemetry spans
|
|
|
|
// and aggregates them into a tree structure for use by Diagnose.
|
|
|
|
// It also outputs the status of main sections to that writer.
|
|
|
|
func NewTelemetryCollector(w io.Writer) *TelemetryCollector {
|
2021-04-29 18:32:41 +00:00
|
|
|
return &TelemetryCollector{
|
2021-05-22 02:21:11 +00:00
|
|
|
ui: w,
|
2021-04-29 18:32:41 +00:00
|
|
|
spans: make(map[trace.SpanID]sdktrace.ReadOnlySpan),
|
|
|
|
results: make(map[trace.SpanID]*Result),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnStart tracks spans by id for later retrieval
|
|
|
|
func (t *TelemetryCollector) OnStart(_ context.Context, s sdktrace.ReadWriteSpan) {
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
t.spans[s.SpanContext().SpanID()] = s
|
2021-05-22 02:21:11 +00:00
|
|
|
if isMainSection(s) {
|
|
|
|
fmt.Fprintf(t.ui, status_unknown+s.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isMainSection(s sdktrace.ReadOnlySpan) bool {
|
|
|
|
for _, a := range s.Attributes() {
|
|
|
|
if a.Key == "diagnose" && a.Value.AsString() == "main-section" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2021-04-29 18:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TelemetryCollector) OnEnd(e sdktrace.ReadOnlySpan) {
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
if !e.Parent().HasSpanID() {
|
|
|
|
// First walk the span structs to construct the top down tree results we want
|
|
|
|
for _, s := range t.spans {
|
|
|
|
r := t.getOrBuildResult(s.SpanContext().SpanID())
|
|
|
|
if r != nil {
|
|
|
|
if s.Parent().HasSpanID() {
|
|
|
|
p := t.getOrBuildResult(s.Parent().SpanID())
|
|
|
|
if p != nil {
|
|
|
|
p.Children = append(p.Children, r)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.RootResult = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then walk the results sorting children by time
|
2021-05-22 02:21:11 +00:00
|
|
|
t.RootResult.finalize()
|
|
|
|
} else if isMainSection(e) {
|
|
|
|
r := t.getOrBuildResult(e.SpanContext().SpanID())
|
|
|
|
if r != nil {
|
|
|
|
fmt.Print(same_line)
|
|
|
|
fmt.Fprintln(t.ui, r.String())
|
|
|
|
}
|
2021-04-29 18:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// required to implement SpanProcessor, but noops for our purposes
|
|
|
|
func (t *TelemetryCollector) Shutdown(_ context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// required to implement SpanProcessor, but noops for our purposes
|
|
|
|
func (t *TelemetryCollector) ForceFlush(_ context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TelemetryCollector) getOrBuildResult(id trace.SpanID) *Result {
|
|
|
|
s := t.spans[id]
|
|
|
|
if s == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r, ok := t.results[id]
|
|
|
|
if !ok {
|
|
|
|
r = &Result{
|
|
|
|
Name: s.Name(),
|
|
|
|
Message: s.StatusMessage(),
|
|
|
|
Time: s.StartTime(),
|
|
|
|
}
|
|
|
|
for _, e := range s.Events() {
|
|
|
|
switch e.Name {
|
|
|
|
case warningEventName:
|
|
|
|
for _, a := range e.Attributes {
|
|
|
|
if a.Key == messageKey {
|
|
|
|
r.Warnings = append(r.Warnings, a.Value.AsString())
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 17:54:40 +00:00
|
|
|
case skippedEventName:
|
|
|
|
r.Status = SkippedStatus
|
2021-05-22 02:21:11 +00:00
|
|
|
case "fail":
|
2021-06-07 16:29:36 +00:00
|
|
|
message, action := findAttributes(e, errorMessageKey, actionKey)
|
2021-04-29 18:32:41 +00:00
|
|
|
if message != "" && action != "" {
|
|
|
|
r.Children = append(r.Children, &Result{
|
|
|
|
Name: action,
|
|
|
|
Status: ErrorStatus,
|
|
|
|
Message: message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
case spotCheckOkEventName:
|
2021-06-07 16:29:36 +00:00
|
|
|
checkName, message := findAttributes(e, nameKey, messageKey)
|
2021-04-29 18:32:41 +00:00
|
|
|
if checkName != "" {
|
|
|
|
r.Children = append(r.Children,
|
|
|
|
&Result{
|
|
|
|
Name: checkName,
|
|
|
|
Status: OkStatus,
|
|
|
|
Message: message,
|
|
|
|
Time: e.Time,
|
2021-07-15 23:01:51 +00:00
|
|
|
Advice: findAttribute(e, adviceKey),
|
2021-04-29 18:32:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
case spotCheckWarnEventName:
|
2021-06-07 16:29:36 +00:00
|
|
|
checkName, message := findAttributes(e, nameKey, messageKey)
|
2021-04-29 18:32:41 +00:00
|
|
|
if checkName != "" {
|
|
|
|
r.Children = append(r.Children,
|
|
|
|
&Result{
|
|
|
|
Name: checkName,
|
|
|
|
Status: WarningStatus,
|
|
|
|
Message: message,
|
|
|
|
Time: e.Time,
|
2021-07-15 23:01:51 +00:00
|
|
|
Advice: findAttribute(e, adviceKey),
|
2021-04-29 18:32:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
case spotCheckErrorEventName:
|
2021-06-07 16:29:36 +00:00
|
|
|
checkName, message := findAttributes(e, nameKey, messageKey)
|
2021-04-29 18:32:41 +00:00
|
|
|
if checkName != "" {
|
|
|
|
r.Children = append(r.Children,
|
|
|
|
&Result{
|
|
|
|
Name: checkName,
|
|
|
|
Status: ErrorStatus,
|
|
|
|
Message: message,
|
|
|
|
Time: e.Time,
|
2021-07-15 23:01:51 +00:00
|
|
|
Advice: findAttribute(e, adviceKey),
|
2021-04-29 18:32:41 +00:00
|
|
|
})
|
|
|
|
}
|
2021-06-02 17:17:52 +00:00
|
|
|
case spotCheckSkippedEventName:
|
2021-06-07 16:29:36 +00:00
|
|
|
checkName, message := findAttributes(e, nameKey, messageKey)
|
2021-06-02 17:17:52 +00:00
|
|
|
if checkName != "" {
|
|
|
|
r.Children = append(r.Children,
|
|
|
|
&Result{
|
|
|
|
Name: checkName,
|
|
|
|
Status: SkippedStatus,
|
|
|
|
Message: message,
|
|
|
|
Time: e.Time,
|
2021-07-15 23:01:51 +00:00
|
|
|
Advice: findAttribute(e, adviceKey),
|
2021-06-02 17:17:52 +00:00
|
|
|
})
|
|
|
|
}
|
2021-06-07 16:29:36 +00:00
|
|
|
case adviceEventName:
|
|
|
|
message, _ := findAttributes(e, adviceKey, "")
|
|
|
|
if message != "" {
|
|
|
|
r.Advice = message
|
|
|
|
}
|
2021-04-29 18:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
switch s.StatusCode() {
|
|
|
|
case codes.Unset:
|
|
|
|
if len(r.Warnings) > 0 {
|
|
|
|
r.Status = WarningStatus
|
2021-05-12 17:54:40 +00:00
|
|
|
} else if r.Status != SkippedStatus {
|
2021-04-29 18:32:41 +00:00
|
|
|
r.Status = OkStatus
|
|
|
|
}
|
|
|
|
case codes.Ok:
|
2021-05-12 17:54:40 +00:00
|
|
|
if r.Status != SkippedStatus {
|
|
|
|
r.Status = OkStatus
|
|
|
|
}
|
2021-04-29 18:32:41 +00:00
|
|
|
case codes.Error:
|
2021-06-03 16:01:14 +00:00
|
|
|
if r.Status != SkippedStatus {
|
|
|
|
r.Status = ErrorStatus
|
|
|
|
}
|
2021-04-29 18:32:41 +00:00
|
|
|
}
|
|
|
|
t.results[id] = r
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2021-07-15 23:01:51 +00:00
|
|
|
func findAttribute(e trace.Event, attr attribute.Key) string {
|
|
|
|
for _, a := range e.Attributes {
|
|
|
|
if a.Key == attr {
|
|
|
|
return a.Value.AsString()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-06-07 16:29:36 +00:00
|
|
|
func findAttributes(e trace.Event, attr1, attr2 attribute.Key) (string, string) {
|
|
|
|
var av1, av2 string
|
|
|
|
for _, a := range e.Attributes {
|
|
|
|
switch a.Key {
|
|
|
|
case attr1:
|
|
|
|
av1 = a.Value.AsString()
|
|
|
|
case attr2:
|
|
|
|
av2 = a.Value.AsString()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return av1, av2
|
|
|
|
}
|
|
|
|
|
2021-04-29 18:32:41 +00:00
|
|
|
// Write outputs a human readable version of the results tree
|
2021-06-07 16:29:36 +00:00
|
|
|
func (r *Result) Write(writer io.Writer, wrapLimit int) error {
|
2021-04-29 18:32:41 +00:00
|
|
|
var sb strings.Builder
|
2021-06-07 16:29:36 +00:00
|
|
|
r.write(&sb, 0, wrapLimit)
|
2021-04-29 18:32:41 +00:00
|
|
|
_, err := writer.Write([]byte(sb.String()))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-07 16:29:36 +00:00
|
|
|
const (
|
|
|
|
indentString = " "
|
|
|
|
statusPrefixLen = 9
|
|
|
|
)
|
|
|
|
|
|
|
|
func indent(sb *strings.Builder, depth int) {
|
2021-05-22 02:21:11 +00:00
|
|
|
for i := 0; i < depth; i++ {
|
2021-06-07 16:29:36 +00:00
|
|
|
sb.WriteString(indentString)
|
2021-05-22 02:21:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Result) String() string {
|
2021-06-07 16:29:36 +00:00
|
|
|
return r.StringWrapped(80)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Result) StringWrapped(wrapLimit int) string {
|
2021-05-22 02:21:11 +00:00
|
|
|
var sb strings.Builder
|
2021-06-07 16:29:36 +00:00
|
|
|
r.write(&sb, 0, wrapLimit)
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Result) write(sb *strings.Builder, depth int, limit int) {
|
|
|
|
indent(sb, depth)
|
|
|
|
var prelude string
|
2021-06-24 17:43:49 +00:00
|
|
|
switch r.Status {
|
|
|
|
case OkStatus:
|
|
|
|
prelude = status_ok
|
|
|
|
case WarningStatus:
|
|
|
|
prelude = status_warn
|
|
|
|
case ErrorStatus:
|
|
|
|
prelude = status_failed
|
|
|
|
case SkippedStatus:
|
|
|
|
prelude = status_skipped
|
|
|
|
}
|
|
|
|
prelude = prelude + r.Name
|
2021-04-29 18:32:41 +00:00
|
|
|
|
2021-06-24 17:43:49 +00:00
|
|
|
if r.Message != "" {
|
|
|
|
prelude = prelude + ": " + r.Message
|
2021-04-29 18:32:41 +00:00
|
|
|
}
|
2021-05-22 02:21:11 +00:00
|
|
|
warnings := r.Warnings
|
|
|
|
if r.Message == "" && len(warnings) > 0 {
|
2021-06-17 18:09:37 +00:00
|
|
|
prelude = status_warn + r.Name + ": "
|
|
|
|
if len(warnings) == 1 {
|
|
|
|
prelude = prelude + warnings[0]
|
|
|
|
warnings = warnings[1:]
|
|
|
|
}
|
2021-05-22 02:21:11 +00:00
|
|
|
}
|
2021-06-24 17:43:49 +00:00
|
|
|
|
2021-06-07 16:29:36 +00:00
|
|
|
writeWrapped(sb, prelude, depth+1, limit)
|
2021-05-22 02:21:11 +00:00
|
|
|
for _, w := range warnings {
|
2021-04-29 18:32:41 +00:00
|
|
|
sb.WriteRune('\n')
|
2021-06-17 18:09:37 +00:00
|
|
|
indent(sb, depth+1)
|
2021-04-29 18:32:41 +00:00
|
|
|
sb.WriteString(status_warn)
|
2021-06-17 18:09:37 +00:00
|
|
|
writeWrapped(sb, w, depth+2, limit)
|
2021-04-29 18:32:41 +00:00
|
|
|
}
|
2021-05-22 02:21:11 +00:00
|
|
|
|
2021-06-07 16:29:36 +00:00
|
|
|
if r.Advice != "" {
|
2021-07-11 22:44:19 +00:00
|
|
|
advice := "\u001b[35m" + r.Advice + "\u001b[0m"
|
2021-06-07 16:29:36 +00:00
|
|
|
sb.WriteRune('\n')
|
2021-07-11 22:44:19 +00:00
|
|
|
indent(sb, depth+1)
|
|
|
|
writeWrapped(sb, advice, depth+1, limit)
|
2021-06-07 16:29:36 +00:00
|
|
|
}
|
|
|
|
sb.WriteRune('\n')
|
|
|
|
for _, c := range r.Children {
|
|
|
|
c.write(sb, depth+1, limit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeWrapped(sb *strings.Builder, msg string, depth int, limit int) {
|
|
|
|
if limit > 0 {
|
|
|
|
sz := uint(limit - depth*len(indentString))
|
|
|
|
msg = wordwrap.WrapString(msg, sz)
|
|
|
|
parts := strings.Split(msg, "\n")
|
|
|
|
sb.WriteString(parts[0])
|
|
|
|
for _, p := range parts[1:] {
|
|
|
|
sb.WriteRune('\n')
|
|
|
|
indent(sb, depth)
|
|
|
|
sb.WriteString(p)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sb.WriteString(msg)
|
|
|
|
}
|
2021-04-29 18:32:41 +00:00
|
|
|
}
|