2019-08-14 10:51:42 -06:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package export
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
"golang.org/x/tools/internal/telemetry/event"
|
2019-08-14 10:51:42 -06:00
|
|
|
)
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
// LogWriter returns an Exporter that logs events to the supplied writer.
|
2019-08-14 10:51:42 -06:00
|
|
|
// If onlyErrors is true it does not log any event that did not have an
|
|
|
|
// associated error.
|
|
|
|
// It ignores all telemetry other than log events.
|
2020-03-07 16:02:27 -07:00
|
|
|
func LogWriter(w io.Writer, onlyErrors bool) event.Exporter {
|
2020-03-18 21:28:57 -06:00
|
|
|
lw := &logWriter{writer: w, onlyErrors: onlyErrors}
|
|
|
|
return lw.ProcessEvent
|
2019-08-14 10:51:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type logWriter struct {
|
|
|
|
writer io.Writer
|
|
|
|
onlyErrors bool
|
|
|
|
}
|
|
|
|
|
2020-03-20 06:29:48 -06:00
|
|
|
func (w *logWriter) ProcessEvent(ctx context.Context, ev event.Event, tagMap event.TagMap) context.Context {
|
2020-03-07 16:02:27 -07:00
|
|
|
switch {
|
|
|
|
case ev.IsLog():
|
|
|
|
if w.onlyErrors && ev.Error == nil {
|
2020-03-20 06:29:48 -06:00
|
|
|
return ctx
|
2020-03-01 16:35:55 -07:00
|
|
|
}
|
2020-03-07 16:02:27 -07:00
|
|
|
fmt.Fprintf(w.writer, "%v\n", ev)
|
|
|
|
case ev.IsStartSpan():
|
2020-03-01 16:35:55 -07:00
|
|
|
if span := GetSpan(ctx); span != nil {
|
|
|
|
fmt.Fprintf(w.writer, "start: %v %v", span.Name, span.ID)
|
|
|
|
if span.ParentID.IsValid() {
|
|
|
|
fmt.Fprintf(w.writer, "[%v]", span.ParentID)
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 16:02:27 -07:00
|
|
|
case ev.IsEndSpan():
|
2020-03-01 16:35:55 -07:00
|
|
|
if span := GetSpan(ctx); span != nil {
|
|
|
|
fmt.Fprintf(w.writer, "finish: %v %v", span.Name, span.ID)
|
|
|
|
}
|
2019-08-14 10:51:42 -06:00
|
|
|
}
|
2020-03-20 06:29:48 -06:00
|
|
|
return ctx
|
2019-08-14 10:51:42 -06:00
|
|
|
}
|