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

org.apache.camel.component.micrometer.TimerProducer Maven / Gradle / Ivy

The 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.micrometer;

import java.util.function.Function;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Timer;
import org.apache.camel.Exchange;
import org.apache.camel.Message;

import static org.apache.camel.component.micrometer.MicrometerConstants.HEADER_TIMER_ACTION;

public class TimerProducer extends AbstractMicrometerProducer {

    public TimerProducer(MicrometerEndpoint endpoint) {
        super(endpoint);
    }

    @Override
    protected Function registrar(String name, String description, Iterable tags) {
        return meterRegistry -> Timer.builder(name).description(description).tags(tags).register(meterRegistry);
    }

    @Override
    protected void doProcess(Exchange exchange, MicrometerEndpoint endpoint, Timer timer) {
        String propertyName = getPropertyName(timer.getId().getName());
        Timer.Sample sample = getTimerSampleFromExchange(exchange, propertyName);
        sample.stop(timer);
        exchange.removeProperty(propertyName);
    }

    @Override
    protected void doProcess(Exchange exchange, String metricsName, String metricsDescription, Iterable tags) {
        MeterRegistry registry = getEndpoint().getRegistry();
        Message in = exchange.getIn();
        MicrometerTimerAction action = simple(exchange, getEndpoint().getAction(), MicrometerTimerAction.class);
        MicrometerTimerAction finalAction = in.getHeader(HEADER_TIMER_ACTION, action, MicrometerTimerAction.class);
        if (finalAction == MicrometerTimerAction.start) {
            handleStart(exchange, registry, metricsName);
        } else if (finalAction == MicrometerTimerAction.stop) {
            handleStop(exchange, metricsName, metricsDescription, tags);
        }
    }

    private void handleStop(Exchange exchange, String metricsName, String metricsDescription, Iterable tags) {
        if (getTimerSampleFromExchange(exchange, getPropertyName(metricsName)) != null) {
            doProcess(exchange, getEndpoint(), getOrRegisterMeter(metricsName, metricsDescription, tags));
        }
    }

    void handleStart(Exchange exchange, MeterRegistry registry, String metricsName) {
        String propertyName = getPropertyName(metricsName);
        Timer.Sample sample = getTimerSampleFromExchange(exchange, propertyName);
        if (sample == null) {
            sample = Timer.start(registry);
            exchange.setProperty(propertyName, sample);
        }
    }

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

    Timer.Sample getTimerSampleFromExchange(Exchange exchange, String propertyName) {
        return exchange.getProperty(propertyName, Timer.Sample.class);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy