Skip to content

Commit 2e887e3

Browse files
committed
internal/telemetry: removing the concept of exporter lists
Instead we only have a single exporter, and it must delegate behaviour to any other exporters it wants to include. This removes a whole collection of suprises caused by init functions adding new exporters to a list, as well as generally making things faster, at the small expense of needing to implement a custom exporter if you want to combine the features of a few other exporters. This is essentially the opposite of https://go-review.googlesource.com/c/tools/+/212243 which will now be abandoned in favor of this approach. Change-Id: Icacb4c1f0f40f99ddd1d82c73d4f25a3486e56ce Reviewed-on: https://go-review.googlesource.com/c/tools/+/220857 Run-TryBot: Ian Cottrell <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Emmanuel Odeke <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent eb7c562 commit 2e887e3

File tree

10 files changed

+75
-119
lines changed

10 files changed

+75
-119
lines changed

internal/lsp/debug/rpc.go

-5
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,6 @@ type rpcCodeBucket struct {
9090
Count int64
9191
}
9292

93-
func (r *rpcs) StartSpan(ctx context.Context, span *telemetry.Span) {}
94-
func (r *rpcs) FinishSpan(ctx context.Context, span *telemetry.Span) {}
95-
func (r *rpcs) Log(ctx context.Context, event telemetry.Event) {}
96-
func (r *rpcs) Flush() {}
97-
9893
func (r *rpcs) Metric(ctx context.Context, data telemetry.MetricData) {
9994
for i, group := range data.Groups() {
10095
set := &r.Inbound

internal/lsp/debug/serve.go

+54-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import (
2727
"sync"
2828
"time"
2929

30+
"golang.org/x/tools/internal/lsp/protocol"
3031
"golang.org/x/tools/internal/span"
32+
"golang.org/x/tools/internal/telemetry"
3133
"golang.org/x/tools/internal/telemetry/export"
3234
"golang.org/x/tools/internal/telemetry/export/ocagent"
3335
"golang.org/x/tools/internal/telemetry/export/prometheus"
@@ -368,7 +370,7 @@ func NewInstance(workdir, agent string) *Instance {
368370
i.rpcs = &rpcs{}
369371
i.traces = &traces{}
370372
i.State = &State{}
371-
export.AddExporters(i.ocagent, i.prometheus, i.rpcs, i.traces)
373+
export.SetExporter(i)
372374
return i
373375
}
374376

@@ -493,6 +495,57 @@ func (i *Instance) writeMemoryDebug(threshold uint64) error {
493495
return nil
494496
}
495497

498+
func (i *Instance) StartSpan(ctx context.Context, spn *telemetry.Span) {
499+
if i.ocagent != nil {
500+
i.ocagent.StartSpan(ctx, spn)
501+
}
502+
if i.traces != nil {
503+
i.traces.StartSpan(ctx, spn)
504+
}
505+
}
506+
507+
func (i *Instance) FinishSpan(ctx context.Context, spn *telemetry.Span) {
508+
if i.ocagent != nil {
509+
i.ocagent.FinishSpan(ctx, spn)
510+
}
511+
if i.traces != nil {
512+
i.traces.FinishSpan(ctx, spn)
513+
}
514+
}
515+
516+
//TODO: remove this hack
517+
// capture stderr at startup because it gets modified in a way that this
518+
// logger should not respect
519+
var stderr = os.Stderr
520+
521+
func (i *Instance) Log(ctx context.Context, event telemetry.Event) {
522+
if event.Error != nil {
523+
fmt.Fprintf(stderr, "%v\n", event)
524+
}
525+
protocol.LogEvent(ctx, event)
526+
if i.ocagent != nil {
527+
i.ocagent.Log(ctx, event)
528+
}
529+
}
530+
531+
func (i *Instance) Metric(ctx context.Context, data telemetry.MetricData) {
532+
if i.ocagent != nil {
533+
i.ocagent.Metric(ctx, data)
534+
}
535+
if i.traces != nil {
536+
i.prometheus.Metric(ctx, data)
537+
}
538+
if i.rpcs != nil {
539+
i.rpcs.Metric(ctx, data)
540+
}
541+
}
542+
543+
func (i *Instance) Flush() {
544+
if i.ocagent != nil {
545+
i.ocagent.Flush()
546+
}
547+
}
548+
496549
type dataFunc func(*http.Request) interface{}
497550

498551
func render(tmpl *template.Template, fun dataFunc) func(http.ResponseWriter, *http.Request) {

internal/lsp/debug/trace.go

-6
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,6 @@ func (t *traces) FinishSpan(ctx context.Context, span *telemetry.Span) {
130130
}
131131
}
132132

133-
func (t *traces) Log(ctx context.Context, event telemetry.Event) {}
134-
135-
func (t *traces) Metric(ctx context.Context, data telemetry.MetricData) {}
136-
137-
func (t *traces) Flush() {}
138-
139133
func (t *traces) getData(req *http.Request) interface{} {
140134
if len(t.sets) == 0 {
141135
return nil

internal/lsp/protocol/context.go

+1-15
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@ import (
55
"fmt"
66

77
"golang.org/x/tools/internal/telemetry"
8-
"golang.org/x/tools/internal/telemetry/export"
98
"golang.org/x/tools/internal/xcontext"
109
)
1110

12-
func init() {
13-
export.AddExporters(logExporter{})
14-
}
15-
1611
type contextKey int
1712

1813
const (
@@ -23,16 +18,7 @@ func WithClient(ctx context.Context, client Client) context.Context {
2318
return context.WithValue(ctx, clientKey, client)
2419
}
2520

26-
// logExporter sends the log event back to the client if there is one stored on the
27-
// context.
28-
type logExporter struct{}
29-
30-
func (logExporter) StartSpan(context.Context, *telemetry.Span) {}
31-
func (logExporter) FinishSpan(context.Context, *telemetry.Span) {}
32-
func (logExporter) Metric(context.Context, telemetry.MetricData) {}
33-
func (logExporter) Flush() {}
34-
35-
func (logExporter) Log(ctx context.Context, event telemetry.Event) {
21+
func LogEvent(ctx context.Context, event telemetry.Event) {
3622
client, ok := ctx.Value(clientKey).(Client)
3723
if !ok {
3824
return

internal/telemetry/export/export.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,32 @@ func SetExporter(e Exporter) {
4141
exporter = e
4242
}
4343

44-
func AddExporters(e ...Exporter) {
45-
exporterMu.Lock()
46-
defer exporterMu.Unlock()
47-
exporter = Multi(append([]Exporter{exporter}, e...)...)
48-
}
49-
5044
func StartSpan(ctx context.Context, span *telemetry.Span, at time.Time) {
5145
exporterMu.Lock()
5246
defer exporterMu.Unlock()
47+
if exporter == nil {
48+
return
49+
}
5350
span.Start = at
5451
exporter.StartSpan(ctx, span)
5552
}
5653

5754
func FinishSpan(ctx context.Context, span *telemetry.Span, at time.Time) {
5855
exporterMu.Lock()
5956
defer exporterMu.Unlock()
57+
if exporter == nil {
58+
return
59+
}
6060
span.Finish = at
6161
exporter.FinishSpan(ctx, span)
6262
}
6363

6464
func Tag(ctx context.Context, at time.Time, tags telemetry.TagList) {
6565
exporterMu.Lock()
6666
defer exporterMu.Unlock()
67+
if exporter == nil {
68+
return
69+
}
6770
// If context has a span we need to add the tags to it
6871
span := telemetry.GetSpan(ctx)
6972
if span == nil {
@@ -84,6 +87,9 @@ func Tag(ctx context.Context, at time.Time, tags telemetry.TagList) {
8487
func Log(ctx context.Context, event telemetry.Event) {
8588
exporterMu.Lock()
8689
defer exporterMu.Unlock()
90+
if exporter == nil {
91+
return
92+
}
8793
// If context has a span we need to add the event to it
8894
span := telemetry.GetSpan(ctx)
8995
if span != nil {
@@ -96,11 +102,17 @@ func Log(ctx context.Context, event telemetry.Event) {
96102
func Metric(ctx context.Context, data telemetry.MetricData) {
97103
exporterMu.Lock()
98104
defer exporterMu.Unlock()
105+
if exporter == nil {
106+
return
107+
}
99108
exporter.Metric(ctx, data)
100109
}
101110

102111
func Flush() {
103112
exporterMu.Lock()
104113
defer exporterMu.Unlock()
114+
if exporter == nil {
115+
return
116+
}
105117
exporter.Flush()
106118
}

internal/telemetry/export/multi.go

-55
This file was deleted.

internal/telemetry/export/null.go

-24
This file was deleted.

internal/telemetry/export/ocagent/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func main() {
7979
Rate: 5 * time.Second,
8080
Client: &http.Client{},
8181
})
82-
export.AddExporters(exporter)
82+
export.SetExporter(exporter)
8383

8484
ctx := context.TODO()
8585
mLatency := stats.Float64("latency", "the latency in milliseconds", "ms")

internal/telemetry/export/prometheus/prometheus.go

-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ type Exporter struct {
2525
metrics []telemetry.MetricData
2626
}
2727

28-
func (e *Exporter) StartSpan(ctx context.Context, span *telemetry.Span) {}
29-
func (e *Exporter) FinishSpan(ctx context.Context, span *telemetry.Span) {}
30-
func (e *Exporter) Log(ctx context.Context, event telemetry.Event) {}
31-
func (e *Exporter) Flush() {}
32-
3328
func (e *Exporter) Metric(ctx context.Context, data telemetry.MetricData) {
3429
e.mu.Lock()
3530
defer e.mu.Unlock()

internal/telemetry/log/bench_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func BenchmarkBaseline(b *testing.B) {
9191

9292
func BenchmarkLoggingNoExporter(b *testing.B) {
9393
ctx := context.Background()
94-
export.SetExporter(export.Null())
94+
export.SetExporter(nil)
9595
b.ReportAllocs()
9696
b.ResetTimer()
9797
for i := 0; i < b.N; i++ {

0 commit comments

Comments
 (0)