Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
run: ./mvnw clean package -DskipTests

- name: Run using JDK 7
run: docker run -i --rm -v $(pwd):/app -w /app openjdk:7-jdk java -jar ./target/jmxfetch-0.51.1-SNAPSHOT-jar-with-dependencies.jar --help
run: docker run -i --rm -v $(pwd):/app -w /app azul/zulu-openjdk:7 java -jar ./target/jmxfetch-0.51.1-SNAPSHOT-jar-with-dependencies.jar --help

test:
name: Test (OpenJDK ${{ matrix.java-version }})
Expand Down Expand Up @@ -95,10 +95,7 @@ jobs:
restore-keys: |
${{ runner.os }}-m2-${{ github.ref }}-
${{ runner.os }}-m2-

- name: Set up Docker
uses: docker/setup-docker-action@b60f85385d03ac8acfca6d9996982511d8620a19 # v4.3.0


- name: Run tests
run: ./mvnw test -B -Dhttps.protocols=TLSv1.2 -Dcheckstyle.skip=true -Dtests.log_level=info -Djdk.attach.allowAttachSelf=true

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.datadog.jmxfetch.util.LogLevel;
import org.datadog.jmxfetch.util.ServiceCheckHelper;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -764,7 +765,8 @@ private void loadFileConfigs(final AppConfig config, final Map<String, YamlParse
final String name = file.getName().replace(".yaml", "");
final String yamlPath = file.getAbsolutePath();
log.info("Reading {}", yamlPath);
try (FileInputStream yamlInputStream = new FileInputStream(yamlPath)) {
try (BufferedInputStream yamlInputStream =
new BufferedInputStream(new FileInputStream(yamlPath))) {
configs.put(name, new YamlParser(yamlInputStream));
} catch (FileNotFoundException e) {
log.warn("Cannot find " + yamlPath);
Expand Down
76 changes: 40 additions & 36 deletions src/main/java/org/datadog/jmxfetch/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import org.datadog.jmxfetch.util.InstanceTelemetry;
import org.yaml.snakeyaml.Yaml;



import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -244,7 +243,13 @@ public Instance(

Boolean collectDefaultJvmMetrics = (Boolean) instanceMap.get("collect_default_jvm_metrics");
if (collectDefaultJvmMetrics == null || collectDefaultJvmMetrics) {
loadDefaultConfig("default-jmx-metrics.yaml");
// override the test definitions
Object value = instanceMap.get("default-jmx-metrics-definitions");
if (value instanceof String) {
loadDefaultConfig((String) value);
} else {
loadDefaultConfig("default-jmx-metrics.yaml");
}
loadDefaultConfig(gcMetricConfig);
} else {
log.info("collect_default_jvm_metrics is false - not collecting default JVM metrics");
Expand Down Expand Up @@ -296,11 +301,18 @@ public static boolean isDirectInstance(Map<String, Object> configInstance) {
}

private void loadDefaultConfig(String configResourcePath) {
List<Map<String, Object>> defaultConf =
(List<Map<String, Object>>)
YAML.get().load(this.getClass().getResourceAsStream(configResourcePath));
for (Map<String, Object> conf : defaultConf) {
configurationList.add(new Configuration(conf));
try (InputStream is = this.getClass().getResourceAsStream(configResourcePath)) {
if (is == null) {
log.warn("Cannot find internal default config file {}", configResourcePath);
return;
}
List<Map<String, Object>> defaultConf;
defaultConf = YAML.get().load(is);
for (Map<String, Object> conf : defaultConf) {
configurationList.add(new Configuration(conf));
}
} catch (IOException e) {
log.warn("Cannot parse internal default config file {}", configResourcePath, e);
}
}

Expand All @@ -316,10 +328,10 @@ static void loadMetricConfigFiles(
+ "migrate to using standard agent config files in the conf.d directory.");
for (String fileName : metricConfigFiles) {
String yamlPath = new File(fileName).getAbsolutePath();
FileInputStream yamlInputStream = null;

log.info("Reading metric config file " + yamlPath);
try {
yamlInputStream = new FileInputStream(yamlPath);
try (BufferedInputStream yamlInputStream =
new BufferedInputStream(new FileInputStream(yamlPath))) {
List<Map<String, Object>> confs =
(List<Map<String, Object>>)
YAML.get().load(yamlInputStream);
Expand All @@ -330,14 +342,6 @@ static void loadMetricConfigFiles(
log.warn("Cannot find metric config file " + yamlPath);
} catch (Exception e) {
log.warn("Cannot parse yaml file " + yamlPath, e);
} finally {
if (yamlInputStream != null) {
try {
yamlInputStream.close();
} catch (IOException e) {
// ignore
}
}
}
}
}
Expand Down Expand Up @@ -448,40 +452,40 @@ public void init(boolean forceNewConnection)
throws IOException, FailedLoginException, SecurityException {
log.info("Trying to connect to JMX Server at " + this.toString());
connection = getConnection(instanceMap, forceNewConnection);

log.info(
"Trying to collect bean list for the first time for JMX Server at {}", this);
this.refreshBeansList();
this.initialRefreshTime = this.lastRefreshTime;
log.info("Connected to JMX Server at {} with {} beans", this, this.beans.size());

// Resolve configuration-level dynamic tags for all configurations
// Must be done after refreshBeansList() so the beans exist
resolveConfigurationDynamicTags();

this.getMatchingAttributes();
log.info("Done initializing JMX Server at {}", this);
}

private void resolveConfigurationDynamicTags() {
if (configurationList == null || configurationList.isEmpty()) {
return;
}

this.dynamicTagsCache = new HashMap<>();
List<DynamicTag> allDynamicTags = new ArrayList<>();

for (Configuration config : configurationList) {
List<DynamicTag> dynamicTags = config.getDynamicTags();
if (dynamicTags != null && !dynamicTags.isEmpty()) {
allDynamicTags.addAll(dynamicTags);
}
}

if (allDynamicTags.isEmpty()) {
return;
}

int successfulResolutions = 0;
for (DynamicTag dynamicTag : allDynamicTags) {
String cacheKey = dynamicTag.getBeanAttributeKey();
Expand All @@ -495,38 +499,38 @@ private void resolveConfigurationDynamicTags() {
successfulResolutions++;
}
}
log.info("Resolved {} unique dynamic tag(s) from {} total references for instance {}",

log.info("Resolved {} unique dynamic tag(s) from {} total references for instance {}",
successfulResolutions, allDynamicTags.size(), instanceName);
}

/**
* Get resolved dynamic tags for a specific configuration.
* This resolves the dynamic tags defined in the configuration using the cached values.
*
*
* @param config the configuration to get resolved tags for
* @return map of tag name to tag value
*/
private Map<String, String> getResolvedDynamicTagsForConfig(Configuration config) {
Map<String, String> resolvedTags = new HashMap<>();

if (this.dynamicTagsCache == null || this.dynamicTagsCache.isEmpty()) {
return resolvedTags;
}

List<DynamicTag> dynamicTags = config.getDynamicTags();
if (dynamicTags == null || dynamicTags.isEmpty()) {
return resolvedTags;
}

for (DynamicTag dynamicTag : dynamicTags) {
String cacheKey = dynamicTag.getBeanAttributeKey();
Map.Entry<String, String> cached = this.dynamicTagsCache.get(cacheKey);
if (cached != null) {
resolvedTags.put(cached.getKey(), cached.getValue());
}
}

return resolvedTags;
}

Expand Down Expand Up @@ -764,7 +768,7 @@ private void getMatchingAttributes() throws IOException {
for (Configuration conf : configurationList) {
try {
if (jmxAttribute.match(conf)) {
Map<String, String> resolvedDynamicTags =
Map<String, String> resolvedDynamicTags =
getResolvedDynamicTagsForConfig(conf);
jmxAttribute.setResolvedDynamicTags(resolvedDynamicTags);
jmxAttribute.setMatchingConf(conf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
Usage.used:
alias: jvm.gc.eden_size
metric_type: gauge
# Generational ZGC
- include:
domain: java.lang
type: MemoryPool
Expand Down Expand Up @@ -197,6 +198,16 @@
Usage.used:
alias: jvm.gc.old_gen_size
metric_type: gauge
# Non-Generational ZGC
- include:
domain: java.lang
type: MemoryPool
name: ZHeap
attribute:
Usage.used:
alias: jvm.gc.old_gen_size
metric_type: gauge
# Generational ZGC
- include:
domain: java.lang
type: MemoryPool
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/datadog/jmxfetch/TestApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -964,8 +964,8 @@ public void testServiceDiscovery() throws Exception {
run();
List<Map<String, Object>> metrics = getMetrics();

// 14 = 13 metrics from java.lang + 1 metric explicitly defined in the yaml config file
assertEquals(63, metrics.size());
// Using simplified default JVM metrics for instances with process_name_regex
assertEquals(35, metrics.size());

List<String> tags = Arrays.asList(
"type:SimpleTestJavaApp",
Expand Down
Loading
Loading