Skip to content

Commit

Permalink
bugfix jmx memory_pool unit and time unit error (#1273)
Browse files Browse the repository at this point in the history
Co-authored-by: zhuoshangyi <zhuoshangyi@hntradesp.com>
  • Loading branch information
rbsrcy and zhuoshangyi authored Oct 9, 2023
1 parent cfc0ade commit b053261
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.dromara.hertzbeat.collector.dispatch.unit;

import java.util.concurrent.TimeUnit;

/**
* the enum of time length
* 时间长短的枚举类
* @author rbsrcy
*
*/
public enum TimeLengthUnit {
/**
* NANOSECONDS
*/
NS("NS", 1),
/**
* MICROSECONDS
*/
US("US", 1000),
/**
* MILLISECONDS
*/
MS("MS", 1000_000),
/**
* SECONDS
*/
S("S", 1000_000_000),
/**
* MINUTES
*/
MIN("MIN", 60_000_000_000L),
/**
* HOURS
*/
H("H", 3600_000_000_000L),
/**
* DAYS
*/
D("D", 86_400_000_000_000L);

private final String unit;
private final long scale;

private TimeLengthUnit(String unit, long scale) {
this.unit = unit;
this.scale = scale;
}

public String getUnit() {
return unit;
}

public long getScale() {
return scale;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.dromara.hertzbeat.collector.dispatch.unit.impl;

import org.dromara.hertzbeat.collector.dispatch.unit.TimeLengthUnit;
import org.dromara.hertzbeat.collector.dispatch.unit.UnitConvert;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
* the convert of time length
* 时间长短转换
* @author rbsrcy
*
*/
@Component
public final class TimeLengthConvert implements UnitConvert {


@Override
public String convert(String value, String originUnit, String newUnit) {
if (value == null || "".equals(value)) {
return null;
}
BigDecimal length = new BigDecimal(value);
// 思路:value通过originUnit转换为纳秒,在转换为newUnit单位对应的值
for (TimeLengthUnit timeLengthUnit : TimeLengthUnit.values()) {
if (timeLengthUnit.getUnit().equals(originUnit.toUpperCase())) {
length = length.multiply(new BigDecimal(timeLengthUnit.getScale()));
}
if (timeLengthUnit.getUnit().equals(newUnit.toUpperCase())) {
length = length.divide(new BigDecimal(timeLengthUnit.getScale()), 12, RoundingMode.HALF_UP);
}
}
return length.setScale(4, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
}

@Override
public boolean checkUnit(String unit) {
if (unit == null || "".equals(unit)) {
return false;
}
for (TimeLengthUnit timeUnit : TimeLengthUnit.values()) {
// 不区分大小写
if (timeUnit.getUnit().equals(unit.toUpperCase())) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.dromara.hertzbeat.collector.dispatch.unit;

import org.dromara.hertzbeat.collector.dispatch.unit.impl.TimeLengthConvert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Test case for {@link TimeLengthConvert}
*/
class TimeLengthConvertTest {

private TimeLengthConvert convert;

@BeforeEach
void setUp() {
this.convert = new TimeLengthConvert();
}

/**
* 测试纳秒转秒
*/
@Test
void convertNs2Sec() {
String result = convert.convert("1000000000", TimeLengthUnit.NS.getUnit(), TimeLengthUnit.S.getUnit());
assertEquals("1", result);
}

/**
* 测试纳秒转毫秒
*/
@Test
void convertNs2Ms() {
String result = convert.convert("1000123450", TimeLengthUnit.NS.getUnit(), TimeLengthUnit.MS.getUnit());
assertEquals("1000.1235", result);
}

/**
* 测试纳秒转微秒
*/
@Test
void convertNs2Us() {
String result = convert.convert("1000000000", TimeLengthUnit.NS.getUnit(), TimeLengthUnit.US.getUnit());
assertEquals("1000000", result);
}


/**
* 测试纳秒转天
*/
@Test
void convertNs2Day() {
String result = convert.convert("86400000000000", TimeLengthUnit.NS.getUnit(), TimeLengthUnit.D.getUnit());
assertEquals("1", result);
}


}
3 changes: 3 additions & 0 deletions manager/src/main/resources/define/app-activemq.yml
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ metrics:
- field: CurrentThreadCpuTime
type: 0
unit: s
units:
- CurrentThreadUserTime=NS->S
- CurrentThreadCpuTime=NS->S
protocol: jmx
jmx:
host: ^_^host^_^
Expand Down
3 changes: 3 additions & 0 deletions manager/src/main/resources/define/app-hadoop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ metrics:
- field: CurrentThreadCpuTime
type: 0
unit: s
units:
- CurrentThreadUserTime=NS->S
- CurrentThreadCpuTime=NS->S
protocol: jmx
jmx:
host: ^_^host^_^
Expand Down
3 changes: 3 additions & 0 deletions manager/src/main/resources/define/app-jetty.yml
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ metrics:
- field: CurrentThreadCpuTime
type: 0
unit: s
units:
- CurrentThreadUserTime=NS->S
- CurrentThreadCpuTime=NS->S
# the protocol used for monitoring, eg: sql, ssh, http, telnet, wmi, snmp, sdk
# 用于监控的协议, 比如: sql, ssh, http, telnet, wmi, snmp, sdk
protocol: jmx
Expand Down
3 changes: 3 additions & 0 deletions manager/src/main/resources/define/app-jvm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ metrics:
- field: CurrentThreadCpuTime
type: 0
unit: s
units:
- CurrentThreadUserTime=NS->S
- CurrentThreadCpuTime=NS->S
protocol: jmx
jmx:
host: ^_^host^_^
Expand Down
3 changes: 3 additions & 0 deletions manager/src/main/resources/define/app-spark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ metrics:
- field: CurrentThreadCpuTime
type: 0
unit: s
units:
- CurrentThreadUserTime=NS->S
- CurrentThreadCpuTime=NS->S
protocol: jmx
jmx:
host: ^_^host^_^
Expand Down
3 changes: 3 additions & 0 deletions manager/src/main/resources/define/app-tomcat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ metrics:
- field: CurrentThreadCpuTime
type: 0
unit: s
units:
- CurrentThreadUserTime=NS->S
- CurrentThreadCpuTime=NS->S
protocol: jmx
jmx:
host: ^_^host^_^
Expand Down

0 comments on commit b053261

Please sign in to comment.