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
019package org.apache.oozie.service;
020
021import org.apache.oozie.util.Instrumentation;
022import org.apache.oozie.util.MetricsInstrumentation;
023
024
025/**
026 * This service provides an {@link Instrumentation} instance mostly compatible with the original Instrumentation, but backed by
027 * Codahale Metrics. <p> This service depends on the {@link SchedulerService}. <p> The {@link #CONF_LOGGING_INTERVAL}
028 * configuration property indicates how often snapshots of the instrumentation should be logged.
029 */
030public class MetricsInstrumentationService extends InstrumentationService {
031
032    private static boolean isEnabled = false;
033
034    /**
035     * Initialize the metrics instrumentation service.
036     *
037     * @param services services instance.
038     * @throws org.apache.oozie.service.ServiceException
039     */
040    @Override
041    public void init(Services services) throws ServiceException {
042        final MetricsInstrumentation instr = new MetricsInstrumentation();
043        int interval = services.getConf().getInt(CONF_LOGGING_INTERVAL, 60);
044        initLogging(services, instr, interval);
045        instrumentation = instr;
046        isEnabled = true;
047    }
048
049    /**
050     * Destroy the metrics instrumentation service.
051     */
052    @Override
053    public void destroy() {
054        isEnabled = false;
055        instrumentation.stop();
056        instrumentation = null;
057    }
058
059    /**
060     * Returns if the MetricsInstrumentationService is enabled or not.
061     *
062     * @return true if the MetricsInstrumentationService is enabled; false if not
063     */
064    public static boolean isEnabled() {
065        return isEnabled;
066    }
067}