-
Notifications
You must be signed in to change notification settings - Fork 609
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Undertow 2.1.7.final+ worker thread pool metrics. (#744)
- Loading branch information
1 parent
d53f04b
commit b358267
Showing
9 changed files
with
305 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...e/skywalking/apm/plugin/undertow/worker/thread/pool/XnioWorkerConstructorInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.apm.plugin.undertow.worker.thread.pool; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import org.apache.skywalking.apm.agent.core.meter.MeterFactory; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; | ||
import org.apache.skywalking.apm.plugin.undertow.worker.thread.pool.util.XnioWorkerTaskPoolAccessor; | ||
import org.xnio.XnioWorker; | ||
|
||
public class XnioWorkerConstructorInterceptor implements InstanceConstructorInterceptor { | ||
|
||
private static final String THREAD_POOL_NAME = "undertow_worker_pool"; | ||
|
||
private static final Map<String, Function<XnioWorkerTaskPoolAccessor, Supplier<Double>>> METRIC_MAP = new HashMap<String, Function<XnioWorkerTaskPoolAccessor, Supplier<Double>>>() {{ | ||
put("core_pool_size", (XnioWorkerTaskPoolAccessor threadPoolExecutor) -> () -> (double) threadPoolExecutor.getCorePoolSize()); | ||
put("max_pool_size", (XnioWorkerTaskPoolAccessor threadPoolExecutor) -> () -> (double) threadPoolExecutor.getMaximumPoolSize()); | ||
put("pool_size", (XnioWorkerTaskPoolAccessor threadPoolExecutor) -> () -> (double) threadPoolExecutor.getPoolSize()); | ||
put("queue_size", (XnioWorkerTaskPoolAccessor threadPoolExecutor) -> () -> (double) threadPoolExecutor.getQueueSize()); | ||
put("active_size", (XnioWorkerTaskPoolAccessor threadPoolExecutor) -> () -> (double) threadPoolExecutor.getActiveCount()); | ||
}}; | ||
|
||
@Override | ||
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) throws Throwable { | ||
buildThreadPoolMeterMetric(new XnioWorkerTaskPoolAccessor((XnioWorker) objInst)); | ||
} | ||
|
||
private void buildThreadPoolMeterMetric(XnioWorkerTaskPoolAccessor xnioWorkerTaskPoolAccessor) { | ||
String threadPoolMeterName = "thread_pool"; | ||
String poolNameTag = "pool_name"; | ||
String metricTypeTag = "metric_type"; | ||
METRIC_MAP.forEach((key, value) -> { | ||
if (Objects.equals(key, "pool_size")) { | ||
if (xnioWorkerTaskPoolAccessor.isContainsGetPoolSizeMethod()) { | ||
MeterFactory.gauge(threadPoolMeterName, value.apply(xnioWorkerTaskPoolAccessor)) | ||
.tag(poolNameTag, THREAD_POOL_NAME).tag(metricTypeTag, key).build(); | ||
} | ||
} else { | ||
MeterFactory.gauge(threadPoolMeterName, value.apply(xnioWorkerTaskPoolAccessor)) | ||
.tag(poolNameTag, THREAD_POOL_NAME).tag(metricTypeTag, key).build(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...g/apm/plugin/undertow/worker/thread/pool/define/XnioWorkerConstructorInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.apm.plugin.undertow.worker.thread.pool.define; | ||
|
||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.StaticMethodsInterceptPoint; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassEnhancePluginDefine; | ||
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.any; | ||
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName; | ||
|
||
/** | ||
* xnio task pool new implementation since 3.6.0 | ||
* https://github.com/xnio/xnio/commit/071800e0a85c9da9b88a976ac7ecb85760924dbf | ||
*/ | ||
public class XnioWorkerConstructorInstrumentation extends ClassEnhancePluginDefine { | ||
|
||
private static final String XNIO_WORKER_CLASS = "org.xnio.XnioWorker"; | ||
|
||
private static final String UNDERTOW_WORKER_THREAD_POOL_INTERCEPT = "org.apache.skywalking.apm.plugin.undertow.worker.thread.pool.XnioWorkerConstructorInterceptor"; | ||
|
||
@Override | ||
protected String[] witnessClasses() { | ||
return new String[] {"org.xnio.XnioWorker$EnhancedQueueExecutorTaskPool"}; | ||
} | ||
|
||
@Override | ||
protected ClassMatch enhanceClass() { | ||
return byName(XNIO_WORKER_CLASS); | ||
} | ||
|
||
@Override | ||
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { | ||
return new ConstructorInterceptPoint[] { | ||
new ConstructorInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getConstructorMatcher() { | ||
return any(); | ||
} | ||
|
||
@Override | ||
public String getConstructorInterceptor() { | ||
return UNDERTOW_WORKER_THREAD_POOL_INTERCEPT; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { | ||
return new InstanceMethodsInterceptPoint[0]; | ||
} | ||
|
||
@Override | ||
public StaticMethodsInterceptPoint[] getStaticMethodsInterceptPoints() { | ||
return new StaticMethodsInterceptPoint[0]; | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
...he/skywalking/apm/plugin/undertow/worker/thread/pool/util/XnioWorkerTaskPoolAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.apm.plugin.undertow.worker.thread.pool.util; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import lombok.Getter; | ||
import org.xnio.XnioWorker; | ||
|
||
public class XnioWorkerTaskPoolAccessor { | ||
|
||
private final Object taskPool; | ||
@Getter | ||
private boolean containsGetPoolSizeMethod; | ||
|
||
private Method getCorePoolSizeMethod; | ||
private Method getMaximumPoolSizeMethod; | ||
private Method getActiveCountMethod; | ||
private Method getPoolSizeMethod; | ||
private Method getQueueSizeMethod; | ||
|
||
public XnioWorkerTaskPoolAccessor(final XnioWorker worker) throws NoSuchFieldException, IllegalAccessException { | ||
Field field = worker.getClass().getSuperclass().getDeclaredField("taskPool"); | ||
field.setAccessible(true); | ||
this.taskPool = field.get(worker); | ||
|
||
try { | ||
getCorePoolSizeMethod = taskPool.getClass().getDeclaredMethod("getCorePoolSize"); | ||
getCorePoolSizeMethod.setAccessible(true); | ||
} catch (NoSuchMethodException e) { | ||
// ignore | ||
} | ||
try { | ||
getMaximumPoolSizeMethod = taskPool.getClass().getDeclaredMethod("getMaximumPoolSize"); | ||
getMaximumPoolSizeMethod.setAccessible(true); | ||
} catch (NoSuchMethodException e) { | ||
// ignore | ||
} | ||
try { | ||
getActiveCountMethod = taskPool.getClass().getDeclaredMethod("getActiveCount"); | ||
getActiveCountMethod.setAccessible(true); | ||
} catch (NoSuchMethodException e) { | ||
// ignore | ||
} | ||
try { | ||
// getPoolSize add since 3.8.0 | ||
getPoolSizeMethod = taskPool.getClass().getDeclaredMethod("getPoolSize"); | ||
getPoolSizeMethod.setAccessible(true); | ||
containsGetPoolSizeMethod = true; | ||
} catch (NoSuchMethodException e) { | ||
containsGetPoolSizeMethod = false; | ||
} | ||
try { | ||
getQueueSizeMethod = taskPool.getClass().getDeclaredMethod("getQueueSize"); | ||
getQueueSizeMethod.setAccessible(true); | ||
} catch (NoSuchMethodException e) { | ||
// ignore | ||
} | ||
} | ||
|
||
public int getCorePoolSize() { | ||
try { | ||
return (int) getCorePoolSizeMethod.invoke(taskPool); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public int getMaximumPoolSize() { | ||
try { | ||
return (int) getMaximumPoolSizeMethod.invoke(taskPool); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public int getActiveCount() { | ||
try { | ||
return (int) getActiveCountMethod.invoke(taskPool); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public int getPoolSize() { | ||
try { | ||
return (int) getPoolSizeMethod.invoke(taskPool); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public int getQueueSize() { | ||
try { | ||
return (int) getQueueSizeMethod.invoke(taskPool); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.