Search in sources :

Example 1 with SamplingException

use of com.linkedin.kafka.cruisecontrol.exception.SamplingException in project cruise-control by linkedin.

the class PrometheusMetricSampler method retrieveMetricsForProcessing.

@Override
protected int retrieveMetricsForProcessing(MetricSamplerOptions metricSamplerOptions) throws SamplingException {
    int metricsAdded = 0;
    int resultsSkipped = 0;
    for (Map.Entry<RawMetricType, String> metricToQueryEntry : _metricToPrometheusQueryMap.entrySet()) {
        final RawMetricType metricType = metricToQueryEntry.getKey();
        final String prometheusQuery = metricToQueryEntry.getValue();
        final List<PrometheusQueryResult> prometheusQueryResults;
        try {
            prometheusQueryResults = _prometheusAdapter.queryMetric(prometheusQuery, metricSamplerOptions.startTimeMs(), metricSamplerOptions.endTimeMs());
        } catch (IOException e) {
            LOG.error("Error when attempting to query Prometheus metrics", e);
            throw new SamplingException("Could not query metrics from Prometheus");
        }
        for (PrometheusQueryResult result : prometheusQueryResults) {
            try {
                switch(metricType.metricScope()) {
                    case BROKER:
                        metricsAdded += addBrokerMetrics(metricSamplerOptions.cluster(), metricType, result);
                        break;
                    case TOPIC:
                        metricsAdded += addTopicMetrics(metricSamplerOptions.cluster(), metricType, result);
                        break;
                    case PARTITION:
                        metricsAdded += addPartitionMetrics(metricSamplerOptions.cluster(), metricType, result);
                        break;
                    default:
                        // Not supported.
                        break;
                }
            } catch (InvalidPrometheusResultException e) {
                /* We can ignore invalid or malformed Prometheus results, for example one which has a hostname
                    that could not be matched to any broker, or one where the topic name is null. Such records
                    will not be converted to metrics. There are valid use cases where this may occur - for instance,
                    when a Prometheus server store metrics from multiple Kafka clusters, in which case the hostname
                    may not correspond to any of this cluster's broker hosts.

                    This can be really frequent, and hence, we are only going to log them at trace level.
                     */
                LOG.trace("Invalid query result received from Prometheus for query {}", prometheusQuery, e);
                resultsSkipped++;
            }
        }
    }
    LOG.info("Added {} metric values. Skipped {} invalid query results.", metricsAdded, resultsSkipped);
    return metricsAdded;
}
Also used : RawMetricType(com.linkedin.kafka.cruisecontrol.metricsreporter.metric.RawMetricType) PrometheusQueryResult(com.linkedin.kafka.cruisecontrol.monitor.sampling.prometheus.model.PrometheusQueryResult) IOException(java.io.IOException) SamplingException(com.linkedin.kafka.cruisecontrol.exception.SamplingException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

SamplingException (com.linkedin.kafka.cruisecontrol.exception.SamplingException)1 RawMetricType (com.linkedin.kafka.cruisecontrol.metricsreporter.metric.RawMetricType)1 PrometheusQueryResult (com.linkedin.kafka.cruisecontrol.monitor.sampling.prometheus.model.PrometheusQueryResult)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1