Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Metricbeat] Add 'query' metricset for prometheus module #17104

Merged
merged 29 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7126e8a
[Metricbeat] Add 'query' metricset for prometheus module
estherk0 Dec 13, 2019
4d6e4fa
refactor the output structure for Prometheus query API
estherk0 Dec 19, 2019
c511e95
[Metricbeat] Support all expression query result formats
estherk0 Dec 21, 2019
bc303c6
[Metricbeat] convert numeric string to integer/float type
estherk0 Feb 5, 2020
be76be0
Merge branch 'support-prom-api-query' of https://github.com/openinfra…
ChrsMark Mar 17, 2020
55de062
wip
ChrsMark Mar 18, 2020
5bddf26
Fix fields, docs, config
ChrsMark Mar 18, 2020
e27ba4a
refactor and cleanups
ChrsMark Mar 18, 2020
8d2de92
Add unit&system tests
ChrsMark Mar 18, 2020
c87af0f
Add system tests
ChrsMark Mar 18, 2020
ccc1e54
Add changelog and fixes
ChrsMark Mar 19, 2020
2c38863
Merge remote-tracking branch 'upstream/master' into openinfradev-supp…
ChrsMark Mar 19, 2020
9e414dc
make update
ChrsMark Mar 19, 2020
5568fd4
review fixes
ChrsMark Mar 19, 2020
532baae
refactor code
ChrsMark Mar 19, 2020
a007fa7
Add unit tests
ChrsMark Mar 19, 2020
f6b5b6d
Add more unit tests to cover scalar and string
ChrsMark Mar 19, 2020
c60017d
Split range and instant vector responses into 2 different structs
ChrsMark Mar 19, 2020
0615a35
Make use of scalar and string info when converting their types
ChrsMark Mar 19, 2020
4a3f35c
Convert types explicitely according the dataType
ChrsMark Mar 19, 2020
509d62c
fixes
ChrsMark Mar 19, 2020
b7a50a7
Increase promtetheus warmup waiting time
ChrsMark Mar 19, 2020
b49dcfb
Fix configuration docstrings
ChrsMark Mar 20, 2020
65057ab
Improve metricset configuration
ChrsMark Mar 20, 2020
8c01056
make update
ChrsMark Mar 20, 2020
2bd38b2
Move parts to helper functions
ChrsMark Mar 20, 2020
175104d
Fix labels and move string query result to labels
ChrsMark Mar 20, 2020
f8c2501
fixes
ChrsMark Mar 23, 2020
c76cfc9
put __name__ labels back
ChrsMark Mar 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Add overview dashboard for googlecloud compute metricset. {issue}16534[16534] {pull}16819[16819]
- Add Prometheus remote write endpoint {pull}16609[16609]
- Release STAN module as GA. {pull}16980[16980]
- Add query metricset for prometheus module. {pull}17104[17104]
- Release ActiveMQ module as GA. {issue}17047[17047] {pull}17049[17049]
- Release Zookeeper/connection module as GA. {issue}14281[14281] {pull}17043[17043]
- Add support for CouchDB v2 {issue}16352[16352] {pull}16455[16455]
Expand Down
16 changes: 16 additions & 0 deletions metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -31605,6 +31605,22 @@ type: object

--

*`prometheus.query.*`*::
+
--
Prometheus value resulted from PromQL


type: object

--

[float]
=== query

query metricset


[float]
=== remote_write

Expand Down
27 changes: 27 additions & 0 deletions metricbeat/docs/modules/prometheus.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ metricbeat.modules:
#ssl.certificate_authorities:
# - /var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt


# Metrics sent by a Prometheus server using remote_write option
- module: prometheus
metricsets: ["remote_write"]
Expand All @@ -54,6 +55,28 @@ metricbeat.modules:
# Secure settings for the server using TLS/SSL:
#ssl.certificate: "/etc/pki/server/cert.pem"
#ssl.key: "/etc/pki/server/cert.key"


- module: prometheus
period: 10s
hosts: ["localhost:9090"]
metricsets: ["query"]
queries:
- query_name: "instant_vector"
path: "/api/v1/query"
query_params:
query: "sum(rate(prometheus_http_requests_total[1m]))"
- query_name: "range_vector"
path: "/api/v1/query_range"
query_params:
query: "up"
start: "2019-12-20T00:00:00.000Z"
end: "2019-12-21T00:00:00.000Z"
step: 1h
- query_name: "scalar"
path: "/api/v1/query"
query_params:
query: "100"
----

