Skip to content

Commit

Permalink
[pinpoint-apm#10741] Added service id
Browse files Browse the repository at this point in the history
  • Loading branch information
smilu97 committed Apr 2, 2024
1 parent 129a5fd commit f40870c
Show file tree
Hide file tree
Showing 114 changed files with 2,068 additions and 497 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.navercorp.pinpoint.bootstrap;

import com.navercorp.pinpoint.common.PinpointConstants;
import com.navercorp.pinpoint.common.id.ServiceId;
import com.navercorp.pinpoint.common.util.AgentUuidUtils;
import com.navercorp.pinpoint.common.util.StringUtils;
import com.navercorp.pinpoint.common.util.UuidUtils;
Expand All @@ -30,17 +31,17 @@
*/
public class AgentIdResolver {
public static final String APPLICATION_NAME = "applicationName";
public static final String AGENT_ID = "agentId";
public static final String SERVICE_NAME = "serviceName";
public static final String AGENT_NAME = "agentName";

public static final String SYSTEM_PROPERTY_PREFIX = "pinpoint.";
public static final String APPLICATION_NAME_SYSTEM_PROPERTY = SYSTEM_PROPERTY_PREFIX + "applicationName";
public static final String AGENT_ID_SYSTEM_PROPERTY = SYSTEM_PROPERTY_PREFIX + "agentId";
public static final String SERVICE_NAME_SYSTEM_PROPERTY = SYSTEM_PROPERTY_PREFIX + "serviceName";
public static final String AGENT_NAME_SYSTEM_PROPERTY = SYSTEM_PROPERTY_PREFIX + "agentName";

public static final String ENV_PROPERTY_PREFIX = "PINPOINT_";
public static final String APPLICATION_NAME_ENV_PROPERTY = ENV_PROPERTY_PREFIX + "APPLICATION_NAME";
public static final String AGENT_ID_ENV_PROPERTY = ENV_PROPERTY_PREFIX + "AGENT_ID";
public static final String SERVICE_NAME_ENV_PROPERTY = ENV_PROPERTY_PREFIX + "SERVICE_NAME";
public static final String AGENT_NAME_ENV_PROPERTY = ENV_PROPERTY_PREFIX + "AGENT_NAME";

private final BootLogger logger = BootLogger.getLogger(this.getClass());
Expand All @@ -49,53 +50,41 @@ public class AgentIdResolver {

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

public AgentIdResolver(List<AgentProperties> agentPropertyList) {
this.agentPropertyList = Objects.requireNonNull(agentPropertyList, "agentPropertyList");
}

public AgentIds resolve() {
String agentId = getAgentId();
if (StringUtils.isEmpty(agentId)) {
logger.info("Failed to resolve AgentId(-Dpinpoint.agentId)");
agentId = newRandomAgentId();
logger.info("Auto generate AgentId='" + agentId + "'");
}
String agentId = newRandomAgentId();

final String applicationName = getApplicationName();
if (StringUtils.isEmpty(applicationName)) {
logger.warn("Failed to resolve ApplicationName(-Dpinpoint.applicationName)");
return null;
}

String serviceName = getServiceName();
if (StringUtils.isEmpty(serviceName)) {
logger.info("Failed to resolve ServiceName(-Dpinpoint.serviceName)");
serviceName = ServiceId.DEFAULT_SERVICE_NAME;
logger.info("Using default serviceName='" + agentId + "'");
}

final String agentName = getAgentName();
if (StringUtils.isEmpty(agentName)) {
logger.info("No AgentName(-Dpinpoint.agentName) provided, it's optional!");
}

return new AgentIds(agentId, agentName, applicationName);
return new AgentIds(agentId, agentName, applicationName, serviceName);
}

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

private String getAgentId() {
String source = null;
for (AgentProperties agentProperty : agentPropertyList) {
final String agentId = agentProperty.getAgentId();
if (StringUtils.isEmpty(agentId)) {
continue;
}
if (idValidator.validateAgentId(agentProperty.getType(), agentId)) {
logger.info(agentProperty.getType() + " " + agentProperty.getAgentIdKey() + "=" + agentId);
source = agentId;
}
}
return source;
}

private String getAgentName() {
String source = "";
for (AgentProperties agentProperty : agentPropertyList) {
Expand Down Expand Up @@ -123,4 +112,19 @@ private String getApplicationName() {
return source;
}

private String getServiceName() {
String source = null;
for (AgentProperties agentProperty : agentPropertyList) {
final String serviceName = agentProperty.getServiceName();
if (StringUtils.isEmpty(serviceName)) {
continue;
}
if (serviceNameValidator.validateServiceName(agentProperty.getType(), serviceName)) {
logger.info(agentProperty.getType() + " " + agentProperty.getServiceNameKey() + "=" + serviceName);
source = serviceName;
}
}
return source;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,29 @@ public void addSystemProperties(Properties system) {
Objects.requireNonNull(system, "system");

AgentProperties systemProperties = new AgentProperties(AgentIdSourceType.SYSTEM, system,
AgentIdResolver.AGENT_ID_SYSTEM_PROPERTY,
AgentIdResolver.AGENT_NAME_SYSTEM_PROPERTY,
AgentIdResolver.APPLICATION_NAME_SYSTEM_PROPERTY);
AgentIdResolver.APPLICATION_NAME_SYSTEM_PROPERTY,
AgentIdResolver.SERVICE_NAME_SYSTEM_PROPERTY);
this.agentProperties.add(systemProperties);
}

public void addEnvProperties(Map<String, String> env) {
Objects.requireNonNull(env, "env");

AgentProperties envProperties = new AgentProperties(AgentIdSourceType.SYSTEM_ENV, env,
AgentIdResolver.AGENT_ID_ENV_PROPERTY,
AgentIdResolver.AGENT_NAME_ENV_PROPERTY,
AgentIdResolver.APPLICATION_NAME_ENV_PROPERTY);
AgentIdResolver.APPLICATION_NAME_ENV_PROPERTY,
AgentIdResolver.SERVICE_NAME_ENV_PROPERTY);
this.agentProperties.add(envProperties);
}

public void addAgentArgument(Map<String, String> agentArguments) {
Objects.requireNonNull(agentArguments, "agentArguments");

AgentProperties agentArgument = new AgentProperties(AgentIdSourceType.AGENT_ARGUMENT, agentArguments,
AgentIdResolver.AGENT_ID,
AgentIdResolver.AGENT_NAME,
AgentIdResolver.APPLICATION_NAME);
AgentIdResolver.APPLICATION_NAME,
AgentIdResolver.SERVICE_NAME);
this.agentProperties.add(agentArgument);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ public class AgentIds {
private final String agentId;
private final String agentName;
private final String applicationName;
private final String serviceName;

public AgentIds(String agentId, String agentName, String applicationName) {
public AgentIds(String agentId, String agentName, String applicationName, String serviceName) {
this.agentId = Objects.requireNonNull(agentId, "agentId");
this.agentName = agentName;
this.applicationName = Objects.requireNonNull(applicationName, "applicationName");
this.serviceName = Objects.requireNonNull(serviceName, "serviceName");
}


public String getAgentId() {
return agentId;
}
Expand All @@ -44,4 +45,9 @@ public String getAgentName() {
public String getApplicationName() {
return applicationName;
}

public String getServiceName() {
return serviceName;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public interface AgentOption {

String getApplicationName();

String getServiceName();

boolean isContainer();

ProfilerConfig getProfilerConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@
public class AgentProperties {
private final AgentIdSourceType type;
private final Properties properties;
private final String agentIdKey;
private final String agentNameKey;
private final String applicationNameKey;
private final String serviceNameKey;

public AgentProperties(AgentIdSourceType type, Properties properties, String agentIdKey, String agentNameKey, String applicationNameKey) {
public AgentProperties(AgentIdSourceType type, Properties properties, String agentNameKey, String applicationNameKey, String serviceNameKey) {
this.type = Objects.requireNonNull(type, "type");
this.properties = Objects.requireNonNull(properties, "properties");
this.agentIdKey = Objects.requireNonNull(agentIdKey, "agentIdKey");
this.agentNameKey = Objects.requireNonNull(agentNameKey, "agentNameKey");
this.applicationNameKey = Objects.requireNonNull(applicationNameKey, "applicationNameKey");
this.serviceNameKey = Objects.requireNonNull(serviceNameKey, "serviceNameKey");
}

public AgentProperties(AgentIdSourceType type, Map<String, String> properties, String agentIdKey, String agentNameKey, String applicationNameKey) {
this(type, toProperties(properties), agentIdKey, agentNameKey, applicationNameKey);
public AgentProperties(AgentIdSourceType type, Map<String, String> properties, String agentNameKey, String applicationNameKey, String serviceNameKey) {
this(type, toProperties(properties), agentNameKey, applicationNameKey, serviceNameKey);
}

private static Properties toProperties(Map<String, String> properties) {
Expand All @@ -54,18 +54,10 @@ public AgentIdSourceType getType() {
return type;
}

public String getAgentId() {
return trim(this.properties.getProperty(agentIdKey));
}

public String getAgentName() {
return trim(this.properties.getProperty(agentNameKey));
}

public String getAgentIdKey() {
return agentIdKey;
}

public String getAgentNameKey() {
return agentNameKey;
}
Expand All @@ -78,6 +70,14 @@ public String getApplicationNameKey() {
return applicationNameKey;
}

public String getServiceName() {
return trim(this.properties.getProperty(serviceNameKey));
}

public String getServiceNameKey() {
return serviceNameKey;
}

private String trim(String string) {
if (string == null) {
return null;
Expand All @@ -87,13 +87,11 @@ private String trim(String string) {

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("AgentProperties{");
sb.append("type=").append(type);
sb.append(", properties=").append(properties);
sb.append(", agentIdKey='").append(agentIdKey).append('\'');
sb.append(", agentNameKey='").append(agentNameKey).append('\'');
sb.append(", applicationNameKey='").append(applicationNameKey).append('\'');
sb.append('}');
return sb.toString();
return "AgentProperties{" + "type=" + type +
", properties=" + properties +
", agentNameKey='" + agentNameKey + '\'' +
", applicationNameKey='" + applicationNameKey + '\'' +
", serviceNameKey='" + serviceNameKey + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,21 @@ public class DefaultAgentOption implements AgentOption {
private final String agentId;
private final String agentName;
private final String applicationName;
private final String serviceName;
private final boolean isContainer;

private final ProfilerConfig profilerConfig;
private final List<String> pluginJars;
private final List<String> bootstrapJarPaths;

public DefaultAgentOption(final Instrumentation instrumentation,
String agentId, String agentName, String applicationName, final boolean isContainer,
String agentId, String agentName, String applicationName, String serviceName, final boolean isContainer,
final ProfilerConfig profilerConfig, final List<String> pluginJars, final List<String> bootstrapJarPaths) {
this.instrumentation = Objects.requireNonNull(instrumentation, "instrumentation");
this.agentId = Objects.requireNonNull(agentId, "agentId");
this.agentName = Objects.requireNonNull(agentName, "agentName");
this.applicationName = Objects.requireNonNull(applicationName, "applicationName");
this.serviceName = Objects.requireNonNull(serviceName, "serviceName");
this.isContainer = isContainer;
this.profilerConfig = Objects.requireNonNull(profilerConfig, "profilerConfig");
this.pluginJars = Objects.requireNonNull(pluginJars, "pluginJars");
Expand All @@ -71,6 +73,11 @@ public String getApplicationName() {
return applicationName;
}

@Override
public String getServiceName() {
return serviceName;
}

@Override
public boolean isContainer() {
return isContainer;
Expand All @@ -93,16 +100,15 @@ public ProfilerConfig getProfilerConfig() {

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("DefaultAgentOption{");
sb.append("instrumentation=").append(instrumentation);
sb.append(", agentId='").append(agentId).append('\'');
sb.append(", agentName='").append(agentName).append('\'');
sb.append(", applicationName='").append(applicationName).append('\'');
sb.append(", isContainer=").append(isContainer);
sb.append(", profilerConfig=").append(profilerConfig);
sb.append(", pluginJars=").append(pluginJars);
sb.append(", bootstrapJarPaths=").append(bootstrapJarPaths);
sb.append('}');
return sb.toString();
return "DefaultAgentOption{" + "instrumentation=" + instrumentation +
", agentId='" + agentId + '\'' +
", agentName='" + agentName + '\'' +
", applicationName='" + applicationName + '\'' +
", serviceName='" + serviceName + '\'' +
", isContainer=" + isContainer +
", profilerConfig=" + profilerConfig +
", pluginJars=" + pluginJars +
", bootstrapJarPaths=" + bootstrapJarPaths +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public boolean validateApplicationName(AgentIdSourceType type, String applicatio
return validate0(type + " applicationName", applicationName);
}

public boolean validateServiceName(AgentIdSourceType type, String serviceName) {
Objects.requireNonNull(serviceName, "serviceName");
return validate0(type + " serviceName", serviceName);
}

public boolean validateAgentName(AgentIdSourceType type, String agentName) {
if (StringUtils.isEmpty(agentName)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ boolean start() {
logger.warn("applicationName is null");
return false;
}
final String serviceName = agentIds.getServiceName();
if (serviceName == null) {
logger.warn("serviceName is null");
return false;
}

final ContainerResolver containerResolver = new ContainerResolver();
final boolean isContainer = containerResolver.isContainer();
Expand All @@ -123,7 +128,6 @@ boolean start() {
}

// set the path of log file as a system property
saveAgentIdForLog(agentIds);
saveLogFilePath(agentDirectory.getAgentLogFilePath());
savePinpointVersion();

Expand All @@ -144,7 +148,7 @@ boolean start() {

final List<Path> pluginJars = agentDirectory.getPlugins();
final String agentName = agentIds.getAgentName();
AgentOption option = createAgentOption(agentId, agentName, applicationName, isContainer,
AgentOption option = createAgentOption(agentId, agentName, applicationName, serviceName, isContainer,
profilerConfig,
instrumentation,
pluginJars,
Expand Down Expand Up @@ -258,14 +262,16 @@ private String getAgentType() {

}

private AgentOption createAgentOption(String agentId, String agentName, String applicationName, boolean isContainer,
private AgentOption createAgentOption(String agentId, String agentName, String applicationName, String serviceName,
boolean isContainer,
ProfilerConfig profilerConfig,
Instrumentation instrumentation,
List<Path> pluginJars,
List<Path> bootstrapJarPaths) {
List<String> pluginJarStrPath = toPathList(pluginJars);
List<String> bootstrapJarPathStrPath = toPathList(bootstrapJarPaths);
return new DefaultAgentOption(instrumentation, agentId, agentName, applicationName, isContainer, profilerConfig, pluginJarStrPath, bootstrapJarPathStrPath);
return new DefaultAgentOption(instrumentation, agentId, agentName, applicationName, serviceName,
isContainer, profilerConfig, pluginJarStrPath, bootstrapJarPathStrPath);
}

private List<String> toPathList(List<Path> paths) {
Expand All @@ -281,10 +287,6 @@ void setSystemProperty(SimpleProperty systemProperty) {
this.systemProperty = systemProperty;
}

private void saveAgentIdForLog(AgentIds agentIds) {
systemProperty.setProperty(AgentIdResolver.AGENT_ID_SYSTEM_PROPERTY, agentIds.getAgentId());
}

private void saveLogFilePath(Path agentLogFilePath) {
logger.info("logPath:" + agentLogFilePath);
systemProperty.setProperty(ProductInfo.NAME + ".log", agentLogFilePath.toString());
Expand Down
Loading

0 comments on commit f40870c

Please # to comment.