All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.avanza.astrix.context.metrics.BeanMetricsProxy Maven / Gradle / Ivy

/*
 * Copyright 2014 Avanza Bank AB
 *
 * 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 com.avanza.astrix.context.metrics;

import java.util.function.Supplier;

import com.avanza.astrix.beans.config.AstrixConfig;
import com.avanza.astrix.beans.core.AstrixBeanKey;
import com.avanza.astrix.beans.core.AstrixBeanSettings;
import com.avanza.astrix.beans.core.AstrixSettings;
import com.avanza.astrix.beans.core.BeanProxy;
import com.avanza.astrix.beans.core.BeanProxyNames;
import com.avanza.astrix.config.DynamicBooleanProperty;
import com.avanza.astrix.core.function.CheckedCommand;

import rx.Observable;

class BeanMetricsProxy implements BeanProxy {

	private final DynamicBooleanProperty beanMetricsEnabledGlobally;
	private final DynamicBooleanProperty beanMetricsEnabled;
	private final Timer timer;
	
	public BeanMetricsProxy(AstrixBeanKey beanKey, Metrics metrics, AstrixConfig astrixConfig) {
		this.beanMetricsEnabledGlobally = astrixConfig.get(AstrixSettings.ENABLE_BEAN_METRICS);
		this.beanMetricsEnabled = astrixConfig.getBeanConfiguration(beanKey).get(AstrixBeanSettings.BEAN_METRICS_ENABLED);
		this.timer = metrics.createTimer();
	}

	@Override
	public  CheckedCommand proxyInvocation(CheckedCommand command) {
		return timer.timeCheckedExecution(command);
	}

	@Override
	public  Supplier> proxyReactiveInvocation(Supplier> command) {
		return timer.timeObservable(command);
	}
	
	Timer getTimer() {
		return timer;
	}
	
	@Override
	public boolean isEnabled() {
		return beanMetricsEnabledGlobally.get() && beanMetricsEnabled.get();
	}

	@Override
	public String name() {
		return BeanProxyNames.METRICS;
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy