Skip to content

Commit

Permalink
[pinpoint-apm#10741] Added applicationId
Browse files Browse the repository at this point in the history
  • Loading branch information
smilu97 committed Mar 19, 2024
1 parent bc79029 commit cf77496
Show file tree
Hide file tree
Showing 194 changed files with 2,873 additions and 718 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package com.navercorp.pinpoint.bootstrap;

import com.navercorp.pinpoint.common.PinpointConstants;
import com.navercorp.pinpoint.common.util.AgentUuidUtils;
import com.navercorp.pinpoint.common.util.StringUtils;
import com.navercorp.pinpoint.common.util.UuidUtils;

import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -46,6 +48,7 @@ public class AgentIdResolver {
private final List<AgentProperties> agentPropertyList;

private final IdValidator idValidator = new IdValidator();
private final IdValidator applicationNameValidator = new IdValidator(PinpointConstants.APPLICATION_NAME_MAX_LEN);

public AgentIdResolver(List<AgentProperties> agentPropertyList) {
this.agentPropertyList = Objects.requireNonNull(agentPropertyList, "agentPropertyList");
Expand Down Expand Up @@ -74,7 +77,7 @@ public AgentIds resolve() {
}

private String newRandomAgentId() {
UUID agentUUID = UUID.randomUUID();
UUID agentUUID = UuidUtils.createV4();
return AgentUuidUtils.encode(agentUUID);
}

Expand Down Expand Up @@ -112,7 +115,7 @@ private String getApplicationName() {
if (StringUtils.isEmpty(applicationName)) {
continue;
}
if (idValidator.validateApplicationName(agentProperty.getType(), applicationName)) {
if (applicationNameValidator.validateApplicationName(agentProperty.getType(), applicationName)) {
logger.info(agentProperty.getType() + " " + agentProperty.getApplicationNameKey() + "=" + applicationName);
source = applicationName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.navercorp.pinpoint.bootstrap.util.NetworkUtils;
import com.navercorp.pinpoint.common.PinpointConstants;
import com.navercorp.pinpoint.common.Version;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.common.util.IdValidateUtils;
Expand Down Expand Up @@ -54,8 +55,8 @@ public AgentInformationProvider(@AgentId String agentId, @AgentName String agent
Objects.requireNonNull(agentId, "agentId");
Objects.requireNonNull(applicationName, "applicationName");

this.agentId = checkId("agentId", agentId);
this.applicationName = checkId("applicationName", applicationName);
this.agentId = checkId("agentId", agentId, PinpointConstants.AGENT_ID_MAX_LEN);
this.applicationName = checkId("applicationName", applicationName, PinpointConstants.APPLICATION_NAME_MAX_LEN);
this.agentName = agentName;
this.isContainer = isContainer;
this.agentStartTime = agentStartTime;
Expand All @@ -68,7 +69,6 @@ public AgentInformation get() {
}

public AgentInformation createAgentInformation() {

final String machineName = NetworkUtils.getHostName();
final String hostIp = NetworkUtils.getRepresentationHostIp();

Expand All @@ -77,10 +77,11 @@ public AgentInformation createAgentInformation() {
return new DefaultAgentInformation(agentId, agentName, applicationName, isContainer, agentStartTime, pid, machineName, hostIp, serverType, jvmVersion, Version.VERSION);
}

private String checkId(String keyName,String id) {
if (!IdValidateUtils.validateId(id)) {
private String checkId(String keyName, String id, int maxLen) {
if (!IdValidateUtils.validateId(id, maxLen)) {
throw new IllegalArgumentException("invalid " + keyName + "=" + id);
}
return id;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public AlarmMailTemplate(AlarmCheckerInterface checker, String pinpointUrl, Stri

public String createSubject() {
RuleInterface rule = checker.getRule();
return String.format("[PINPOINT-%s] %s Alarm for %s Service. #%d", batchEnv, rule.getCheckerName(), rule.getApplicationId(), sequenceCount);
return String.format("[PINPOINT-%s] %s Alarm for %s Service. #%d", batchEnv, rule.getCheckerName(), rule.getApplicationName(), sequenceCount);
}

public String getCurrentTime() {
Expand All @@ -58,21 +58,18 @@ public String getCurrentTime() {

public String createBody() {
RuleInterface rule = checker.getRule();
return newBody(createSubject(), rule.getCheckerName(), rule.getApplicationId(), rule.getServiceType(), getCurrentTime());
return newBody(createSubject(), rule.getCheckerName(), rule.getApplicationName(), rule.getServiceType(), getCurrentTime());
}

private String newBody(String subject, String rule, String applicationId, String serviceType, String currentTime) {
StringBuilder body = new StringBuilder();
body.append("<strong>").append(subject).append("</strong>");
body.append(LINE_FEED);
body.append(LINE_FEED);
body.append(String.format("Rule : %s", rule));
body.append(LINE_FEED);
body.append(checker.getEmailMessage(pinpointUrl, applicationId, serviceType, currentTime));
body.append(String.format(LINK_FORMAT, pinpointUrl));
body.append(LINE_FEED);
body.append(String.format(SCATTER_CHART_LINK_FORMAT, pinpointUrl, applicationId, serviceType, currentTime, applicationId));

return body.toString();
return "<strong>" + subject + "</strong>" +
LINE_FEED +
LINE_FEED +
String.format("Rule : %s", rule) +
LINE_FEED +
checker.getEmailMessage(pinpointUrl, applicationId, serviceType, currentTime) +
String.format(LINK_FORMAT, pinpointUrl) +
LINE_FEED +
String.format(SCATTER_CHART_LINK_FORMAT, pinpointUrl, applicationId, serviceType, currentTime, applicationId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.navercorp.pinpoint.batch.alarm.checker.AlarmCheckerInterface;
import com.navercorp.pinpoint.batch.alarm.checker.PinotAlarmCheckerInterface;

/**
* @author Jongjin.Bae
Expand All @@ -28,7 +27,7 @@ public class WebhookPayload {

private final String pinpointUrl;
private final String batchEnv;
private final String applicationId;
private final String applicationName;
private final String serviceType;
private final String checkerName;
private final String checkerType;
Expand All @@ -43,7 +42,7 @@ public WebhookPayload(String pinpointUrl, String batchEnv, AlarmCheckerInterface
this.pinpointUrl = pinpointUrl;
this.batchEnv = batchEnv;

this.applicationId = checker.getRule().getApplicationId();
this.applicationName = checker.getRule().getApplicationName();
this.serviceType = checker.getRule().getServiceType();
this.checkerName = checker.getRule().getCheckerName();
this.checkerType = checker.getCheckerType();
Expand All @@ -63,8 +62,8 @@ public String getBatchEnv() {
return batchEnv;
}

public String getApplicationId() {
return applicationId;
public String getApplicationName() {
return applicationName;
}

public String getServiceType() {
Expand Down Expand Up @@ -108,7 +107,7 @@ public String toString() {
return "WebhookPayload{" +
"pinpointUrl='" + pinpointUrl + '\'' +
", batchEnv='" + batchEnv + '\'' +
", applicationId='" + applicationId + '\'' +
", applicationId='" + applicationName + '\'' +
", serviceType='" + serviceType + '\'' +
", checkerName='" + checkerName + '\'' +
", checkerType='" + checkerType + '\'' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,30 @@
public class WebhookPayloadSerializer extends JsonSerializer<WebhookPayload> {

@Override
public void serialize(WebhookPayload webhookPayload, JsonGenerator jgen, SerializerProvider serializers) throws IOException {
jgen.writeStartObject();
public void serialize(WebhookPayload webhookPayload, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();

jgen.writeStringField("pinpointUrl", webhookPayload.getPinpointUrl());
jgen.writeStringField("batchEnv", webhookPayload.getBatchEnv());
jgen.writeStringField("applicationId", webhookPayload.getApplicationId());
jgen.writeStringField("serviceType", webhookPayload.getServiceType());
jgen.writeObjectField("userGroup", webhookPayload.getUserGroup());
gen.writeStringField("pinpointUrl", webhookPayload.getPinpointUrl());
gen.writeStringField("batchEnv", webhookPayload.getBatchEnv());
gen.writeStringField("applicationId", webhookPayload.getApplicationName());
gen.writeStringField("serviceType", webhookPayload.getServiceType());
gen.writeObjectField("userGroup", webhookPayload.getUserGroup());

writeChecker(webhookPayload, jgen);
writeChecker(webhookPayload, gen);

jgen.writeStringField("unit", webhookPayload.getUnit());
gen.writeStringField("unit", webhookPayload.getUnit());
Number threshold = webhookPayload.getThreshold();
if (threshold instanceof Integer integer) {
jgen.writeNumberField("threshold", integer);
gen.writeNumberField("threshold", integer);
} else if (threshold instanceof BigDecimal bigDecimal){
jgen.writeNumberField("threshold", bigDecimal);
gen.writeNumberField("threshold", bigDecimal);
} else {
throw new IOException("threshold type should be either Integer or BigDecimal");
}
jgen.writeStringField("notes", webhookPayload.getNotes());
jgen.writeNumberField("sequenceCount", webhookPayload.getSequenceCount());
gen.writeStringField("notes", webhookPayload.getNotes());
gen.writeNumberField("sequenceCount", webhookPayload.getSequenceCount());

jgen.writeEndObject();
gen.writeEndObject();
}

private void writeChecker(WebhookPayload webhookPayload, JsonGenerator jgen) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public interface RuleInterface {
String getCheckerName();
String getApplicationId();
String getApplicationName();
String getServiceType();
Number getThreshold();
String getNotes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import com.navercorp.pinpoint.web.alarm.CheckerCategory;
import com.navercorp.pinpoint.web.alarm.DataCollectorCategory;
import com.navercorp.pinpoint.web.alarm.vo.Rule;
import com.navercorp.pinpoint.web.dao.ApplicationIndexDao;
import com.navercorp.pinpoint.web.service.AgentInfoService;
import com.navercorp.pinpoint.web.service.AlarmService;
import com.navercorp.pinpoint.web.service.ApplicationService;
import com.navercorp.pinpoint.web.vo.Application;
import jakarta.annotation.Nonnull;
import org.springframework.batch.item.ItemProcessor;
Expand All @@ -37,6 +37,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

Expand All @@ -51,7 +52,7 @@ public class AlarmProcessor implements ItemProcessor<Application, AppAlarmChecke

private final DataCollectorFactory dataCollectorFactory;

private final ApplicationIndexDao applicationIndexDao;
private final ApplicationService applicationService;

private final AgentInfoService agentInfoService;

Expand All @@ -60,13 +61,13 @@ public class AlarmProcessor implements ItemProcessor<Application, AppAlarmChecke
public AlarmProcessor(
DataCollectorFactory dataCollectorFactory,
AlarmService alarmService,
ApplicationIndexDao applicationIndexDao,
ApplicationService applicationService,
AgentInfoService agentInfoService,
CheckerRegistry checkerRegistry
) {
this.dataCollectorFactory = Objects.requireNonNull(dataCollectorFactory, "dataCollectorFactory");
this.alarmService = Objects.requireNonNull(alarmService, "alarmService");
this.applicationIndexDao = Objects.requireNonNull(applicationIndexDao, "applicationIndexDao");
this.applicationService = Objects.requireNonNull(applicationService, "applicationService");
this.agentInfoService = Objects.requireNonNull(agentInfoService, "agentInfoService");
this.checkerRegistry = Objects.requireNonNull(checkerRegistry, "checkerRegistry");
}
Expand All @@ -85,7 +86,7 @@ public AppAlarmChecker process(@Nonnull Application application) {
}

private List<AlarmChecker<?>> getAlarmCheckers(Application application) {
List<Rule> rules = alarmService.selectRuleByApplicationId(application.getName());
List<Rule> rules = alarmService.selectRuleByApplicationId(application.name());

long now = System.currentTimeMillis();
Supplier<List<String>> agentIds = getAgentIdsSupplier(application, now);
Expand All @@ -102,11 +103,11 @@ private List<AlarmChecker<?>> getAlarmCheckers(Application application) {

private Supplier<List<String>> getAgentIdsSupplier(Application application, long now) {
Range range = Range.between(now - activeDuration, now);
return Suppliers.memoize(() -> fetchActiveAgents(application.getName(), range));
return Suppliers.memoize(() -> fetchActiveAgents(application.id(), range));
}

private List<String> fetchActiveAgents(String applicationId, Range activeRange) {
return applicationIndexDao.selectAgentIds(applicationId)
private List<String> fetchActiveAgents(UUID applicationId, Range activeRange) {
return this.applicationService.getAgents(applicationId)
.stream()
.filter(id -> agentInfoService.isActiveAgent(id, activeRange))
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

package com.navercorp.pinpoint.batch.alarm;

import com.navercorp.pinpoint.web.dao.ApplicationIndexDao;
import com.navercorp.pinpoint.web.service.AlarmService;
import com.navercorp.pinpoint.web.service.ApplicationService;
import com.navercorp.pinpoint.web.vo.Application;
import jakarta.annotation.Nonnull;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.item.ItemReader;

import jakarta.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand All @@ -36,13 +36,13 @@
*/
public class AlarmReader implements ItemReader<Application>, StepExecutionListener {

private final ApplicationIndexDao applicationIndexDao;
private final ApplicationService applicationService;
private final AlarmService alarmService;

private Queue<Application> applicationQueue;

public AlarmReader(ApplicationIndexDao applicationIndexDao, AlarmService alarmService) {
this.applicationIndexDao = Objects.requireNonNull(applicationIndexDao, "applicationIndexDao");
public AlarmReader(ApplicationService applicationService, AlarmService alarmService) {
this.applicationService = Objects.requireNonNull(applicationService, "applicationService");
this.alarmService = Objects.requireNonNull(alarmService, "alarmService");
}

Expand All @@ -56,12 +56,12 @@ public void beforeStep(@Nonnull StepExecution stepExecution) {
}

private List<Application> fetchApplications() {
List<Application> applications = applicationIndexDao.selectAllApplicationNames();
List<Application> applications = this.applicationService.getApplications();
List<String> validApplicationIds = alarmService.selectApplicationId();

List<Application> validApplications = new ArrayList<>(applications.size());
for (Application application: applications) {
if (validApplicationIds.contains(application.getName())) {
if (validApplicationIds.contains(application.name())) {
validApplications.add(application);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ public String getUnit() {
public void check() {
dataCollector.collect();
detected = decideResult(getDetectedValue());
logger.info("{} result is {} for application ({}). value is {}. (threshold : {}).", this.getClass().getSimpleName(), detected, rule.getApplicationId(), getDetectedValue(), rule.getThreshold());
logger.info("{} result is {} for application ({}). value is {}. (threshold : {}).", this.getClass().getSimpleName(), detected, rule.getApplicationName(), getDetectedValue(), rule.getThreshold());
}

public List<String> getSmsMessage() {
List<String> messages = new ArrayList<>();
messages.add(String.format("[PINPOINT Alarm - %s] %s is %s%s (Threshold : %s%s)", rule.getApplicationId(), rule.getCheckerName(), getDetectedValue(), unit, rule.getThreshold(), unit));
messages.add(String.format("[PINPOINT Alarm - %s] %s is %s%s (Threshold : %s%s)", rule.getApplicationName(), rule.getCheckerName(), getDetectedValue(), unit, rule.getThreshold(), unit));
return messages;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ protected Long getDetectedValue() {

@Override
public String getEmailMessage(String pinpointUrl, String applicationId, String serviceType, String currentTime) {
return String.format("%s value is %s%s during the past 5 mins.(Threshold : %s%s) %s For From '%s' To '%s'.<br>", rule.getCheckerName(), getDetectedValue(), unit, rule.getThreshold(), unit, rule.getCheckerName(), rule.getApplicationId(), rule.getNotes());
return String.format("%s value is %s%s during the past 5 mins.(Threshold : %s%s) %s For From '%s' To '%s'.<br>",
rule.getCheckerName(), getDetectedValue(), unit, rule.getThreshold(), unit, rule.getCheckerName(), rule.getApplicationName(), rule.getNotes());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ protected Long getDetectedValue() {

@Override
public String getEmailMessage(String pinpointUrl, String applicationId, String serviceType, String currentTime) {
return String.format("%s value is %s%s during the past 5 mins.(Threshold : %s%s) %s For From '%s' To '%s'.<br>", rule.getCheckerName(), getDetectedValue(), unit, rule.getThreshold(), unit, rule.getCheckerName(), rule.getApplicationId(), rule.getNotes());
return String.format("%s value is %s%s during the past 5 mins.(Threshold : %s%s) %s For From '%s' To '%s'.<br>",
rule.getCheckerName(), getDetectedValue(), unit, rule.getThreshold(), unit, rule.getCheckerName(), rule.getApplicationName(), rule.getNotes());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ protected Long getDetectedValue() {

@Override
public String getEmailMessage(String pinpointUrl, String applicationId, String serviceType, String currentTime) {
return String.format("%s value is %s%s during the past 5 mins.(Threshold : %s%s) %s For From '%s' To '%s'.<br>", rule.getCheckerName(), getDetectedValue(), unit, rule.getThreshold(), unit, rule.getCheckerName(), rule.getApplicationId(), rule.getNotes());
return String.format("%s value is %s%s during the past 5 mins.(Threshold : %s%s) %s For From '%s' To '%s'.<br>",
rule.getCheckerName(), getDetectedValue(), unit, rule.getThreshold(), unit, rule.getCheckerName(), rule.getApplicationName(), rule.getNotes());
}

}

0 comments on commit cf77496

Please sign in to comment.