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 instrumenting
032 * processing of exchanges.
033 * <p/>
034 * This implementation has been optimised to work in dual mode, either as an advice or as a processor.
035 * The former is 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
043    private PerformanceCounter counter;
044    private String type;
045
046    public DefaultInstrumentationProcessor(String type, Processor processor) {
047        super(processor);
048        this.type = type;
049    }
050
051    public DefaultInstrumentationProcessor(String type) {
052        super(null);
053        this.type = type;
054    }
055
056    @Override
057    public void setCounter(Object counter) {
058        ManagedPerformanceCounter mpc = null;
059        if (counter instanceof ManagedPerformanceCounter) {
060            mpc = (ManagedPerformanceCounter) counter;
061        }
062
063        if (this.counter instanceof DelegatePerformanceCounter) {
064            ((DelegatePerformanceCounter) this.counter).setCounter(mpc);
065        } else if (mpc != null) {
066            this.counter = mpc;
067        } else if (counter instanceof PerformanceCounter) {
068            this.counter = (PerformanceCounter) counter;
069        }
070    }
071
072    @Override
073    public boolean process(final Exchange exchange, final AsyncCallback callback) {
074        final StopWatch watch = before(exchange);
075
076        // optimize to only create a new callback if needed
077        AsyncCallback ac = callback;
078        boolean newCallback = watch != null;
079        if (newCallback) {
080            ac = doneSync -> {
081                try {
082                    // record end time
083                    after(exchange, watch);
084                } finally {
085                    // and let the original callback know we are done as well
086                    callback.done(doneSync);
087                }
088            };
089        }
090
091        return processor.process(exchange, ac);
092    }
093
094    protected void beginTime(Exchange exchange) {
095        counter.processExchange(exchange);
096    }
097
098    protected void recordTime(Exchange exchange, long duration) {
099        if (LOG.isTraceEnabled()) {
100            LOG.trace("{}Recording duration: {} millis for exchange: {}", type != null ? type + ": " : "", duration, exchange);
101        }
102
103        if (!exchange.isFailed() && exchange.getException() == null) {
104            counter.completedExchange(exchange, duration);
105        } else {
106            counter.failedExchange(exchange);
107        }
108    }
109
110    public String getType() {
111        return type;
112    }
113
114    public void setType(String type) {
115        this.type = type;
116    }
117
118    @Override
119    public StopWatch before(Exchange exchange) {
120        // only record time if stats is enabled
121        StopWatch answer = counter != null && counter.isStatisticsEnabled() ? new StopWatch() : null;
122        if (answer != null) {
123            beginTime(exchange);
124        }
125        return answer;
126    }
127
128    @Override
129    public void after(Exchange exchange, StopWatch watch) {
130        // record end time
131        if (watch != null) {
132            recordTime(exchange, watch.taken());
133        }
134    }
135
136    @Override
137    public String toString() {
138        return "InstrumentProcessorAdvice";
139    }
140
141    @Override
142    public int getOrder() {
143        // we want instrumentation before calling the processor (but before tracer/debugger)
144        return Ordered.LOWEST - 2;
145    }
146}