WillemJiang closed pull request #452: [JAV-544] & [SCB-10] Implement Runtime System Resources Usage Metrics URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/452
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/extra/DefaultSystemResource.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/extra/DefaultSystemResource.java new file mode 100644 index 000000000..c045dc662 --- /dev/null +++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/extra/DefaultSystemResource.java @@ -0,0 +1,97 @@ +/* + * 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 io.servicecomb.metrics.core.extra; + +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryMXBean; +import java.lang.management.OperatingSystemMXBean; +import java.lang.management.ThreadMXBean; + +import org.springframework.stereotype.Component; + +@Component +public class DefaultSystemResource implements SystemResource { + + private final OperatingSystemMXBean systemMXBean; + + private final ThreadMXBean threadMXBean; + + private final MemoryMXBean memoryMXBean; + + public DefaultSystemResource() { + this(ManagementFactory.getOperatingSystemMXBean(), ManagementFactory.getThreadMXBean(), + ManagementFactory.getMemoryMXBean()); + } + + public DefaultSystemResource(OperatingSystemMXBean systemMXBean, ThreadMXBean threadMXBean, + MemoryMXBean memoryMXBean) { + this.systemMXBean = systemMXBean; + this.threadMXBean = threadMXBean; + this.memoryMXBean = memoryMXBean; + } + + @Override + public double getCpuLoad() { + return systemMXBean.getSystemLoadAverage(); + } + + @Override + public int getCpuRunningThreads() { + return threadMXBean.getThreadCount(); + } + + @Override + public long getHeapInit() { + return memoryMXBean.getHeapMemoryUsage().getInit(); + } + + @Override + public long getHeapMax() { + return memoryMXBean.getHeapMemoryUsage().getMax(); + } + + @Override + public long getHeapCommit() { + return memoryMXBean.getHeapMemoryUsage().getCommitted(); + } + + @Override + public long getHeapUsed() { + return memoryMXBean.getHeapMemoryUsage().getUsed(); + } + + @Override + public long getNonHeapInit() { + return memoryMXBean.getNonHeapMemoryUsage().getInit(); + } + + @Override + public long getNonHeapMax() { + return memoryMXBean.getNonHeapMemoryUsage().getMax(); + } + + @Override + public long getNonHeapCommit() { + return memoryMXBean.getNonHeapMemoryUsage().getCommitted(); + } + + @Override + public long getNonHeapUsed() { + return memoryMXBean.getNonHeapMemoryUsage().getUsed(); + } +} diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/extra/SystemResource.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/extra/SystemResource.java new file mode 100644 index 000000000..d1b72beb9 --- /dev/null +++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/extra/SystemResource.java @@ -0,0 +1,40 @@ +/* + * 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 io.servicecomb.metrics.core.extra; + +public interface SystemResource { + double getCpuLoad(); + + int getCpuRunningThreads(); + + long getHeapInit(); + + long getHeapMax(); + + long getHeapCommit(); + + long getHeapUsed(); + + long getNonHeapInit(); + + long getNonHeapMax(); + + long getNonHeapCommit(); + + long getNonHeapUsed(); +} diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/metric/InstanceMetric.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/metric/InstanceMetric.java index 1bb5b6339..c3e8fa1b1 100644 --- a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/metric/InstanceMetric.java +++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/metric/InstanceMetric.java @@ -18,8 +18,17 @@ package io.servicecomb.metrics.core.metric; public class InstanceMetric extends ModelMetric { - public InstanceMetric(long waitInQueue, TimerMetric lifeTimeInQueue, - TimerMetric executionTime, TimerMetric consumerLatency, TimerMetric producerLatency) { + + private final SystemMetric systemMetric; + + public SystemMetric getSystemMetric() { + return systemMetric; + } + + public InstanceMetric(long waitInQueue, SystemMetric systemMetric, + TimerMetric lifeTimeInQueue, TimerMetric executionTime, TimerMetric consumerLatency, + TimerMetric producerLatency) { super(waitInQueue, lifeTimeInQueue, executionTime, consumerLatency, producerLatency); + this.systemMetric = systemMetric; } } diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/metric/RegistryMetric.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/metric/RegistryMetric.java index 4008251d9..7569420bc 100644 --- a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/metric/RegistryMetric.java +++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/metric/RegistryMetric.java @@ -19,6 +19,7 @@ import java.util.Map; +import io.servicecomb.metrics.core.extra.SystemResource; import io.servicecomb.metrics.core.monitor.RegistryMonitor; public class RegistryMetric { @@ -35,7 +36,7 @@ public InstanceMetric getInstanceMetric() { return invocationMetrics; } - public RegistryMetric(RegistryMonitor registryMonitor, int pollerIndex) { + public RegistryMetric(SystemResource systemResource, RegistryMonitor registryMonitor, int pollerIndex) { invocationMetrics = registryMonitor.toInvocationMetrics(pollerIndex); //sum instance level metric @@ -51,6 +52,14 @@ public RegistryMetric(RegistryMonitor registryMonitor, int pollerIndex) { consumerLatency = consumerLatency.merge(metric.getConsumerLatency()); producerLatency = producerLatency.merge(metric.getProducerLatency()); } - instanceMetric = new InstanceMetric(waitInQueue, lifeTimeInQueue, executionTime, consumerLatency, producerLatency); + + SystemMetric systemMetric = new SystemMetric(systemResource.getCpuLoad(), systemResource.getCpuRunningThreads(), + systemResource.getHeapInit(), systemResource.getHeapMax(), systemResource.getHeapCommit(), + systemResource.getHeapUsed(), + systemResource.getNonHeapInit(), systemResource.getNonHeapMax(), systemResource.getNonHeapCommit(), + systemResource.getNonHeapUsed()); + + instanceMetric = new InstanceMetric(waitInQueue, systemMetric, lifeTimeInQueue, executionTime, consumerLatency, + producerLatency); } } diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/metric/SystemMetric.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/metric/SystemMetric.java new file mode 100644 index 000000000..be7725dfb --- /dev/null +++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/metric/SystemMetric.java @@ -0,0 +1,95 @@ +/* + * 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 io.servicecomb.metrics.core.metric; + +public class SystemMetric { + private final double cpuLoad; + + private final int cpuRunningThreads; + + private final long heapInit; + + private final long heapMax; + + private final long heapCommit; + + private final long heapUsed; + + private final long nonHeapInit; + + private final long nonHeapMax; + + private final long nonHeapCommit; + + private final long nonHeapUsed; + + public double getCpuLoad() { + return cpuLoad; + } + + public int getCpuRunningThreads() { + return cpuRunningThreads; + } + + public long getHeapInit() { + return heapInit; + } + + public long getHeapMax() { + return heapMax; + } + + public long getHeapCommit() { + return heapCommit; + } + + public long getHeapUsed() { + return heapUsed; + } + + public long getNonHeapInit() { + return nonHeapInit; + } + + public long getNonHeapMax() { + return nonHeapMax; + } + + public long getNonHeapCommit() { + return nonHeapCommit; + } + + public long getNonHeapUsed() { + return nonHeapUsed; + } + + public SystemMetric(double cpuLoad, int cpuRunningThreads, + long heapInit, long heapMax, long heapCommit, long heapUsed, + long nonHeapInit, long nonHeapMax, long nonHeapCommit, long nonHeapUsed) { + this.cpuLoad = cpuLoad; + this.cpuRunningThreads = cpuRunningThreads; + this.heapInit = heapInit; + this.heapMax = heapMax; + this.heapCommit = heapCommit; + this.heapUsed = heapUsed; + this.nonHeapInit = nonHeapInit; + this.nonHeapMax = nonHeapMax; + this.nonHeapCommit = nonHeapCommit; + this.nonHeapUsed = nonHeapUsed; + } +} diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultDataSource.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultDataSource.java index bd84e544d..70ddb18d7 100644 --- a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultDataSource.java +++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultDataSource.java @@ -31,6 +31,7 @@ import com.netflix.config.DynamicPropertyFactory; import com.netflix.servo.monitor.Pollers; +import io.servicecomb.metrics.core.extra.SystemResource; import io.servicecomb.metrics.core.metric.RegistryMetric; import io.servicecomb.metrics.core.monitor.RegistryMonitor; @@ -44,15 +45,19 @@ private final RegistryMonitor registryMonitor; + private final SystemResource systemResource; + private final Map<Integer, RegistryMetric> registryMetrics; @Autowired - public DefaultDataSource(RegistryMonitor registryMonitor) { - this(registryMonitor, DynamicPropertyFactory.getInstance().getStringProperty(METRICS_POLLING_TIME, "10000").get()); + public DefaultDataSource(SystemResource systemResource, RegistryMonitor registryMonitor) { + this(systemResource, registryMonitor, + DynamicPropertyFactory.getInstance().getStringProperty(METRICS_POLLING_TIME, "10000").get()); } - public DefaultDataSource(RegistryMonitor registryMonitor, String pollingSettings) { + public DefaultDataSource(SystemResource systemResource, RegistryMonitor registryMonitor, String pollingSettings) { this.registryMetrics = new ConcurrentHashMap<>(); + this.systemResource = systemResource; this.registryMonitor = registryMonitor; //??????Polling???????? Servo???10000?10??????????100?? long minPollingTime = DynamicPropertyFactory.getInstance().getLongProperty(METRICS_POLLING_MIN, 100).get(); @@ -74,7 +79,7 @@ public DefaultDataSource(RegistryMonitor registryMonitor, String pollingSettings @Override public RegistryMetric getRegistryMetric(int pollerIndex) { - return registryMetrics.getOrDefault(pollerIndex, new RegistryMetric(registryMonitor, pollerIndex)); + return registryMetrics.getOrDefault(pollerIndex, new RegistryMetric(systemResource, registryMonitor, pollerIndex)); } @Override @@ -83,6 +88,6 @@ public RegistryMetric getRegistryMetric(int pollerIndex) { } private void reloadRegistryMetric(Integer pollingIndex) { - registryMetrics.put(pollingIndex, new RegistryMetric(registryMonitor, pollingIndex)); + registryMetrics.put(pollingIndex, new RegistryMetric(systemResource, registryMonitor, pollingIndex)); } } diff --git a/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestEventAndRunner.java b/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestEventAndRunner.java index 02aeb6359..f98f04fbb 100644 --- a/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestEventAndRunner.java +++ b/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestEventAndRunner.java @@ -17,16 +17,24 @@ package io.servicecomb.metrics.core; +import static org.mockito.Mockito.when; + +import java.lang.management.MemoryMXBean; +import java.lang.management.MemoryUsage; +import java.lang.management.OperatingSystemMXBean; +import java.lang.management.ThreadMXBean; import java.util.concurrent.TimeUnit; import org.junit.Assert; import org.junit.Test; +import org.mockito.Mockito; import io.servicecomb.core.metrics.InvocationFinishedEvent; import io.servicecomb.core.metrics.InvocationStartProcessingEvent; import io.servicecomb.core.metrics.InvocationStartedEvent; import io.servicecomb.foundation.common.utils.EventUtils; import io.servicecomb.metrics.core.event.DefaultEventListenerManager; +import io.servicecomb.metrics.core.extra.DefaultSystemResource; import io.servicecomb.metrics.core.metric.RegistryMetric; import io.servicecomb.metrics.core.monitor.RegistryMonitor; import io.servicecomb.metrics.core.publish.DefaultDataSource; @@ -36,8 +44,27 @@ @Test public void test() throws InterruptedException { + OperatingSystemMXBean systemMXBean = Mockito.mock(OperatingSystemMXBean.class); + when(systemMXBean.getSystemLoadAverage()).thenReturn(1.0); + ThreadMXBean threadMXBean = Mockito.mock(ThreadMXBean.class); + when(threadMXBean.getThreadCount()).thenReturn(2); + MemoryMXBean memoryMXBean = Mockito.mock(MemoryMXBean.class); + MemoryUsage heap = Mockito.mock(MemoryUsage.class); + when(memoryMXBean.getHeapMemoryUsage()).thenReturn(heap); + when(heap.getCommitted()).thenReturn(100L); + when(heap.getInit()).thenReturn(200L); + when(heap.getMax()).thenReturn(300L); + when(heap.getUsed()).thenReturn(400L); + MemoryUsage nonHeap = Mockito.mock(MemoryUsage.class); + when(memoryMXBean.getNonHeapMemoryUsage()).thenReturn(nonHeap); + when(nonHeap.getCommitted()).thenReturn(500L); + when(nonHeap.getInit()).thenReturn(600L); + when(nonHeap.getMax()).thenReturn(700L); + when(nonHeap.getUsed()).thenReturn(800L); + RegistryMonitor monitor = new RegistryMonitor(); - DefaultDataSource dataSource = new DefaultDataSource(monitor,"2000"); + DefaultSystemResource systemResource = new DefaultSystemResource(systemMXBean, threadMXBean, memoryMXBean); + DefaultDataSource dataSource = new DefaultDataSource(systemResource, monitor, "2000"); DefaultEventListenerManager manager = new DefaultEventListenerManager(monitor); @@ -104,7 +131,6 @@ public void test() throws InterruptedException { Assert .assertEquals(model.getInstanceMetric().getExecutionTime().getAverage(), TimeUnit.MILLISECONDS.toNanos(400), 0); - Assert .assertEquals(model.getInvocationMetrics().get("fun1").getProducerLatency().getMin(), TimeUnit.MILLISECONDS.toNanos(300), 0); @@ -116,9 +142,11 @@ public void test() throws InterruptedException { TimeUnit.MILLISECONDS.toNanos(500), 0); Assert.assertEquals(model.getInstanceMetric().getProducerLatency().getMin(), TimeUnit.MILLISECONDS.toNanos(300), 0); - Assert.assertEquals(model.getInstanceMetric().getProducerLatency().getMax(), TimeUnit.MILLISECONDS.toNanos(1100), 0); Assert - .assertEquals(model.getInstanceMetric().getProducerLatency().getAverage(), TimeUnit.MILLISECONDS.toNanos(700), 0); + .assertEquals(model.getInstanceMetric().getProducerLatency().getMax(), TimeUnit.MILLISECONDS.toNanos(1100), 0); + Assert + .assertEquals(model.getInstanceMetric().getProducerLatency().getAverage(), TimeUnit.MILLISECONDS.toNanos(700), + 0); Assert .assertEquals(model.getInvocationMetrics().get("fun1").getConsumerLatency().getMin(), @@ -134,5 +162,16 @@ public void test() throws InterruptedException { Assert.assertEquals(model.getInstanceMetric().getConsumerLatency().getMax(), TimeUnit.MILLISECONDS.toNanos(0), 0); Assert .assertEquals(model.getInstanceMetric().getConsumerLatency().getAverage(), TimeUnit.MILLISECONDS.toNanos(0), 0); + + Assert.assertEquals(model.getInstanceMetric().getSystemMetric().getCpuLoad(), 1.0, 0); + Assert.assertEquals(model.getInstanceMetric().getSystemMetric().getCpuRunningThreads(), 2, 0); + Assert.assertEquals(model.getInstanceMetric().getSystemMetric().getHeapCommit(), 100, 0); + Assert.assertEquals(model.getInstanceMetric().getSystemMetric().getHeapInit(), 200, 0); + Assert.assertEquals(model.getInstanceMetric().getSystemMetric().getHeapMax(), 300, 0); + Assert.assertEquals(model.getInstanceMetric().getSystemMetric().getHeapUsed(), 400, 0); + Assert.assertEquals(model.getInstanceMetric().getSystemMetric().getNonHeapCommit(), 500, 0); + Assert.assertEquals(model.getInstanceMetric().getSystemMetric().getNonHeapInit(), 600, 0); + Assert.assertEquals(model.getInstanceMetric().getSystemMetric().getNonHeapMax(), 700, 0); + Assert.assertEquals(model.getInstanceMetric().getSystemMetric().getNonHeapUsed(), 800, 0); } -} +} \ No newline at end of file ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services