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 {
|
2020-03-01 10:16:26 -07:00
|
|
|
// ProcessEvent is a function that handles all events.
|
|
|
|
// Exporters may use information in the context to decide what to do with a
|
|
|
|
// given event.
|
|
|
|
ProcessEvent(context.Context, telemetry.Event) context.Context
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
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 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
|
2020-03-01 16:35:55 -07:00
|
|
|
span := GetSpan(ctx)
|
2019-08-17 21:26:28 -06:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-01 10:16:26 -07:00
|
|
|
func ProcessEvent(ctx context.Context, event telemetry.Event) context.Context {
|
2019-12-20 13:04:24 -07:00
|
|
|
exporterPtr := (*Exporter)(atomic.LoadPointer(&exporter))
|
|
|
|
if exporterPtr == nil {
|
2020-03-01 10:16:26 -07:00
|
|
|
return ctx
|
2020-02-05 11:30:35 -07:00
|
|
|
}
|
2020-03-01 10:16:26 -07:00
|
|
|
// and now also hand the event of to the current exporter
|
|
|
|
return (*exporterPtr).ProcessEvent(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
|
|
|
}
|