|
| 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.asf.dubbo; |
| 20 | + |
| 21 | +import org.apache.dubbo.common.URL; |
| 22 | +import org.apache.dubbo.remoting.transport.AbstractServer; |
| 23 | +import org.apache.skywalking.apm.agent.core.meter.MeterFactory; |
| 24 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 25 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; |
| 26 | + |
| 27 | +import java.lang.reflect.Field; |
| 28 | +import java.util.concurrent.ExecutorService; |
| 29 | +import java.util.concurrent.ThreadPoolExecutor; |
| 30 | + |
| 31 | +public class AbstractServerConstructorInterceptor implements InstanceConstructorInterceptor { |
| 32 | + private static final String METER_NAME = "thread_pool"; |
| 33 | + private static final String METRIC_POOL_NAME_TAG_NAME = "pool_name"; |
| 34 | + private static final String METRIC_TYPE_TAG_NAME = "metric_type"; |
| 35 | + |
| 36 | + @Override |
| 37 | + public void onConstruct(EnhancedInstance objInst, Object[] allArguments) throws Throwable { |
| 38 | + Field executorField = AbstractServer.class.getDeclaredField("executor"); |
| 39 | + executorField.setAccessible(true); |
| 40 | + ExecutorService executor = (ExecutorService) executorField.get(objInst); |
| 41 | + |
| 42 | + URL url = (URL) allArguments[0]; |
| 43 | + int port = url.getPort(); |
| 44 | + |
| 45 | + if (!(executor instanceof ThreadPoolExecutor)) { |
| 46 | + return; |
| 47 | + } |
| 48 | + ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor; |
| 49 | + // TODO String.format("DubboServerHandler-%s:%s", host, port) will be better |
| 50 | + String threadPoolName = String.format("DubboServerHandler-%s", port); |
| 51 | + |
| 52 | + MeterFactory.gauge(METER_NAME, () -> (double) (threadPoolExecutor.getCorePoolSize())) |
| 53 | + .tag(METRIC_POOL_NAME_TAG_NAME, threadPoolName) |
| 54 | + .tag(METRIC_TYPE_TAG_NAME, "core_pool_size") |
| 55 | + .build(); |
| 56 | + MeterFactory.gauge(METER_NAME, () -> (double) (threadPoolExecutor.getMaximumPoolSize())) |
| 57 | + .tag(METRIC_POOL_NAME_TAG_NAME, threadPoolName) |
| 58 | + .tag(METRIC_TYPE_TAG_NAME, "max_pool_size") |
| 59 | + .build(); |
| 60 | + MeterFactory.gauge(METER_NAME, () -> (double) (threadPoolExecutor.getLargestPoolSize())) |
| 61 | + .tag(METRIC_POOL_NAME_TAG_NAME, threadPoolName) |
| 62 | + .tag(METRIC_TYPE_TAG_NAME, "largest_pool_size") |
| 63 | + .build(); |
| 64 | + MeterFactory.gauge(METER_NAME, () -> (double) (threadPoolExecutor.getPoolSize())) |
| 65 | + .tag(METRIC_POOL_NAME_TAG_NAME, threadPoolName) |
| 66 | + .tag(METRIC_TYPE_TAG_NAME, "pool_size") |
| 67 | + .build(); |
| 68 | + MeterFactory.gauge(METER_NAME, () -> (double) (threadPoolExecutor.getQueue().size())) |
| 69 | + .tag(METRIC_POOL_NAME_TAG_NAME, threadPoolName) |
| 70 | + .tag(METRIC_TYPE_TAG_NAME, "queue_size") |
| 71 | + .build(); |
| 72 | + MeterFactory.gauge(METER_NAME, () -> (double) (threadPoolExecutor.getActiveCount())) |
| 73 | + .tag(METRIC_POOL_NAME_TAG_NAME, threadPoolName) |
| 74 | + .tag(METRIC_TYPE_TAG_NAME, "active_size") |
| 75 | + .build(); |
| 76 | + MeterFactory.gauge(METER_NAME, () -> (double) (threadPoolExecutor.getTaskCount())) |
| 77 | + .tag(METRIC_POOL_NAME_TAG_NAME, threadPoolName) |
| 78 | + .tag(METRIC_TYPE_TAG_NAME, "task_count") |
| 79 | + .build(); |
| 80 | + MeterFactory.gauge(METER_NAME, () -> (double) (threadPoolExecutor.getCompletedTaskCount())) |
| 81 | + .tag(METRIC_POOL_NAME_TAG_NAME, threadPoolName) |
| 82 | + .tag(METRIC_TYPE_TAG_NAME, "completed_task_count") |
| 83 | + .build(); |
| 84 | + } |
| 85 | +} |
0 commit comments