Skip to content

Commit 8734d3b

Browse files
authored
Make Add, Delete, GetAll associated with producer.Manager (census-instrumentation#1047)
fixes census-instrumentation#1045
1 parent f305e5c commit 8734d3b

File tree

2 files changed

+53
-63
lines changed

2 files changed

+53
-63
lines changed

metric/producer/manager.go

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,53 +18,54 @@ import (
1818
"sync"
1919
)
2020

21-
type manager struct {
21+
// Manager maintains a list of active producers. Producers can register
22+
// with the manager to allow readers to read all metrics provided by them.
23+
// Readers can retrieve all producers registered with the manager,
24+
// read metrics from the producers and export them.
25+
type Manager struct {
2226
mu sync.RWMutex
2327
producers map[Producer]struct{}
2428
}
2529

26-
var prodMgr *manager
30+
var prodMgr *Manager
2731
var once sync.Once
2832

29-
func getManager() *manager {
33+
// GlobalManager is a single instance of producer manager
34+
// that is used by all producers and all readers.
35+
func GlobalManager() *Manager {
3036
once.Do(func() {
31-
prodMgr = &manager{}
37+
prodMgr = &Manager{}
3238
prodMgr.producers = make(map[Producer]struct{})
3339
})
3440
return prodMgr
3541
}
3642

37-
// Add adds the producer to the manager if it is not already present.
38-
// The manager maintains the list of active producers. It provides
39-
// this list to a reader to read metrics from each producer and then export.
40-
func Add(producer Producer) {
43+
// AddProducer adds the producer to the Manager if it is not already present.
44+
func (pm *Manager) AddProducer(producer Producer) {
4145
if producer == nil {
4246
return
4347
}
44-
pm := getManager()
45-
pm.add(producer)
48+
pm.mu.Lock()
49+
defer pm.mu.Unlock()
50+
pm.producers[producer] = struct{}{}
4651
}
4752

48-
// Delete deletes the producer from the manager if it is present.
49-
func Delete(producer Producer) {
53+
// DeleteProducer deletes the producer from the Manager if it is present.
54+
func (pm *Manager) DeleteProducer(producer Producer) {
5055
if producer == nil {
5156
return
5257
}
53-
pm := getManager()
54-
pm.delete(producer)
58+
pm.mu.Lock()
59+
defer pm.mu.Unlock()
60+
delete(pm.producers, producer)
5561
}
5662

5763
// GetAll returns a slice of all producer currently registered with
58-
// the manager. For each call it generates a new slice. The slice
64+
// the Manager. For each call it generates a new slice. The slice
5965
// should not be cached as registration may change at any time. It is
6066
// typically called periodically by exporter to read metrics from
6167
// the producers.
62-
func GetAll() []Producer {
63-
pm := getManager()
64-
return pm.getAll()
65-
}
66-
67-
func (pm *manager) getAll() []Producer {
68+
func (pm *Manager) GetAll() []Producer {
6869
pm.mu.Lock()
6970
defer pm.mu.Unlock()
7071
producers := make([]Producer, len(pm.producers))
@@ -75,15 +76,3 @@ func (pm *manager) getAll() []Producer {
7576
}
7677
return producers
7778
}
78-
79-
func (pm *manager) add(producer Producer) {
80-
pm.mu.Lock()
81-
defer pm.mu.Unlock()
82-
pm.producers[producer] = struct{}{}
83-
}
84-
85-
func (pm *manager) delete(producer Producer) {
86-
pm.mu.Lock()
87-
defer pm.mu.Unlock()
88-
delete(pm.producers, producer)
89-
}

metric/producer/manager_test.go

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var (
2828
myProd1 = newTestProducer("foo")
2929
myProd2 = newTestProducer("bar")
3030
myProd3 = newTestProducer("foobar")
31+
pm = GlobalManager()
3132
)
3233

3334
func newTestProducer(name string) *testProducer {
@@ -39,83 +40,83 @@ func (mp *testProducer) Read() []*metricdata.Metric {
3940
}
4041

4142
func TestAdd(t *testing.T) {
42-
Add(myProd1)
43-
Add(myProd2)
43+
pm.AddProducer(myProd1)
44+
pm.AddProducer(myProd2)
4445

45-
got := GetAll()
46+
got := pm.GetAll()
4647
want := []*testProducer{myProd1, myProd2}
4748
checkSlice(got, want, t)
4849
deleteAll()
4950
}
5051

5152
func TestAddExisting(t *testing.T) {
52-
Add(myProd1)
53-
Add(myProd2)
54-
Add(myProd1)
53+
pm.AddProducer(myProd1)
54+
pm.AddProducer(myProd2)
55+
pm.AddProducer(myProd1)
5556

56-
got := GetAll()
57+
got := pm.GetAll()
5758
want := []*testProducer{myProd2, myProd1}
5859
checkSlice(got, want, t)
5960
deleteAll()
6061
}
6162

6263
func TestAddNil(t *testing.T) {
63-
Add(nil)
64+
pm.AddProducer(nil)
6465

65-
got := GetAll()
66+
got := pm.GetAll()
6667
want := []*testProducer{}
6768
checkSlice(got, want, t)
6869
deleteAll()
6970
}
7071

7172
func TestDelete(t *testing.T) {
72-
Add(myProd1)
73-
Add(myProd2)
74-
Add(myProd3)
75-
Delete(myProd2)
73+
pm.AddProducer(myProd1)
74+
pm.AddProducer(myProd2)
75+
pm.AddProducer(myProd3)
76+
pm.DeleteProducer(myProd2)
7677

77-
got := GetAll()
78+
got := pm.GetAll()
7879
want := []*testProducer{myProd1, myProd3}
7980
checkSlice(got, want, t)
8081
deleteAll()
8182
}
8283

8384
func TestDeleteNonExisting(t *testing.T) {
84-
Add(myProd1)
85-
Add(myProd3)
86-
Delete(myProd2)
85+
pm.AddProducer(myProd1)
86+
pm.AddProducer(myProd3)
87+
pm.DeleteProducer(myProd2)
8788

88-
got := GetAll()
89+
got := pm.GetAll()
8990
want := []*testProducer{myProd1, myProd3}
9091
checkSlice(got, want, t)
9192
deleteAll()
9293
}
9394

9495
func TestDeleteNil(t *testing.T) {
95-
Add(myProd1)
96-
Add(myProd3)
97-
Delete(nil)
96+
pm.AddProducer(myProd1)
97+
pm.AddProducer(myProd3)
98+
pm.DeleteProducer(nil)
9899

99-
got := GetAll()
100+
got := pm.GetAll()
100101
want := []*testProducer{myProd1, myProd3}
101102
checkSlice(got, want, t)
102103
deleteAll()
103104
}
104105

105106
func TestGetAllNil(t *testing.T) {
106-
got := GetAll()
107+
got := pm.GetAll()
107108
want := []*testProducer{}
108109
checkSlice(got, want, t)
109110
deleteAll()
110111
}
111112

112113
func TestImmutableProducerList(t *testing.T) {
113-
Add(myProd1)
114-
Add(myProd2)
114+
pm.AddProducer(myProd1)
115+
pm.AddProducer(myProd2)
115116

116-
producersToMutate := GetAll()
117+
producersToMutate := pm.GetAll()
117118
producersToMutate[0] = myProd3
118-
got := GetAll()
119+
got := pm.GetAll()
119120
want := []*testProducer{myProd1, myProd2}
120121
checkSlice(got, want, t)
121122
deleteAll()
@@ -141,7 +142,7 @@ func checkSlice(got []Producer, want []*testProducer, t *testing.T) {
141142
}
142143

143144
func deleteAll() {
144-
Delete(myProd1)
145-
Delete(myProd2)
146-
Delete(myProd3)
145+
pm.DeleteProducer(myProd1)
146+
pm.DeleteProducer(myProd2)
147+
pm.DeleteProducer(myProd3)
147148
}

0 commit comments

Comments
 (0)