aboutsummaryrefslogtreecommitdiff
path: root/impl_core/src/main/java/io/opencensus/implcore/stats/StatsComponentImplBase.java
blob: d07b0f591493477211cf1542afd1f72004855975 (plain)
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
/*
 * Copyright 2017, OpenCensus Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.opencensus.implcore.stats;

import com.google.common.base.Preconditions;
import io.opencensus.common.Clock;
import io.opencensus.implcore.internal.EventQueue;
import io.opencensus.metrics.MetricProducer;
import io.opencensus.metrics.Metrics;
import io.opencensus.stats.StatsCollectionState;
import io.opencensus.stats.StatsComponent;

/** Base implementation of {@link StatsComponent}. */
public class StatsComponentImplBase extends StatsComponent {

  // The StatsCollectionState shared between the StatsComponent, StatsRecorder and ViewManager.
  private final CurrentStatsState state = new CurrentStatsState();

  private final ViewManagerImpl viewManager;
  private final StatsRecorderImpl statsRecorder;

  /**
   * Creates a new {@code StatsComponentImplBase}.
   *
   * @param queue the queue implementation.
   * @param clock the clock to use when recording stats.
   */
  public StatsComponentImplBase(EventQueue queue, Clock clock) {
    StatsManager statsManager = new StatsManager(queue, clock, state);
    this.viewManager = new ViewManagerImpl(statsManager);
    this.statsRecorder = new StatsRecorderImpl(statsManager);

    // Create a new MetricProducerImpl and register it to MetricProducerManager when
    // StatsComponentImplBase is initialized.
    MetricProducer metricProducer = new MetricProducerImpl(statsManager);
    Metrics.getExportComponent().getMetricProducerManager().add(metricProducer);
  }

  @Override
  public ViewManagerImpl getViewManager() {
    return viewManager;
  }

  @Override
  public StatsRecorderImpl getStatsRecorder() {
    return statsRecorder;
  }

  @Override
  public StatsCollectionState getState() {
    return state.get();
  }

  @Override
  @SuppressWarnings("deprecation")
  public synchronized void setState(StatsCollectionState newState) {
    boolean stateChanged = state.set(Preconditions.checkNotNull(newState, "newState"));
    if (stateChanged) {
      if (newState == StatsCollectionState.DISABLED) {
        viewManager.clearStats();
      } else {
        viewManager.resumeStatsCollection();
      }
    }
  }
}