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 Mar 19, 2024
1 parent cf77496 commit 2f17c3d
Show file tree
Hide file tree
Showing 113 changed files with 2,077 additions and 448 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,16 +31,19 @@
*/
public class AgentIdResolver {
public static final String APPLICATION_NAME = "applicationName";
public static final String SERVICE_NAME = "serviceName";
public static final String AGENT_ID = "agentId";
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 SERVICE_NAME_SYSTEM_PROPERTY = SYSTEM_PROPERTY_PREFIX + "serviceName";
public static final String AGENT_ID_SYSTEM_PROPERTY = SYSTEM_PROPERTY_PREFIX + "agentId";
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 SERVICE_NAME_ENV_PROPERTY = ENV_PROPERTY_PREFIX + "SERVICE_NAME";
public static final String AGENT_ID_ENV_PROPERTY = ENV_PROPERTY_PREFIX + "AGENT_ID";
public static final String AGENT_NAME_ENV_PROPERTY = ENV_PROPERTY_PREFIX + "AGENT_NAME";

Expand All @@ -49,6 +53,7 @@ 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");
Expand All @@ -68,12 +73,19 @@ public AgentIds resolve() {
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() {
Expand Down Expand Up @@ -123,4 +135,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 @@ -34,7 +34,8 @@ public void addSystemProperties(Properties 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);
}

Expand All @@ -44,7 +45,8 @@ public void addEnvProperties(Map<String, String> 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);
}

Expand All @@ -54,7 +56,8 @@ public void addAgentArgument(Map<String, String> 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 @@ -29,17 +29,19 @@ public class AgentProperties {
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 agentIdKey, 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 agentIdKey, String agentNameKey, String applicationNameKey, String serviceNameKey) {
this(type, toProperties(properties), agentIdKey, agentNameKey, applicationNameKey, serviceNameKey);
}

private static Properties toProperties(Map<String, String> properties) {
Expand Down Expand Up @@ -78,6 +80,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 +97,12 @@ 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 +
", agentIdKey='" + agentIdKey + '\'' +
", 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 Down Expand Up @@ -144,7 +149,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 +263,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private void boot(String agentName) {
ClassLoader classLoader = AgentBootLoaderTest.class.getClassLoader();
AgentBootLoader agentBootLoader = new AgentBootLoader("com.navercorp.pinpoint.bootstrap.DummyAgent", classLoader);
Instrumentation instrumentation = mock(Instrumentation.class);
AgentOption option = new DefaultAgentOption(instrumentation, "testCaseAgent", agentName, "testCaseAppName", false,
AgentOption option = new DefaultAgentOption(instrumentation, "testCaseAgent", agentName, "testCaseAppName", "testCaseServiceName", false,
new DefaultProfilerConfig(), Collections.emptyList(), Collections.emptyList());
Agent boot = agentBootLoader.boot(option);
boot.start();
Expand Down
Loading

0 comments on commit 2f17c3d

Please sign in to comment.