Search in sources :

Example 31 with MetricsTag

use of org.apache.hadoop.metrics2.MetricsTag in project phoenix by apache.

the class PhoenixMetricsSink method putMetrics.

/**
 * Add a new metric record to be written.
 *
 * @param record
 */
@Override
public void putMetrics(MetricsRecord record) {
    // 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);
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) AbstractMetric(org.apache.hadoop.metrics2.AbstractMetric) PreparedStatement(java.sql.PreparedStatement) PhoenixPreparedStatement(org.apache.phoenix.jdbc.PhoenixPreparedStatement) MetricsTag(org.apache.hadoop.metrics2.MetricsTag) MutationPlan(org.apache.phoenix.compile.MutationPlan) MutationState(org.apache.phoenix.execute.MutationState) PhoenixPreparedStatement(org.apache.phoenix.jdbc.PhoenixPreparedStatement)

Example 32 with MetricsTag

use of org.apache.hadoop.metrics2.MetricsTag in project hbase by apache.

the class DynamicMetricsRegistry method tag.

/**
 * Add a tag to the metrics
 * @param info  metadata of the tag
 * @param value of the tag
 * @param override existing tag if true
 * @return the registry (for keep adding tags etc.)
 */
public DynamicMetricsRegistry tag(MetricsInfo info, String value, boolean override) {
    MetricsTag tag = Interns.tag(info, value);
    if (!override) {
        MetricsTag existing = tagsMap.putIfAbsent(info.name(), tag);
        if (existing != null) {
            throw new MetricsException("Tag " + info.name() + " already exists!");
        }
        return this;
    }
    tagsMap.put(info.name(), tag);
    return this;
}
Also used : MetricsException(org.apache.hadoop.metrics2.MetricsException) MetricsTag(org.apache.hadoop.metrics2.MetricsTag)

Example 33 with MetricsTag

use of org.apache.hadoop.metrics2.MetricsTag in project hbase by apache.

the class Interns method tag.

/**
 * Get a metrics tag
 *
 * @param info  of the tag
 * @param value of the tag
 * @return an interned metrics tag
 */
public static MetricsTag tag(MetricsInfo info, String value) {
    Map<String, MetricsTag> map = tagCache.getUnchecked(info);
    MetricsTag tag = map.get(value);
    if (tag == null) {
        tag = new MetricsTag(info, value);
        map.put(value, tag);
    }
    return tag;
}
Also used : MetricsTag(org.apache.hadoop.metrics2.MetricsTag)

Aggregations

MetricsTag (org.apache.hadoop.metrics2.MetricsTag)30 AbstractMetric (org.apache.hadoop.metrics2.AbstractMetric)19 MetricsRecord (org.apache.hadoop.metrics2.MetricsRecord)13 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)11 IOException (java.io.IOException)7 HashSet (java.util.HashSet)7 GraphiteSink (org.apache.hadoop.metrics2.sink.GraphiteSink)4 MetricsException (org.apache.hadoop.metrics2.MetricsException)3 MetricsSourceBuilder (org.apache.hadoop.metrics2.lib.MetricsSourceBuilder)3 Matchers.anyString (org.mockito.Matchers.anyString)3 DatagramPacket (java.net.DatagramPacket)2 DatagramSocket (java.net.DatagramSocket)2 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)2 MBeanInfo (javax.management.MBeanInfo)2 MetricsSource (org.apache.hadoop.metrics2.MetricsSource)2 StatsDSink (org.apache.hadoop.metrics2.sink.StatsDSink)2 StatsD (org.apache.hadoop.metrics2.sink.StatsDSink.StatsD)2 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)2 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)2