-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redirect InstrumentationConfig requests to our own ConfigProvider
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...ain/java/datadog/trace/bootstrap/otel/instrumentation/internal/InstrumentationConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package datadog.trace.bootstrap.otel.instrumentation.internal; | ||
|
||
import datadog.trace.bootstrap.config.provider.ConfigProvider; | ||
import java.time.Duration; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** Redirects requests to our own {@link ConfigProvider}. */ | ||
public final class InstrumentationConfig { | ||
private static final InstrumentationConfig INSTANCE = new InstrumentationConfig(); | ||
|
||
private static final Pattern DURATION_PATTERN = Pattern.compile("(\\d+)(ms|[DdHhMmSs]?)"); | ||
|
||
private static final ConfigProvider delegate = ConfigProvider.getInstance(); | ||
|
||
public static InstrumentationConfig get() { | ||
return INSTANCE; | ||
} | ||
|
||
public String getString(String name) { | ||
return delegate.getString(name); | ||
} | ||
|
||
public String getString(String name, String defaultValue) { | ||
return delegate.getString(name, defaultValue); | ||
} | ||
|
||
public boolean getBoolean(String name, boolean defaultValue) { | ||
return delegate.getBoolean(name, defaultValue); | ||
} | ||
|
||
public int getInt(String name, int defaultValue) { | ||
return delegate.getInteger(name, defaultValue); | ||
} | ||
|
||
public long getLong(String name, long defaultValue) { | ||
return delegate.getLong(name, defaultValue); | ||
} | ||
|
||
public double getDouble(String name, double defaultValue) { | ||
return delegate.getDouble(name, defaultValue); | ||
} | ||
|
||
public Duration getDuration(String name, Duration defaultValue) { | ||
String durationString = delegate.getString(name); | ||
if (null == durationString) { | ||
return defaultValue; | ||
} | ||
Matcher matcher = DURATION_PATTERN.matcher(durationString); | ||
if (matcher.matches()) { | ||
long value = Integer.parseInt(matcher.group(1)); | ||
String unit = matcher.group(2); | ||
if ("D".equalsIgnoreCase(unit)) { | ||
return Duration.ofDays(value); | ||
} else if ("H".equalsIgnoreCase(unit)) { | ||
return Duration.ofHours(value); | ||
} else if ("M".equalsIgnoreCase(unit)) { | ||
return Duration.ofMinutes(value); | ||
} else if ("S".equalsIgnoreCase(unit)) { | ||
return Duration.ofSeconds(value); | ||
} else { | ||
return Duration.ofMillis(value); // already in ms | ||
} | ||
} else { | ||
throw new IllegalArgumentException( | ||
"Invalid duration property " + name + "=" + durationString); | ||
} | ||
} | ||
|
||
public List<String> getList(String name) { | ||
return getList(name, Collections.emptyList()); | ||
} | ||
|
||
public List<String> getList(String name, List<String> defaultValue) { | ||
return delegate.getList(name, defaultValue); | ||
} | ||
|
||
public Set<String> getSet(String name, Set<String> defaultValue) { | ||
return delegate.getSet(name, defaultValue); | ||
} | ||
|
||
public Map<String, String> getMap(String name, Map<String, String> defaultValue) { | ||
Map<String, String> map = delegate.getMergedMap(name); | ||
if (map.isEmpty()) { | ||
map = defaultValue; | ||
} | ||
return map; | ||
} | ||
} |