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

expanding possibilities of the agent configuration #29

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/main/java/org/jmxtrans/agent/JConsoleNameStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.jmxtrans.agent;

import javax.management.ObjectName;
import java.util.Map;

public class JConsoleNameStrategy implements ResultNameStrategy {
@Override
public String getResultName(Query query, ObjectName objectName, String key, String attribute) {
String result = objectName.getDomain();
if(objectName.getKeyProperty("type") != null) {
result += "." + objectName.getKeyProperty("type");
}
if(objectName.getKeyProperty("name") != null) {
result += "." + objectName.getKeyProperty("name");
}
for (Map.Entry<String, String> entry : objectName.getKeyPropertyList().entrySet()) {
if(!entry.getKey().equalsIgnoreCase("name") && !entry.getKey().equalsIgnoreCase("type")){
result += "." + entry.getValue();
}
}

result += "." + attribute;

if(key != null && !key.isEmpty()){
result = result + "." + key;
}

return result;
}

@Override
public void postConstruct(Map<String, String> settings) {

}
}
105 changes: 65 additions & 40 deletions src/main/java/org/jmxtrans/agent/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.*;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeType;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -129,53 +129,78 @@ public void collectAndExport(@Nonnull MBeanServer mbeanServer, @Nonnull OutputWr
Set<ObjectName> objectNames = mbeanServer.queryNames(objectName, null);

for (ObjectName on : objectNames) {
try {
if (attribute == null || attribute.isEmpty()) {
for (MBeanAttributeInfo mBeanAttributeInfo : mbeanServer.getMBeanInfo(on).getAttributes()) {
collectAndExportAttribute(mbeanServer, outputWriter, on, mBeanAttributeInfo.getName());
}

} else {
collectAndExportAttribute(mbeanServer, outputWriter, on, attribute);
}
} catch (Exception e) {
logger.log(Level.WARNING, "Exception collecting " + on + "#" + attribute + (key == null ? "" : "#" + key), e);
}
}
}

private void collectAndExportAttribute(MBeanServer mbeanServer, OutputWriter outputWriter, ObjectName objectName, String attribute) {
try {
Object attributeValue = null;
try {
Object attributeValue = mbeanServer.getAttribute(on, attribute);

Object value;
if (attributeValue instanceof CompositeData) {
CompositeData compositeData = (CompositeData) attributeValue;
if (key == null) {
logger.warning("Ignore compositeData without key specified for '" + on + "'#" + attribute + ": " + attributeValue);
continue;
} else {
attributeValue = mbeanServer.getAttribute(objectName, attribute);
} catch (Exception ex) {return;}

Object value;
if (attributeValue instanceof CompositeData) {
CompositeData compositeData = (CompositeData) attributeValue;
if (key == null) {
CompositeType compositeType = compositeData.getCompositeType();
for (String key : compositeType.keySet()) {
value = compositeData.get(key);
processValue(outputWriter, objectName, attribute, value, key);
}
return;
} else {
if (key == null) {
value = attributeValue;
} else {
logger.warning("Ignore NON compositeData for specified key for '" + on + "'#" + attribute + "#" + key + ": " + attributeValue);
continue;
}
value = compositeData.get(key);
}
if (value != null && value.getClass().isArray()) {
List valueAsList = new ArrayList();
for (int i = 0; i < Array.getLength(value); i++) {
valueAsList.add(Array.get(value, i));
}
value = valueAsList;
} else {
if (key == null) {
value = attributeValue;
} else {
logger.warning("Ignore NON compositeData for specified key for '" + objectName + "'#" + attribute + "#" + key + ": " + attributeValue);
return;
}
}
if (value != null && value.getClass().isArray()) {
List valueAsList = new ArrayList();
for (int i = 0; i < Array.getLength(value); i++) {
valueAsList.add(Array.get(value, i));
}
value = valueAsList;
}

String resultName = resultNameStrategy.getResultName(this, on, key);
if (value instanceof Iterable) {
Iterable iterable = (Iterable) value;
if (position == null) {
int idx = 0;
for (Object entry : iterable) {
outputWriter.writeQueryResult(resultName + "_" + idx++, type, entry);
}
} else {
value = Iterables2.get((Iterable) value, position);
outputWriter.writeQueryResult(resultName, type, value);
}
} else {
outputWriter.writeQueryResult(resultName, type, value);
processValue(outputWriter, objectName, attribute, value, key);
} catch (Exception e) {
logger.log(Level.WARNING, "Exception collecting " + objectName + "#" + attribute + (key == null ? "" : "#" + key), e);
}
}

private void processValue(OutputWriter outputWriter, ObjectName objectName, String attribute, Object value, String key) throws IOException {
String resultName = resultNameStrategy.getResultName(this, objectName, key, attribute);
if (value instanceof Iterable) {
Iterable iterable = (Iterable) value;
if (position == null) {
int idx = 0;
for (Object entry : iterable) {
outputWriter.writeQueryResult(resultName + "_" + idx++, type, entry);
}
} catch (Exception e) {
logger.log(Level.WARNING, "Exception collecting " + on + "#" + attribute + (key == null ? "" : "#" + key), e);
} else {
value = Iterables2.get((Iterable) value, position);
outputWriter.writeQueryResult(resultName, type, value);
}
} else {
outputWriter.writeQueryResult(resultName, type, value);
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/jmxtrans/agent/ResultNameStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@
public interface ResultNameStrategy {

@Nonnull
String getResultName(@Nonnull Query query, @Nonnull ObjectName objectName);
@Nonnull
String getResultName(@Nonnull Query query, @Nonnull ObjectName objectName, @Nullable String key);
String getResultName(@Nonnull Query query, @Nonnull ObjectName objectName, @Nullable String key, @Nullable String attribute);

void postConstruct(@Nonnull Map<String, String> settings);
}
12 changes: 3 additions & 9 deletions src/main/java/org/jmxtrans/agent/ResultNameStrategyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,13 @@ public class ResultNameStrategyImpl implements ResultNameStrategy {

@Nonnull
@Override
public String getResultName(@Nonnull Query query, @Nonnull ObjectName objectName) {
return getResultName(query, objectName, null);
}

@Nonnull
@Override
public String getResultName(@Nonnull Query query, @Nonnull ObjectName objectName, @Nullable String key) {
public String getResultName(@Nonnull Query query, @Nonnull ObjectName objectName, @Nullable String key, @Nonnull String attribute) {
String result;
if (query.getResultAlias() == null) {
if(key == null) {
result = escapeObjectName(objectName) + "." + query.getAttribute();
result = escapeObjectName(objectName) + "." + attribute;
} else {
result = escapeObjectName(objectName) + "." + query.getAttribute() + "." + key;
result = escapeObjectName(objectName) + "." + attribute + "." + key;
}
} else {
result = expressionLanguageEngine.resolveExpression(query.getResultAlias(), objectName);
Expand Down