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

org.apache.camel.component.microprofile.metrics.MicroProfileMetricsTimerProducer Maven / Gradle / Ivy

There is a newer version: 3.22.3
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF 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.apache.camel.component.microprofile.metrics;

import java.util.List;
import java.util.Locale;
import java.util.function.Function;

import org.apache.camel.Exchange;
import org.eclipse.microprofile.metrics.Metadata;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.Tag;
import org.eclipse.microprofile.metrics.Timer;
import org.eclipse.microprofile.metrics.Timer.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.camel.component.microprofile.metrics.MicroProfileMetricsConstants.HEADER_TIMER_ACTION;

public class MicroProfileMetricsTimerProducer extends AbstractMicroProfileMetricsProducer {

    private static final Logger LOG = LoggerFactory.getLogger(MicroProfileMetricsTimerProducer.class);

    public MicroProfileMetricsTimerProducer(MicroProfileMetricsEndpoint endpoint) {
        super(endpoint);
    }

    @Override
    protected void doProcess(Exchange exchange, MicroProfileMetricsEndpoint endpoint, Timer timer) {
        String propertyName = getPropertyName(endpoint.getMetricName());
        Context context = getTimerContextFromExchange(exchange, propertyName);
        if (context != null) {
            context.stop();
        } else {
            LOG.warn("Timer context for metric '{}' was not found", propertyName);
        }
        exchange.removeProperty(propertyName);
    }

    @Override
    protected void doProcess(Exchange exchange, Metadata metadata, List tags) {
        String actionString = getEndpoint().getAction().toUpperCase(Locale.US);
        TimerAction action = exchange.getIn().getHeader(HEADER_TIMER_ACTION, actionString, TimerAction.class);
        if (action == TimerAction.START) {
            handleStart(exchange, metadata, tags);
        } else {
            handleStop(exchange, metadata, tags);
        }
    }

    @Override
    protected Function registerMetric(Metadata metadata, List tags) {
        return metricRegistry -> metricRegistry.timer(metadata, tags.toArray(new Tag[0]));
    }

    private void handleStart(Exchange exchange, Metadata metadata, List tags) {
        String propertyName = getPropertyName(metadata.getName());
        Context context = getTimerContextFromExchange(exchange, propertyName);
        if (context == null) {
            Timer timer = getOrRegisterMetric(metadata, tags);
            exchange.setProperty(propertyName, timer.time());
        } else {
            LOG.warn("Timer '{}' is already running", metadata.getName());
        }
    }

    private void handleStop(Exchange exchange, Metadata metadata, List tags) {
        if (getTimerContextFromExchange(exchange, getPropertyName(metadata.getName())) != null) {
            doProcess(exchange, getEndpoint(), getOrRegisterMetric(metadata, tags));
        }
    }

    private String getPropertyName(String metricName) {
        return "timer:" + metricName;
    }

    private Context getTimerContextFromExchange(Exchange exchange, String propertyName) {
        return exchange.getProperty(propertyName, Context.class);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy