public void collectMetrics()

in src/main/java/com/uber/rss/metrics/ScheduledMetricCollector.java [72:129]


    public void collectMetrics(String dataCenter, String cluster) {
        try {
            List<ServerDetail> nodes = serviceRegistry.getServers(dataCenter, cluster, Integer.MAX_VALUE, Collections.emptyList());
            int count = nodes == null ? 0 : nodes.size();
            activeNodeCount.update(count);

            numFileDescriptors.update(SystemUtils.getFileDescriptorCount());

            PooledByteBufAllocatorMetric pooledByteBufAllocatorMetric = PooledByteBufAllocator.DEFAULT.metric();
            nettyPooledHeapMemory.update(pooledByteBufAllocatorMetric.usedHeapMemory());
            nettyPooledDirectMemory.update(pooledByteBufAllocatorMetric.usedDirectMemory());

            MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
            jvmUsedHeapMemory.update(memoryUsage.getUsed());

            // check whether current host is first server.
            // if current host is first server, check whether other servers are reachable.
            // this is to make sure there is only one server to do such checking to avoid servers ping each other too much
            if (nodes != null && !nodes.isEmpty()) {
                String firstServerConnectionString = nodes.stream().min(new Comparator<ServerDetail>() {
                    @Override
                    public int compare(ServerDetail o1, ServerDetail o2) {
                        return StringUtils.compare(o1.getConnectionString(), o2.getConnectionString());
                    }
                })
                    .get()
                    .getConnectionString();
                String localHostName = NetworkUtils.getLocalHostName();
                boolean isMyselfFirstServer = firstServerConnectionString.toLowerCase().contains(localHostName.toLowerCase());
                if (isMyselfFirstServer) {
                    long startTime = System.currentTimeMillis();
                    List<String> unreachableHostList = new ArrayList<>(nodes.size());
                    for (ServerDetail node: nodes) {
                        if (node.getConnectionString().equals(firstServerConnectionString)) {
                            continue;
                        }
                        ServerHostAndPort hostAndPort = ServerHostAndPort.fromString(node.getConnectionString());
                        boolean isReachable = NetworkUtils.isReachable(hostAndPort.getHost(), NetworkUtils.DEFAULT_REACHABLE_TIMEOUT);
                        if (!isReachable) {
                            unreachableHostList.add(hostAndPort.getHost());
                        }
                    }
                    unreachableHosts.update(unreachableHostList.size());
                    if (!unreachableHostList.isEmpty()) {
                        logger.warn(String.format("Detected unreachable hosts: %s", StringUtils.join(unreachableHostList, ",")));
                    }
                    unreachableHostsCheckLatency.update(System.currentTimeMillis() - startTime);
                }
            }

            // check disk storage available space
            long usableSpace = FileUtils.getFileStoreUsableSpace();
            fileStoreUsableSpace.update(usableSpace);
        } catch (Throwable ex) {
            M3Stats.addException(ex, this.getClass().getSimpleName());
            logger.warn("Failed to collect metrics", ex);
        }
    }