List of usage examples for org.apache.hadoop.metrics2 MetricsTag MetricsTag
public MetricsTag(MetricsInfo info, String value)
From source file:org.apache.phoenix.trace.PhoenixMetricsWriterTest.java
License:Apache License
@Test public void testTranslation() throws Exception { // hook up a sink we can test MetricsWriter mockSink = Mockito.mock(MetricsWriter.class); // writer that will translate to the sink (specific to hadoop version used) PhoenixMetricsSink writer = new PhoenixMetricsSink(); writer.setWriterForTesting(mockSink); // create a simple metrics record final long traceid = 987654; MetricsInfo info = new ExposedMetricsInfoImpl(TracingCompat.getTraceMetricName(traceid), "Some generic trace"); // setup some metrics for the span long spanid = 10; AbstractMetric span = new ExposedMetricCounterLong( new ExposedMetricsInfoImpl(MetricInfo.SPAN.traceName, ""), spanid); long parentid = 11; AbstractMetric parent = new ExposedMetricCounterLong( new ExposedMetricsInfoImpl(MetricInfo.PARENT.traceName, ""), parentid); long startTime = 12; AbstractMetric start = new ExposedMetricCounterLong( new ExposedMetricsInfoImpl(MetricInfo.START.traceName, ""), startTime); long endTime = 13; AbstractMetric end = new ExposedMetricCounterLong(new ExposedMetricsInfoImpl(MetricInfo.END.traceName, ""), endTime);//from www .ja v a 2s . com final List<AbstractMetric> metrics = Lists.newArrayList(span, parent, start, end); // create an annotation as well String annotation = "test annotation for a span"; MetricsTag tag = new MetricsTag(new ExposedMetricsInfoImpl(MetricInfo.ANNOTATION.traceName, "0"), annotation); String hostnameValue = "host-name.value"; MetricsTag hostname = new MetricsTag(new ExposedMetricsInfoImpl(MetricInfo.HOSTNAME.traceName, ""), hostnameValue); final List<MetricsTag> tags = Lists.newArrayList(hostname, tag); MetricsRecord record = new ExposedMetricsRecordImpl(info, System.currentTimeMillis(), tags, metrics); // setup the mocking/validation for the sink Mockito.doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { PhoenixMetricsRecord record = (PhoenixMetricsRecord) invocation.getArguments()[0]; //validate that we got the right fields in the record assertEquals("phoenix.987654", record.name()); assertEquals("Some generic trace", record.description()); int count = 0; for (PhoenixAbstractMetric metric : record.metrics()) { count++; //find the matching metric in the list boolean found = false; for (AbstractMetric expected : metrics) { if (expected.name().equals(metric.getName())) { found = true; // make sure the rest of the info matches assertEquals("Metric value mismatch", expected.value(), metric.value()); } } assertTrue("Didn't find an expected metric to match " + metric, found); } assertEquals("Number of metrics is received is wrong", metrics.size(), count); count = 0; for (PhoenixMetricTag tag : record.tags()) { count++; // find the matching metric in the list boolean found = false; for (MetricsTag expected : tags) { if (expected.name().equals(tag.name())) { found = true; // make sure the rest of the info matches assertEquals("Tag value mismatch", expected.value(), tag.value()); assertEquals("Tag description mismatch", expected.description(), tag.description()); } } assertTrue("Didn't find an expected metric to match " + tag, found); } assertEquals("Number of tags is received is wrong", tags.size(), count); return null; } }).when(mockSink).addMetrics(Mockito.any(PhoenixMetricsRecord.class)); // actually do the update writer.putMetrics(record); writer.flush(); Mockito.verify(mockSink).addMetrics(Mockito.any(PhoenixMetricsRecord.class)); Mockito.verify(mockSink).flush(); }
From source file:org.apache.phoenix.trace.TraceMetricSource.java
License:Apache License
@Override public void receiveSpan(Span span) { Metric builder = new Metric(span); // add all the metrics for the span builder.addCounter(Interns.info(SPAN.traceName, EMPTY_STRING), span.getSpanId()); builder.addCounter(Interns.info(PARENT.traceName, EMPTY_STRING), span.getParentId()); builder.addCounter(Interns.info(START.traceName, EMPTY_STRING), span.getStartTimeMillis()); builder.addCounter(Interns.info(END.traceName, EMPTY_STRING), span.getStopTimeMillis()); // add the tags to the span. They were written in order received so we mark them as such for (TimelineAnnotation ta : span.getTimelineAnnotations()) { builder.add(new MetricsTag(Interns.info(TAG.traceName, Long.toString(ta.getTime())), ta.getMessage())); }//from w ww . j av a2 s.c o m // add the annotations. We assume they are serialized as strings and integers, but that can // change in the future Map<byte[], byte[]> annotations = span.getKVAnnotations(); for (Entry<byte[], byte[]> annotation : annotations.entrySet()) { Pair<String, String> val = TracingUtils.readAnnotation(annotation.getKey(), annotation.getValue()); builder.add(new MetricsTag(Interns.info(ANNOTATION.traceName, val.getFirst()), val.getSecond())); } // add the span to the list we care about synchronized (this) { spans.add(builder); } }
From source file:org.apache.phoenix.trace.TraceMetricSource.java
License:Apache License
@Override public void getMetrics(MetricsCollector collector, boolean all) { // add a marker record so we know how many spans are used // this is also necessary to ensure that we register the metrics source as an MBean (avoiding a // runtime warning) MetricsRecordBuilder marker = collector.addRecord(TracingUtils.METRICS_MARKER_CONTEXT); marker.add(new MetricsTag(new MetricsInfoImpl("stat", "num spans"), Integer.toString(spans.size()))); // actually convert the known spans into metric records as well synchronized (this) { for (Metric span : spans) { MetricsRecordBuilder builder = collector .addRecord(new MetricsInfoImpl(TracingUtils.getTraceMetricName(span.id), span.desc)); builder.setContext(TracingUtils.METRICS_CONTEXT); for (Pair<MetricsInfo, Long> metric : span.counters) { builder.addCounter(metric.getFirst(), metric.getSecond()); }/*from w w w . j a v a 2 s . co m*/ for (MetricsTag tag : span.tags) { builder.add(tag); } } // reset the spans so we don't keep a big chunk of memory around spans = new ArrayList<Metric>(); } }