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.
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
18 var exporter unsafe.Pointer
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)
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
32 atomic.StorePointer(&exporter, p)
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
40 // hand the event off to the current exporter
41 return exporter(ctx, ev, ev)
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 {
51 return deliver(ctx, *exporterPtr, ev)
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
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 {
64 ctx = deliver(ctx, *exporterPtr, begin)
65 return ctx, func() { deliver(ctx, *exporterPtr, end) }