-
Notifications
You must be signed in to change notification settings - Fork 18
/
dev_o11y.go
56 lines (46 loc) · 1.25 KB
/
dev_o11y.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package o11y
import (
"context"
"sync"
"github.com/circleci/ex/o11y"
)
// DevInit needs to be called to construct the coordinator (it is best to avoid init())
// If this is not called we will panic early. This is expected to only be called in development testing
func DevInit() {
coordinator = &closeCoord{}
}
var coordinator *closeCoord
// coordinator ensures we only init once (to satisfy the race detector) and
// ensures we only call the real close once, this allows us to use the same signature for
// Setup above, so this is all
type closeCoord struct {
mu sync.Mutex
closes int
provider o11y.Provider
realClose func(context.Context)
}
func (c *closeCoord) setup(ctx context.Context, o Config) (context.Context, func(context.Context), error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.closes == 0 {
ctx, cleanup, err := setup(ctx, o)
if err != nil {
return ctx, nil, err
}
coordinator.realClose = cleanup
coordinator.provider = o11y.FromContext(ctx)
c.closes++
return ctx, coordinator.close, nil
}
ctx = o11y.WithProvider(ctx, coordinator.provider)
c.closes++
return ctx, coordinator.close, nil
}
func (c *closeCoord) close(ctx context.Context) {
c.mu.Lock()
defer c.mu.Unlock()
c.closes--
if c.closes == 0 {
c.realClose(ctx)
}
}