Example usage for org.apache.hadoop.metrics2 MetricsRecord description

List of usage examples for org.apache.hadoop.metrics2 MetricsRecord description

Introduction

In this page you can find the example usage for org.apache.hadoop.metrics2 MetricsRecord description.

Prototype

String description();

Source Link

Usage

From source file:org.apache.phoenix.trace.PhoenixMetricsSink.java

License:Apache License

/**
 * Add a new metric record to be written.
 *
 * @param record// www.  j av a2s.c o m
 */
@Override
public void putMetrics(MetricsRecord record) {
    // its not a tracing record, we are done. This could also be handled by filters, but safer
    // to do it here, in case it gets misconfigured
    if (!record.name().startsWith(TracingUtils.METRIC_SOURCE_KEY)) {
        return;
    }

    // don't initialize until we actually have something to write
    lazyInitialize();

    String stmt = "UPSERT INTO " + table + " (";
    // drop it into the queue of things that should be written
    List<String> keys = new ArrayList<String>();
    List<Object> values = new ArrayList<Object>();
    // we need to keep variable values in a separate set since they may have spaces, which
    // causes the parser to barf. Instead, we need to add them after the statement is prepared
    List<String> variableValues = new ArrayList<String>(record.tags().size());
    keys.add(TRACE.columnName);
    values.add(Long.parseLong(record.name().substring(TracingUtils.METRIC_SOURCE_KEY.length())));

    keys.add(DESCRIPTION.columnName);
    values.add(VARIABLE_VALUE);
    variableValues.add(record.description());

    // add each of the metrics
    for (AbstractMetric metric : record.metrics()) {
        // name of the metric is also the column name to which we write
        keys.add(MetricInfo.getColumnName(metric.name()));
        values.add(metric.value());
    }

    // get the tags out so we can set them later (otherwise, need to be a single value)
    int annotationCount = 0;
    int tagCount = 0;
    for (MetricsTag tag : record.tags()) {
        if (tag.name().equals(ANNOTATION.traceName)) {
            addDynamicEntry(keys, values, variableValues, ANNOTATION_FAMILY, tag, ANNOTATION, annotationCount);
            annotationCount++;
        } else if (tag.name().equals(TAG.traceName)) {
            addDynamicEntry(keys, values, variableValues, TAG_FAMILY, tag, TAG, tagCount);
            tagCount++;
        } else if (tag.name().equals(HOSTNAME.traceName)) {
            keys.add(HOSTNAME.columnName);
            values.add(VARIABLE_VALUE);
            variableValues.add(tag.value());
        } else if (tag.name().equals("Context")) {
            // ignored
        } else {
            LOG.error("Got an unexpected tag: " + tag);
        }
    }

    // add the tag count, now that we know it
    keys.add(TAG_COUNT);
    // ignore the hostname in the tags, if we know it
    values.add(tagCount);

    keys.add(ANNOTATION_COUNT);
    values.add(annotationCount);

    // compile the statement together
    stmt += COMMAS.join(keys);
    stmt += ") VALUES (" + COMMAS.join(values) + ")";

    if (LOG.isTraceEnabled()) {
        LOG.trace("Logging metrics to phoenix table via: " + stmt);
        LOG.trace("With tags: " + variableValues);
    }
    try {
        PreparedStatement ps = conn.prepareStatement(stmt);
        // add everything that wouldn't/may not parse
        int index = 1;
        for (String tag : variableValues) {
            ps.setString(index++, tag);
        }
        // Not going through the standard route of using statement.execute() as that code path
        // is blocked if the metadata hasn't been been upgraded to the new minor release. 
        MutationPlan plan = ps.unwrap(PhoenixPreparedStatement.class).compileMutation(stmt);
        MutationState state = conn.unwrap(PhoenixConnection.class).getMutationState();
        MutationState newState = plan.execute();
        state.join(newState);
    } catch (SQLException e) {
        LOG.error("Could not write metric: \n" + record + " to prepared statement:\n" + stmt, e);
    }
}

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 w w w  . ja va2s  .  c  om*/
    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();
}