001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *      http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.oozie.service;
019    
020    import org.apache.oozie.util.Instrumentation;
021    import org.apache.oozie.util.MetricsInstrumentation;
022    
023    
024    /**
025     * This service provides an {@link Instrumentation} instance mostly compatible with the original Instrumentation, but backed by
026     * Codahale Metrics. <p/> This service depends on the {@link SchedulerService}. <p/> The {@link #CONF_LOGGING_INTERVAL}
027     * configuration property indicates how often snapshots of the instrumentation should be logged.
028     */
029    public class MetricsInstrumentationService extends InstrumentationService {
030    
031        private static boolean isEnabled = false;
032    
033        /**
034         * Initialize the metrics instrumentation service.
035         *
036         * @param services services instance.
037         * @throws org.apache.oozie.service.ServiceException
038         */
039        @Override
040        public void init(Services services) throws ServiceException {
041            final MetricsInstrumentation instr = new MetricsInstrumentation();
042            int interval = services.getConf().getInt(CONF_LOGGING_INTERVAL, 60);
043            initLogging(services, instr, interval);
044            instrumentation = instr;
045            isEnabled = true;
046        }
047    
048        /**
049         * Destroy the metrics instrumentation service.
050         */
051        @Override
052        public void destroy() {
053            isEnabled = false;
054            instrumentation = null;
055        }
056    
057        /**
058         * Returns if the MetricsInstrumentationService is enabled or not.
059         *
060         * @return true if the MetricsInstrumentationService is enabled; false if not
061         */
062        public static boolean isEnabled() {
063            return isEnabled;
064        }
065    }