org.apache.hadoop.metrics2.MetricType Java Examples

The following examples show how to use org.apache.hadoop.metrics2.MetricType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PrometheusMetricsSink.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
@Override
public void putMetrics(MetricsRecord metricsRecord) {
  for (AbstractMetric metrics : metricsRecord.metrics()) {
    if (metrics.type() == MetricType.COUNTER
        || metrics.type() == MetricType.GAUGE) {

      String key = prometheusName(
          metricsRecord.name(), metrics.name());

      StringBuilder builder = new StringBuilder();
      builder.append("# TYPE ")
          .append(key)
          .append(" ")
          .append(metrics.type().toString().toLowerCase())
          .append("\n");

      StringBuilder prometheusMetricKey = new StringBuilder();
      prometheusMetricKey.append(key)
          .append("{");
      String sep = "";

      //add tags
      for (MetricsTag tag : metricsRecord.tags()) {
        String tagName = tag.name().toLowerCase();

        //ignore specific tag which includes sub-hierarchy
        if (!tagName.equals("numopenconnectionsperuser")) {
          prometheusMetricKey.append(sep)
              .append(tagName)
              .append("=\"")
              .append(tag.value())
              .append("\"");
          sep = ",";
        }
      }
      prometheusMetricKey.append("}");

      String prometheusMetricKeyAsString = prometheusMetricKey.toString();
      builder.append(prometheusMetricKeyAsString);
      builder.append(" ");
      builder.append(metrics.value());
      builder.append("\n");
      metricLines.put(prometheusMetricKeyAsString, builder.toString());

    }
  }
}
 
Example #2
Source File: HadoopTimelineMetricsSinkTest.java    From ambari-metrics with Apache License 2.0 4 votes vote down vote up
@Test
@PrepareForTest({URL.class, OutputStream.class, AbstractTimelineMetricsSink.class, HttpURLConnection.class, TimelineMetric.class, HadoopTimelineMetricsSink.class, SubsetConfiguration.class})
public void testPutMetrics() throws Exception {
  HadoopTimelineMetricsSink sink = new HadoopTimelineMetricsSink();

  HttpURLConnection connection = PowerMock.createNiceMock(HttpURLConnection.class);
  URL url = PowerMock.createNiceMock(URL.class);
  InputStream is = IOUtils.toInputStream(gson.toJson(Collections.singletonList("localhost")));
  TimelineMetric timelineMetric = PowerMock.createNiceMock(TimelineMetric.class);
  expectNew(TimelineMetric.class).andReturn(timelineMetric).times(2);
  expect(timelineMetric.getMetricValues()).andReturn(new TreeMap<Long, Double>()).anyTimes();
  expect(timelineMetric.getMetricName()).andReturn("metricName").anyTimes();
  expectNew(URL.class, anyString()).andReturn(url).anyTimes();
  expect(url.openConnection()).andReturn(connection).anyTimes();
  expect(connection.getInputStream()).andReturn(is).anyTimes();
  expect(connection.getResponseCode()).andReturn(200).anyTimes();
  OutputStream os = PowerMock.createNiceMock(OutputStream.class);
  expect(connection.getOutputStream()).andReturn(os).anyTimes();

  SubsetConfiguration conf = PowerMock.createNiceMock(SubsetConfiguration.class);
  expect(conf.getString("slave.host.name")).andReturn("localhost").anyTimes();
  expect(conf.getParent()).andReturn(null).anyTimes();
  expect(conf.getPrefix()).andReturn("service").anyTimes();
  expect(conf.getStringArray(eq(COLLECTOR_HOSTS_PROPERTY))).andReturn(new String[]{"localhost"," localhost2"}).anyTimes();
  expect(conf.getString(eq("serviceName-prefix"), eq(""))).andReturn("").anyTimes();
  expect(conf.getString(eq(COLLECTOR_PROTOCOL), eq("http"))).andReturn("http").anyTimes();
  expect(conf.getString(eq(COLLECTOR_PORT), eq("6188"))).andReturn("6188").anyTimes();

  expect(conf.getInt(eq(MAX_METRIC_ROW_CACHE_SIZE), anyInt())).andReturn(10).anyTimes();
  expect(conf.getInt(eq(METRICS_SEND_INTERVAL), anyInt())).andReturn(1000).anyTimes();
  expect(conf.getBoolean(eq(SET_INSTANCE_ID_PROPERTY), eq(false))).andReturn(true).anyTimes();
  expect(conf.getString(eq(INSTANCE_ID_PROPERTY), anyString())).andReturn("instanceId").anyTimes();
  expect(conf.getString(eq(HOST_IN_MEMORY_AGGREGATION_PROTOCOL_PROPERTY), anyString())).andReturn("http").anyTimes();

  conf.setListDelimiterHandler(new DefaultListDelimiterHandler(eq(',')));
  expectLastCall().anyTimes();

  expect(conf.getKeys()).andReturn(new Iterator() {
    @Override
    public boolean hasNext() {
      return false;
    }

    @Override
    public Object next() {
      return null;
    }

    @Override
    public void remove() {

    }
  }).once();

  AbstractMetric metric = createNiceMock(AbstractMetric.class);
  expect(metric.name()).andReturn("metricName").anyTimes();
  expect(metric.value()).andReturn(9.5687).anyTimes();
  expect(metric.type()).andReturn(MetricType.COUNTER).anyTimes();
  //TODO currently only numeric metrics are supported

  MetricsRecord record = createNiceMock(MetricsRecord.class);
  expect(record.name()).andReturn("testName").anyTimes();
  expect(record.context()).andReturn("testContext").anyTimes();
  expect(record.timestamp()).andAnswer(new IAnswer<Long>() {
    @Override
    public Long answer() throws Throwable {
      return System.currentTimeMillis();
    }
  }).anyTimes();

  expect(record.metrics()).andReturn(Arrays.asList(metric)).anyTimes();

  timelineMetric.setInstanceId(eq("instanceId"));
  EasyMock.expectLastCall();

  replay(record, metric);
  replayAll();

  sink.init(conf);

  sink.putMetrics(record);

  Thread.sleep(1500L);

  sink.putMetrics(record);

  verifyAll();
}
 
Example #3
Source File: MetricCounterInt.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public MetricType type() {
  return MetricType.COUNTER;
}
 
Example #4
Source File: MetricGaugeDouble.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public MetricType type() {
  return MetricType.GAUGE;
}
 
Example #5
Source File: MetricGaugeLong.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public MetricType type() {
  return MetricType.GAUGE;
}
 
Example #6
Source File: MetricGaugeInt.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public MetricType type() {
  return MetricType.GAUGE;
}
 
Example #7
Source File: MetricGaugeFloat.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public MetricType type() {
  return MetricType.GAUGE;
}
 
Example #8
Source File: MetricCounterLong.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public MetricType type() {
  return MetricType.COUNTER;
}
 
Example #9
Source File: MetricCounterInt.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public MetricType type() {
  return MetricType.COUNTER;
}
 
Example #10
Source File: MetricGaugeDouble.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public MetricType type() {
  return MetricType.GAUGE;
}
 
Example #11
Source File: MetricGaugeLong.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public MetricType type() {
  return MetricType.GAUGE;
}
 
Example #12
Source File: MetricGaugeInt.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public MetricType type() {
  return MetricType.GAUGE;
}
 
Example #13
Source File: MetricGaugeFloat.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public MetricType type() {
  return MetricType.GAUGE;
}
 
Example #14
Source File: MetricCounterLong.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public MetricType type() {
  return MetricType.COUNTER;
}