16
16
package com .arpnetworking .metrics .proxy .actors ;
17
17
18
18
import akka .actor .ActorRef ;
19
- import akka .actor .Cancellable ;
20
19
import akka .actor .PoisonPill ;
21
20
import akka .actor .Props ;
22
21
import akka .actor .UntypedActor ;
23
- import akka .dispatch .ExecutionContexts ;
24
22
import akka .http .javadsl .model .ws .Message ;
25
23
import akka .http .javadsl .model .ws .TextMessage ;
26
24
import com .arpnetworking .commons .jackson .databind .ObjectMapperFactory ;
27
25
import com .arpnetworking .logback .annotations .LogValue ;
28
- import com .arpnetworking .metrics .Metrics ;
29
- import com .arpnetworking .metrics .MetricsFactory ;
26
+ import com .arpnetworking .metrics .incubator .PeriodicMetrics ;
30
27
import com .arpnetworking .metrics .proxy .models .messages .Command ;
31
28
import com .arpnetworking .metrics .proxy .models .messages .Connect ;
32
29
import com .arpnetworking .metrics .proxy .models .protocol .MessageProcessorsFactory ;
38
35
import com .fasterxml .jackson .databind .ObjectMapper ;
39
36
import com .fasterxml .jackson .databind .node .JsonNodeFactory ;
40
37
import com .fasterxml .jackson .databind .node .ObjectNode ;
41
- import scala .concurrent .duration .FiniteDuration ;
42
38
43
39
import java .util .List ;
44
- import java .util .concurrent .TimeUnit ;
45
40
46
41
/**
47
42
* Actor class to hold the state for a single connection.
@@ -52,37 +47,29 @@ public class Connection extends UntypedActor {
52
47
/**
53
48
* Public constructor.
54
49
*
55
- * @param metricsFactory Instance of <code>MetricsFactory </code>.
50
+ * @param metrics Instance of <code>PeriodicMetrics </code>.
56
51
* @param processorsFactory Factory for producing the protocol's <code>MessagesProcessor</code>
57
52
*/
58
53
public Connection (
59
- final MetricsFactory metricsFactory ,
54
+ final PeriodicMetrics metrics ,
60
55
final MessageProcessorsFactory processorsFactory ) {
61
- _metricsFactory = metricsFactory ;
62
- _instrument = context ().system ().scheduler ().schedule (
63
- new FiniteDuration (0 , TimeUnit .SECONDS ), // Initial delay
64
- new FiniteDuration (500 , TimeUnit .MILLISECONDS ), // Interval
65
- getSelf (),
66
- "instrument" ,
67
- ExecutionContexts .global (),
68
- getSelf ());
69
- _messageProcessors = processorsFactory .create (this );
70
- _metrics = createMetrics ();
56
+ _metrics = metrics ;
57
+ _messageProcessors = processorsFactory .create (this , metrics );
71
58
}
72
59
73
60
/**
74
61
* Factory for creating a <code>Props</code> with strong typing.
75
62
*
76
- * @param metricsFactory Instance of <code>MetricsFactory </code>.
63
+ * @param metrics Instance of <code>PeriodicMetrics </code>.
77
64
* @param messageProcessorsFactory Factory to create a <code>Metrics</code> object.
78
65
* @return a new Props object to create a <code>ConnectionContext</code>.
79
66
*/
80
67
public static Props props (
81
- final MetricsFactory metricsFactory ,
68
+ final PeriodicMetrics metrics ,
82
69
final MessageProcessorsFactory messageProcessorsFactory ) {
83
70
return Props .create (
84
71
Connection .class ,
85
- metricsFactory ,
72
+ metrics ,
86
73
messageProcessorsFactory );
87
74
}
88
75
@@ -97,10 +84,7 @@ public void onReceive(final Object message) throws Exception {
97
84
.addData ("data" , message )
98
85
.addData ("channel" , _channel )
99
86
.log ();
100
- if ("instrument" .equals (message )) {
101
- periodicInstrumentation ();
102
- return ;
103
- } else if (message instanceof Connect ) {
87
+ if (message instanceof Connect ) {
104
88
final Connect connect = (Connect ) message ;
105
89
_telemetry = connect .getTelemetry ();
106
90
_channel = connect .getChannel ();
@@ -142,9 +126,9 @@ public void onReceive(final Object message) throws Exception {
142
126
}
143
127
}
144
128
if (!messageProcessed ) {
145
- _metrics .incrementCounter (UNKNOWN_COUNTER );
129
+ _metrics .recordCounter (UNKNOWN_COUNTER , 1 );
146
130
if (message instanceof Command ) {
147
- _metrics .incrementCounter ( UNKONOWN_COMMAND_COUNTER );
131
+ _metrics .recordCounter ( UNKNOWN_COMMAND_COUNTER , 1 );
148
132
LOGGER .warn ()
149
133
.setMessage ("Unable to process message" )
150
134
.addData ("reason" , "unsupported command" )
@@ -153,7 +137,7 @@ public void onReceive(final Object message) throws Exception {
153
137
.log ();
154
138
unhandled (message );
155
139
} else {
156
- _metrics .incrementCounter ("Actors/Connection/UNKNOWN" );
140
+ _metrics .recordCounter ("Actors/Connection/UNKNOWN" , 1 );
157
141
LOGGER .warn ()
158
142
.setMessage ("Unable to process message" )
159
143
.addData ("reason" , "unsupported message" )
@@ -165,15 +149,6 @@ public void onReceive(final Object message) throws Exception {
165
149
}
166
150
}
167
151
168
- /**
169
- * {@inheritDoc}
170
- */
171
- @ Override
172
- public void postStop () throws Exception {
173
- _instrument .cancel ();
174
- super .postStop ();
175
- }
176
-
177
152
/**
178
153
* Sends a json object to the connected client.
179
154
*
@@ -236,32 +211,14 @@ public String toString() {
236
211
return toLogValue ().toString ();
237
212
}
238
213
239
- private Metrics createMetrics () {
240
- final Metrics metrics = _metricsFactory .create ();
241
- metrics .resetCounter (UNKONOWN_COMMAND_COUNTER );
242
- metrics .resetCounter (UNKNOWN_COUNTER );
243
- for (final MessagesProcessor messageProcessor : _messageProcessors ) {
244
- messageProcessor .initializeMetrics (metrics );
245
- }
246
- return metrics ;
247
- }
248
-
249
- private void periodicInstrumentation () {
250
- _metrics .close ();
251
- _metrics = createMetrics ();
252
- }
253
-
254
214
private ActorRef _telemetry ;
255
215
private ActorRef _channel ;
256
216
257
- private final MetricsFactory _metricsFactory ;
258
- private final Cancellable _instrument ;
217
+ private final PeriodicMetrics _metrics ;
259
218
private final List <MessagesProcessor > _messageProcessors ;
260
219
261
- private Metrics _metrics ;
262
-
263
220
private static final String METRICS_PREFIX = "actors/connection/" ;
264
- private static final String UNKONOWN_COMMAND_COUNTER = METRICS_PREFIX + "command/UNKNOWN" ;
221
+ private static final String UNKNOWN_COMMAND_COUNTER = METRICS_PREFIX + "command/UNKNOWN" ;
265
222
private static final String UNKNOWN_COUNTER = METRICS_PREFIX + "UNKNOWN" ;
266
223
267
224
private static final ObjectMapper OBJECT_MAPPER = ObjectMapperFactory .getInstance ();
0 commit comments