Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / mvdan.cc / gofumpt@v0.0.0-20200802201014-ab5a8192947d / gofumports / internal / telemetry / event / export.go
1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package event
6
7 import (
8         "context"
9         "sync/atomic"
10         "time"
11         "unsafe"
12 )
13
14 // Exporter is a function that handles events.
15 // It may return a modified context and event.
16 type Exporter func(context.Context, Event, TagMap) context.Context
17
18 var exporter unsafe.Pointer
19
20 // SetExporter sets the global exporter function that handles all events.
21 // The exporter is called synchronously from the event call site, so it should
22 // return quickly so as not to hold up user code.
23 func SetExporter(e Exporter) {
24         p := unsafe.Pointer(&e)
25         if e == nil {
26                 // &e is always valid, and so p is always valid, but for the early abort
27                 // of ProcessEvent to be efficient it needs to make the nil check on the
28                 // pointer without having to dereference it, so we make the nil function
29                 // also a nil pointer
30                 p = nil
31         }
32         atomic.StorePointer(&exporter, p)
33 }
34
35 // deliver is called to deliver an event to the supplied exporter.
36 // it will fill in the time and generate the basic tag source.
37 func deliver(ctx context.Context, exporter Exporter, ev Event) context.Context {
38         // add the current time to the event
39         ev.At = time.Now()
40         // hand the event off to the current exporter
41         return exporter(ctx, ev, ev)
42 }
43
44 // dispatch is called to deliver an event to the global exporter if set.
45 func dispatch(ctx context.Context, ev Event) context.Context {
46         // get the global exporter and abort early if there is not one
47         exporterPtr := (*Exporter)(atomic.LoadPointer(&exporter))
48         if exporterPtr == nil {
49                 return ctx
50         }
51         return deliver(ctx, *exporterPtr, ev)
52 }
53
54 // dispatchPair is called to deliver a start event to the supplied exporter.
55 // It also returns a function that will deliver the end event to the same
56 // exporter.
57 // it will fill in the time and generate the basic tag source.
58 func dispatchPair(ctx context.Context, begin, end Event) (context.Context, func()) {
59         // get the global exporter and abort early if there is not one
60         exporterPtr := (*Exporter)(atomic.LoadPointer(&exporter))
61         if exporterPtr == nil {
62                 return ctx, func() {}
63         }
64         ctx = deliver(ctx, *exporterPtr, begin)
65         return ctx, func() { deliver(ctx, *exporterPtr, end) }
66 }