Search in sources :

Example 1 with KafkaMetricMutableWrapper

use of org.apache.flink.streaming.connectors.kafka.internals.metrics.KafkaMetricMutableWrapper in project flink by apache.

the class FlinkKafkaProducer method initProducer.

private FlinkKafkaInternalProducer<byte[], byte[]> initProducer(boolean registerMetrics) {
    FlinkKafkaInternalProducer<byte[], byte[]> producer = createProducer();
    LOG.info("Starting FlinkKafkaInternalProducer ({}/{}) to produce into default topic {}", getRuntimeContext().getIndexOfThisSubtask() + 1, getRuntimeContext().getNumberOfParallelSubtasks(), defaultTopicId);
    // register Kafka metrics to Flink accumulators
    if (registerMetrics && !Boolean.parseBoolean(producerConfig.getProperty(KEY_DISABLE_METRICS, "false"))) {
        Map<MetricName, ? extends Metric> metrics = producer.metrics();
        if (metrics == null) {
            // MapR's Kafka implementation returns null here.
            LOG.info("Producer implementation does not support metrics");
        } else {
            final MetricGroup kafkaMetricGroup = getRuntimeContext().getMetricGroup().addGroup("KafkaProducer");
            for (Map.Entry<MetricName, ? extends Metric> entry : metrics.entrySet()) {
                String name = entry.getKey().name();
                Metric metric = entry.getValue();
                KafkaMetricMutableWrapper wrapper = previouslyCreatedMetrics.get(name);
                if (wrapper != null) {
                    wrapper.setKafkaMetric(metric);
                } else {
                    // TODO: somehow merge metrics from all active producers?
                    wrapper = new KafkaMetricMutableWrapper(metric);
                    previouslyCreatedMetrics.put(name, wrapper);
                    kafkaMetricGroup.gauge(name, wrapper);
                }
            }
        }
    }
    return producer;
}
Also used : MetricName(org.apache.kafka.common.MetricName) KafkaMetricMutableWrapper(org.apache.flink.streaming.connectors.kafka.internals.metrics.KafkaMetricMutableWrapper) MetricGroup(org.apache.flink.metrics.MetricGroup) Metric(org.apache.kafka.common.Metric) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 MetricGroup (org.apache.flink.metrics.MetricGroup)1 KafkaMetricMutableWrapper (org.apache.flink.streaming.connectors.kafka.internals.metrics.KafkaMetricMutableWrapper)1 Metric (org.apache.kafka.common.Metric)1 MetricName (org.apache.kafka.common.MetricName)1