001/* 002 * (C) Copyright 2013-2017 Nuxeo (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 activate(ComponentContext context) { 059 SharedMetricRegistries.getOrCreate(MetricsService.class.getName());; 060 } 061 062 @Override 063 public void deactivate(ComponentContext context) { 064 SharedMetricRegistries.remove(MetricsService.class.getName());; 065 log.debug("Deactivate component."); 066 } 067 068 @Override 069 public void start(ComponentContext context) { 070 if (config == null) { 071 // Use a default config 072 config = new MetricsDescriptor(); 073 } 074 log.info("Setting up metrics configuration"); 075 config.enable(registry); 076 instanceUp.inc(); 077 } 078 079 @Override 080 public void stop(ComponentContext context) { 081 try { 082 config.disable(registry); 083 } finally { 084 instanceUp.dec(); 085 } 086 } 087 088 @Override 089 public <T> T getAdapter(Class<T> adapter) { 090 if (adapter.isAssignableFrom(MetricRegistry.class)) { 091 return adapter.cast(registry); 092 } 093 return super.getAdapter(adapter); 094 } 095 096}