|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.skywalking.apm.plugin.grizzly.workthreadpool; |
| 20 | + |
| 21 | +import org.apache.skywalking.apm.agent.core.meter.MeterFactory; |
| 22 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 23 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; |
| 24 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; |
| 25 | +import org.glassfish.grizzly.threadpool.AbstractThreadPool; |
| 26 | +import org.glassfish.grizzly.threadpool.GrizzlyExecutorService; |
| 27 | +import org.glassfish.grizzly.threadpool.ThreadPoolConfig; |
| 28 | + |
| 29 | +import java.lang.reflect.Field; |
| 30 | +import java.lang.reflect.Method; |
| 31 | + |
| 32 | +public class TransportInterceptor implements InstanceMethodsAroundInterceptor { |
| 33 | + |
| 34 | + private static final String METER_NAME = "thread_pool"; |
| 35 | + private static final String METRIC_POOL_NAME_TAG_NAME = "pool_name"; |
| 36 | + private static final String THREAD_POOL_NAME = "grizzly_execute_pool"; |
| 37 | + private static final String METRIC_TYPE_TAG_NAME = "metric_type"; |
| 38 | + |
| 39 | + @Override |
| 40 | + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable { |
| 41 | + GrizzlyExecutorService executorServices = (GrizzlyExecutorService) allArguments[0]; |
| 42 | + // reflect get private field just once |
| 43 | + Field poolField = GrizzlyExecutorService.class.getDeclaredField("pool"); |
| 44 | + poolField.setAccessible(true); |
| 45 | + AbstractThreadPool abstractThreadPool = (AbstractThreadPool) poolField.get(executorServices); |
| 46 | + ThreadPoolConfig threadPoolConfig = abstractThreadPool.getConfig(); |
| 47 | + MeterFactory.gauge(METER_NAME, () -> (double) threadPoolConfig.getCorePoolSize()) |
| 48 | + .tag(METRIC_POOL_NAME_TAG_NAME, THREAD_POOL_NAME) |
| 49 | + .tag(METRIC_TYPE_TAG_NAME, "core_pool_size") |
| 50 | + .build(); |
| 51 | + MeterFactory.gauge(METER_NAME, () -> (double) threadPoolConfig.getMaxPoolSize()) |
| 52 | + .tag(METRIC_POOL_NAME_TAG_NAME, THREAD_POOL_NAME) |
| 53 | + .tag(METRIC_TYPE_TAG_NAME, "max_pool_size") |
| 54 | + .build(); |
| 55 | + MeterFactory.gauge(METER_NAME, () -> (double) abstractThreadPool.getSize()) |
| 56 | + .tag(METRIC_POOL_NAME_TAG_NAME, THREAD_POOL_NAME) |
| 57 | + .tag(METRIC_TYPE_TAG_NAME, "pool_size") |
| 58 | + .build(); |
| 59 | + MeterFactory.gauge(METER_NAME, () -> (double) abstractThreadPool.getQueue().size()) |
| 60 | + .tag(METRIC_POOL_NAME_TAG_NAME, THREAD_POOL_NAME) |
| 61 | + .tag(METRIC_TYPE_TAG_NAME, "queue_size") |
| 62 | + .build(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable { |
| 67 | + return ret; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) { |
| 72 | + |
| 73 | + } |
| 74 | +} |
0 commit comments