use of org.apache.hadoop.metrics2.MetricsVisitor in project hadoop by apache.
the class TestKafkaMetrics method testPutMetrics.
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testPutMetrics() throws Exception {
// Create a record by mocking MetricsRecord class.
MetricsRecord record = mock(MetricsRecord.class);
when(record.tags()).thenReturn(Lists.newArrayList(new MetricsTag(KafkaMetricsInfo.KafkaTag, "test_tag")));
when(record.timestamp()).thenReturn(System.currentTimeMillis());
// Create a metric using AbstractMetric class.
AbstractMetric metric = new AbstractMetric(KafkaMetricsInfo.KafkaCounter) {
@Override
public Number value() {
return new Integer(123);
}
@Override
public MetricType type() {
return null;
}
@Override
public void visit(MetricsVisitor visitor) {
}
};
// Create a list of metrics.
Iterable<AbstractMetric> metrics = Lists.newArrayList(metric);
when(record.name()).thenReturn("Kafka record name");
when(record.metrics()).thenReturn(metrics);
SubsetConfiguration conf = mock(SubsetConfiguration.class);
when(conf.getString(KafkaSink.BROKER_LIST)).thenReturn("localhost:9092");
String topic = "myTestKafkaTopic";
when(conf.getString(KafkaSink.TOPIC)).thenReturn(topic);
// Create the KafkaSink object and initialize it.
kafkaSink = new KafkaSink();
kafkaSink.init(conf);
// Create a mock KafkaProducer as a producer for KafkaSink.
Producer<Integer, byte[]> mockProducer = mock(KafkaProducer.class);
kafkaSink.setProducer(mockProducer);
// Create the json object from the record.
StringBuilder jsonLines = recordToJson(record);
if (LOG.isDebugEnabled()) {
LOG.debug("kafka message: " + jsonLines.toString());
}
// Send the record and store the result in a mock Future.
Future<RecordMetadata> f = mock(Future.class);
when(mockProducer.send((ProducerRecord) anyObject())).thenReturn(f);
kafkaSink.putMetrics(record);
// Get the argument and verity it.
ArgumentCaptor<ProducerRecord> argument = ArgumentCaptor.forClass(ProducerRecord.class);
verify(mockProducer).send(argument.capture());
// Compare the received data with the original one.
ProducerRecord<Integer, byte[]> data = (argument.getValue());
String jsonResult = new String(data.value());
if (LOG.isDebugEnabled()) {
LOG.debug("kafka result: " + jsonResult);
}
assertEquals(jsonLines.toString(), jsonResult);
}
use of org.apache.hadoop.metrics2.MetricsVisitor in project hadoop by apache.
the class TestMetricsVisitor method testCommon.
/**
* Test the common use cases
*/
@Test
public void testCommon() {
MetricsVisitor visitor = mock(MetricsVisitor.class);
MetricsRegistry registry = new MetricsRegistry("test");
List<AbstractMetric> metrics = MetricsLists.builder("test").addCounter(info("c1", "int counter"), 1).addCounter(info("c2", "long counter"), 2L).addGauge(info("g1", "int gauge"), 5).addGauge(info("g2", "long gauge"), 6L).addGauge(info("g3", "float gauge"), 7f).addGauge(info("g4", "double gauge"), 8d).metrics();
for (AbstractMetric metric : metrics) {
metric.visit(visitor);
}
verify(visitor).counter(c1.capture(), eq(1));
assertEquals("c1 name", "c1", c1.getValue().name());
assertEquals("c1 description", "int counter", c1.getValue().description());
verify(visitor).counter(c2.capture(), eq(2L));
assertEquals("c2 name", "c2", c2.getValue().name());
assertEquals("c2 description", "long counter", c2.getValue().description());
verify(visitor).gauge(g1.capture(), eq(5));
assertEquals("g1 name", "g1", g1.getValue().name());
assertEquals("g1 description", "int gauge", g1.getValue().description());
verify(visitor).gauge(g2.capture(), eq(6L));
assertEquals("g2 name", "g2", g2.getValue().name());
assertEquals("g2 description", "long gauge", g2.getValue().description());
verify(visitor).gauge(g3.capture(), eq(7f));
assertEquals("g3 name", "g3", g3.getValue().name());
assertEquals("g3 description", "float gauge", g3.getValue().description());
verify(visitor).gauge(g4.capture(), eq(8d));
assertEquals("g4 name", "g4", g4.getValue().name());
assertEquals("g4 description", "double gauge", g4.getValue().description());
}
Aggregations