use of org.apache.hadoop.metrics2.MetricsTag in project hadoop by apache.
the class TestPatternFilter method mockMetricsRecord.
/**
* Creates a mock MetricsRecord with the given name and tags.
*
* @param name String name
* @param tags List<MetricsTag> tags
* @return MetricsRecord newly created mock
*/
private static MetricsRecord mockMetricsRecord(String name, List<MetricsTag> tags) {
MetricsRecord record = mock(MetricsRecord.class);
when(record.name()).thenReturn(name);
when(record.tags()).thenReturn(tags);
return record;
}
use of org.apache.hadoop.metrics2.MetricsTag in project hadoop by apache.
the class TestGangliaMetrics method testTagsForPrefix.
@Test
public void testTagsForPrefix() throws Exception {
ConfigBuilder cb = new ConfigBuilder().add(testNamePrefix + ".sink.ganglia.tagsForPrefix.all", "*").add(testNamePrefix + ".sink.ganglia.tagsForPrefix.some", "NumActiveSinks, " + "NumActiveSources").add(testNamePrefix + ".sink.ganglia.tagsForPrefix.none", "");
GangliaSink30 sink = new GangliaSink30();
sink.init(cb.subset(testNamePrefix + ".sink.ganglia"));
List<MetricsTag> tags = new ArrayList<MetricsTag>();
tags.add(new MetricsTag(MsInfo.Context, "all"));
tags.add(new MetricsTag(MsInfo.NumActiveSources, "foo"));
tags.add(new MetricsTag(MsInfo.NumActiveSinks, "bar"));
tags.add(new MetricsTag(MsInfo.NumAllSinks, "haa"));
tags.add(new MetricsTag(MsInfo.Hostname, "host"));
Set<AbstractMetric> metrics = new HashSet<AbstractMetric>();
MetricsRecord record = new MetricsRecordImpl(MsInfo.Context, (long) 1, tags, metrics);
StringBuilder sb = new StringBuilder();
sink.appendPrefix(record, sb);
assertEquals(".NumActiveSources=foo.NumActiveSinks=bar.NumAllSinks=haa", sb.toString());
tags.set(0, new MetricsTag(MsInfo.Context, "some"));
sb = new StringBuilder();
sink.appendPrefix(record, sb);
assertEquals(".NumActiveSources=foo.NumActiveSinks=bar", sb.toString());
tags.set(0, new MetricsTag(MsInfo.Context, "none"));
sb = new StringBuilder();
sink.appendPrefix(record, sb);
assertEquals("", sb.toString());
tags.set(0, new MetricsTag(MsInfo.Context, "nada"));
sb = new StringBuilder();
sink.appendPrefix(record, sb);
assertEquals("", sb.toString());
}
use of org.apache.hadoop.metrics2.MetricsTag in project hadoop by apache.
the class TestGraphiteMetrics method testPutMetrics2.
@Test
public void testPutMetrics2() {
GraphiteSink sink = new GraphiteSink();
List<MetricsTag> tags = new ArrayList<MetricsTag>();
tags.add(new MetricsTag(MsInfo.Context, "all"));
tags.add(new MetricsTag(MsInfo.Hostname, null));
Set<AbstractMetric> metrics = new HashSet<AbstractMetric>();
metrics.add(makeMetric("foo1", 1));
metrics.add(makeMetric("foo2", 2));
MetricsRecord record = new MetricsRecordImpl(MsInfo.Context, (long) 10000, tags, metrics);
ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
final GraphiteSink.Graphite mockGraphite = makeGraphite();
Whitebox.setInternalState(sink, "graphite", mockGraphite);
sink.putMetrics(record);
try {
verify(mockGraphite).write(argument.capture());
} catch (IOException e) {
e.printStackTrace();
}
String result = argument.getValue();
assertEquals(true, result.equals("null.all.Context.Context=all.foo1 1 10\n" + "null.all.Context.Context=all.foo2 2 10\n") || result.equals("null.all.Context.Context=all.foo2 2 10\n" + "null.all.Context.Context=all.foo1 1 10\n"));
}
use of org.apache.hadoop.metrics2.MetricsTag in project hadoop by apache.
the class TestGraphiteMetrics method testPutMetrics3.
/**
* Assert that timestamps are converted correctly, ticket HADOOP-11182
*/
@Test
public void testPutMetrics3() {
// setup GraphiteSink
GraphiteSink sink = new GraphiteSink();
final GraphiteSink.Graphite mockGraphite = makeGraphite();
Whitebox.setInternalState(sink, "graphite", mockGraphite);
// given two metrics records with timestamps 1000 milliseconds apart.
List<MetricsTag> tags = Collections.emptyList();
Set<AbstractMetric> metrics = new HashSet<AbstractMetric>();
metrics.add(makeMetric("foo1", 1));
MetricsRecord record1 = new MetricsRecordImpl(MsInfo.Context, 1000000000000L, tags, metrics);
MetricsRecord record2 = new MetricsRecordImpl(MsInfo.Context, 1000000001000L, tags, metrics);
sink.putMetrics(record1);
sink.putMetrics(record2);
sink.flush();
try {
sink.close();
} catch (IOException e) {
e.printStackTrace();
}
// then the timestamps in the graphite stream should differ by one second.
try {
verify(mockGraphite).write(eq("null.default.Context.foo1 1 1000000000\n"));
verify(mockGraphite).write(eq("null.default.Context.foo1 1 1000000001\n"));
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.apache.hadoop.metrics2.MetricsTag in project hadoop by apache.
the class TestMetricsSourceAdapter method testPurgeOldMetrics.
@Test
public void testPurgeOldMetrics() throws Exception {
// create test source with a single metric counter of value 1
PurgableSource source = new PurgableSource();
MetricsSourceBuilder sb = MetricsAnnotations.newSourceBuilder(source);
final MetricsSource s = sb.build();
List<MetricsTag> injectedTags = new ArrayList<MetricsTag>();
MetricsSourceAdapter sa = new MetricsSourceAdapter("tst", "tst", "testdesc", s, injectedTags, null, null, 1, false);
MBeanInfo info = sa.getMBeanInfo();
boolean sawIt = false;
for (MBeanAttributeInfo mBeanAttributeInfo : info.getAttributes()) {
sawIt |= mBeanAttributeInfo.getName().equals(source.lastKeyName);
}
;
assertTrue("The last generated metric is not exported to jmx", sawIt);
// skip JMX cache TTL
Thread.sleep(1000);
info = sa.getMBeanInfo();
sawIt = false;
for (MBeanAttributeInfo mBeanAttributeInfo : info.getAttributes()) {
sawIt |= mBeanAttributeInfo.getName().equals(source.lastKeyName);
}
;
assertTrue("The last generated metric is not exported to jmx", sawIt);
}
Aggregations