@@ -24,6 +24,7 @@ import (
24
24
"time"
25
25
26
26
"github.com/optimizely/go-sdk/pkg/logging"
27
+ "github.com/optimizely/go-sdk/pkg/metrics"
27
28
"github.com/optimizely/go-sdk/pkg/utils"
28
29
)
29
30
@@ -36,19 +37,13 @@ var dispatcherLogger = logging.GetLogger("EventDispatcher")
36
37
// Dispatcher dispatches events
37
38
type Dispatcher interface {
38
39
DispatchEvent (event LogEvent ) (bool , error )
39
- GetMetrics () Metrics
40
40
}
41
41
42
42
// HTTPEventDispatcher is the HTTP implementation of the Dispatcher interface
43
43
type HTTPEventDispatcher struct {
44
44
requester * utils.HTTPRequester
45
45
}
46
46
47
- // GetMetrics is the metric accessor
48
- func (ed * HTTPEventDispatcher ) GetMetrics () Metrics {
49
- return nil
50
- }
51
-
52
47
// DispatchEvent dispatches event with callback
53
48
func (ed * HTTPEventDispatcher ) DispatchEvent (event LogEvent ) (bool , error ) {
54
49
@@ -77,7 +72,11 @@ type QueueEventDispatcher struct {
77
72
eventFlushLock sync.Mutex
78
73
Dispatcher Dispatcher
79
74
80
- metrics Metrics
75
+ // metrics
76
+ queueSize metrics.Gauge
77
+ sucessFlush metrics.Counter
78
+ failFlushCounter metrics.Counter
79
+ retryFlushCounter metrics.Counter
81
80
}
82
81
83
82
// DispatchEvent queues event with callback and calls flush in a go routine.
@@ -89,17 +88,6 @@ func (ed *QueueEventDispatcher) DispatchEvent(event LogEvent) (bool, error) {
89
88
return true , nil
90
89
}
91
90
92
- // GetMetrics is the metric accessor
93
- func (ed * QueueEventDispatcher ) GetMetrics () Metrics {
94
- ed .eventFlushLock .Lock ()
95
-
96
- defer func () {
97
- ed .eventFlushLock .Unlock ()
98
- }()
99
- return ed .metrics
100
-
101
- }
102
-
103
91
// flush the events
104
92
func (ed * QueueEventDispatcher ) flushEvents () {
105
93
@@ -110,12 +98,11 @@ func (ed *QueueEventDispatcher) flushEvents() {
110
98
}()
111
99
112
100
retryCount := 0
113
-
114
- ed .metrics .SetQueueSize (ed .eventQueue .Size ())
101
+ ed .queueSize .Set (float64 (ed .eventQueue .Size ()))
115
102
for ed .eventQueue .Size () > 0 {
116
103
if retryCount > maxRetries {
117
104
dispatcherLogger .Error (fmt .Sprintf ("event failed to send %d times. It will retry on next event sent" , maxRetries ), nil )
118
- ed .metrics . IncrFailFlushCount ( )
105
+ ed .failFlushCounter . Add ( 1 )
119
106
break
120
107
}
121
108
@@ -129,7 +116,7 @@ func (ed *QueueEventDispatcher) flushEvents() {
129
116
// remove it
130
117
dispatcherLogger .Error ("invalid type passed to event Dispatcher" , nil )
131
118
ed .eventQueue .Remove (1 )
132
- ed .metrics . IncrFailFlushCount ( )
119
+ ed .failFlushCounter . Add ( 1 )
133
120
continue
134
121
}
135
122
@@ -140,16 +127,15 @@ func (ed *QueueEventDispatcher) flushEvents() {
140
127
dispatcherLogger .Debug (fmt .Sprintf ("Dispatched log event %+v" , event ))
141
128
ed .eventQueue .Remove (1 )
142
129
retryCount = 0
143
- ed .metrics . IncrSuccessFlushCount ( )
130
+ ed .sucessFlush . Add ( 1 )
144
131
} else {
145
132
dispatcherLogger .Warning ("dispatch event failed" )
146
133
// we failed. Sleep some seconds and try again.
147
134
time .Sleep (sleepTime )
148
135
// increase retryCount. We exit if we have retried x times.
149
136
// we will retry again next event that is added.
150
137
retryCount ++
151
- ed .metrics .IncrRetryFlushCount ()
152
-
138
+ ed .retryFlushCounter .Add (1 )
153
139
}
154
140
} else {
155
141
dispatcherLogger .Error ("Error dispatching " , err )
@@ -158,17 +144,29 @@ func (ed *QueueEventDispatcher) flushEvents() {
158
144
// increase retryCount. We exit if we have retried x times.
159
145
// we will retry again next event that is added.
160
146
retryCount ++
161
- ed .metrics . IncrRetryFlushCount ( )
147
+ ed .retryFlushCounter . Add ( 1 )
162
148
}
163
149
}
164
- ed .metrics . SetQueueSize ( ed .eventQueue .Size ())
150
+ ed .queueSize . Set ( float64 ( ed .eventQueue .Size () ))
165
151
}
166
152
167
153
// NewQueueEventDispatcher creates a Dispatcher that queues in memory and then sends via go routine.
168
- func NewQueueEventDispatcher () * QueueEventDispatcher {
154
+ func NewQueueEventDispatcher (metricsRegistry metrics.Registry ) * QueueEventDispatcher {
155
+
156
+ var dispatcherMetricsRegistry metrics.Registry
157
+ if metricsRegistry != nil {
158
+ dispatcherMetricsRegistry = metricsRegistry
159
+ } else {
160
+ dispatcherMetricsRegistry = metrics .NewNoopRegistry () // protective code to set
161
+ }
162
+
169
163
return & QueueEventDispatcher {
170
164
eventQueue : NewInMemoryQueue (defaultQueueSize ),
171
165
Dispatcher : & HTTPEventDispatcher {requester : utils .NewHTTPRequester ()},
172
- metrics : & DefaultMetrics {},
166
+
167
+ queueSize : dispatcherMetricsRegistry .GetGauge (metrics .DispatcherQueueSize ),
168
+ retryFlushCounter : dispatcherMetricsRegistry .GetCounter (metrics .DispatcherRetryFlush ),
169
+ failFlushCounter : dispatcherMetricsRegistry .GetCounter (metrics .DispatcherFailedFlush ),
170
+ sucessFlush : dispatcherMetricsRegistry .GetCounter (metrics .DispatcherSuccessFlush ),
173
171
}
174
172
}
0 commit comments