Skip to content

Commit

Permalink
[#7647] Includes raw data of related datasource alarm
Browse files Browse the repository at this point in the history
  • Loading branch information
koo-taejin committed Feb 10, 2021
1 parent 8766fdf commit 8640c1b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
*/
public class DataSourceConnectionUsageRateChecker extends DataSourceAlarmListValueAgentChecker {

private static String SMS_MESSAGE_FORMAT = "[PINPOINT Alarm - %s] DataSource %s connection pool usage %s%s (Threshold : %s%s, Raw : %s/%s)";
private static String EMAIL_MESSAGE_FORMAT = " Value of agent(%s) has %s%s(DataSource %s connection pool usage) during the past 5 mins.(Threshold : %s%s, Raw : %s/%s)";

public DataSourceConnectionUsageRateChecker(DataSourceDataCollector dataSourceDataCollector, Rule rule) {
super(rule, "%", dataSourceDataCollector);
}
Expand Down Expand Up @@ -61,10 +64,12 @@ protected Map<String, List<DataSourceAlarmVO>> getAgentValues() {
public List<String> getSmsMessage() {
List<String> messages = new LinkedList<>();


for (Map.Entry<String, List<DataSourceAlarmVO>> detected : detectedAgents.entrySet()) {
for (DataSourceAlarmVO dataSourceAlarmVO : detected.getValue()) {
if (decideResult0(dataSourceAlarmVO)) {
messages.add(String.format("[PINPOINT Alarm - %s] DataSource %s connection pool usage %s%s (Threshold : %s%s)", detected.getKey(), dataSourceAlarmVO.getDatabaseName(), dataSourceAlarmVO.getConnectionUsedRate(), unit, rule.getThreshold(), unit));
for (DataSourceAlarmVO eachVo : detected.getValue()) {
if (decideResult0(eachVo)) {
String message = String.format(SMS_MESSAGE_FORMAT, detected.getKey(), eachVo.getDatabaseName(), eachVo.getConnectionUsedRate(), unit, rule.getThreshold(), unit, eachVo.getActiveConnectionAvg(), eachVo.getMaxConnectionAvg());
messages.add(message);
}
}
}
Expand All @@ -74,16 +79,17 @@ public List<String> getSmsMessage() {

@Override
public String getEmailMessage() {
StringBuilder message = new StringBuilder();
StringBuilder contents = new StringBuilder();
for (Map.Entry<String, List<DataSourceAlarmVO>> detected : detectedAgents.entrySet()) {
for (DataSourceAlarmVO dataSourceAlarmVO : detected.getValue()) {
if (decideResult0(dataSourceAlarmVO)) {
message.append(String.format(" Value of agent(%s) has %s%s(DataSource %s connection pool usage) during the past 5 mins.(Threshold : %s%s)", detected.getKey(), dataSourceAlarmVO.getConnectionUsedRate(), unit, dataSourceAlarmVO.getDatabaseName(), rule.getThreshold(), unit));
for (DataSourceAlarmVO eachVo : detected.getValue()) {
if (decideResult0(eachVo)) {
String message = String.format(EMAIL_MESSAGE_FORMAT, detected.getKey(), eachVo.getConnectionUsedRate(), unit, eachVo.getDatabaseName(), rule.getThreshold(), unit, eachVo.getActiveConnectionAvg(), eachVo.getMaxConnectionAvg());
contents.append(message);
}
}

}
return message.toString();
return contents.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@

package com.navercorp.pinpoint.batch.alarm.collector;

import com.navercorp.pinpoint.batch.alarm.DataCollectorFactory;
import com.navercorp.pinpoint.batch.alarm.vo.DataSourceAlarmVO;
import com.navercorp.pinpoint.batch.util.ListUtils;
import com.navercorp.pinpoint.common.server.bo.stat.DataSourceBo;
import com.navercorp.pinpoint.common.server.bo.stat.DataSourceListBo;
import com.navercorp.pinpoint.common.util.CollectionUtils;
import com.navercorp.pinpoint.batch.util.ListUtils;
import com.navercorp.pinpoint.batch.alarm.DataCollectorFactory;
import com.navercorp.pinpoint.batch.alarm.vo.DataSourceAlarmVO;
import com.navercorp.pinpoint.web.dao.ApplicationIndexDao;
import com.navercorp.pinpoint.web.dao.stat.AgentStatDao;
import com.navercorp.pinpoint.web.vo.Application;
import com.navercorp.pinpoint.web.vo.Range;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

/**
* @author Taejin Koo
Expand Down Expand Up @@ -75,11 +77,14 @@ public void collect() {

for (Map.Entry<Integer, List<DataSourceBo>> entry : partitions.entrySet()) {
List<DataSourceBo> dataSourceBoList = entry.getValue();

if (CollectionUtils.hasLength(dataSourceBoList)) {
long usedPercent = getPercent(dataSourceBoList);
double activeConnectionAvg = dataSourceBoList.stream().mapToInt(b -> b.getActiveConnectionSize()).average().getAsDouble();
double maxConnectionAvg = dataSourceBoList.stream().mapToInt(b -> b.getMaxConnectionSize()).average().getAsDouble();

DataSourceBo dataSourceBo = ListUtils.getFirst(dataSourceBoList);
DataSourceAlarmVO dataSourceAlarmVO = new DataSourceAlarmVO(dataSourceBo.getId(), dataSourceBo.getDatabaseName(), usedPercent);
DataSourceAlarmVO dataSourceAlarmVO = new DataSourceAlarmVO(dataSourceBo.getId(), dataSourceBo.getDatabaseName(),
new Double(Math.floor(activeConnectionAvg)).intValue(), new Double(Math.floor(maxConnectionAvg)).intValue());

agentDataSourceConnectionUsageRateMap.add(agentId, dataSourceAlarmVO);
}
Expand Down Expand Up @@ -112,21 +117,6 @@ private MultiValueMap<Integer, DataSourceBo> partitionDataSourceId(List<DataSour
return result;
}

private long getPercent(List<DataSourceBo> dataSourceBos) {
long totalMaxConnectionSize = 0;
long totalActiveConnectionSize = 0;

for (DataSourceBo dataSourceBo : dataSourceBos) {
int maxConnectionSize = dataSourceBo.getMaxConnectionSize();
totalMaxConnectionSize += maxConnectionSize;

int activeConnectionSize = dataSourceBo.getActiveConnectionSize();
totalActiveConnectionSize += activeConnectionSize;
}

return calculatePercent(totalActiveConnectionSize, totalMaxConnectionSize);
}

@Override
public DataCollectorFactory.DataCollectorCategory getDataCollectorCategory() {
return super.getDataCollectorCategory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ public class DataSourceAlarmVO {

private final int id;
private final String databaseName;
private final long connectionUsedRate;
private final int activeConnectionAvg;
private final int maxConnectionAvg;

public DataSourceAlarmVO(int id, String databaseName, long connectionUsedRate) {
public DataSourceAlarmVO(int id, String databaseName, int activeConnectionAvg, int maxConnectionAvg) {
this.id = id;
this.databaseName = databaseName;
this.connectionUsedRate = connectionUsedRate;

this.activeConnectionAvg = activeConnectionAvg;
this.maxConnectionAvg = maxConnectionAvg;
}

public int getId() {
Expand All @@ -43,8 +46,20 @@ public String getDatabaseName() {
return databaseName;
}

public int getActiveConnectionAvg() {
return activeConnectionAvg;
}

public int getMaxConnectionAvg() {
return maxConnectionAvg;
}

public long getConnectionUsedRate() {
return connectionUsedRate;
if (activeConnectionAvg == 0 || maxConnectionAvg == 0) {
return 0;
} else {
return (activeConnectionAvg * 100L) / maxConnectionAvg;
}
}

@Override
Expand All @@ -55,16 +70,17 @@ public boolean equals(Object o) {
DataSourceAlarmVO that = (DataSourceAlarmVO) o;

if (id != that.id) return false;
if (connectionUsedRate != that.connectionUsedRate) return false;
if (activeConnectionAvg != that.activeConnectionAvg) return false;
if (maxConnectionAvg != that.maxConnectionAvg) return false;
return databaseName != null ? databaseName.equals(that.databaseName) : that.databaseName == null;

}

@Override
public int hashCode() {
int result = id;
result = 31 * result + (databaseName != null ? databaseName.hashCode() : 0);
result = 31 * result + (int) (connectionUsedRate ^ (connectionUsedRate >>> 32));
result = 31 * result + activeConnectionAvg;
result = 31 * result + maxConnectionAvg;
return result;
}

Expand All @@ -73,9 +89,9 @@ public String toString() {
final StringBuilder sb = new StringBuilder("DataSourceAlarmVO{");
sb.append("id=").append(id);
sb.append(", databaseName='").append(databaseName).append('\'');
sb.append(", connectionUsedRate=").append(connectionUsedRate);
sb.append(", activeConnectionAvg=").append(activeConnectionAvg);
sb.append(", maxConnectionAvg=").append(maxConnectionAvg);
sb.append('}');
return sb.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public void serialize(DataSourceAlarmVO dataSourceAlarmVO, JsonGenerator jgen, S
jgen.writeStartObject();

jgen.writeStringField("databaseName", dataSourceAlarmVO.getDatabaseName());

jgen.writeNumberField("activeConnectionAvg", dataSourceAlarmVO.getActiveConnectionAvg());
jgen.writeNumberField("maxConnectionAvg", dataSourceAlarmVO.getMaxConnectionAvg());

jgen.writeNumberField("connectionValue", dataSourceAlarmVO.getConnectionUsedRate());

jgen.writeEndObject();
Expand Down

0 comments on commit 8640c1b

Please sign in to comment.