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
|
|
|
"os"
|
2019-08-14 10:51:42 -06:00
|
|
|
|
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
|
|
|
func init() {
|
|
|
|
event.SetExporter(LogWriter(os.Stderr, true))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2019-08-14 10:51:42 -06:00
|
|
|
return &logWriter{writer: w, onlyErrors: onlyErrors}
|
|
|
|
}
|
|
|
|
|
|
|
|
type logWriter struct {
|
|
|
|
writer io.Writer
|
|
|
|
onlyErrors bool
|
|
|
|
}
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
func (w *logWriter) ProcessEvent(ctx context.Context, ev event.Event) context.Context {
|
|
|
|
switch {
|
|
|
|
case ev.IsLog():
|
|
|
|
if w.onlyErrors && ev.Error == nil {
|
2020-03-01 16:35:55 -07:00
|
|
|
return ctx
|
|
|
|
}
|
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-01 10:16:26 -07:00
|
|
|
return ctx
|
2019-08-14 10:51:42 -06:00
|
|
|
}
|
2020-03-07 16:02:27 -07:00
|
|
|
|
|
|
|
func (w *logWriter) Metric(context.Context, event.MetricData) {}
|