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 5, 2024
1 parent 48194d6 commit 8ff8b0c
Show file tree
Hide file tree
Showing 234 changed files with 2,603 additions and 970 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.navercorp.pinpoint.bootstrap.plugin.jdbc.JdbcContext;
import com.navercorp.pinpoint.common.annotations.InterfaceAudience;
import com.navercorp.pinpoint.common.annotations.InterfaceStability;
import com.navercorp.pinpoint.common.id.AgentId;

/**
* @author emeroad
Expand Down Expand Up @@ -73,7 +74,7 @@ public interface TraceContext {

// ActiveThreadCounter getActiveThreadCounter();

String getAgentId();
AgentId getAgentId();

String getApplicationName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.navercorp.pinpoint.bootstrap.context;

import com.navercorp.pinpoint.common.id.AgentId;

/**
* @author emeroad
*/
Expand All @@ -27,7 +29,7 @@ public interface TraceId {

String getTransactionId();

String getAgentId();
AgentId getAgentId();

long getAgentStartTime();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,28 @@
package com.navercorp.pinpoint.bootstrap;

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

import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
* @author Woonduk Kang(emeroad)
*/
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,51 +47,32 @@ 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 + "'");
}

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='" + serviceName + "'");
}

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

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

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;
return new AgentIds(agentName, applicationName, serviceName);
}

private String getAgentName() {
Expand Down Expand Up @@ -123,4 +102,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 @@ -22,19 +22,14 @@
* @author Woonduk Kang(emeroad)
*/
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) {
this.agentId = Objects.requireNonNull(agentId, "agentId");
public AgentIds(String agentName, String applicationName, String serviceName) {
this.agentName = agentName;
this.applicationName = Objects.requireNonNull(applicationName, "applicationName");
}


public String getAgentId() {
return agentId;
this.serviceName = Objects.requireNonNull(serviceName, "serviceName");
}

public String getAgentName() {
Expand All @@ -44,4 +39,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 @@ -17,6 +17,7 @@
package com.navercorp.pinpoint.bootstrap;

import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;
import com.navercorp.pinpoint.common.id.AgentId;

import java.lang.instrument.Instrumentation;
import java.util.List;
Expand All @@ -28,12 +29,14 @@ public interface AgentOption {

Instrumentation getInstrumentation();

String getAgentId();
AgentId getAgentId();

String getAgentName();

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 + '\'' +
'}';
}
}

0 comments on commit 8ff8b0c

Please sign in to comment.