msb http client
[msb/java-sdk.git] / src / main / java / org / onap / msb / sdk / httpclient / metric / MetricManager.java
1 /*******************************************************************************
2  * Copyright 2017 ZTE, Inc. and others.
3  * 
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
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
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
12  * the License.
13  ******************************************************************************/
14 package org.onap.msb.sdk.httpclient.metric;
15
16 import static com.codahale.metrics.MetricRegistry.name;
17
18 import java.lang.reflect.Method;
19 import java.util.HashMap;
20 import java.util.Map;
21
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;
27
28 /**
29  * @author hu.rui
30  *
31  */
32 public class MetricManager {
33
34   private static MetricRegistry registry = new MetricRegistry();
35
36   private static Slf4jReporter slf4jReporter;
37
38   private static JmxReporter jmxReporter;
39
40   static {
41     initMetricReporters();
42   }
43
44   protected Map<Method, Timer> timers = new HashMap<>();
45   protected Map<Method, Meter> meters = new HashMap<>();
46   protected Map<Method, Meter> exceptionmeters = new HashMap<>();
47
48
49
50   private static void initMetricReporters() {
51
52     /*
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);
57      */
58
59     jmxReporter = JmxReporter.forRegistry(registry).build();
60     jmxReporter.start();
61
62   }
63
64   public void initMetric(Class retrofitSrvInterfaceClazz) {
65
66     for (Method method : retrofitSrvInterfaceClazz.getMethods()) {
67
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")));
71     }
72
73
74   }
75
76   private String chooseName(final Method method, final String... suffixes) {
77     return name(name(method.getDeclaringClass(), method.getName()), suffixes);
78   }
79
80   public MetricObject getMetricObject(Method method) {
81     return new MetricObject(timers.get(method), meters.get(method), exceptionmeters.get(method));
82   }
83
84 }