1 /* 2 * Copyright 2017, OpenCensus Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.opencensus.implcore.stats; 18 19 import com.google.common.base.Preconditions; 20 import io.opencensus.common.Clock; 21 import io.opencensus.implcore.internal.CurrentState; 22 import io.opencensus.implcore.internal.CurrentState.State; 23 import io.opencensus.implcore.internal.EventQueue; 24 import io.opencensus.metrics.Metrics; 25 import io.opencensus.metrics.export.MetricProducer; 26 import io.opencensus.stats.StatsCollectionState; 27 import io.opencensus.stats.StatsComponent; 28 29 /** Base implementation of {@link StatsComponent}. */ 30 public class StatsComponentImplBase extends StatsComponent { 31 private static final State DEFAULT_STATE = State.ENABLED; 32 33 // The State shared between the StatsComponent, StatsRecorder and ViewManager. 34 private final CurrentState currentState = new CurrentState(DEFAULT_STATE); 35 36 private final ViewManagerImpl viewManager; 37 private final StatsRecorderImpl statsRecorder; 38 39 /** 40 * Creates a new {@code StatsComponentImplBase}. 41 * 42 * @param queue the queue implementation. 43 * @param clock the clock to use when recording stats. 44 */ StatsComponentImplBase(EventQueue queue, Clock clock)45 public StatsComponentImplBase(EventQueue queue, Clock clock) { 46 StatsManager statsManager = new StatsManager(queue, clock, currentState); 47 this.viewManager = new ViewManagerImpl(statsManager); 48 this.statsRecorder = new StatsRecorderImpl(statsManager); 49 50 // Create a new MetricProducerImpl and register it to MetricProducerManager when 51 // StatsComponentImplBase is initialized. 52 MetricProducer metricProducer = new MetricProducerImpl(statsManager); 53 Metrics.getExportComponent().getMetricProducerManager().add(metricProducer); 54 } 55 56 @Override getViewManager()57 public ViewManagerImpl getViewManager() { 58 return viewManager; 59 } 60 61 @Override getStatsRecorder()62 public StatsRecorderImpl getStatsRecorder() { 63 return statsRecorder; 64 } 65 66 @Override getState()67 public StatsCollectionState getState() { 68 return stateToStatsState(currentState.get()); 69 } 70 71 @Override 72 @SuppressWarnings("deprecation") setState(StatsCollectionState newState)73 public synchronized void setState(StatsCollectionState newState) { 74 boolean stateChanged = 75 currentState.set(statsStateToState(Preconditions.checkNotNull(newState, "newState"))); 76 if (stateChanged) { 77 if (newState == StatsCollectionState.DISABLED) { 78 viewManager.clearStats(); 79 } else { 80 viewManager.resumeStatsCollection(); 81 } 82 } 83 } 84 statsStateToState(StatsCollectionState statsCollectionState)85 private static State statsStateToState(StatsCollectionState statsCollectionState) { 86 return statsCollectionState == StatsCollectionState.ENABLED ? State.ENABLED : State.DISABLED; 87 } 88 stateToStatsState(State state)89 private static StatsCollectionState stateToStatsState(State state) { 90 return state == State.ENABLED ? StatsCollectionState.ENABLED : StatsCollectionState.DISABLED; 91 } 92 } 93