/
MetricsClient.java
512 lines (452 loc) · 18.5 KB
/
MetricsClient.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package org.wikimedia.metrics_platform;
import static java.lang.Math.max;
import static java.time.Instant.now;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.WARNING;
import static org.wikimedia.metrics_platform.config.StreamConfigFetcher.ANALYTICS_API_ENDPOINT;
import static org.wikimedia.metrics_platform.event.EventProcessed.fromEvent;
import java.io.IOException;
import java.net.URL;
import java.time.Duration;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.NotThreadSafe;
import org.wikimedia.metrics_platform.config.SourceConfig;
import org.wikimedia.metrics_platform.config.StreamConfig;
import org.wikimedia.metrics_platform.config.StreamConfigFetcher;
import org.wikimedia.metrics_platform.context.ClientData;
import org.wikimedia.metrics_platform.context.InteractionData;
import org.wikimedia.metrics_platform.context.PerformerData;
import org.wikimedia.metrics_platform.event.Event;
import org.wikimedia.metrics_platform.event.EventProcessed;
import lombok.Setter;
import lombok.SneakyThrows;
import lombok.experimental.Accessors;
import lombok.extern.java.Log;
@Log
public final class MetricsClient {
public static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT)
.withZone(ZoneId.of("UTC"));
private static final ScheduledExecutorService EXECUTOR_SERVICE = Executors.newScheduledThreadPool(1, new SimpleThreadFactory());
public static final String METRICS_PLATFORM_LIBRARY_VERSION = "2.4";
public static final String METRICS_PLATFORM_BASE_VERSION = "1.2.0";
public static final String METRICS_PLATFORM_SCHEMA_BASE = "/analytics/product_metrics/app/base/" + METRICS_PLATFORM_BASE_VERSION;
private final AtomicReference<SourceConfig> sourceConfig;
/**
* Handles logging session management. A new session begins (and a new session ID is created)
* if the app has been inactive for 15 minutes or more.
*/
private final SessionController sessionController;
/**
* Evaluates whether events for a given stream are in-sample based on the stream configuration.
*/
private final SamplingController samplingController;
private final BlockingQueue<EventProcessed> eventQueue;
private final EventProcessor eventProcessor;
/**
* MetricsClient constructor.
*/
private MetricsClient(
SessionController sessionController,
SamplingController samplingController,
AtomicReference<SourceConfig> sourceConfig,
BlockingQueue<EventProcessed> eventQueue,
EventProcessor eventProcessor
) {
this.sessionController = sessionController;
this.samplingController = samplingController;
this.sourceConfig = sourceConfig;
this.eventQueue = eventQueue;
this.eventProcessor = eventProcessor;
}
/**
* Submit an event to be enqueued and sent to the Event Platform.
* <p>
* If stream configs are not yet fetched, the event will be held temporarily in the input
* buffer (provided there is space to do so).
* <p>
* If stream configs are available, the event will be validated and enqueued for submission
* to the configured event platform intake service.
* <p>
* Supplemental metadata is added immediately on intake, regardless of the presence or absence
* of stream configs, so that the event timestamp is recorded accurately.
*
* @param event event data
*/
public void submit(Event event) {
EventProcessed eventProcessed = fromEvent(event);
addRequiredMetadata(eventProcessed);
addToEventQueue(eventProcessed);
}
/**
* Construct and submits a Metrics Platform Event from the event name and custom data for each
* stream specified.
* <p>
* The Metrics Platform Event for a stream (S) is constructed by: first initializing the minimum
* valid event (E) that can be submitted to S; and, second mixing the context attributes requested
* in the configuration for S into E.
* <p>
* The Metrics Platform Event is submitted to a stream (S) if: 1) S is in sample; and 2) the event
* is filtered due to the filtering rules for S.
* <p>
* This particular submitMetricsEvent method accepts unformatted custom data and calls the following
* submitMetricsEvent method with the custom data properly formatted.
* <p>
* @see <a href="https://wikitech.wikimedia.org/wiki/Metrics_Platform">Metrics Platform</a>
*
* @param streamName stream name
* @param schemaId schema id
* @param eventName event name
* @param customData custom data
*/
public void submitMetricsEvent(
String streamName,
String schemaId,
String eventName,
Map<String, Object> customData
) {
submitMetricsEvent(streamName, schemaId, eventName, null, customData, null);
}
/**
* Construct and submits a Metrics Platform Event from the schema id, event name, page metadata, and custom data for
* the stream that is interested in those events.
*
* @param streamName stream name
* @param schemaId schema id
* @param eventName event name
* @param clientData client context data
* @param customData custom data
*/
public void submitMetricsEvent(
String streamName,
String schemaId,
String eventName,
ClientData clientData,
Map<String, Object> customData
) {
submitMetricsEvent(streamName, schemaId, eventName, clientData, customData, null);
}
/**
* Construct and submits a Metrics Platform Event from the schema id, stream name, event name, page metadata, custom
* data, and interaction data for the specified stream.
*
* @param streamName stream name
* @param schemaId schema id
* @param eventName event name
* @param clientData client context data
* @param customData custom data
* @param interactionData common data for an interaction schema
*/
public void submitMetricsEvent(
String streamName,
String schemaId,
String eventName,
ClientData clientData,
Map<String, Object> customData,
InteractionData interactionData
) {
SourceConfig sourceConfig = this.sourceConfig.get();
if (sourceConfig == null) {
log.log(Level.FINE, "Configuration not loaded yet, the submitMetricsEvent event is ignored and dropped.");
return;
}
if (streamName == null) {
log.log(Level.FINE, "No stream has been specified, the submitMetricsEvent event is ignored and dropped.");
return;
}
StreamConfig streamConfig = this.sourceConfig.get().getStreamConfigByName(streamName);
if (streamConfig == null) {
log.log(Level.FINE, "No stream config exists for this stream, the submitMetricsEvent event is ignored and dropped.");
return;
}
if (!this.samplingController.isInSample(streamConfig)) {
log.log(Level.FINE, "Not in sample, the submitMetricsEvent event is ignored and dropped.");
return;
}
Event event = new Event(schemaId, streamName, eventName);
event.setClientData(clientData);
if (customData != null) {
event.setCustomData(customData);
}
event.setInteractionData(interactionData);
if (streamConfig.hasSampleConfig()) {
event.setSample(streamConfig.getSampleConfig());
}
submit(event);
}
/**
* Submit an interaction event to a stream.
* <p>
* An interaction event is meant to represent a basic interaction with some target or some event
* occurring, e.g. the user (**performer**) tapping/clicking a UI element, or an app notifying the
* server of its current state.
*
* @param streamName stream name
* @param eventName event name
* @param clientData client context data
* @param interactionData common data for the base interaction schema
*/
public void submitInteraction(
String streamName,
String eventName,
ClientData clientData,
InteractionData interactionData
) {
submitMetricsEvent(streamName, METRICS_PLATFORM_SCHEMA_BASE, eventName, clientData, null, interactionData);
}
/**
* Submit an interaction event to a stream.
* <p>
* See above - takes additional parameters (custom data + custom schema id) to submit an interaction event.
*
* @param streamName stream name
* @param schemaId schema id
* @param eventName event name
* @param clientData client context data
* @param interactionData common data for the base interaction schema
* @param customData custom data for the interaction
*/
public void submitInteraction(
String streamName,
String schemaId,
String eventName,
ClientData clientData,
InteractionData interactionData,
Map<String, Object> customData
) {
submitMetricsEvent(streamName, schemaId, eventName, clientData, customData, interactionData);
}
/**
* Submit a click event to a stream.
*
* @param streamName stream name
* @param clientData client context data
* @param interactionData common data for the base interaction schema
*/
public void submitClick(
String streamName,
ClientData clientData,
InteractionData interactionData
) {
submitMetricsEvent(streamName, METRICS_PLATFORM_SCHEMA_BASE, "click", clientData, null, interactionData);
}
/**
* Submit a click event to a stream with custom data.
*
* @param streamName stream name
* @param schemaId schema id
* @param eventName event name
* @param clientData client context data
* @param customData custom data for the interaction
* @param interactionData common data for the base interaction schema
*/
public void submitClick(
String streamName,
String schemaId,
String eventName,
ClientData clientData,
Map<String, Object> customData,
InteractionData interactionData
) {
submitMetricsEvent(streamName, schemaId, eventName, clientData, customData, interactionData);
}
/**
* Submit a view event to a stream.
*
* @param streamName stream name
* @param clientData client context data
* @param interactionData common data for the base interaction schema
*/
public void submitView(
String streamName,
ClientData clientData,
InteractionData interactionData
) {
submitMetricsEvent(streamName, METRICS_PLATFORM_SCHEMA_BASE, "view", clientData, null, interactionData);
}
/**
* Submit a view event to a stream with custom data.
*
* @param streamName stream name
* @param schemaId schema id
* @param eventName event name
* @param clientData client context data
* @param customData custom data for the interaction
* @param interactionData common data for the base interaction schema
*/
public void submitView(
String streamName,
String schemaId,
String eventName,
ClientData clientData,
Map<String, Object> customData,
InteractionData interactionData
) {
submitMetricsEvent(streamName, schemaId, eventName, clientData, customData, interactionData);
}
/**
* Convenience method to be called when
* <a href="https://developer.android.com/guide/components/activities/activity-lifecycle#onpause">
* the onPause() activity lifecycle callback</a> is called.
* <p>
* Touches the session so that we can determine whether it's session has expired if and when the
* application is resumed.
*/
public void onAppPause() {
EXECUTOR_SERVICE.schedule(eventProcessor::sendEnqueuedEvents, 0, MILLISECONDS);
sessionController.touchSession();
}
/**
* Convenience method to be called when
* <a href="https://developer.android.com/guide/components/activities/activity-lifecycle#onresume">
* the onResume() activity lifecycle callback</a> is called.
* <p>
* Touches the session so that we can determine whether it has expired.
*/
public void onAppResume() {
sessionController.touchSession();
}
/**
* Closes the session.
*/
public void onAppClose() {
EXECUTOR_SERVICE.schedule(eventProcessor::sendEnqueuedEvents, 0, MILLISECONDS);
sessionController.closeSession();
}
/**
* Begins a new session and touches the session.
*/
public void resetSession() {
sessionController.beginSession();
}
/**
* Supplement the outgoing event with additional metadata.
* These include:
* - app_session_id: the current session ID
* - dt: ISO 8601 timestamp
* - domain: hostname
*
* @param event event
*/
private void addRequiredMetadata(EventProcessed event) {
event.setPerformerData(
PerformerData.builderFrom(event.getPerformerData())
.sessionId(sessionController.getSessionId())
.build());
event.setTimestamp(DATE_FORMAT.format(now()));
event.setDomain(event.getClientData().getDomain());
}
/**
* Append an enriched event to the queue.
* If the queue is full, we remove the oldest events from the queue to add the current event.
* Number of attempts to add to the queue is 1/50 of the number queue capacity but at least 10
*
* @param event a processed event
*/
private void addToEventQueue(EventProcessed event) {
int eventQueueAppendAttempts = max(eventQueue.size() / 50, 10);
while (!eventQueue.offer(event)) {
EventProcessed removedEvent = eventQueue.remove();
if (removedEvent != null) {
log.log(FINE, removedEvent.getName() + " was dropped so that a newer event could be added to the queue.");
}
if (eventQueueAppendAttempts-- <= 0) break;
}
}
public boolean isFullyInitialized() {
return sourceConfig.get() != null;
}
public boolean isEventQueueEmpty() {
return eventQueue.isEmpty();
}
public static Builder builder(ClientData clientData) {
return new Builder(clientData);
}
@NotThreadSafe @ParametersAreNonnullByDefault
@Setter @Accessors(fluent = true)
public static final class Builder {
private final ClientData clientData;
private AtomicReference<SourceConfig> sourceConfigRef = new AtomicReference<>();
private BlockingQueue<EventProcessed> eventQueue = new LinkedBlockingQueue<>(10);
private SessionController sessionController = new SessionController();
private CurationController curationController = new CurationController();
@Nullable
private SamplingController samplingController;
private EventSender eventSender = new EventSenderDefault();
private URL streamConfigURL = safeURL(ANALYTICS_API_ENDPOINT);
private Duration streamConfigFetchInitialDelay = Duration.ofSeconds(0);
private Duration streamConfigFetchInterval = Duration.ofSeconds(30);
private Duration sendEventsInitialDelay = Duration.ofSeconds(3);
private Duration sendEventsInterval = Duration.ofSeconds(30);
private boolean isDebug;
public Builder(ClientData clientData) {
this.clientData = clientData;
}
public Builder eventQueueCapacity(int capacity) {
eventQueue = new LinkedBlockingQueue<>(capacity);
return this;
}
public MetricsClient build() {
if (samplingController == null) {
samplingController = new SamplingController(clientData, sessionController);
}
EventProcessor eventProcessor = new EventProcessor(
new ContextController(),
curationController,
sourceConfigRef,
eventSender,
eventQueue,
isDebug
);
MetricsClient metricsClient = new MetricsClient(
sessionController,
samplingController,
sourceConfigRef,
eventQueue,
eventProcessor
);
StreamConfigFetcher streamConfigFetcher = new StreamConfigFetcher(streamConfigURL);
startScheduledOperations(eventProcessor, streamConfigFetcher);
return metricsClient;
}
private void startScheduledOperations(EventProcessor eventProcessor, StreamConfigFetcher streamConfigFetcher) {
EXECUTOR_SERVICE.scheduleAtFixedRate(
() -> {
try {
sourceConfigRef.set(streamConfigFetcher.fetchStreamConfigs());
} catch (IOException e) {
log.log(WARNING, "Could not fetch configuration.", e);
}
},
streamConfigFetchInitialDelay.toMillis(), streamConfigFetchInterval.toMillis(), MILLISECONDS);
EXECUTOR_SERVICE.scheduleAtFixedRate(
eventProcessor::sendEnqueuedEvents,
sendEventsInitialDelay.toMillis(), sendEventsInterval.toMillis(), MILLISECONDS);
}
@SneakyThrows
private static URL safeURL(String url) {
return new URL(url);
}
}
private static final class SimpleThreadFactory implements ThreadFactory {
private final AtomicLong counter = new AtomicLong();
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "metrics-client-" + counter.incrementAndGet());
}
}
}