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

org.killbill.commons.skeleton.modules.StatsModule Maven / Gradle / Ivy

There is a newer version: 0.40.13
Show newest version
/*
 * Copyright 2010-2014 Ning, Inc.
 * Copyright 2014-2017 Groupon, Inc
 * Copyright 2014-2017 The Billing Project, LLC
 *
 * The Billing Project licenses this file to you 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 org.killbill.commons.skeleton.modules;

import java.util.Set;

import org.killbill.commons.skeleton.metrics.TimedResourceListener;
import org.killbill.commons.skeleton.metrics.health.KillBillHealthCheckRegistry;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.health.HealthCheck;
import com.codahale.metrics.health.HealthCheckRegistry;
import com.google.common.collect.ImmutableList;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.matcher.Matchers;
import com.google.inject.multibindings.Multibinder;
import com.palominolabs.metrics.guice.MetricsInstrumentationModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;

public class StatsModule extends AbstractModule {

    private final String healthCheckUri;
    private final String metricsUri;
    private final String pingUri;
    private final String threadsUri;
    private final Iterable> healthChecks;

    public StatsModule() {
        this(ImmutableList.>of());
    }

    public StatsModule(final Class healthCheck) {
        this(ImmutableList.>of(healthCheck));
    }

    public StatsModule(final Class... healthChecks) {
        this(ImmutableList.>copyOf(healthChecks));
    }

    public StatsModule(final Iterable> healthChecks) {
        this("/1.0/healthcheck", "/1.0/metrics", "/1.0/ping", "/1.0/threads", healthChecks);
    }

    public StatsModule(final String healthCheckUri, final String metricsUri, final String pingUri, final String threadsUri,
                       final Iterable> healthChecks) {
        this.healthCheckUri = healthCheckUri;
        this.metricsUri = metricsUri;
        this.pingUri = pingUri;
        this.threadsUri = threadsUri;
        this.healthChecks = healthChecks;
    }

    @Override
    protected void configure() {
        // Dropwizard metrics
        final MetricRegistry metricRegistry = createMetricRegistry();
        bind(MetricRegistry.class).toInstance(metricRegistry);
        install(MetricsInstrumentationModule.builder().withMetricRegistry(metricRegistry).build());

        // Dropwizard healthChecks
        final Multibinder healthChecksBinder = Multibinder.newSetBinder(binder(), HealthCheck.class);
        for (final Class healthCheckClass : healthChecks) {
            healthChecksBinder.addBinding().to(healthCheckClass).asEagerSingleton();
        }
        install(new AdminServletModule(healthCheckUri, metricsUri, pingUri, threadsUri));

        // Metrics/Jersey integration
        final TimedResourceListener timedResourceTypeListener =
                new TimedResourceListener(getProvider(GuiceContainer.class), getProvider(MetricRegistry.class));
        bindListener(Matchers.any(), timedResourceTypeListener);

        bind(HealthCheckRegistry.class).toInstance(createHealthCheckRegistry());
    }

    /**
     * Override to provide a custom {@link HealthCheckRegistry}
     *
     * @return HealthCheckRegistry instance to bind
     */
    protected HealthCheckRegistry createHealthCheckRegistry() {
        return new KillBillHealthCheckRegistry();
    }

    /**
     * Override to provide a custom {@link MetricRegistry}
     *
     * @return MetricRegistry instance to bind
     */
    protected MetricRegistry createMetricRegistry() {
        return new MetricRegistry();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy