aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/metrics/simple/MetricManager.java
blob: 99465a55a315d083cde1c61289ac4d1b1cd2ca99 (plain) (blame)
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
// Copyright Yahoo. 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 java.util.logging.Level;

/**
 * This is the coordinating class owning the executor and the top level objects
 * for measured metrics.
 *
 * @author Steinar Knutsen
 */
public class MetricManager extends AbstractComponent implements Provider<MetricReceiver> {

    private static final Logger log = Logger.getLogger(MetricManager.class.getName());

    private final ScheduledThreadPoolExecutor executor;
    private final MetricReceiver receiver;
    private final ThreadLocalDirectory<Bucket, Sample> metricsCollection;

    public MetricManager(ManagerConfig settings) {
        this(settings, new MetricUpdater());
    }

    private MetricManager(ManagerConfig settings, Updater<Bucket, Sample> updater) {
        log.log(Level.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;
    }

}