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-12-20 13:04:24 -07:00
|
|
|
"sync/atomic"
|
2019-08-14 10:51:42 -06:00
|
|
|
"time"
|
2019-12-20 13:04:24 -07:00
|
|
|
"unsafe"
|
2019-08-14 10:51:42 -06:00
|
|
|
|
|
|
|
"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-17 21:26:28 -06:00
|
|
|
var (
|
2019-12-20 13:04:24 -07:00
|
|
|
exporter unsafe.Pointer
|
2019-08-17 21:26:28 -06:00
|
|
|
)
|
2019-08-14 10:51:42 -06:00
|
|
|
|
2019-12-20 13:04:24 -07:00
|
|
|
func init() {
|
|
|
|
SetExporter(LogWriter(os.Stderr, true))
|
|
|
|
}
|
|
|
|
|
2019-12-20 13:10:23 -07:00
|
|
|
func SetExporter(e Exporter) {
|
2019-12-20 13:04:24 -07:00
|
|
|
p := unsafe.Pointer(&e)
|
|
|
|
if e == nil {
|
|
|
|
p = nil
|
|
|
|
}
|
|
|
|
atomic.StorePointer(&exporter, p)
|
2019-12-20 13:10:23 -07:00
|
|
|
}
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
func StartSpan(ctx context.Context, span *telemetry.Span, at time.Time) {
|
2019-12-20 13:04:24 -07:00
|
|
|
exporterPtr := (*Exporter)(atomic.LoadPointer(&exporter))
|
|
|
|
if exporterPtr == nil {
|
2020-02-05 11:30:35 -07:00
|
|
|
return
|
|
|
|
}
|
2019-08-17 21:26:28 -06:00
|
|
|
span.Start = at
|
2019-12-20 13:04:24 -07:00
|
|
|
(*exporterPtr).StartSpan(ctx, span)
|
2019-08-14 10:51:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func FinishSpan(ctx context.Context, span *telemetry.Span, at time.Time) {
|
2019-12-20 13:04:24 -07:00
|
|
|
exporterPtr := (*Exporter)(atomic.LoadPointer(&exporter))
|
|
|
|
if exporterPtr == nil {
|
2020-02-05 11:30:35 -07:00
|
|
|
return
|
|
|
|
}
|
2019-08-17 21:26:28 -06:00
|
|
|
span.Finish = at
|
2019-12-20 13:04:24 -07:00
|
|
|
(*exporterPtr).FinishSpan(ctx, span)
|
2019-08-14 10:51:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func Tag(ctx context.Context, at time.Time, tags telemetry.TagList) {
|
2019-12-20 13:04:24 -07:00
|
|
|
exporterPtr := (*Exporter)(atomic.LoadPointer(&exporter))
|
|
|
|
if exporterPtr == nil {
|
2020-02-05 11:30:35 -07:00
|
|
|
return
|
|
|
|
}
|
2019-08-17 21:26:28 -06:00
|
|
|
// 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-12-20 13:04:24 -07:00
|
|
|
exporterPtr := (*Exporter)(atomic.LoadPointer(&exporter))
|
|
|
|
if exporterPtr == nil {
|
2020-02-05 11:30:35 -07:00
|
|
|
return
|
|
|
|
}
|
2019-08-17 21:26:28 -06:00
|
|
|
// 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
|
2019-12-20 13:04:24 -07:00
|
|
|
(*exporterPtr).Log(ctx, event)
|
2019-08-14 10:51:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func Metric(ctx context.Context, data telemetry.MetricData) {
|
2019-12-20 13:04:24 -07:00
|
|
|
exporterPtr := (*Exporter)(atomic.LoadPointer(&exporter))
|
|
|
|
if exporterPtr == nil {
|
2020-02-05 11:30:35 -07:00
|
|
|
return
|
|
|
|
}
|
2019-12-20 13:04:24 -07:00
|
|
|
(*exporterPtr).Metric(ctx, data)
|
2019-08-14 10:51:42 -06:00
|
|
|
}
|