1
0
mirror of https://github.com/golang/go synced 2024-11-06 09:26:18 -07:00
go/internal/telemetry/export/export.go
Ian Cottrell a0ec867d51 internal/telemetry: add a benchmark with a null writer for comparison
Make all the benchmarks cleanly pass around the context to remove a non logging
difference.
Rename the non logging calls benchmark to Baseline

Change-Id: I24a33b0a817df403fb43c53b5f097bc1e9418af4
Reviewed-on: https://go-review.googlesource.com/c/tools/+/220520
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
2020-02-25 02:20:59 +00:00

107 lines
2.5 KiB
Go

// 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"
"sync"
"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
// given log event.
Log(context.Context, telemetry.Event)
Metric(context.Context, telemetry.MetricData)
Flush()
}
var (
exporterMu sync.Mutex
exporter = LogWriter(os.Stderr, true)
)
func SetExporter(e Exporter) {
exporterMu.Lock()
defer exporterMu.Unlock()
exporter = e
}
func AddExporters(e ...Exporter) {
exporterMu.Lock()
defer exporterMu.Unlock()
exporter = Multi(append([]Exporter{exporter}, e...)...)
}
func StartSpan(ctx context.Context, span *telemetry.Span, at time.Time) {
exporterMu.Lock()
defer exporterMu.Unlock()
span.Start = at
exporter.StartSpan(ctx, span)
}
func FinishSpan(ctx context.Context, span *telemetry.Span, at time.Time) {
exporterMu.Lock()
defer exporterMu.Unlock()
span.Finish = at
exporter.FinishSpan(ctx, span)
}
func Tag(ctx context.Context, at time.Time, tags telemetry.TagList) {
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,
})
}
func Log(ctx context.Context, event telemetry.Event) {
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)
}
func Metric(ctx context.Context, data telemetry.MetricData) {
exporterMu.Lock()
defer exporterMu.Unlock()
exporter.Metric(ctx, data)
}
func Flush() {
exporterMu.Lock()
defer exporterMu.Unlock()
exporter.Flush()
}