Commit 84615ad2 by Torkel Ödegaard

Merge pull request #2855 from jimmidyson/prometheus-datasource

Prometheus docs & better template queries
parents 2f55c18d 2e291d73
......@@ -66,6 +66,7 @@ pages:
- ['datasources/influxdb.md', 'Data Sources', 'InfluxDB']
- ['datasources/opentsdb.md', 'Data Sources', 'OpenTSDB']
- ['datasources/kairosdb.md', 'Data Sources', 'KairosDB']
- ['datasources/prometheus.md', 'Data Sources', 'Prometheus']
- ['tutorials/index.md', 'Tutorials', 'Tutorials']
- ['tutorials/hubot_howto.md', 'Tutorials', 'How To integrate Hubot and Grafana']
......
----
page_title: Data Source Overview
page_description: Data Source Overview
page_keywords: grafana, graphite, influxDB, KairosDB, OpenTSDB, documentation
page_keywords: grafana, graphite, influxDB, KairosDB, OpenTSDB, Prometheus, documentation
---
# Data Source Overview
......@@ -18,5 +18,6 @@ The following datasources are officially supported:
* [InfluxDB](/datasources/influxdb/)
* [OpenTSDB](/datasources/opentsdb/)
* [KairosDB](/datasources/kairosdb)
* [Prometheus](/datasources/prometheus)
Grafana can query any Elasticsearch index for annotation events, but at this time, it's not supported for metric queries. Learn more about [annotations](/reference/annotations/#elasticsearch-annotations)
Grafana can query Failcsearch index for annotation events, but at this time, it's not supported for metric queries. Learn more about [annotations](/reference/annotations/#elasticsearch-annotations)
----
page_title: Prometheus query guide
page_description: Prometheus query guide
page_keywords: grafana, prometheus, metrics, query, documentation
---
# Prometheus
Grafana includes support for Prometheus Datasources. While the process of adding the datasource is similar to adding a Graphite or OpenTSDB datasource type, Prometheus does have a few different options for building queries.
## Adding the data source to Grafana
![](/img/v2/add_Prometheus.jpg)
1. Open the side menu by clicking the the Grafana icon in the top header.
2. In the side menu under the `Dashboards` link you should find a link named `Data Sources`.
> NOTE: If this link is missing in the side menu it means that your current user does not have the `Admin` role for the current organization.
3. Click the `Add new` link in the top header.
4. Select `Prometheus` from the dropdown.
Name | Description
------------ | -------------
Name | The data source name, important that this is the same as in Grafana v1.x if you plan to import old dashboards.
Default | Default data source means that it will be pre-selected for new panels.
Url | The http protocol, ip and port of you Prometheus server (default port is usually 9090)
Access | Proxy = access via Grafana backend, Direct = access directory from browser.
Basic Auth | Enable basic authentication to the Prometheus datasource.
User | Name of your Prometheus user
Password | Database user's password
> Proxy access means that the Grafana backend will proxy all requests from the browser, and send them on to the Data Source. This is useful because it can eliminate CORS (Cross Origin Site Resource) issues, as well as eliminate the need to disseminate authentication details to the Data Source to the brower.
> Direct access is still supported because in some cases it may be useful to access a Data Source directly depending on the use case and topology of Grafana, the user, and the Data Source.
## Query editor
Open a graph in edit mode by click the title.
![](/img/v2/prometheus_editor.png)
For details on Prometheus metric queries check out the Prometheus documentation
- [Query Metrics - Prometheus documentation](http://prometheus.io/docs/querying/basics/).
## Templated queries
Prometheus Datasource Plugin provides the following functions in `Variables values query` field in Templating Editor to query `metric names` and `labels names` on the Prometheus server.
Name | Description
------- | --------
`label_values(label)` | Returns a list of label values for the `label` in every metric.
`label_values(metric, label)` | Returns a list of label values for the `label` in the specified metric.
`metrics(metric)` | Returns a list of metrics matching the specified `metric` regex.
For details of `metric names` & `label names`, and `label values`, please refer to the [Prometheus documentation](http://prometheus.io/docs/concepts/data_model/#metric-names-and-labels).
You can create a template variable in Grafana and have that variable filled with values from any Prometheus metric exploration query.
You can then use this variable in your Prometheus metric queries.
For example you can have a variable that contains all values for label `hostname` if you specify a query like this
in the templating edit view.
```sql
label_values(hostname)
```
You can also use raw queries & regular expressions to extract anything you might need.
![](/img/v2/prometheus_templating.png)
......@@ -128,7 +128,7 @@ function (angular, _, moment, dateMath) {
};
PrometheusDatasource.prototype.metricFindQuery = function(query) {
var url;
if (!query) { return $q.when([]); }
var interpolated;
try {
......@@ -138,27 +138,52 @@ function (angular, _, moment, dateMath) {
return $q.reject(err);
}
var metricsQuery = interpolated.match(/^[a-zA-Z_:*][a-zA-Z0-9_:*]*/);
var labelValuesQuery = interpolated.match(/^label_values\((.+)\)/);
if (labelValuesQuery) {
// return label values
url = '/api/v1/label/' + labelValuesQuery[1] + '/values';
var label_values_regex = /^label_values\(([^,]+)(?:,\s*(.+))?\)$/;
var metric_names_regex = /^metrics\((.+)\)$/;
return this._request('GET', url).then(function(result) {
return _.map(result.data.data, function(value) {
return {text: value};
var url;
var label_values_query = interpolated.match(label_values_regex);
if (label_values_query) {
if (!label_values_query[2]) {
// return label values globally
url = '/api/v1/label/' + label_values_query[1] + '/values';
return this._request('GET', url).then(function(result) {
return _.map(result.data.data, function(value) {
return {text: value};
});
});
});
} else if (metricsQuery != null && metricsQuery[0].indexOf('*') >= 0) {
// if query has wildcard character, return metric name list
} else {
var metric_query = 'count(' + label_values_query[1] + ') by (' +
label_values_query[2] + ')';
url = '/api/v1/query?query=' + encodeURIComponent(metric_query) +
'&time=' + (moment().valueOf() / 1000);
return this._request('GET', url)
.then(function(result) {
if (result.data.data.result.length === 0 ||
_.keys(result.data.data.result[0].metric).length === 0) {
return [];
}
return _.map(result.data.data.result, function(metricValue) {
return {
text: metricValue.metric[label_values_query[2]],
expandable: true
};
});
});
}
}
var metric_names_query = interpolated.match(metric_names_regex);
if (metric_names_query) {
url = '/api/v1/label/__name__/values';
return this._request('GET', url)
.then(function(result) {
return _.chain(result.data.data)
.filter(function(metricName) {
var r = new RegExp(metricsQuery[0].replace(/\*/g, '.*'));
var r = new RegExp(metric_names_query[1]);
return r.test(metricName);
})
.map(function(matchedMetricName) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment