001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *      Delbosc Benoit
018 */
019package org.nuxeo.runtime.metrics;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.runtime.model.ComponentContext;
024import org.nuxeo.runtime.model.ComponentInstance;
025import org.nuxeo.runtime.model.DefaultComponent;
026
027import com.codahale.metrics.Counter;
028import com.codahale.metrics.MetricRegistry;
029import com.codahale.metrics.SharedMetricRegistries;
030
031public class MetricsServiceImpl extends DefaultComponent implements MetricsService {
032
033    protected static final Log log = LogFactory.getLog(MetricsServiceImpl.class);
034
035    protected MetricRegistry registry = SharedMetricRegistries.getOrCreate(MetricsService.class.getName());
036
037    private final Counter instanceUp = registry.counter(MetricRegistry.name("nuxeo", "instance-up"));
038
039    protected static final String CONFIGURATION_EP = "configuration";
040
041    public static MetricsDescriptor config;
042
043    public MetricsServiceImpl() {
044        super();
045    }
046
047    @Override
048    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
049        if (CONFIGURATION_EP.equals(extensionPoint) && contribution instanceof MetricsDescriptor) {
050            log.debug("Registering metrics contribution");
051            config = (MetricsDescriptor) contribution;
052        } else {
053            log.warn("Unknown EP " + extensionPoint);
054        }
055    }
056
057    @Override
058    public void deactivate(ComponentContext context) {
059        try {
060            config.disable(registry);
061        } finally {
062            instanceUp.dec();
063            // cleanup registry
064            for (String name:registry.getNames()) {
065                registry.remove(name);
066            }
067            // should remove the reference also
068            SharedMetricRegistries.remove(MetricsService.class.getName());;
069        }
070        log.debug("Deactivate component.");
071    }
072
073    @Override
074    public void applicationStarted(ComponentContext context) {
075        if (config == null) {
076            // Use a default config
077            config = new MetricsDescriptor();
078        }
079        log.info("Setting up metrics configuration");
080        config.enable(registry);
081        instanceUp.inc();
082    }
083
084}