Skip to content

Commit 19ad84b

Browse files
committed
♻️ refactor HypertraceConfig to use thread-safe AgentConfigSupplier
1 parent c1eb262 commit 19ad84b

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

javaagent-core/src/main/java/org/hypertrace/agent/core/config/HypertraceConfig.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ private HypertraceConfig() {}
5151
// so avoiding for perf reasons
5252
private static volatile boolean servletCausingException;
5353

54-
private static AgentConfig agentConfig;
54+
private static final AgentConfigSupplier agentConfigSupplier =
55+
new AgentConfigSupplier(HypertraceConfig::load);
5556

5657
static final String DEFAULT_SERVICE_NAME = "unknown";
5758
static final String DEFAULT_REPORTING_ENDPOINT = "http://localhost:9411/api/v2/spans";
@@ -61,21 +62,7 @@ private HypertraceConfig() {}
6162
static final int DEFAULT_BODY_MAX_SIZE_BYTES = 128 * 1024;
6263

6364
public static AgentConfig get() {
64-
if (agentConfig == null) {
65-
synchronized (HypertraceConfig.class) {
66-
if (agentConfig == null) {
67-
try {
68-
agentConfig = load();
69-
log.info(
70-
"Config loaded: {}",
71-
JsonFormat.printer().omittingInsignificantWhitespace().print(agentConfig));
72-
} catch (IOException e) {
73-
throw new RuntimeException("Could not load config", e);
74-
}
75-
}
76-
}
77-
}
78-
return agentConfig;
65+
return agentConfigSupplier.get();
7966
}
8067

8168
public static boolean isInstrumentationEnabled(String primaryName, String[] otherNames) {
@@ -123,16 +110,20 @@ private static boolean isHypertraceType(String message) {
123110
/** Reset the config, use only in tests. */
124111
@VisibleForTesting
125112
public static void reset() {
126-
agentConfig = null;
113+
agentConfigSupplier.reset();
127114
}
128115

129-
private static AgentConfig load() throws IOException {
116+
private static AgentConfig load() {
130117
String configFile = EnvironmentConfig.getProperty(EnvironmentConfig.CONFIG_FILE_PROPERTY);
131118
if (configFile == null) {
132119
return EnvironmentConfig.applyPropertiesAndEnvVars(applyDefaults(AgentConfig.newBuilder()))
133120
.build();
134121
}
135-
return load(configFile);
122+
try {
123+
return load(configFile);
124+
} catch (IOException e) {
125+
throw new RuntimeException("Could not load config", e);
126+
}
136127
}
137128

138129
@VisibleForTesting

0 commit comments

Comments
 (0)