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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.metrics.simple;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;
import com.yahoo.component.AbstractComponent;
import com.yahoo.concurrent.ThreadLocalDirectory;
import com.yahoo.concurrent.ThreadLocalDirectory.Updater;
import com.yahoo.container.di.componentgraph.Provider;
import com.yahoo.metrics.ManagerConfig;
import com.yahoo.log.LogLevel;
/**
* This is the coordinating class owning the executor and the top level objects
* for measured metrics.
*
* @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
*/
public class MetricManager extends AbstractComponent implements Provider<MetricReceiver> {
private static Logger log = Logger.getLogger(MetricManager.class.getName());
private final ScheduledThreadPoolExecutor executor;
private final MetricReceiver receiver;
private ThreadLocalDirectory<Bucket, Sample> metricsCollection;
public MetricManager(ManagerConfig settings) {
this(settings, new MetricUpdater());
}
private MetricManager(ManagerConfig settings, Updater<Bucket, Sample> updater) {
log.log(LogLevel.CONFIG, "setting up simple metrics gathering." +
" reportPeriodSeconds=" + settings.reportPeriodSeconds() +
", pointsToKeepPerMetric=" + settings.pointsToKeepPerMetric());
metricsCollection = new ThreadLocalDirectory<>(updater);
final AtomicReference<Bucket> currentSnapshot = new AtomicReference<>(null);
executor = new ScheduledThreadPoolExecutor(1);
// Fixed rate, not fixed delay, is it is not too important that each
// bucket has data for exactly one second, but one should strive for
// this.buffer to contain data for as close a period to the report
// interval as possible
executor.scheduleAtFixedRate(new MetricAggregator(metricsCollection, currentSnapshot, settings), 1, 1, TimeUnit.SECONDS);
receiver = new MetricReceiver(metricsCollection, currentSnapshot);
}
static MetricManager constructWithCustomUpdater(ManagerConfig settings, Updater<Bucket, Sample> updater) {
return new MetricManager(settings, updater);
}
@Override
public void deconstruct() {
executor.shutdown();
}
@Override
public MetricReceiver get() {
return receiver;
}
}
|