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

added support on jmx attributes of type java.util.Date #449

Merged
merged 6 commits into from
Nov 14, 2019
Merged

added support on jmx attributes of type java.util.Date #449

merged 6 commits into from
Nov 14, 2019

Conversation

tianhai
Copy link
Contributor

@tianhai tianhai commented Nov 12, 2019

Dear maintainers,

We recently had an requirement to expose certain JMX attributes of java Date type to prometheus, specifically, a number of timestamps in the built-in JMX of Apache Camel library.

We thought that it might be useful for other users of the exporter, would you review our changes and let us know any feedbacks and critics please?

Thank you
Jacky

@@ -190,7 +190,12 @@ private void processBeanValue(
Object value) {
if (value == null) {
logScrape(domain + beanProperties + attrName, "null");
} else if (value instanceof Number || value instanceof String || value instanceof Boolean) {
} else if (value instanceof Number || value instanceof String || value instanceof Boolean || value instanceof java.util.Date) {
//output Date as Long (the number of milliseconds since January 1, 1970, 00:00:00 GMT)
Copy link
Contributor

Choose a reason for hiding this comment

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

Seconds are usual for Prometheus.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated to output the number of seconds instead of milliseconds.

} else if (value instanceof Number || value instanceof String || value instanceof Boolean) {
} else if (value instanceof Number || value instanceof String || value instanceof Boolean || value instanceof java.util.Date) {
//output Date as Long (the number of milliseconds since January 1, 1970, 00:00:00 GMT)
if( value instanceof java.util.Date){
Copy link
Contributor

Choose a reason for hiding this comment

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

Watch your spaces

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed the unintended spaces.

}

@Override
public long getLastExchangeFailureTimestamp() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't a Date

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed the test :)
thank you very much for the feedbacks and could you review the latest commit please, thank you!

} else if (value instanceof Number || value instanceof String || value instanceof Boolean) {
} else if (value instanceof Number || value instanceof String || value instanceof Boolean || value instanceof java.util.Date) {
//output Date as Long (the number of seconds since January 1, 1970, 00:00:00 GMT)
if(value instanceof java.util.Date){
Copy link
Contributor

Choose a reason for hiding this comment

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

formatting is still off here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed formatting

Copy link
Contributor

Choose a reason for hiding this comment

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

There should be a space after the if and before the {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated to desired style


@Override
public Date getLastExchangeFailureTimestamp() {
int theMillisecondAtTheMoment = Calendar.getInstance().get(Calendar.MILLISECOND);
Copy link
Contributor

Choose a reason for hiding this comment

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

We should preserve milliseconds if we can.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed data type from long to double to preserve milliseconds in decimal places, thank you.

jacky and others added 5 commits November 14, 2019 09:15
Signed-off-by: jacky <jacky.chen@nzqa.govt.nz>
Signed-off-by: jacky <jac.chen@gmail.com>
…heus conventions

Signed-off-by: jacky <jac.chen@gmail.com>
…ents milliseconds

Signed-off-by: jacky <jac.chen@gmail.com>
Signed-off-by: jacky <jac.chen@gmail.com>
Signed-off-by: jacky <jac.chen@gmail.com>
} else if (value instanceof Number || value instanceof String || value instanceof Boolean) {
} else if (value instanceof Number || value instanceof String || value instanceof Boolean || value instanceof java.util.Date) {
//output Date as Long (the number of seconds since January 1, 1970, 00:00:00 GMT)
if(value instanceof java.util.Date){
Copy link
Contributor

Choose a reason for hiding this comment

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

There should be a space after the if and before the {

} else if (value instanceof Number || value instanceof String || value instanceof Boolean || value instanceof java.util.Date) {
if(value instanceof java.util.Date){
attrType = "java.lang.Double";
value = (double) ((java.util.Date) value).getTime() / 1000;
Copy link
Contributor

Choose a reason for hiding this comment

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

Usually I do this as value / 1000.0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated to desired style

" route: \"$2\"\n" +
" type: routes";
JmxCollector jc = new JmxCollector(rulePattern).register(registry);
assertEquals(Double.valueOf((double)Camel.EXPECTED_DATE_IN_MILLISECONDS / 1000), registry.getSampleValue("org_apache_camel_LastExchangeFailureTimestamp", new String[]{"context", "route", "type"}, new String[]{"my-camel-context", "my-route-name", "routes"}));
Copy link
Contributor

Choose a reason for hiding this comment

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

Check for the actual literal float value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated test to do that, cheers

Signed-off-by: jacky <jac.chen@gmail.com>
@brian-brazil brian-brazil merged commit 20e8954 into prometheus:master Nov 14, 2019
@brian-brazil
Copy link
Contributor

Thanks!

qinghui-xu pushed a commit to qinghui-xu/jmx_exporter that referenced this pull request Sep 18, 2020
* added support on jmx attributes of type java.util.Date

Signed-off-by: jacky <jacky.chen@nzqa.govt.nz>
Signed-off-by: jacky <jac.chen@gmail.com>
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.

2 participants