This module supports TLS connections when using `ssl` config field, as described in <<configuration-ssl>>.
Expand All @@ -66,9 +89,13 @@ The following metricsets are available:

* <<metricbeat-metricset-prometheus-collector,collector>>

* <<metricbeat-metricset-prometheus-query,query>>

* <<metricbeat-metricset-prometheus-remote_write,remote_write>>

include::prometheus/collector.asciidoc[]

include::prometheus/query.asciidoc[]

include::prometheus/remote_write.asciidoc[]

23 changes: 23 additions & 0 deletions metricbeat/docs/modules/prometheus/query.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
////
This file is generated! See scripts/mage/docs_collector.go
////

[[metricbeat-metricset-prometheus-query]]
=== Prometheus query metricset

beta[]

include::../../../module/prometheus/query/_meta/docs.asciidoc[]


==== Fields

For a description of each field in the metricset, see the
<<exported-fields-prometheus,exported fields>> section.

Here is an example document generated by this metricset:

[source,json]
----
include::../../../module/prometheus/query/_meta/data.json[]
----
3 changes: 2 additions & 1 deletion metricbeat/docs/modules_list.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ This file is generated! See scripts/mage/docs_collector.go
|<<metricbeat-metricset-postgresql-database,database>>
|<<metricbeat-metricset-postgresql-statement,statement>>
|<<metricbeat-module-prometheus,Prometheus>> |image:./images/icon-yes.png[Prebuilt dashboards are available] |
.2+| .2+| |<<metricbeat-metricset-prometheus-collector,collector>>
.3+| .3+| |<<metricbeat-metricset-prometheus-collector,collector>>
|<<metricbeat-metricset-prometheus-query,query>> beta[]
|<<metricbeat-metricset-prometheus-remote_write,remote_write>> beta[]
|<<metricbeat-module-rabbitmq,RabbitMQ>> |image:./images/icon-yes.png[Prebuilt dashboards are available] |
.4+| .4+| |<<metricbeat-metricset-rabbitmq-connection,connection>>
Expand Down
1 change: 1 addition & 0 deletions metricbeat/include/list_common.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions metricbeat/metricbeat.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ metricbeat.modules:
#ssl.certificate_authorities:
# - /var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt


# Metrics sent by a Prometheus server using remote_write option
- module: prometheus
metricsets: ["remote_write"]
Expand All @@ -727,6 +728,28 @@ metricbeat.modules:
#ssl.certificate: "/etc/pki/server/cert.pem"
#ssl.key: "/etc/pki/server/cert.key"


- module: prometheus
period: 10s
hosts: ["localhost:9090"]
metricsets: ["query"]
queries:
- query_name: "instant_vector"
path: "/api/v1/query"
query_params:
query: "sum(rate(prometheus_http_requests_total[1m]))"
- query_name: "range_vector"
path: "/api/v1/query_range"
query_params:
query: "up"
start: "2019-12-20T00:00:00.000Z"
end: "2019-12-21T00:00:00.000Z"
step: 1h
- query_name: "scalar"
path: "/api/v1/query"
query_params:
query: "100"

#------------------------------- RabbitMQ Module -------------------------------
- module: rabbitmq
metricsets: ["node", "queue", "connection"]
Expand Down
23 changes: 23 additions & 0 deletions metricbeat/module/prometheus/_meta/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ssl.certificate_authorities:
# - /var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt


# Metrics sent by a Prometheus server using remote_write option
- module: prometheus
metricsets: ["remote_write"]
Expand All @@ -24,3 +25,25 @@
# Secure settings for the server using TLS/SSL:
#ssl.certificate: "/etc/pki/server/cert.pem"
#ssl.key: "/etc/pki/server/cert.key"


