Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dimovelev committed Jul 16, 2014
2 parents 1603e44 + 73f1f48 commit 097ac08
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ bin/
.classpath
.idea/
*.iml
*.ipr
*.iws

Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
import java.util.Map.Entry;
import java.util.ResourceBundle;

import org.metricssampler.cmd.CheckCommand;
import org.metricssampler.cmd.HelpCommand;
import org.metricssampler.cmd.MetadataCommand;
import org.metricssampler.cmd.SamplerCommand;
import org.metricssampler.cmd.StartCommand;
import org.metricssampler.cmd.StatusCommand;
import org.metricssampler.cmd.StopCommand;
import org.metricssampler.cmd.TestCommand;
import org.metricssampler.cmd.*;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
Expand Down Expand Up @@ -59,6 +52,7 @@ protected static HelpCommand addCommands(final ResourceBundle bundle, final JCom
commander.addCommand(new MetadataCommand());
commander.addCommand(new CheckCommand());
commander.addCommand(new TestCommand());
commander.addCommand(new MetricsCommand());

fixResourceBundleBug(commander, bundle);
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.metricssampler.cmd;

import com.beust.jcommander.Parameters;
import org.metricssampler.reader.MetricReadException;
import org.metricssampler.sampler.Sampler;

import java.util.Set;

@Parameters(commandNames="metrics", commandDescriptionKey="help.metrics.command")
public class MetricsCommand extends SamplersCommand {
@Override
protected void process(final Sampler sampler) {
logger.info("Listing metrics of {}", sampler);
try {
Set<String> metrics = sampler.metrics();
for (final String name : metrics) {
System.out.println(name);
}
} catch (final MetricReadException e) {
logger.warn("Sampler threw exception during check", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.metricssampler.config.SamplerConfig;

import java.util.Set;

/**
* An active component which fetches metrics from a configured input, selects and transforms them and sends them to a configured list of outputs.
*/
Expand All @@ -15,7 +17,9 @@ public interface Sampler {
* @return fetch metrics and return {@code true} if all selectors match at least one metric
*/
boolean check();


Set<String> metrics();

SamplerConfig getConfig();

void reset();
Expand Down
3 changes: 2 additions & 1 deletion metrics-sampler-core/src/main/resources/help.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ help.param.logback=The logback configuration file to use
help.param.sampler.action=What to do with the samplers. One of enable|disable|reset
help.param.resource.names=A comma separated list of shared resource names (or regular expressions).
help.param.resource.action=What to do with the matching shared resources. Either start or stop.
help.missingCommand=Please specify a command
help.missingCommand=Please specify a command
help.metrics.command=List the metrics that are matched by the given sampler in an alphabetical order
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

import static org.metricssampler.util.Preconditions.checkArgumentNotNull;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Random;

import org.metricssampler.reader.MetricReadException;
import org.metricssampler.reader.MetricValue;
Expand Down Expand Up @@ -262,4 +257,25 @@ public void reset() {
selector.reset();
}
}

@Override
public Set<String> metrics() {
logger.debug("Listing the matched metrics");
try {
final Map<String, MetricValue> rawMetrics = readMetrics();
final Map<String, MetricValue> metrics = transformValues(rawMetrics);
final Set<String> result = new TreeSet<String>();
result.addAll(metrics.keySet());
return result;
} catch (final OpenMetricsReaderException e) {
final String msg = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
logger.warn("Failed to open reader: {}", msg);
} catch (final MetricReadException e) {
logger.warn("Failed to read metrics", e);
} catch (final MetricWriteException e) {
logger.warn("Failed to write metrics", e);
}
return Collections.emptySet();
}

}

0 comments on commit 097ac08

Please sign in to comment.