use of com.linkedin.kafka.cruisecontrol.monitor.sampling.prometheus.model.PrometheusQueryResult 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;
}
use of com.linkedin.kafka.cruisecontrol.monitor.sampling.prometheus.model.PrometheusQueryResult in project cruise-control by linkedin.
the class PrometheusAdapterTest method testSuccessfulResponseDeserialized.
@Test
public void testSuccessfulResponseDeserialized() throws Exception {
this.serverBootstrap.registerHandler(PrometheusAdapter.QUERY_RANGE_API_PATH, new HttpRequestHandler() {
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context) {
response.setStatusCode(HttpServletResponse.SC_OK);
response.setEntity(buildSuccessResponseEntity());
}
});
HttpHost httpHost = this.start();
PrometheusAdapter prometheusAdapter = new PrometheusAdapter(this.httpclient, httpHost, SAMPLING_INTERVAL_MS);
final List<PrometheusQueryResult> prometheusQueryResults = prometheusAdapter.queryMetric("kafka_server_BrokerTopicMetrics_OneMinuteRate{name=\"BytesOutPerSec\",topic=\"\"}", START_TIME_MS, END_TIME_MS);
assertEquals(expectedResults().toString(), prometheusQueryResults.toString());
assertEquals(expectedResults(), prometheusQueryResults);
}
Aggregations