Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow metrics configs to be loaded via resources #205

Merged
merged 3 commits into from
Jan 16, 2019

Conversation

tylerbenson
Copy link
Contributor

Metrics configs embedded inside the agent aren’t directly accessible on the file system. Loading as a resource avoids having to copy to an external location.

These files should match the standard metrics.yaml format, and include jmx_metrics blocks at the top level.

@@ -220,7 +219,7 @@ public boolean processAutoDiscovery(byte[] buffer) {
String[] discovered;

try {
String configs = new String(buffer, CharEncoding.UTF_8);
String configs = new String(buffer, "UTF-8");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch to remove this dependency since it was only used for this.
What about creating constant instead of having duplicated strings?

Copy link
Contributor Author

@tylerbenson tylerbenson Jan 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I changed it to use a proper Charset instead.

@@ -38,6 +39,13 @@
public static final String PROCESS_NAME_REGEX = "process_name_regex";
public static final String ATTRIBUTE = "Attribute: ";

private static final ThreadLocal<Yaml> YAML = new ThreadLocal<Yaml>() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it need to be a ThreadLocal object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seemed to be non-trivial amount of initialization happening in the constructor, so I wanted to avoid it. The problem is (I believe) Yaml is not thread safe, so I used the ThreadLocal to ensure that safety.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I trust you judgement here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is accurate. We weren't thread-unsafe due to how we deal with instances, but this is safer. Thank you for this cleanup.

@@ -199,6 +208,40 @@ private void loadMetricConfigFiles(AppConfig appConfig, LinkedList<Configuration
}
}

private void loadMetricConfigResources(AppConfig config, LinkedList<Configuration> configurationList) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it legit to add a unit tests for this method, unless it is covered already?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was doing manual testing since the application is not structured well for easy testing. Do you have any suggestions for better ways to validate this in a unit test?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You most probably will have to create a method to get the inputStream so this method can be testable as such by creating an object instance and calling this method while providing your inputStream.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've added a few simple tests. Is this what you were looking for?

Avoids the need to catch the exception for an unknown charset.
Metrics configs embedded inside the agent aren’t directly accessible on the file system.  Loading as a resource avoids having to copy to an external location.
@tylerbenson tylerbenson force-pushed the tyler/metrics-from-resources branch from 314bb0a to 2125937 Compare January 3, 2019 19:41
@@ -51,6 +50,7 @@
private static final String AD_LEGACY_CONFIG_TERM = "#### SERVICE-DISCOVERY TERM ####";
private static final int AD_MAX_NAME_LEN = 80;
private static final int AD_MAX_MAG_INSTANCES = 4; // 1000 instances ought to be enough for anyone
private static final Charset UTF_8 = Charset.forName("UTF-8");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice refactoring! 👍

@@ -38,6 +39,13 @@
public static final String PROCESS_NAME_REGEX = "process_name_regex";
public static final String ATTRIBUTE = "Attribute: ";

private static final ThreadLocal<Yaml> YAML = new ThreadLocal<Yaml>() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I trust you judgement here

@@ -199,6 +208,40 @@ private void loadMetricConfigFiles(AppConfig appConfig, LinkedList<Configuration
}
}

private void loadMetricConfigResources(AppConfig config, LinkedList<Configuration> configurationList) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You most probably will have to create a method to get the inputStream so this method can be testable as such by creating an object instance and calling this method while providing your inputStream.

Copy link

@gzussa gzussa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding tests. This is solid work :) 💯

@tylerbenson
Copy link
Contributor Author

@gzussa would you like to merge this or should I request access to merge it myself?

tylerbenson added a commit to DataDog/dd-trace-java that referenced this pull request Jan 8, 2019
Using copy/paste from integrations-core.  Will depend on a new jmxfetch release with DataDog/jmxfetch#205 before this will work.
Copy link
Member

@truthbk truthbk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me :shipit:

@@ -219,38 +219,29 @@ public boolean processAutoDiscovery(byte[] buffer) {
boolean reinit = false;
String[] discovered;

try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it looks like we were try...catching an Exception that didn't even come into play here.

@@ -38,6 +39,13 @@
public static final String PROCESS_NAME_REGEX = "process_name_regex";
public static final String ATTRIBUTE = "Attribute: ";

private static final ThreadLocal<Yaml> YAML = new ThreadLocal<Yaml>() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is accurate. We weren't thread-unsafe due to how we deal with instances, but this is safer. Thank you for this cleanup.

@tylerbenson tylerbenson merged commit 59fbc29 into DataDog:master Jan 16, 2019
@tylerbenson tylerbenson deleted the tyler/metrics-from-resources branch January 16, 2019 21:20
@olivielpeau olivielpeau added this to the 0.25.0 milestone Jan 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants