From 45cbf8b5c440d93d10c0ec9454c09a07a774c161 Mon Sep 17 00:00:00 2001 From: wusheng Date: Thu, 27 Jul 2017 22:10:37 +0800 Subject: [PATCH] Add OS Info when register and registerRecover. --- .../src/main/proto/DiscoveryService.proto | 8 ++ .../skywalking/apm/agent/core/os/OSUtil.java | 82 +++++++++++++++++++ .../remote/AppAndServiceRegisterClient.java | 3 + 3 files changed, 93 insertions(+) create mode 100644 apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/os/OSUtil.java diff --git a/apm-network/src/main/proto/DiscoveryService.proto b/apm-network/src/main/proto/DiscoveryService.proto index cda0fa286e..e94e732081 100644 --- a/apm-network/src/main/proto/DiscoveryService.proto +++ b/apm-network/src/main/proto/DiscoveryService.proto @@ -22,6 +22,7 @@ message ApplicationInstance { int32 applicationId = 1; string agentUUID = 2; int64 registerTime = 3; + OSInfo osinfo = 4; } message ApplicationInstanceMapping { @@ -33,6 +34,7 @@ message ApplicationInstanceRecover { int32 applicationId = 1; int32 applicationInstanceId = 2; int64 registerTime = 3; + OSInfo osinfo = 4; } message ApplicationInstanceHeartbeat { @@ -40,6 +42,12 @@ message ApplicationInstanceHeartbeat { int64 heartbeatTime = 2; } +message OSInfo { + string osName = 1; + string hostname = 2; + repeated string ipv4s = 3; +} + //discovery service for ServiceName by Network address or application code service ServiceNameDiscoveryService { rpc discovery (ServiceNameCollection) returns (ServiceNameMappingCollection) { diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/os/OSUtil.java b/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/os/OSUtil.java new file mode 100644 index 0000000000..469ec91a2c --- /dev/null +++ b/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/os/OSUtil.java @@ -0,0 +1,82 @@ +package org.skywalking.apm.agent.core.os; + +import java.net.Inet4Address; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.util.Enumeration; +import java.util.LinkedList; +import java.util.List; +import org.skywalking.apm.network.proto.OSInfo; + +/** + * @author wusheng + */ +public class OSUtil { + private static volatile String OS_NAME; + private static volatile String HOST_NAME; + private static volatile List IPV4_LIST; + + public static String getOsName() { + if (OS_NAME == null) { + OS_NAME = System.getProperty("os.name"); + } + return OS_NAME; + } + + public static String getHostName() { + if (HOST_NAME == null) { + try { + InetAddress host = InetAddress.getLocalHost(); + HOST_NAME = host.getHostName(); + } catch (UnknownHostException e) { + HOST_NAME = "unknown"; + } + } + return HOST_NAME; + } + + public static List getAllIPV4() { + if (IPV4_LIST == null) { + IPV4_LIST = new LinkedList(); + try { + Enumeration interfs = NetworkInterface.getNetworkInterfaces(); + while (interfs.hasMoreElements()) { + NetworkInterface networkInterface = interfs.nextElement(); + Enumeration inetAddresses = networkInterface.getInetAddresses(); + while (inetAddresses.hasMoreElements()) { + InetAddress address = inetAddresses.nextElement(); + if (address instanceof Inet4Address) { + String addressStr = address.getHostAddress(); + if ("127.0.0.1".equals(addressStr)) { + continue; + } + IPV4_LIST.add(addressStr); + } + } + } + } catch (SocketException e) { + + } + } + return IPV4_LIST; + } + + public static OSInfo buildOSInfo() { + OSInfo.Builder builder = OSInfo.newBuilder(); + String osName = getOsName(); + if (osName != null) { + builder.setOsName(osName); + } + String hostName = getHostName(); + if (hostName != null) { + builder.setHostname(hostName); + } + List allIPV4 = getAllIPV4(); + if (allIPV4.size() > 0) { + builder.addAllIpv4S(allIPV4); + } + return builder.build(); + } +} diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/remote/AppAndServiceRegisterClient.java b/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/remote/AppAndServiceRegisterClient.java index 2493616b4b..0b18cc3d56 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/remote/AppAndServiceRegisterClient.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/remote/AppAndServiceRegisterClient.java @@ -15,6 +15,7 @@ import org.skywalking.apm.agent.core.context.trace.TraceSegment; import org.skywalking.apm.agent.core.dictionary.ApplicationDictionary; import org.skywalking.apm.agent.core.dictionary.DictionaryUtil; import org.skywalking.apm.agent.core.dictionary.OperationNameDictionary; +import org.skywalking.apm.agent.core.os.OSUtil; import org.skywalking.apm.logging.ILog; import org.skywalking.apm.logging.LogManager; import org.skywalking.apm.network.proto.Application; @@ -104,6 +105,7 @@ public class AppAndServiceRegisterClient implements BootService, GRPCChannelList .setApplicationId(RemoteDownstreamConfig.Agent.APPLICATION_ID) .setAgentUUID(PROCESS_UUID) .setRegisterTime(System.currentTimeMillis()) + .setOsinfo(OSUtil.buildOSInfo()) .build()); if (instanceMapping.getApplicationInstanceId() != DictionaryUtil.nullValue()) { RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID @@ -115,6 +117,7 @@ public class AppAndServiceRegisterClient implements BootService, GRPCChannelList .setApplicationId(RemoteDownstreamConfig.Agent.APPLICATION_ID) .setApplicationInstanceId(RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID) .setRegisterTime(System.currentTimeMillis()) + .setOsinfo(OSUtil.buildOSInfo()) .build()); } else { if (lastSegmentTime - System.currentTimeMillis() > 60 * 1000) { -- GitLab