-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat(inputs.promql): Add plugin #17701
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8bd31e7
feat(inputs.promql): Add plugin
srebhan dbbb8c2
Fix linter issues
srebhan c2871b6
Add license information for dependency
srebhan b4b6e2d
Allow to set a fallback metric name for queries
srebhan 80bd4b4
Update plugins/inputs/promql/README.md
srebhan 29037e3
Update plugins/inputs/promql/README.md
srebhan 6b4b9d2
Only set timeout if specified
srebhan f0e6324
Update plugins/inputs/promql/README.md
srebhan 9801478
Update plugins/inputs/promql/README.md
srebhan 2d82bea
Update plugins/inputs/promql/README.md
srebhan dd8e2cd
Whitespaces
srebhan b34e771
Check for closer interface
srebhan 5694fcf
Validate queries on init
srebhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat(inputs.promql): Add plugin
- Loading branch information
commit 8bd31e780f08dab39a9f9fdd369e1b5ae3864ed6
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| ## HTTP connection settings | ||
| # idle_conn_timeout = "0s" | ||
| # max_idle_conn = 0 | ||
| # max_idle_conn_per_host = 0 | ||
| # response_timeout ="0s" | ||
|
|
||
| ## Optional proxy settings | ||
| # use_system_proxy = false | ||
| # http_proxy_url = "" | ||
|
|
||
| ## Optional TLS settings | ||
| ## Set to true/false to enforce TLS being enabled/disabled. If not set, | ||
| ## enable TLS only if any of the other options are specified. | ||
| # tls_enable = | ||
| ## Trusted root certificates for server | ||
| # tls_ca = "/path/to/cafile" | ||
| ## Used for TLS client certificate authentication | ||
| # tls_cert = "/path/to/certfile" | ||
| ## Used for TLS client certificate authentication | ||
| # tls_key = "/path/to/keyfile" | ||
| ## Password for the key file if it is encrypted | ||
| # tls_key_pwd = "" | ||
| ## Send the specified TLS server name via SNI | ||
| # tls_server_name = "kubernetes.example.com" | ||
| ## Minimal TLS version to accept by the client | ||
| # tls_min_version = "TLS12" | ||
| ## List of ciphers to accept, by default all secure ciphers will be accepted | ||
| ## See https://pkg.go.dev/crypto/tls#pkg-constants for supported values. | ||
| ## Use "all", "secure" and "insecure" to add all support ciphers, secure | ||
| ## suites or insecure suites respectively. | ||
| # tls_cipher_suites = ["secure"] | ||
| ## Renegotiation method, "never", "once" or "freely" | ||
| # tls_renegotiation_method = "never" | ||
| ## Use TLS but skip chain & host verification | ||
| # insecure_skip_verify = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ## HTTP connection settings | ||
| # idle_conn_timeout = "0s" | ||
| # max_idle_conn = 0 | ||
| # max_idle_conn_per_host = 0 | ||
| # response_timeout ="0s" | ||
|
|
||
| ## Optional proxy settings | ||
| {{template "/plugins/common/proxy/proxy.conf"}} | ||
|
|
||
| ## Optional TLS settings | ||
| {{template "/plugins/common/tls/client.conf"}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # use_system_proxy = false | ||
| # http_proxy_url = "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| //go:build !custom || inputs || inputs.promql | ||
|
|
||
| package all | ||
|
|
||
| import _ "github.com/influxdata/telegraf/plugins/inputs/promql" // register plugin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| # PromQL Input Plugin | ||
|
|
||
| This plugin gathers metrics from a [Prometheus][prometheus] endpoint using | ||
| [PromQL queries][promql] via the [HTTP API][http_api]. | ||
|
|
||
| ⭐ Telegraf v1.37.0 | ||
| 🏷️ datastore | ||
| 💻 all | ||
|
|
||
| [prometheus]: https://prometheus.io/ | ||
| [promql]: https://prometheus.io/docs/prometheus/latest/querying/basics/ | ||
| [http_api]: https://prometheus.io/docs/prometheus/latest/querying/api/ | ||
|
|
||
| ## Global configuration options <!-- @/docs/includes/plugin_config.md --> | ||
|
|
||
| In addition to the plugin-specific configuration settings, plugins support | ||
| additional global and plugin configuration settings. These settings are used to | ||
| modify metrics, tags, and field or create aliases and configure ordering, etc. | ||
| See the [CONFIGURATION.md][CONFIGURATION.md] for more details. | ||
|
|
||
| [CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins | ||
|
|
||
| ## Secret-store support | ||
|
|
||
| This plugin supports secrets from secret-stores for the `username`, `password` | ||
| and `token` option. See the [secret-store documentation][SECRETSTORE] for | ||
| more details on how to use them. | ||
|
|
||
| [SECRETSTORE]: ../../../docs/CONFIGURATION.md#secret-store-secrets | ||
|
|
||
| ## Configuration | ||
|
|
||
| ```toml @sample.conf | ||
| # Query prometheus endpoints using PromQL | ||
| [[inputs.promql]] | ||
| ## URL of the prometheus endpoint | ||
| url = "http://localhost:9090" | ||
|
|
||
| ## Basic authentication properties | ||
| # username = "" | ||
| # password = "" | ||
|
|
||
| ## Bearer token based authentication | ||
| # token = "" | ||
|
|
||
| ## Timeout for executing queries with zero meaning no timeout | ||
| # timeout = "5s" | ||
|
|
||
| ## HTTP connection settings | ||
| # idle_conn_timeout = "0s" | ||
| # max_idle_conn = 0 | ||
| # max_idle_conn_per_host = 0 | ||
| # response_timeout ="0s" | ||
|
|
||
| ## Optional proxy settings | ||
| # use_system_proxy = false | ||
| # http_proxy_url = "" | ||
|
|
||
| ## Optional TLS settings | ||
| ## Set to true/false to enforce TLS being enabled/disabled. If not set, | ||
| ## enable TLS only if any of the other options are specified. | ||
| # tls_enable = | ||
| ## Trusted root certificates for server | ||
| # tls_ca = "/path/to/cafile" | ||
| ## Used for TLS client certificate authentication | ||
| # tls_cert = "/path/to/certfile" | ||
| ## Used for TLS client certificate authentication | ||
| # tls_key = "/path/to/keyfile" | ||
| ## Password for the key file if it is encrypted | ||
| # tls_key_pwd = "" | ||
| ## Send the specified TLS server name via SNI | ||
| # tls_server_name = "kubernetes.example.com" | ||
| ## Minimal TLS version to accept by the client | ||
| # tls_min_version = "TLS12" | ||
| ## List of ciphers to accept, by default all secure ciphers will be accepted | ||
| ## See https://pkg.go.dev/crypto/tls#pkg-constants for supported values. | ||
| ## Use "all", "secure" and "insecure" to add all support ciphers, secure | ||
| ## suites or insecure suites respectively. | ||
| # tls_cipher_suites = ["secure"] | ||
| ## Renegotiation method, "never", "once" or "freely" | ||
| # tls_renegotiation_method = "never" | ||
| ## Use TLS but skip chain & host verification | ||
| # insecure_skip_verify = false | ||
|
|
||
| ## Instant queries, multiple instances are allowed | ||
| # [[inputs.promql.instant]] | ||
| # ## Query to execute | ||
| # query = 'prometheus_http_requests_total' | ||
| # | ||
| # ## Limit for the number of results returned by the server with zero | ||
| # ## meaning no limit | ||
| # # limit = 0 | ||
|
|
||
| ## Rangequeries, multiple instances are allowed | ||
|
srebhan marked this conversation as resolved.
Outdated
|
||
| # [[inputs.promql.range]] | ||
| # ## Query to execute | ||
| # query = 'prometheus_http_requests_total{job="prometheus"}' | ||
| # ## Range parameters relative to the gathering time with positive values | ||
| # ## refer to BEFORE and negative to AFTER the gathering time | ||
| # start = "5m" | ||
| # # end = "0s" | ||
| # step = "1m" | ||
| # | ||
| # ## Limit for the number of results returned by the server with zero | ||
| # ## meaning no limit | ||
| # # limit = 0 | ||
| ``` | ||
|
|
||
| > [!NOTE] | ||
| > You can either use no authentication _or_ basic authentication _or_ Bearer | ||
| > token based authentication. Uncommenting both basic and Bearer token based | ||
| > authentication will fail. | ||
|
|
||
| ## Metrics | ||
|
|
||
| The metrics collected by this input plugin will depend on the specified queries. | ||
| However, the resulting metrics will have the following structure for the | ||
| returned results. | ||
|
|
||
| ### Scalar and String Results | ||
|
|
||
| A scalar or string result will produce a single metrics named after the value of | ||
|
srebhan marked this conversation as resolved.
Outdated
|
||
| the Prometheus `__name__` label. Other labels will be kept as tags. The | ||
| resulting metric used the Prometheus timestamp and will have the value stored in | ||
|
srebhan marked this conversation as resolved.
Outdated
|
||
| a `value` field. | ||
|
|
||
| ### Vector and Matrix Results | ||
|
|
||
| A vector result will produce one or more metrics with the metric named after the | ||
| value of the Prometheus `__name__` label for each element of the vector. Other | ||
| labels will be kept as tags. All metrics will use the Prometheus timestamp. | ||
|
|
||
| Non-histogram results will have the value stored in a `value` field. Histogram | ||
| results will contain multiple fields with the fieldname being the upper bound | ||
|
srebhan marked this conversation as resolved.
Outdated
|
||
| of the bin and a value with the bin count. Additionally, the metric will have a | ||
| `count` and a `sum` field. | ||
|
|
||
| ## Example Output | ||
|
|
||
| For example, a range-query for | ||
| `prometheus_http_requests_total{job="prometheus", handler="/api/v1/query"}` | ||
| starting 5 minutes in the past with 1 minute stepping returns | ||
|
|
||
| ```text | ||
| prometheus_http_requests_total,app=prometheus,code=200,handler=/api/v1/query,instance=localhost:9090,job=prometheus value=28 1758806201000000000 | ||
| prometheus_http_requests_total,app=prometheus,code=200,handler=/api/v1/query,instance=localhost:9090,job=prometheus value=28 1758806261000000000 | ||
| prometheus_http_requests_total,app=prometheus,code=200,handler=/api/v1/query,instance=localhost:9090,job=prometheus value=28 1758806321000000000 | ||
| prometheus_http_requests_total,app=prometheus,code=200,handler=/api/v1/query,instance=localhost:9090,job=prometheus value=28 1758806381000000000 | ||
| prometheus_http_requests_total,app=prometheus,code=200,handler=/api/v1/query,instance=localhost:9090,job=prometheus value=28 1758806441000000000 | ||
| prometheus_http_requests_total,app=prometheus,code=200,handler=/api/v1/query,instance=localhost:9090,job=prometheus value=28 1758806501000000000 | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package promql | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/influxdata/telegraf/config" | ||
| "github.com/influxdata/telegraf/internal" | ||
| common_http "github.com/influxdata/telegraf/plugins/common/http" | ||
|
|
||
| "github.com/prometheus/client_golang/api" | ||
| apiv1 "github.com/prometheus/client_golang/api/prometheus/v1" | ||
| promcfg "github.com/prometheus/common/config" | ||
| ) | ||
|
|
||
| type client struct { | ||
| url string | ||
| username config.Secret | ||
| password config.Secret | ||
| token config.Secret | ||
| cfg common_http.TransportConfig | ||
|
|
||
| client api.Client | ||
| apiv1.API | ||
| } | ||
|
|
||
| func (c *client) init() (*client, error) { | ||
| // Create a round-tripper suitable for the given configuration based on the | ||
| // http-client transport... | ||
| transport, err := c.cfg.CreateTransport() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("creating transport failed: %w", err) | ||
| } | ||
| rt := promcfg.NewUserAgentRoundTripper(internal.ProductToken(), transport) | ||
| if !c.username.Empty() { | ||
| rt = promcfg.NewBasicAuthRoundTripper( | ||
| &secretReader{"username", &c.username}, | ||
| &secretReader{"password", &c.password}, | ||
| rt, | ||
| ) | ||
| } else if !c.token.Empty() { | ||
| rt = promcfg.NewAuthorizationCredentialsRoundTripper( | ||
| "Bearer", | ||
| &secretReader{"token", &c.token}, | ||
| rt, | ||
| ) | ||
| } | ||
|
|
||
| // Create API client | ||
| apiClient, err := api.NewClient(api.Config{ | ||
| Address: c.url, | ||
| RoundTripper: rt, | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("creating API client failed: %w", err) | ||
| } | ||
| c.client = apiClient | ||
| c.API = apiv1.NewAPI(c.client) | ||
|
|
||
| return c, nil | ||
| } | ||
|
|
||
| func (c *client) close() { | ||
| if c.client != nil { | ||
| c.client.(api.CloseIdler).CloseIdleConnections() | ||
|
srebhan marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| // Wrapper for reading secrets from Prometheus API client | ||
| type secretReader struct { | ||
| desc string | ||
| secret *config.Secret | ||
| } | ||
|
|
||
| func (r *secretReader) Fetch(ctx context.Context) (string, error) { | ||
| raw, err := r.secret.Get() | ||
| if err != nil { | ||
| return "", fmt.Errorf("getting %s failed: %w", r.desc, err) | ||
| } | ||
| s := raw.String() | ||
| raw.Destroy() | ||
|
|
||
| return s, nil | ||
| } | ||
|
|
||
| func (r *secretReader) Description() string { | ||
| return r.desc | ||
| } | ||
|
|
||
| func (*secretReader) Immutable() bool { | ||
| return true | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.