001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.management;
018
019import org.apache.camel.AsyncCallback;
020import org.apache.camel.Exchange;
021import org.apache.camel.Ordered;
022import org.apache.camel.Processor;
023import org.apache.camel.management.mbean.ManagedPerformanceCounter;
024import org.apache.camel.spi.ManagementInterceptStrategy.InstrumentationProcessor;
025import org.apache.camel.support.processor.DelegateAsyncProcessor;
026import org.apache.camel.util.StopWatch;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * JMX enabled processor or advice that uses the {@link org.apache.camel.management.mbean.ManagedCounter} for
032 * instrumenting processing of exchanges.
033 * <p/>
034 * This implementation has been optimised to work in dual mode, either as an advice or as a processor. The former is
035 * faster and the latter is required when the error handler has been configured with redelivery enabled.
036 */
037public class DefaultInstrumentationProcessor extends DelegateAsyncProcessor
038        implements InstrumentationProcessor<StopWatch>, Ordered {
039
040    private static final Logger LOG = LoggerFactory.getLogger(DefaultInstrumentationProcessor.class);
041
042    private PerformanceCounter counter;
043    private String type;
044
045    public DefaultInstrumentationProcessor(String type, Processor processor) {
046        super(processor);
047        this.type = type;
048    }
049
050    public DefaultInstrumentationProcessor(String type) {
051        super(null);
052        this.type = type;
053    }
054
055    @Override
056    public void setCounter(Object counter) {
057        ManagedPerformanceCounter mpc = null;
058        if (counter instanceof ManagedPerformanceCounter) {
059            mpc = (ManagedPerformanceCounter) counter;
060        }
061
062        if (this.counter instanceof DelegatePerformanceCounter) {
063            ((DelegatePerformanceCounter) this.counter).setCounter(mpc);
064        } else if (mpc != null) {
065            this.counter = mpc;
066        } else if (counter instanceof PerformanceCounter) {
067            this.counter = (PerformanceCounter) counter;
068        }
069    }
070
071    @Override
072    public boolean process(final Exchange exchange, final AsyncCallback callback) {
073        final StopWatch watch = before(exchange);
074
075        // optimize to only create a new callback if needed
076        AsyncCallback ac = callback;
077        boolean newCallback = watch != null;
078        if (newCallback) {
079            ac = doneSync -> {
080                try {
081                    // record end time
082                    after(exchange, watch);
083                } finally {
084                    // and let the original callback know we are done as well
085                    callback.done(doneSync);
086                }
087            };
088        }
089
090        return processor.process(exchange, ac);
091    }
092
093    protected void beginTime(Exchange exchange) {
094        counter.processExchange(exchange, type);
095    }
096
097    protected void recordTime(Exchange exchange, long duration) {
098        if (LOG.isTraceEnabled()) {
099            LOG.trace("{}Recording duration: {} millis for exchange: {}", type != null ? type + ": " : "", duration, exchange);
100        }
101
102        if (!exchange.isFailed() && exchange.getException() == null) {
103            counter.completedExchange(exchange, duration);
104        } else {
105            counter.failedExchange(exchange);
106        }
107    }
108
109    public String getType() {
110        return type;
111    }
112
113    public void setType(String type) {
114        this.type = type;
115    }
116
117    @Override
118    public StopWatch before(Exchange exchange) {
119        // only record time if stats is enabled
120        StopWatch answer = counter != null && counter.isStatisticsEnabled() ? new StopWatch() : null;
121        if (answer != null) {
122            beginTime(exchange);
123        }
124        return answer;
125    }
126
127    @Override
128    public void after(Exchange exchange, StopWatch watch) {
129        // record end time
130        if (watch != null) {
131            recordTime(exchange, watch.taken());
132        }
133    }
134
135    @Override
136    public String toString() {
137        return "InstrumentProcessorAdvice";
138    }
139
140    @Override
141    public int getOrder() {
142        // we want instrumentation before calling the processor (but before tracer/debugger)
143        return Ordered.LOWEST - 2;
144    }
145}