- module: prometheus
period: 10s
hosts: ["localhost:9090"]
metricsets: ["query"]
queries:
- query_name: "instant_vector"
path: "/api/v1/query"
query_params:
query: "sum(rate(prometheus_http_requests_total[1m]))"
- query_name: "range_vector"
path: "/api/v1/query_range"
query_params:
query: "up"
start: "2019-12-20T00:00:00.000Z"
end: "2019-12-21T00:00:00.000Z"
step: 1h
- query_name: "scalar"
path: "/api/v1/query"
query_params:
query: "100"
6 changes: 6 additions & 0 deletions metricbeat/module/prometheus/_meta/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@
object_type_mapping_type: "*"
description: >
Prometheus metric
- name: query.*
type: object
object_type: double
object_type_mapping_type: "*"
description: >
Prometheus value resulted from PromQL
2 changes: 1 addition & 1 deletion metricbeat/module/prometheus/fields.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions metricbeat/module/prometheus/query/_meta/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"@timestamp": "2017-10-12T08:05:34.853Z",
"event": {
"dataset": "prometheus.query",
"duration": 115000,
"module": "prometheus"
},
"metricset": {
"name": "query",
"period": 10000
},
"prometheus": {
"labels": {
"__name__": "go_threads",
ChrsMark marked this conversation as resolved.
Show resolved Hide resolved
"instance": "localhost:9090",
"job": "prometheus"
},
"query": {
"dataType": "vector",
"go_threads": 24
}
},
"service": {
"address": "localhost:32768",
"type": "prometheus"
}
}
62 changes: 62 additions & 0 deletions metricbeat/module/prometheus/query/_meta/docs.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
This is the `query` metricset to query from https://prometheus.io/docs/prometheus/latest/querying/api/#expression-queries[querying API of Promtheus].


[float]
=== Configuration

[float]
==== Instant queries

The following configuration performs an instant query for `up` metric at a single point in time:
[source,yaml]
ChrsMark marked this conversation as resolved.
Show resolved Hide resolved
-------------------------------------------------------------------------------------
- module: prometheus
period: 10s
hosts: ["localhost:9090"]
metricsets: ["query"]
queries:
- query_name: 'up'
path: '/api/v1/query'
query_params:
query: "up"
-------------------------------------------------------------------------------------


More complex PromQL expressions can also be used like the following one which calculates the per-second rate of HTTP
requests as measured over the last 5 minutes.
[source,yaml]
-------------------------------------------------------------------------------------
- module: prometheus
period: 10s
hosts: ["localhost:9090"]
metricsets: ["query"]
queries:
- query_name: "rate_http_requests_total"
path: "/api/v1/query"
query_params:
query: "rate(prometheus_http_requests_total[5m])"
-------------------------------------------------------------------------------------




[float]
==== Range queries


The following example evaluates the expression `up` over a 30-second range with a query resolution of 15 seconds:
[source,yaml]
-------------------------------------------------------------------------------------
- module: prometheus
period: 10s
metricsets: ["query"]
hosts: ["node:9100"]
queries:
- query_name: "up_master"
path: "/api/v1/query_range"
query_params:
query: "up{node="master01"}"
start: "2019-12-20T23:30:30.000Z"
end: "2019-12-21T23:31:00.000Z"
step: 15s
-------------------------------------------------------------------------------------
6 changes: 6 additions & 0 deletions metricbeat/module/prometheus/query/_meta/fields.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- name: query
type: group
description: >
query metricset
release: beta
fields:
24 changes: 24 additions & 0 deletions metricbeat/module/prometheus/query/_meta/test/querymetrics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"status" : "success",
"data" : {
"resultType" : "vector",
"result" : [
{
"metric" : {
"__name__" : "up",
"job" : "prometheus",
"instance" : "localhost:9090"
},
"value": [ 1435781451.781, "1" ]
},
{
"metric" : {
"__name__" : "up",
"job" : "node",
"instance" : "localhost:9100"
},
"value" : [ 1435781451.781, "0" ]
}
]
}
}
59 changes: 59 additions & 0 deletions metricbeat/module/prometheus/query/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package query

import (
"errors"

"github.com/elastic/beats/v7/libbeat/common"
)

// Config defines the "query" metricset's configuration
type Config struct {
ChrsMark marked this conversation as resolved.
Show resolved Hide resolved
Queries []QueryConfig `config:"queries"`
DefaultQuery QueryConfig `config:"default_path"`
}

// QueryConfig is used to make an API request.
type QueryConfig struct {
ChrsMark marked this conversation as resolved.
Show resolved Hide resolved
Path string `config:"path"`
QueryParams common.MapStr `config:"query_params"`
QueryName string `config:"query_name"`
ChrsMark marked this conversation as resolved.
Show resolved Hide resolved
}

func defaultConfig() Config {
return Config{
DefaultQuery: QueryConfig{
Path: "/api/v1/query",
QueryName: "default",
},
}
}

// Validate for Prometheus "query" metricset config
func (p *QueryConfig) Validate() error {
if p.QueryName == "" {
return errors.New("`query_name` can not be empty in path configuration")
}

if p.Path == "" {
return errors.New("`path` can not be empty in path configuration")
}

return nil
}
Loading