/** * build a CruiseControlMetric object. */ private static CruiseControlMetric toCruiseControlMetric(long now, int brokerId, String name, Map<String, String> tags, double value) { return toCruiseControlMetric(now, brokerId, name, tags, value, null); }
@Override public byte[] serialize(String s, CruiseControlMetric cruiseControlMetric) { return toBytes(cruiseControlMetric); }
public static CruiseControlMetric fromBytes(byte[] bytes) throws UnknownVersionException { ByteBuffer buffer = ByteBuffer.wrap(bytes); switch (CruiseControlMetric.MetricClassId.forId(buffer.get())) { case BROKER_METRIC: return BrokerMetric.fromBuffer(buffer); case TOPIC_METRIC: return TopicMetric.fromBuffer(buffer); case PARTITION_METRIC: return PartitionMetric.fromBuffer(buffer); default: // This could happen when a new type of metric is added but we are still running the old code. // simply ignore the metric by returning a null. return null; } }
@Test public void testPartitionMetricSerde() throws UnknownVersionException { PartitionMetric partitionMetric = new PartitionMetric(RawMetricType.PARTITION_SIZE, 123L, 0, TOPIC, PARTITION, 0.1); CruiseControlMetric deserialized = MetricSerde.fromBytes(MetricSerde.toBytes(partitionMetric)); assertEquals(CruiseControlMetric.MetricClassId.PARTITION_METRIC.id(), deserialized.metricClassId().id()); assertEquals(RawMetricType.PARTITION_SIZE.id(), deserialized.rawMetricType().id()); assertEquals(TIME, deserialized.time()); assertEquals(BROKER_ID, deserialized.brokerId()); assertEquals(TOPIC, ((PartitionMetric) deserialized).topic()); assertEquals(PARTITION, ((PartitionMetric) deserialized).partition()); assertEquals(VALUE, deserialized.value(), 0.000001); } }
@Test public void testTopicMetricSerde() throws UnknownVersionException { TopicMetric topicMetric = new TopicMetric(RawMetricType.TOPIC_BYTES_IN, 123L, 0, TOPIC, 0.1); CruiseControlMetric deserialized = MetricSerde.fromBytes(MetricSerde.toBytes(topicMetric)); assertEquals(CruiseControlMetric.MetricClassId.TOPIC_METRIC.id(), deserialized.metricClassId().id()); assertEquals(RawMetricType.TOPIC_BYTES_IN.id(), deserialized.rawMetricType().id()); assertEquals(TIME, deserialized.time()); assertEquals(BROKER_ID, deserialized.brokerId()); assertEquals(TOPIC, ((TopicMetric) deserialized).topic()); assertEquals(VALUE, deserialized.value(), 0.000001); }
@Test public void testBrokerMetricSerde() throws UnknownVersionException { BrokerMetric brokerMetric = new BrokerMetric(RawMetricType.ALL_TOPIC_BYTES_IN, 123L, 0, 0.1); CruiseControlMetric deserialized = MetricSerde.fromBytes(MetricSerde.toBytes(brokerMetric)); assertEquals(CruiseControlMetric.MetricClassId.BROKER_METRIC.id(), deserialized.metricClassId().id()); assertEquals(RawMetricType.ALL_TOPIC_BYTES_IN.id(), deserialized.rawMetricType().id()); assertEquals(TIME, deserialized.time()); assertEquals(BROKER_ID, deserialized.brokerId()); assertEquals(VALUE, deserialized.value(), 0.000001); }
public ByteBuffer toBuffer(int headerPos) { byte[] topic = topic().getBytes(StandardCharsets.UTF_8); ByteBuffer buffer = ByteBuffer.allocate(headerPos + 1 /* version */ + 1 /* metric type */ + Long.BYTES /* time */ + Integer.BYTES /* broker id */ + Integer.BYTES /* topic length */ + topic.length /* topic */ + Integer.BYTES /* partition */ + Double.BYTES /* value */); buffer.position(headerPos); buffer.put(METRIC_VERSION); buffer.put(rawMetricType().id()); buffer.putLong(time()); buffer.putInt(brokerId()); buffer.putInt(topic.length); buffer.put(topic); buffer.putInt(_partition); buffer.putDouble(value()); return buffer; }
@Override public String toString() { return String.format("[%s,%s,time=%d,brokerId=%d,topic=%s,value=%.3f]", MetricClassId.TOPIC_METRIC, rawMetricType(), time(), brokerId(), topic(), value()); } }
@Override public String toString() { return String.format("[%s,%s,time=%d,brokerId=%d,value=%.3f]", MetricClassId.BROKER_METRIC, rawMetricType(), time(), brokerId(), value()); } }
public ByteBuffer toBuffer(int headerPos) { byte[] topic = _topic.getBytes(StandardCharsets.UTF_8); ByteBuffer buffer = ByteBuffer.allocate(headerPos + 1 /* version */ + 1 /* raw metric type */ + Long.BYTES /* time */ + Integer.BYTES /* broker id */ + Integer.BYTES /* topic length */ + topic.length /* topic */ + Double.BYTES /* value */); buffer.position(headerPos); buffer.put(METRIC_VERSION); buffer.put(rawMetricType().id()); buffer.putLong(time()); buffer.putInt(brokerId()); buffer.putInt(topic.length); buffer.put(topic); buffer.putDouble(value()); return buffer; }
public static byte[] toBytes(CruiseControlMetric metric) { ByteBuffer byteBuffer = metric.toBuffer(HEADER_LENGTH); byteBuffer.put(METRIC_TYPE_OFFSET, metric.metricClassId().id()); return byteBuffer.array(); }
static BrokerMetric fromBuffer(ByteBuffer buffer) throws UnknownVersionException { byte version = buffer.get(); if (version > METRIC_VERSION) { throw new UnknownVersionException("Cannot deserialize the topic metrics for version " + version + ". " + "Current version is " + METRIC_VERSION); } RawMetricType rawMetricType = RawMetricType.forId(buffer.get()); long time = buffer.getLong(); int brokerId = buffer.getInt(); double value = buffer.getDouble(); return new BrokerMetric(rawMetricType, time, brokerId, value); }
/** * Check if a yammer metric name is an interested metric */ public static boolean isInterested(com.yammer.metrics.core.MetricName metricName) { return isInterested(metricName.getGroup(), metricName.getName(), metricName.getType(), yammerMetricScopeToTags(metricName.getScope())); }
@Override public CruiseControlMetric deserialize(String topic, byte[] bytes) { try { return fromBytes(bytes); } catch (Exception e) { throw new RuntimeException("Error occurred when deserialize Cruise Control metrics.", e); } }
@Override public void processCounter(MetricName metricName, Counter counter, Context context) { if (MetricsUtils.isInterested(metricName)) { LOG.warn("Not processing metric {} of type Counter.", metricName); } }
@Override public ByteBuffer toBuffer(int headerPos) { ByteBuffer buffer = ByteBuffer.allocate(headerPos + 1 /* version */ + 1 /* raw metric type */ + Long.BYTES /* time */ + Integer.BYTES /* broker id */ + Double.BYTES /* value */); buffer.position(headerPos); buffer.put(METRIC_VERSION); buffer.put(rawMetricType().id()); buffer.putLong(time()); buffer.putInt(brokerId()); buffer.putDouble(value()); return buffer; }
/** * Convert a Yammer metric to a CruiseControlMetric */ public static CruiseControlMetric toCruiseControlMetric(long now, int brokerId, com.yammer.metrics.core.MetricName metricName, double value) { return toCruiseControlMetric(now, brokerId, metricName, value, null); }
/** * Check if a kafkaMetric is an interested metric. */ public static boolean isInterested(org.apache.kafka.common.MetricName metricName) { String group = metricName.group(); String name = metricName.name(); String type = metricName.tags().get(TYPE_KEY); return isInterested(group, name, type, metricName.tags()); }