Skip to content

Commit 0a438d0

Browse files
committed
[#12502] Add alias support for agent profiles
1 parent 793ad90 commit 0a438d0

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

agent-module/agent/src/main/resources/pinpoint-root.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
# Support external property
1212
# - -Dpinpoint.config=$MY_CONFIG_PATH/pinpoint.config
1313
pinpoint.profiler.profiles.active=release
14+
# Profiles aliases
15+
# If the profile is not found, it's treated as an alias
16+
# multiple aliases can be separated by commas.
17+
# Format: pinpoint.profiler.profiles.aliases.<profile>=<alias1>,<alias2>
18+
#pinpoint.profiler.profiles.aliases.release=prod,production
1419

1520
# Pinpoint Disable (default : false)
1621
# Using VM option or environmental variable will disable Pinpoint agent more strictly

agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/ProfilePropertyLoader.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
import com.navercorp.pinpoint.bootstrap.BootLogger;
2020
import com.navercorp.pinpoint.bootstrap.util.ProfileConstants;
21+
import com.navercorp.pinpoint.bootstrap.util.StringUtils;
2122

2223
import java.nio.file.Path;
2324
import java.nio.file.Paths;
25+
import java.util.ArrayList;
2426
import java.util.List;
2527
import java.util.Map;
2628
import java.util.Objects;
@@ -134,11 +136,55 @@ private String getActiveProfile(Properties defaultProperties) {
134136
return supportedProfile.toString();
135137
}
136138
}
139+
140+
// handle alias
141+
String profileFromAlias = resolveAlias(defaultProperties, profile);
142+
if (profileFromAlias != null) {
143+
return profileFromAlias;
144+
}
137145
throw new IllegalStateException("unsupported profile:" + profile);
138146
}
139147

140148
private void loadProperties(Properties dstProperties, Properties property) {
141149
Map<Object, Object> copy = PropertyLoaderUtils.filterAllowedPrefix(property);
142150
dstProperties.putAll(copy);
143151
}
152+
153+
private String resolveAlias(Properties defaultProperties, String profile) {
154+
if (StringUtils.isEmpty(profile)) {
155+
return null;
156+
}
157+
158+
for (Path supportedProfile : supportedProfiles) {
159+
String supportedProfileName = supportedProfile.toString();
160+
String aliasesStr = javaSystemProperty.getProperty(ProfileConstants.PROFILE_ALIAS_KEY_PREFIX + supportedProfileName);
161+
if (aliasesStr == null) {
162+
aliasesStr = defaultProperties.getProperty(ProfileConstants.PROFILE_ALIAS_KEY_PREFIX + supportedProfileName);
163+
if (aliasesStr == null) {
164+
continue;
165+
}
166+
}
167+
168+
List<String> aliases = splitAndTrim(aliasesStr);
169+
for (String aliasForSupportedProfile : aliases) {
170+
if (aliasForSupportedProfile.equalsIgnoreCase(profile)) {
171+
logger.info(String.format("resolved profile alias '%s' to supported profile '%s'", profile, supportedProfileName));
172+
return supportedProfileName;
173+
}
174+
}
175+
}
176+
return null;
177+
}
178+
179+
private List<String> splitAndTrim(String str) {
180+
List<String> result = new ArrayList<>();
181+
String[] splits = str.split(",");
182+
for (String part : splits) {
183+
String trimmed = part.trim();
184+
if (!trimmed.isEmpty()) {
185+
result.add(trimmed);
186+
}
187+
}
188+
return result;
189+
}
144190
}

agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/util/ProfileConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public enum CONFIG_LOAD_MODE {
1212

1313
public static final String ACTIVE_PROFILE_KEY = "pinpoint.profiler.profiles.active";
1414

15+
public static final String PROFILE_ALIAS_KEY_PREFIX = "pinpoint.profiler.profiles.aliases.";
16+
1517
// 1. default config
1618
public static final String CONFIG_FILE_NAME = "pinpoint-root.config";
1719
// 2. profile config

0 commit comments

Comments
 (0)