Debian Codegraph @

event/event.go

raw symbols SCIP
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"
10 "time"
11 )
12
13 // Event holds the information about an event that occurred.
14 // It combines the event metadata with the user supplied labels.
15 type Event struct {
16 ID uint64
17 Parent uint64 // id of the parent event for this event
18 Source Source // source of event; if empty, set by exporter to import path
19 At time.Time // time at which the event is delivered to the exporter.
20 Kind Kind
21 Labels []Label
22
23 ctx context.Context
24 target *target
25 labels [preallocateLabels]Label
26 }
27
28 // Handler is a the type for something that handles events as they occur.
29 type Handler interface {
30 // Event is called with each event.
31 Event(context.Context, *Event) context.Context
32 }
33
34 // preallocateLabels controls the space reserved for labels in a builder.
35 // Storing the first few labels directly in builders can avoid an allocation at
36 // all for the very common cases of simple events. The length needs to be large
37 // enough to cope with the majority of events but no so large as to cause undue
38 // stack pressure.
39 const preallocateLabels = 6
40
41 var eventPool = sync.Pool{New: func() interface{} { return &Event{} }}
42
43 // WithExporter returns a context with the exporter attached.
44 // The exporter is called synchronously from the event call site, so it should
45 // return quickly so as not to hold up user code.
46 func WithExporter(ctx context.Context, e *Exporter) context.Context {
47 return newContext(ctx, e, 0, time.Time{})
48 }
49
50 // SetDefaultExporter sets an exporter that is used if no exporter can be
51 // found on the context.
52 func SetDefaultExporter(e *Exporter) {
53 setDefaultExporter(e)
54 }
55
56 // New prepares a new event.
57 // This is intended to avoid allocations in the steady state case, to do this
58 // it uses a pool of events.
59 // Events are returned to the pool when Deliver is called. Failure to call
60 // Deliver will exhaust the pool and cause allocations.
61 // It returns nil if there is no active exporter for this kind of event.
62 func New(ctx context.Context, kind Kind) *Event {
63 var t *target
64 if v, ok := ctx.Value(contextKey).(*target); ok {
65 t = v
66 } else {
67 t = getDefaultTarget()
68 }
69 if t == nil {
70 return nil
71 }
72 //TODO: we can change this to a much faster test
73 switch kind {
74 case LogKind:
75 if !t.exporter.loggingEnabled() {
76 return nil
77 }
78 case MetricKind:
79 if !t.exporter.metricsEnabled() {
80 return nil
81 }
82 case StartKind, EndKind:
83 if !t.exporter.tracingEnabled() {
84 return nil
85 }
86 }
87 ev := eventPool.Get().(*Event)
88 *ev = Event{
89 ctx: ctx,
90 target: t,
91 Kind: kind,
92 Parent: t.parent,
93 }
94 ev.Labels = ev.labels[:0]
95 return ev
96 }
97
98 // Clone makes a deep copy of the Event.
99 // Deliver can be called on both Events independently.
100 func (ev *Event) Clone() *Event {
101 ev2 := eventPool.Get().(*Event)
102 *ev2 = *ev
103 ev2.Labels = append(ev2.labels[:0], ev.Labels...)
104 return ev2
105 }
106
107 func (ev *Event) Trace() {
108 ev.prepare()
109 ev.ctx = newContext(ev.ctx, ev.target.exporter, ev.ID, ev.At)
110 }
111
112 // Deliver the event to the exporter that was found in New.
113 // This also returns the event to the pool, it is an error to do anything
114 // with the event after it is delivered.
115 func (ev *Event) Deliver() context.Context {
116 // get the event ready to send
117 ev.prepare()
118 ctx := ev.deliver()
119 eventPool.Put(ev)
120 return ctx
121 }
122
123 func (ev *Event) deliver() context.Context {
124 // hold the lock while we deliver the event
125 e := ev.target.exporter
126 e.mu.Lock()
127 defer e.mu.Unlock()
128 return e.handler.Event(ev.ctx, ev)
129 }
130
131 func (ev *Event) Find(name string) Label {
132 for _, l := range ev.Labels {
133 if l.Name == name {
134 return l
135 }
136 }
137 return Label{}
138 }