1 /*******************************************************************************
2 * Copyright 2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
13 ******************************************************************************/
14 package org.onap.msb.sdk.httpclient.metric;
16 import static com.codahale.metrics.MetricRegistry.name;
18 import java.lang.reflect.Method;
19 import java.util.HashMap;
22 import com.codahale.metrics.JmxReporter;
23 import com.codahale.metrics.Meter;
24 import com.codahale.metrics.MetricRegistry;
25 import com.codahale.metrics.Slf4jReporter;
26 import com.codahale.metrics.Timer;
32 public class MetricManager {
34 private static MetricRegistry registry = new MetricRegistry();
36 private static Slf4jReporter slf4jReporter;
38 private static JmxReporter jmxReporter;
41 initMetricReporters();
44 protected Map<Method, Timer> timers = new HashMap<>();
45 protected Map<Method, Meter> meters = new HashMap<>();
46 protected Map<Method, Meter> exceptionmeters = new HashMap<>();
50 private static void initMetricReporters() {
53 * slf4jReporter = Slf4jReporter.forRegistry(registry)
54 * .outputTo(LoggerFactory.getLogger(RetrofitServiceHandler.class))
55 * .convertRatesTo(TimeUnit.SECONDS) .convertDurationsTo(TimeUnit.MILLISECONDS) .build();
56 * slf4jReporter.start(10, TimeUnit.MINUTES);
59 jmxReporter = JmxReporter.forRegistry(registry).build();
64 public void initMetric(Class retrofitSrvInterfaceClazz) {
66 for (Method method : retrofitSrvInterfaceClazz.getMethods()) {
68 timers.put(method, registry.timer(chooseName(method, "timer")));
69 meters.put(method, registry.meter(chooseName(method, "meter")));
70 exceptionmeters.put(method, registry.meter(chooseName(method, "exception")));
76 private String chooseName(final Method method, final String... suffixes) {
77 return name(name(method.getDeclaringClass(), method.getName()), suffixes);
80 public MetricObject getMetricObject(Method method) {
81 return new MetricObject(timers.get(method), meters.get(method), exceptionmeters.get(method));