001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.metrics2;
020
021import org.apache.hadoop.classification.InterfaceAudience;
022import org.apache.hadoop.classification.InterfaceStability;
023
024/**
025 * Build a string dump of the metrics.
026 *
027 * The {@link #toString()} operator dumps out all values collected.
028 *
029 * Every entry is formatted as
030 * {@code prefix + name + separator + value + suffix}
031 */
032@InterfaceAudience.Public
033@InterfaceStability.Evolving
034public class MetricStringBuilder extends MetricsRecordBuilder {
035
036  private final StringBuilder builder = new StringBuilder(256);
037
038  private final String prefix;
039  private final String suffix;
040  private final String separator;
041  private final MetricsCollector parent;
042
043  /**
044   * Build an instance.
045   * @param parent parent collector. Unused in this instance; only used for
046   * the {@link #parent()} method
047   * @param prefix string before each entry
048   * @param separator separator between name and value
049   * @param suffix suffix after each entry
050   */
051  public MetricStringBuilder(MetricsCollector parent,
052      String prefix,
053      String separator,
054      String suffix) {
055    this.parent = parent;
056    this.prefix = prefix;
057    this.suffix = suffix;
058    this.separator = separator;
059  }
060
061  public MetricStringBuilder add(MetricsInfo info, Object value) {
062    return tuple(info.name(), value.toString());
063  }
064
065  /**
066   * Add any key,val pair to the string, between the prefix and suffix,
067   * separated by the separator.
068   * @param key key
069   * @param value value
070   * @return this instance
071   */
072  public MetricStringBuilder tuple(String key, String value) {
073    builder.append(prefix)
074        .append(key)
075        .append(separator)
076        .append(value)
077        .append(suffix);
078    return this;
079  }
080
081  @Override
082  public MetricsRecordBuilder tag(MetricsInfo info, String value) {
083    return add(info, value);
084  }
085
086  @Override
087  public MetricsRecordBuilder add(MetricsTag tag) {
088    return tuple(tag.name(), tag.value());
089  }
090
091  @Override
092  public MetricsRecordBuilder add(AbstractMetric metric) {
093    add(metric.info(), metric.toString());
094    return this;
095  }
096
097  @Override
098  public MetricsRecordBuilder setContext(String value) {
099    return tuple("context", value);
100  }
101
102  @Override
103  public MetricsRecordBuilder addCounter(MetricsInfo info, int value) {
104    return add(info, value);
105  }
106
107  @Override
108  public MetricsRecordBuilder addCounter(MetricsInfo info, long value) {
109    return add(info, value);
110  }
111
112  @Override
113  public MetricsRecordBuilder addGauge(MetricsInfo info, int value) {
114    return add(info, value);
115  }
116
117  @Override
118  public MetricsRecordBuilder addGauge(MetricsInfo info, long value) {
119    return add(info, value);
120  }
121
122  @Override
123  public MetricsRecordBuilder addGauge(MetricsInfo info, float value) {
124    return add(info, value);
125  }
126
127  @Override
128  public MetricsRecordBuilder addGauge(MetricsInfo info, double value) {
129    return add(info, value);
130  }
131
132  @Override
133  public MetricsCollector parent() {
134    return parent;
135  }
136
137  @Override
138  public String toString() {
139    return builder.toString();
140  }
141}