Преглед изворни кода

core-retrofit模块增加针对okhttp的metricss事件监听

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi пре 2 година
родитељ
комит
28652828c7

+ 4 - 0
core-retrofit/pom.xml

@@ -64,6 +64,10 @@
             <groupId>com.squareup.okhttp3</groupId>
             <groupId>com.squareup.okhttp3</groupId>
             <artifactId>logging-interceptor</artifactId>
             <artifactId>logging-interceptor</artifactId>
         </dependency>
         </dependency>
+        <dependency>
+            <groupId>io.micrometer</groupId>
+            <artifactId>micrometer-core</artifactId>
+        </dependency>
         <dependency>
         <dependency>
             <groupId>junit</groupId>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <artifactId>junit</artifactId>

+ 11 - 0
core-retrofit/src/main/java/com/qmth/boot/core/retrofit/core/RetrofitFactoryBean.java

@@ -14,6 +14,8 @@ import com.qmth.boot.core.retrofit.interceptor.LoggingInterceptor;
 import com.qmth.boot.core.retrofit.interceptor.RetryInterceptor;
 import com.qmth.boot.core.retrofit.interceptor.RetryInterceptor;
 import com.qmth.boot.core.retrofit.interceptor.SignatureInterceptor;
 import com.qmth.boot.core.retrofit.interceptor.SignatureInterceptor;
 import com.qmth.boot.core.retrofit.interfaces.CustomizeRetrofitConfiguration;
 import com.qmth.boot.core.retrofit.interfaces.CustomizeRetrofitConfiguration;
+import io.micrometer.core.instrument.MeterRegistry;
+import io.micrometer.core.instrument.binder.okhttp3.OkHttpMetricsEventListener;
 import okhttp3.ConnectionPool;
 import okhttp3.ConnectionPool;
 import okhttp3.OkHttpClient;
 import okhttp3.OkHttpClient;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -36,6 +38,8 @@ public class RetrofitFactoryBean<T> implements FactoryBean<T>, ApplicationContex
 
 
     private RetrofitProperties retrofitProperties;
     private RetrofitProperties retrofitProperties;
 
 
+    private MeterRegistry meterRegistry;
+
     private ObjectMapper mapper;
     private ObjectMapper mapper;
 
 
     public RetrofitFactoryBean(Class<T> retrofitInterface) {
     public RetrofitFactoryBean(Class<T> retrofitInterface) {
@@ -111,6 +115,12 @@ public class RetrofitFactoryBean<T> implements FactoryBean<T>, ApplicationContex
             okHttpClientBuilder.addNetworkInterceptor(new LoggingInterceptor(logLevel));
             okHttpClientBuilder.addNetworkInterceptor(new LoggingInterceptor(logLevel));
         }
         }
 
 
+        // add metrics event listener
+        if (meterRegistry != null) {
+            okHttpClientBuilder
+                    .eventListener(OkHttpMetricsEventListener.builder(meterRegistry, "okhttp.client").build());
+        }
+
         return okHttpClientBuilder.build();
         return okHttpClientBuilder.build();
     }
     }
 
 
@@ -134,6 +144,7 @@ public class RetrofitFactoryBean<T> implements FactoryBean<T>, ApplicationContex
     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
         this.applicationContext = applicationContext;
         this.applicationContext = applicationContext;
         this.retrofitProperties = applicationContext.getBean(RetrofitProperties.class);
         this.retrofitProperties = applicationContext.getBean(RetrofitProperties.class);
+        this.meterRegistry = applicationContext.getBean(MeterRegistry.class);
     }
     }
 
 
 }
 }