Skywalking-04:扩展Metric监控信息

扩展 Metric 监控信息 官方文档

Source and Scope extension for new metrics

案例:JVM Thread 增加 Metrics 修改 Thread 的定义

在 apm-protocol/apm-network/src/main/proto/language-agent/JVMMetric.proto 协议文件中覆盖 message Thread 的定义

message Thread { int64 liveCount = 1; int64 daemonCount = 2; int64 peakCount = 3; int64 deadlocked = 4; int64 monitorDeadlocked = 5; int64 newThreadCount = 7; int64 runnableThreadCount = 8; int64 blockedThreadCount = 9; int64 waitThreadCount = 10; int64 timeWaitThreadCount = 11; int64 terminatedThreadCount = 12; }

重新构建 apm-network 项目

cd apm-protocol/apm-network mvn clean package -DskipTests=true

PS:可以安装 Protocol Buffer Editor 插件,支持 Protocol Buffer 语法

修改 agent core 中 Thread Metrics 的提供类

直接使用如下代码覆盖 org.apache.skywalking.apm.agent.core.jvm.thread.ThreadProvider 类

/* * 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 * * * * 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.agent.core.jvm.thread; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; import java.lang.management.ThreadMXBean; import java.util.Optional; import org.apache.skywalking.apm.network.language.agent.v3.Thread; public enum ThreadProvider { INSTANCE; private final ThreadMXBean threadMXBean; private static final long [] EMPTY_DEADLOCKED_THREADS = new long[0]; ThreadProvider() { this.threadMXBean = ManagementFactory.getThreadMXBean(); } public Thread getThreadMetrics() { int newThreadCount = 0; int runnableThreadCount = 0; int blockedThreadCount = 0; int waitThreadCount = 0; int timeWaitThreadCount = 0; int terminatedThreadCount = 0; // 基于线程状态信息增加对应状态的线程数 ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds()); if (threadInfos != null) { for (ThreadInfo threadInfo : threadInfos) { if (threadInfo != null) { switch (threadInfo.getThreadState()) { case NEW: newThreadCount++; break; case RUNNABLE: runnableThreadCount++; break; case BLOCKED: blockedThreadCount++; break; case WAITING: waitThreadCount++; break; case TIMED_WAITING: timeWaitThreadCount++; break; case TERMINATED: terminatedThreadCount++; break; default: break; } } else { /* * If a thread of a given ID is not alive or does not exist, * the corresponding element in the returned array will, * contain null,because is mut exist ,so the thread is terminated */ terminatedThreadCount++; } } } // 当前存活线程数 int threadCount = threadMXBean.getThreadCount(); // deamon线程数 int daemonThreadCount = threadMXBean.getDaemonThreadCount(); // 峰值线程数 int peakThreadCount = threadMXBean.getPeakThreadCount(); int deadlocked = Optional.ofNullable(threadMXBean.findDeadlockedThreads()) .orElse(EMPTY_DEADLOCKED_THREADS).length; int monitorDeadlocked = Optional.ofNullable(threadMXBean.findMonitorDeadlockedThreads()) .orElse(EMPTY_DEADLOCKED_THREADS).length; // 构建一个Thread对象,用于发送Thread Metric信息至OAP return Thread.newBuilder().setLiveCount(threadCount) .setDaemonCount(daemonThreadCount) .setPeakCount(peakThreadCount) .setDeadlocked(deadlocked) .setMonitorDeadlocked(monitorDeadlocked) .setNewThreadCount(newThreadCount) .setRunnableThreadCount(runnableThreadCount) .setBlockedThreadCount(blockedThreadCount) .setWaitThreadCount(waitThreadCount) .setTimeWaitThreadCount(timeWaitThreadCount) .setTerminatedThreadCount(terminatedThreadCount) .build(); } } 修改 ServiceInstanceJVMThread

直接使用如下代码覆盖 org.apache.skywalking.oap.server.core.source.ServiceInstanceJVMThread 类,
ServiceInstanceJVMThread继承了 Source 抽象类, Source 类是 Skywalking 中 oal 体系,资源及范围的定义。

/* * 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 * * * * 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.oap.server.core.source; import lombok.Getter; import lombok.Setter; import static org.apache.skywalking.oap.server.core.source.DefaultScopeDefine.SERVICE_INSTANCE_CATALOG_NAME; import static org.apache.skywalking.oap.server.core.source.DefaultScopeDefine.SERVICE_INSTANCE_JVM_THREAD; @ScopeDeclaration(id = SERVICE_INSTANCE_JVM_THREAD, name = "ServiceInstanceJVMThread", catalog = SERVICE_INSTANCE_CATALOG_NAME) @ScopeDefaultColumn.VirtualColumnDefinition(fieldName = "entityId", columnName = "entity_id", isID = true, type = String.class) public class ServiceInstanceJVMThread extends Source { @Override public int scope() { return SERVICE_INSTANCE_JVM_THREAD; } @Override public String getEntityId() { return String.valueOf(id); } @Getter @Setter private String id; @Getter @Setter @ScopeDefaultColumn.DefinedByField(columnName = "name", requireDynamicActive = true) private String name; @Getter @Setter @ScopeDefaultColumn.DefinedByField(columnName = "service_name", requireDynamicActive = true) private String serviceName; @Getter @Setter @ScopeDefaultColumn.DefinedByField(columnName = "service_id") private String serviceId; @Getter @Setter private long liveCount; @Getter @Setter private long daemonCount; @Getter @Setter private long peakCount; @Getter @Setter private long deadlocked; @Getter @Setter private long monitorDeadlocked; @Getter @Setter private long newThreadCount; @Getter @Setter private long runnableThreadCount; @Getter @Setter private long blockedThreadCount; @Getter @Setter private long waitThreadCount; @Getter @Setter private long timeWaitThreadCount; @Getter @Setter private long terminatedThreadCount; } 修改 JVMSourceDispatcher

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwzdsg.html