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 holds the definition of the telemetry Exporter interface,
|
|
|
|
// along with some simple implementations.
|
|
|
|
// Larger more complex exporters are in sub packages of their own.
|
|
|
|
package export
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
2019-08-17 21:26:28 -06:00
|
|
|
"sync"
|
2019-08-14 10:51:42 -06:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/telemetry"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Exporter interface {
|
|
|
|
StartSpan(context.Context, *telemetry.Span)
|
|
|
|
FinishSpan(context.Context, *telemetry.Span)
|
|
|
|
|
|
|
|
// Log is a function that handles logging events.
|
|
|
|
// Observers may use information in the context to decide what to do with a
|
2019-12-20 13:05:54 -07:00
|
|
|
// given log event.
|
2019-08-14 10:51:42 -06:00
|
|
|
Log(context.Context, telemetry.Event)
|
|
|
|
|
|
|
|
Metric(context.Context, telemetry.MetricData)
|
2019-08-19 08:41:38 -06:00
|
|
|
|
|
|
|
Flush()
|
2019-08-14 10:51:42 -06:00
|
|
|
}
|
|
|
|
|
2019-08-17 21:26:28 -06:00
|
|
|
var (
|
|
|
|
exporterMu sync.Mutex
|
|
|
|
exporter = LogWriter(os.Stderr, true)
|
|
|
|
)
|
2019-08-14 10:51:42 -06:00
|
|
|
|
2019-12-20 13:10:23 -07:00
|
|
|
func SetExporter(e Exporter) {
|
|
|
|
exporterMu.Lock()
|
|
|
|
defer exporterMu.Unlock()
|
|
|
|
exporter = e
|
|
|
|
}
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
func AddExporters(e ...Exporter) {
|
2019-08-17 21:26:28 -06:00
|
|
|
exporterMu.Lock()
|
|
|
|
defer exporterMu.Unlock()
|
|
|
|
exporter = Multi(append([]Exporter{exporter}, e...)...)
|
2019-08-14 10:51:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func StartSpan(ctx context.Context, span *telemetry.Span, at time.Time) {
|
2019-08-17 21:26:28 -06:00
|
|
|
exporterMu.Lock()
|
|
|
|
defer exporterMu.Unlock()
|
|
|
|
span.Start = at
|
|
|
|
exporter.StartSpan(ctx, span)
|
2019-08-14 10:51:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func FinishSpan(ctx context.Context, span *telemetry.Span, at time.Time) {
|
2019-08-17 21:26:28 -06:00
|
|
|
exporterMu.Lock()
|
|
|
|
defer exporterMu.Unlock()
|
|
|
|
span.Finish = at
|
|
|
|
exporter.FinishSpan(ctx, span)
|
2019-08-14 10:51:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func Tag(ctx context.Context, at time.Time, tags telemetry.TagList) {
|
2019-08-17 21:26:28 -06:00
|
|
|
exporterMu.Lock()
|
|
|
|
defer exporterMu.Unlock()
|
|
|
|
// If context has a span we need to add the tags to it
|
|
|
|
span := telemetry.GetSpan(ctx)
|
|
|
|
if span == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if span.Start.IsZero() {
|
|
|
|
// span still being created, tag it directly
|
|
|
|
span.Tags = append(span.Tags, tags...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// span in progress, add an event to the span
|
|
|
|
span.Events = append(span.Events, telemetry.Event{
|
|
|
|
At: at,
|
|
|
|
Tags: tags,
|
2019-08-14 10:51:42 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Log(ctx context.Context, event telemetry.Event) {
|
2019-08-17 21:26:28 -06:00
|
|
|
exporterMu.Lock()
|
|
|
|
defer exporterMu.Unlock()
|
|
|
|
// If context has a span we need to add the event to it
|
|
|
|
span := telemetry.GetSpan(ctx)
|
|
|
|
if span != nil {
|
|
|
|
span.Events = append(span.Events, event)
|
|
|
|
}
|
|
|
|
// and now also hand the event of to the current observer
|
|
|
|
exporter.Log(ctx, event)
|
2019-08-14 10:51:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func Metric(ctx context.Context, data telemetry.MetricData) {
|
2019-08-17 21:26:28 -06:00
|
|
|
exporterMu.Lock()
|
|
|
|
defer exporterMu.Unlock()
|
|
|
|
exporter.Metric(ctx, data)
|
2019-08-14 10:51:42 -06:00
|
|
|
}
|
2019-08-19 08:41:38 -06:00
|
|
|
|
|
|
|
func Flush() {
|
|
|
|
exporterMu.Lock()
|
|
|
|
defer exporterMu.Unlock()
|
|
|
|
exporter.Flush()
|
|
|
|
}
|