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

org.eclipse.edc.web.jersey.micrometer.JerseyMicrometerExtension Maven / Gradle / Ivy

There is a newer version: 0.9.1
Show newest version
/*
 *  Copyright (c) 2022 Microsoft Corporation
 *
 *  This program and the accompanying materials are made available under the
 *  terms of the Apache License, Version 2.0 which is available at
 *  https://www.apache.org/licenses/LICENSE-2.0
 *
 *  SPDX-License-Identifier: Apache-2.0
 *
 *  Contributors:
 *       Microsoft Corporation - initial API and implementation
 *
 */

package org.eclipse.edc.web.jersey.micrometer;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.jersey.server.DefaultJerseyTagsProvider;
import io.micrometer.core.instrument.binder.jersey.server.MetricsApplicationEventListener;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.runtime.metamodel.annotation.Setting;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.web.spi.WebService;

/**
 * An extension that register a Micrometer {@link MetricsApplicationEventListener} into Jersey to
 * provide metrics and request timings.
 */
public class JerseyMicrometerExtension implements ServiceExtension {

    @Setting
    public static final String ENABLE_METRICS = "edc.metrics.enabled";
    @Setting
    public static final String ENABLE_JERSEY_METRICS = "edc.metrics.jersey.enabled";

    @Inject
    private WebService webService;

    @Inject
    private MeterRegistry meterRegistry;

    @Override
    public String name() {
        return "Jersey Micrometer Metrics";
    }

    @Override
    public void initialize(ServiceExtensionContext context) {
        var enableMetrics = context.getSetting(ENABLE_METRICS, true);
        var enableJerseyMetrics = context.getSetting(ENABLE_JERSEY_METRICS, true);

        if (enableMetrics && enableJerseyMetrics) {
            enableJerseyControllerMetrics();
        }
    }

    private void enableJerseyControllerMetrics() {
        webService.registerResource(new MetricsApplicationEventListener(
                meterRegistry,
                new DefaultJerseyTagsProvider(),
                "jersey",
                true));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy