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.impl.health;
018
019import java.util.Map;
020
021import org.apache.camel.api.management.mbean.ManagedRouteMBean;
022import org.apache.camel.health.HealthCheckResultBuilder;
023
024public final class RoutePerformanceCounterEvaluators {
025
026    private RoutePerformanceCounterEvaluators() {
027    }
028
029    // ********************************
030    // Helpers
031    // ********************************
032
033    public static PerformanceCounterEvaluator<ManagedRouteMBean> exchangesFailed(long threshold) {
034        return new ExchangesFailed(threshold);
035    }
036
037    public static PerformanceCounterEvaluator<ManagedRouteMBean> exchangesInflight(long threshold) {
038        return new ExchangesInflight(threshold);
039    }
040
041    public static PerformanceCounterEvaluator<ManagedRouteMBean> redeliveries(long threshold) {
042        return new Redeliveries(threshold);
043    }
044
045    public static PerformanceCounterEvaluator<ManagedRouteMBean> externalRedeliveries(long threshold) {
046        return new ExternalRedeliveries(threshold);
047    }
048
049    public static PerformanceCounterEvaluator<ManagedRouteMBean> lastProcessingTime(long timeThreshold, int failuresThreshold) {
050        return new LastProcessingTime(timeThreshold, failuresThreshold);
051    }
052
053    public static PerformanceCounterEvaluator<ManagedRouteMBean> minProcessingTime(long timeThreshold, int failuresThreshold) {
054        return new MinProcessingTime(timeThreshold, failuresThreshold);
055    }
056
057    public static PerformanceCounterEvaluator<ManagedRouteMBean> meanProcessingTime(long timeThreshold, int failuresThreshold) {
058        return new MeanProcessingTime(timeThreshold, failuresThreshold);
059    }
060
061    public static PerformanceCounterEvaluator<ManagedRouteMBean> maxProcessingTime(long timeThreshold, int failuresThreshold) {
062        return new MaxProcessingTime(timeThreshold, failuresThreshold);
063    }
064
065    // ********************************
066    // Impls
067    // ********************************
068
069    public static final class ExchangesFailed implements PerformanceCounterEvaluator<ManagedRouteMBean> {
070        private final long threshold;
071
072        public ExchangesFailed(long threshold) {
073            this.threshold = threshold;
074        }
075
076        @Override
077        public void test(ManagedRouteMBean counter, HealthCheckResultBuilder builder, Map<String, Object> options) {
078            try {
079                long value = counter.getExchangesFailed();
080                if (value > threshold) {
081                    builder.down();
082                }
083
084                builder.detail("exchanges.failed", value);
085                builder.detail("exchanges.failed.threshold", threshold);
086            } catch (Exception e) {
087            }
088        }
089    }
090
091    public static final class ExchangesInflight implements PerformanceCounterEvaluator<ManagedRouteMBean> {
092        private final long threshold;
093
094        public ExchangesInflight(long threshold) {
095            this.threshold = threshold;
096        }
097
098        @Override
099        public void test(ManagedRouteMBean counter, HealthCheckResultBuilder builder, Map<String, Object> options) {
100            try {
101                long value = counter.getExchangesInflight();
102                if (value > threshold) {
103                    builder.down();
104                }
105
106                builder.detail("exchanges.inflight", value);
107                builder.detail("exchanges.inflight.threshold", threshold);
108            } catch (Exception e) {
109            }
110        }
111    }
112
113    public static final class Redeliveries implements PerformanceCounterEvaluator<ManagedRouteMBean> {
114        private final long threshold;
115
116        public Redeliveries(long threshold) {
117            this.threshold = threshold;
118        }
119
120        @Override
121        public void test(ManagedRouteMBean counter, HealthCheckResultBuilder builder, Map<String, Object> options) {
122            try {
123                long value = counter.getRedeliveries();
124                if (value > threshold) {
125                    builder.down();
126                }
127
128                builder.detail("exchanges.redeliveries", value);
129                builder.detail("exchanges.redeliveries.threshold", threshold);
130            } catch (Exception e) {
131            }
132        }
133    }
134
135    public static final class ExternalRedeliveries implements PerformanceCounterEvaluator<ManagedRouteMBean> {
136        private final long threshold;
137
138        public ExternalRedeliveries(long threshold) {
139            this.threshold = threshold;
140        }
141
142        @Override
143        public void test(ManagedRouteMBean counter, HealthCheckResultBuilder builder, Map<String, Object> options) {
144            try {
145                long value = counter.getExternalRedeliveries();
146                if (value > threshold) {
147                    builder.down();
148                }
149
150                builder.detail("exchanges.external-redeliveries", value);
151                builder.detail("exchanges.external-redeliveries.threshold", threshold);
152            } catch (Exception e) {
153            }
154        }
155    }
156
157    public static final class LastProcessingTime implements PerformanceCounterEvaluator<ManagedRouteMBean> {
158        private final long timeThreshold;
159        private final int failuresThreshold;
160
161        private volatile int failureCount;
162
163        public LastProcessingTime(long timeThreshold, int failuresThreshold) {
164            this.timeThreshold = timeThreshold;
165            this.failuresThreshold = failuresThreshold;
166        }
167
168        @Override
169        public void test(ManagedRouteMBean counter, HealthCheckResultBuilder builder, Map<String, Object> options) {
170            try {
171                long value = counter.getLastProcessingTime();
172                if (value > timeThreshold) {
173                    failureCount++;
174
175                    if (failureCount > failuresThreshold) {
176                        builder.down();
177                    }
178                } else {
179                    failureCount = 0;
180                }
181
182                builder.detail("exchanges.last-processing-time", value);
183                builder.detail("exchanges.last-processing-time.threshold.time", timeThreshold);
184                builder.detail("exchanges.last-processing-time.threshold.failures", failuresThreshold);
185            } catch (Exception e) {
186            }
187        }
188    }
189
190    public static final class MinProcessingTime implements PerformanceCounterEvaluator<ManagedRouteMBean> {
191        private final long timeThreshold;
192        private final int failuresThreshold;
193
194        private volatile int failureCount;
195
196        public MinProcessingTime(long timeThreshold, int failuresThreshold) {
197            this.timeThreshold = timeThreshold;
198            this.failuresThreshold = failuresThreshold;
199        }
200
201        @Override
202        public void test(ManagedRouteMBean counter, HealthCheckResultBuilder builder, Map<String, Object> options) {
203            try {
204                long value = counter.getMinProcessingTime();
205                if (value > timeThreshold) {
206                    failureCount++;
207
208                    if (failureCount > failuresThreshold) {
209                        builder.down();
210                    }
211                } else {
212                    failureCount = 0;
213                }
214
215                builder.detail("exchanges.min-processing-time", value);
216                builder.detail("exchanges.min-processing-time.threshold.time", timeThreshold);
217                builder.detail("exchanges.min-processing-time.threshold.failures", failuresThreshold);
218            } catch (Exception e) {
219            }
220        }
221    }
222
223    public static final class MeanProcessingTime implements PerformanceCounterEvaluator<ManagedRouteMBean> {
224        private final long timeThreshold;
225        private final int failuresThreshold;
226
227        private volatile int failureCount;
228
229        public MeanProcessingTime(long timeThreshold, int failuresThreshold) {
230            this.timeThreshold = timeThreshold;
231            this.failuresThreshold = failuresThreshold;
232        }
233
234        @Override
235        public void test(ManagedRouteMBean counter, HealthCheckResultBuilder builder, Map<String, Object> options) {
236            try {
237                long value = counter.getMeanProcessingTime();
238                if (value > timeThreshold) {
239                    failureCount++;
240
241                    if (failureCount > failuresThreshold) {
242                        builder.down();
243                    }
244                } else {
245                    failureCount = 0;
246                }
247
248                builder.detail("exchanges.mean-processing-time", value);
249                builder.detail("exchanges.mean-processing-time.threshold.time", timeThreshold);
250                builder.detail("exchanges.mean-processing-time.threshold.failures", failuresThreshold);
251            } catch (Exception e) {
252            }
253        }
254    }
255
256    public static final class MaxProcessingTime implements PerformanceCounterEvaluator<ManagedRouteMBean> {
257        private final long timeThreshold;
258        private final int failuresThreshold;
259
260        private volatile int failureCount;
261
262        public MaxProcessingTime(long timeThreshold, int failuresThreshold) {
263            this.timeThreshold = timeThreshold;
264            this.failuresThreshold = failuresThreshold;
265        }
266
267        @Override
268        public void test(ManagedRouteMBean counter, HealthCheckResultBuilder builder, Map<String, Object> options) {
269            try {
270                long value = counter.getMaxProcessingTime();
271                if (value > timeThreshold) {
272                    failureCount++;
273
274                    if (failureCount > failuresThreshold) {
275                        builder.down();
276                    }
277                } else {
278                    failureCount = 0;
279                }
280
281                builder.detail("exchanges.max-processing-time", value);
282                builder.detail("exchanges.max-processing-time.threshold.time", timeThreshold);
283                builder.detail("exchanges.max-processing-time.threshold.failures", failuresThreshold);
284            } catch (Exception e) {
285            }
286        }
287    }
288}