├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ ├── config.yml │ ├── feature-request---new-table.md │ └── feature_request.md ├── PULL_REQUEST_TEMPLATE.md ├── dependabot.yml └── workflows │ ├── golangci-lint.yml │ ├── registry-publish.yml │ ├── stale.yml │ ├── steampipe-anywhere.yml │ └── sync-labels.yml ├── .gitignore ├── .goreleaser.yml ├── CHANGELOG.md ├── LICENSE ├── Makefile ├── README.md ├── config └── prometheus.spc ├── docs ├── LICENSE ├── index.md └── tables │ ├── prometheus_alert.md │ ├── prometheus_label.md │ ├── prometheus_metric.md │ ├── prometheus_rule.md │ ├── prometheus_rule_group.md │ ├── prometheus_series.md │ ├── prometheus_target.md │ └── {metric_name}.md ├── go.mod ├── go.sum ├── main.go └── prometheus ├── connection_config.go ├── plugin.go ├── table_prometheus_alert.go ├── table_prometheus_dynamic_metric.go ├── table_prometheus_label.go ├── table_prometheus_metric.go ├── table_prometheus_rule.go ├── table_prometheus_rule_group.go ├── table_prometheus_series.go ├── table_prometheus_target.go └── utils.go /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **Steampipe version (`steampipe -v`)** 14 | Example: v0.3.0 15 | 16 | **Plugin version (`steampipe plugin list`)** 17 | Example: v0.5.0 18 | 19 | **To reproduce** 20 | Steps to reproduce the behavior (please include relevant code and/or commands). 21 | 22 | **Expected behavior** 23 | A clear and concise description of what you expected to happen. 24 | 25 | **Additional context** 26 | Add any other context about the problem here. 27 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: Questions 4 | url: https://turbot.com/community/join 5 | about: GitHub issues in this repository are only intended for bug reports and feature requests. Other issues will be closed. Please ask and answer questions through the Steampipe Slack community. 6 | - name: Steampipe CLI Bug Reports and Feature Requests 7 | url: https://github.com/turbot/steampipe/issues/new/choose 8 | about: Steampipe CLI has its own codebase. Bug reports and feature requests for those pieces of functionality should be directed to that repository. -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature-request---new-table.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request - New table 3 | about: Suggest a new table for this project 4 | title: Add table prometheus_ 5 | labels: enhancement, new table 6 | assignees: "" 7 | --- 8 | 9 | **References** 10 | Add any related links that will help us understand the resource, including vendor documentation, related GitHub issues, and Go SDK documentation. 11 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: enhancement 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | # Integration test logs 2 |
3 | Logs 4 | 5 | ``` 6 | Add passing integration test logs here 7 | ``` 8 |
9 | 10 | # Example query results 11 |
12 | Results 13 | 14 | ``` 15 | Add example SQL query results here (please include the input queries as well) 16 | ``` 17 |
18 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "gomod" # See documentation for possible values 9 | directory: "/" # Location of package manifests 10 | schedule: 11 | interval: "weekly" 12 | pull-request-branch-name: 13 | separator: "-" 14 | assignees: 15 | - "misraved" 16 | - "madhushreeray30" 17 | labels: 18 | - "dependencies" 19 | -------------------------------------------------------------------------------- /.github/workflows/golangci-lint.yml: -------------------------------------------------------------------------------- 1 | name: golangci-lint 2 | on: 3 | push: 4 | tags: 5 | - v* 6 | branches: 7 | - main 8 | pull_request: 9 | 10 | jobs: 11 | golangci_lint_workflow: 12 | uses: turbot/steampipe-workflows/.github/workflows/golangci-lint.yml@main 13 | -------------------------------------------------------------------------------- /.github/workflows/registry-publish.yml: -------------------------------------------------------------------------------- 1 | name: Build and Deploy OCI Image 2 | 3 | on: 4 | push: 5 | tags: 6 | - 'v*' 7 | 8 | jobs: 9 | registry_publish_workflow_ghcr: 10 | uses: turbot/steampipe-workflows/.github/workflows/registry-publish-ghcr.yml@main 11 | secrets: inherit 12 | with: 13 | releaseTimeout: 60m 14 | -------------------------------------------------------------------------------- /.github/workflows/stale.yml: -------------------------------------------------------------------------------- 1 | name: Stale Issues and PRs 2 | on: 3 | schedule: 4 | - cron: "30 23 * * *" 5 | workflow_dispatch: 6 | inputs: 7 | dryRun: 8 | description: Set to true for a dry run 9 | required: false 10 | default: "false" 11 | type: string 12 | 13 | jobs: 14 | stale_workflow: 15 | uses: turbot/steampipe-workflows/.github/workflows/stale.yml@main 16 | with: 17 | dryRun: ${{ github.event.inputs.dryRun }} 18 | -------------------------------------------------------------------------------- /.github/workflows/steampipe-anywhere.yml: -------------------------------------------------------------------------------- 1 | name: Release Steampipe Anywhere Components 2 | 3 | on: 4 | push: 5 | tags: 6 | - 'v*' 7 | 8 | 9 | jobs: 10 | anywhere_publish_workflow: 11 | uses: turbot/steampipe-workflows/.github/workflows/steampipe-anywhere.yml@main 12 | secrets: inherit 13 | -------------------------------------------------------------------------------- /.github/workflows/sync-labels.yml: -------------------------------------------------------------------------------- 1 | name: Sync Labels 2 | on: 3 | schedule: 4 | - cron: "30 22 * * 1" 5 | workflow_dispatch: 6 | 7 | jobs: 8 | sync_labels_workflow: 9 | uses: turbot/steampipe-workflows/.github/workflows/sync-labels.yml@main 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Editor cache and lock files 2 | *.swp 3 | *.swo 4 | 5 | # Binaries for programs and plugins 6 | *.exe 7 | *.exe~ 8 | *.dll 9 | *.so 10 | *.dylib 11 | 12 | # Test binary, built with `go test -c` 13 | *.test 14 | 15 | # Output of the go coverage tool, specifically when used with LiteIDE 16 | *.out 17 | 18 | # Dependency directories (remove the comment below to include it) 19 | # vendor/ 20 | -------------------------------------------------------------------------------- /.goreleaser.yml: -------------------------------------------------------------------------------- 1 | # This is an example goreleaser.yaml file with some sane defaults. 2 | # Make sure to check the documentation at http://goreleaser.com 3 | before: 4 | hooks: 5 | - go mod tidy 6 | builds: 7 | - env: 8 | - CGO_ENABLED=0 9 | - GO111MODULE=on 10 | - GOPRIVATE=github.com/turbot 11 | goos: 12 | - linux 13 | - darwin 14 | 15 | goarch: 16 | - amd64 17 | - arm64 18 | 19 | id: "steampipe" 20 | binary: "{{ .ProjectName }}.plugin" 21 | flags: 22 | - -tags=netgo 23 | 24 | archives: 25 | - format: gz 26 | name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}" 27 | files: 28 | - none* 29 | checksum: 30 | name_template: "{{ .ProjectName }}_{{ .Version }}_SHA256SUMS" 31 | algorithm: sha256 32 | changelog: 33 | sort: asc 34 | filters: 35 | exclude: 36 | - "^docs:" 37 | - "^test:" 38 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## v1.1.1 [2025-04-18] 2 | 3 | _Bug fixes_ 4 | 5 | - Fixed Linux AMD64 plugin build failures for `Postgres 14 FDW`, `Postgres 15 FDW`, and `SQLite Extension` by upgrading GitHub Actions runners from `ubuntu-20.04` to `ubuntu-22.04`. 6 | 7 | ## v1.1.0 [2025-04-17] 8 | 9 | _Dependencies_ 10 | 11 | - Recompiled plugin with Go version `1.23.1`. ([#86](https://github.com/turbot/steampipe-plugin-prometheus/pull/86)) 12 | - Recompiled plugin with [steampipe-plugin-sdk v5.11.5](https://github.com/turbot/steampipe-plugin-sdk/blob/v5.11.5/CHANGELOG.md#v5115-2025-03-31) that addresses critical and high vulnerabilities in dependent packages. ([#86](https://github.com/turbot/steampipe-plugin-prometheus/pull/86)) 13 | 14 | ## v1.0.0 [2024-10-22] 15 | 16 | There are no significant changes in this plugin version; it has been released to align with [Steampipe's v1.0.0](https://steampipe.io/changelog/steampipe-cli-v1-0-0) release. This plugin adheres to [semantic versioning](https://semver.org/#semantic-versioning-specification-semver), ensuring backward compatibility within each major version. 17 | 18 | _Dependencies_ 19 | 20 | - Recompiled plugin with Go version `1.22`. ([#64](https://github.com/turbot/steampipe-plugin-prometheus/pull/64)) 21 | - Recompiled plugin with [steampipe-plugin-sdk v5.10.4](https://github.com/turbot/steampipe-plugin-sdk/blob/develop/CHANGELOG.md#v5104-2024-08-29) that fixes logging in the plugin export tool. ([#64](https://github.com/turbot/steampipe-plugin-prometheus/pull/64)) 22 | 23 | ## v0.6.2 [2024-02-13] 24 | 25 | _Bug fixes_ 26 | 27 | - Fixed the plugin initialization error by returning only the static tables when invalid config parameters were set for dynamic tables. ([#39](https://github.com/turbot/steampipe-plugin-prometheus/pull/39)) 28 | 29 | ## v0.6.1 [2023-12-12] 30 | 31 | _Bug fixes_ 32 | 33 | - Fixed the missing optional tag on the `Metrics` connection config parameter. ([#36](https://github.com/turbot/steampipe-plugin-prometheus/pull/36)) 34 | 35 | ## v0.6.0 [2023-12-12] 36 | 37 | _What's new?_ 38 | 39 | - The plugin can now be downloaded and used with the [Steampipe CLI](https://steampipe.io/docs), as a [Postgres FDW](https://steampipe.io/docs/steampipe_postgres/overview), as a [SQLite extension](https://steampipe.io/docs//steampipe_sqlite/overview) and as a standalone [exporter](https://steampipe.io/docs/steampipe_export/overview). ([#34](https://github.com/turbot/steampipe-plugin-prometheus/pull/34)) 40 | - The table docs have been updated to provide corresponding example queries for Postgres FDW and SQLite extension. ([#34](https://github.com/turbot/steampipe-plugin-prometheus/pull/34)) 41 | - Docs license updated to match Steampipe [CC BY-NC-ND license](https://github.com/turbot/steampipe-plugin-prometheus/blob/main/docs/LICENSE). ([#34](https://github.com/turbot/steampipe-plugin-prometheus/pull/34)) 42 | 43 | _Dependencies_ 44 | 45 | - Recompiled plugin with [steampipe-plugin-sdk v5.8.0](https://github.com/turbot/steampipe-plugin-sdk/blob/main/CHANGELOG.md#v580-2023-12-11) that includes plugin server encapsulation for in-process and GRPC usage, adding Steampipe Plugin SDK version to `_ctx` column, and fixing connection and potential divide-by-zero bugs. ([#33](https://github.com/turbot/steampipe-plugin-prometheus/pull/33)) 46 | 47 | ## v0.5.0 [2023-10-20] 48 | 49 | _What's new?_ 50 | 51 | - The Prometheus address (`address`) can now be set with the `PROMETHEUS_URL` environment variable. ([#23](https://github.com/turbot/steampipe-plugin-prometheus/pull/23)) (Thanks [@beudbeud](https://github.com/beudbeud) for the contribution!) 52 | 53 | ## v0.4.1 [2023-10-05] 54 | 55 | _Dependencies_ 56 | 57 | - Recompiled plugin with [steampipe-plugin-sdk v5.6.2](https://github.com/turbot/steampipe-plugin-sdk/blob/main/CHANGELOG.md#v562-2023-10-03) which prevents nil pointer reference errors for implicit hydrate configs. ([#24](https://github.com/turbot/steampipe-plugin-prometheus/pull/24)) 58 | 59 | ## v0.4.0 [2023-10-02] 60 | 61 | _Dependencies_ 62 | 63 | - Upgraded to [steampipe-plugin-sdk v5.6.1](https://github.com/turbot/steampipe-plugin-sdk/blob/main/CHANGELOG.md#v561-2023-09-29) with support for rate limiters. ([#19](https://github.com/turbot/steampipe-plugin-prometheus/pull/19)) 64 | - Recompiled plugin with Go version `1.21`. ([#19](https://github.com/turbot/steampipe-plugin-prometheus/pull/19)) 65 | 66 | ## v0.3.0 [2023-06-20] 67 | 68 | _Dependencies_ 69 | 70 | - Recompiled plugin with [steampipe-plugin-sdk v5.5.0](https://github.com/turbot/steampipe-plugin-sdk/blob/v5.5.0/CHANGELOG.md#v550-2023-06-16) which significantly reduces API calls and boosts query performance, resulting in faster data retrieval. This update significantly lowers the plugin initialization time of dynamic plugins by avoiding recursing into child folders when not necessary. ([#13](https://github.com/turbot/steampipe-plugin-prometheus/pull/13)) 71 | 72 | ## v0.2.0 [2023-03-22] 73 | 74 | _Dependencies_ 75 | 76 | - Recompiled plugin with [steampipe-plugin-sdk v5.3.0](https://github.com/turbot/steampipe-plugin-sdk/blob/main/CHANGELOG.md#v530-2023-03-16) which includes fixes for query cache pending item mechanism and aggregator connections not working for dynamic tables. ([#9](https://github.com/turbot/steampipe-plugin-prometheus/pull/9)) 77 | - Recompiled plugin with Go version `1.19`. ([#9](https://github.com/turbot/steampipe-plugin-prometheus/pull/9)) 78 | 79 | ## v0.1.0 [2022-05-25] 80 | 81 | _Enhancements_ 82 | 83 | - Added support for native Linux ARM and Mac M1 builds. ([#5](https://github.com/turbot/steampipe-plugin-prometheus/pull/5)) 84 | - Recompiled plugin with [steampipe-plugin-sdk v3.1.0](https://github.com/turbot/steampipe-plugin-sdk/blob/main/CHANGELOG.md#v310--2022-03-30) and Go version `1.18`. ([#4](https://github.com/turbot/steampipe-plugin-prometheus/pull/4)) 85 | 86 | ## v0.0.1 [2022-01-11] 87 | 88 | _What's new?_ 89 | 90 | - New tables added 91 | - [prometheus_alert](https://hub.steampipe.io/plugins/turbot/prometheus/tables/prometheus_alert) 92 | - [prometheus_label](https://hub.steampipe.io/plugins/turbot/prometheus/tables/prometheus_label) 93 | - [prometheus_metric](https://hub.steampipe.io/plugins/turbot/prometheus/tables/prometheus_metric) 94 | - [prometheus_rule](https://hub.steampipe.io/plugins/turbot/prometheus/tables/prometheus_rule) 95 | - [prometheus_rule_group](https://hub.steampipe.io/plugins/turbot/prometheus/tables/prometheus_rule_group) 96 | - [prometheus_series](https://hub.steampipe.io/plugins/turbot/prometheus/tables/prometheus_series) 97 | - [prometheus_target](https://hub.steampipe.io/plugins/turbot/prometheus/tables/prometheus_target) 98 | - [{metric_name}](https://hub.steampipe.io/plugins/turbot/prometheus/tables/{metric_name}) 99 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | STEAMPIPE_INSTALL_DIR ?= ~/.steampipe 2 | BUILD_TAGS = netgo 3 | install: 4 | go build -o $(STEAMPIPE_INSTALL_DIR)/plugins/hub.steampipe.io/plugins/turbot/prometheus@latest/steampipe-plugin-prometheus.plugin -tags "${BUILD_TAGS}" *.go 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![image](https://hub.steampipe.io/images/plugins/turbot/prometheus-social-graphic.png) 2 | 3 | # Prometheus Plugin for Steampipe 4 | 5 | Use SQL to query instances, domains and more from Prometheus. 6 | 7 | - **[Get started →](https://hub.steampipe.io/plugins/turbot/prometheus)** 8 | - Documentation: [Table definitions & examples](https://hub.steampipe.io/plugins/turbot/prometheus/tables) 9 | - Community: [Join #steampipe on Slack →](https://turbot.com/community/join) 10 | - Get involved: [Issues](https://github.com/turbot/steampipe-plugin-prometheus/issues) 11 | 12 | ## Quick start 13 | 14 | Install the plugin with [Steampipe](https://steampipe.io): 15 | 16 | ```shell 17 | steampipe plugin install prometheus 18 | ``` 19 | 20 | Configure the server address in `~/.steampipe/config/prometheus.spc`: 21 | 22 | ```hcl 23 | connection "prometheus" { 24 | plugin = "prometheus" 25 | address = "http://localhost:9090" 26 | } 27 | ``` 28 | 29 | Run steampipe: 30 | 31 | ```shell 32 | steampipe query 33 | ``` 34 | 35 | Query all the labels in your prometheus metrics: 36 | 37 | ```sql 38 | select 39 | name, 40 | values 41 | from 42 | prometheus_label 43 | ``` 44 | 45 | ``` 46 | > select name, values from prometheus_label 47 | +---------------+----------------------------------------------+ 48 | | name | values | 49 | +---------------+----------------------------------------------+ 50 | | alertname | ["TotalRequests"] | 51 | | alertstate | ["firing"] | 52 | | reason | ["refused","resolution","timeout","unknown"] | 53 | | interval | ["10s"] | 54 | | version | ["2.30.3","go1.17.1"] | 55 | | code | ["200","302","400","500","503"] | 56 | +---------------+----------------------------------------------+ 57 | ``` 58 | 59 | Query data for a given metric (tables are dynamically created): 60 | 61 | ```sql 62 | select 63 | code, 64 | handler, 65 | value 66 | from 67 | prometheus_http_requests_total 68 | ``` 69 | 70 | ``` 71 | +------+----------------------------+-------+ 72 | | code | handler | value | 73 | +------+----------------------------+-------+ 74 | | 302 | / | 1 | 75 | | 200 | /-/ready | 1 | 76 | | 200 | /api/v1/alerts | 1 | 77 | | 200 | /api/v1/label/:name/values | 421 | 78 | | 200 | /api/v1/labels | 16 | 79 | | 200 | /graph | 1 | 80 | | 200 | /static/*filepath | 4 | 81 | +------+----------------------------+-------+ 82 | ``` 83 | 84 | ## Engines 85 | 86 | This plugin is available for the following engines: 87 | 88 | | Engine | Description 89 | |---------------|------------------------------------------ 90 | | [Steampipe](https://steampipe.io/docs) | The Steampipe CLI exposes APIs and services as a high-performance relational database, giving you the ability to write SQL-based queries to explore dynamic data. Mods extend Steampipe's capabilities with dashboards, reports, and controls built with simple HCL. The Steampipe CLI is a turnkey solution that includes its own Postgres database, plugin management, and mod support. 91 | | [Postgres FDW](https://steampipe.io/docs/steampipe_postgres/overview) | Steampipe Postgres FDWs are native Postgres Foreign Data Wrappers that translate APIs to foreign tables. Unlike Steampipe CLI, which ships with its own Postgres server instance, the Steampipe Postgres FDWs can be installed in any supported Postgres database version. 92 | | [SQLite Extension](https://steampipe.io/docs//steampipe_sqlite/overview) | Steampipe SQLite Extensions provide SQLite virtual tables that translate your queries into API calls, transparently fetching information from your API or service as you request it. 93 | | [Export](https://steampipe.io/docs/steampipe_export/overview) | Steampipe Plugin Exporters provide a flexible mechanism for exporting information from cloud services and APIs. Each exporter is a stand-alone binary that allows you to extract data using Steampipe plugins without a database. 94 | | [Turbot Pipes](https://turbot.com/pipes/docs) | Turbot Pipes is the only intelligence, automation & security platform built specifically for DevOps. Pipes provide hosted Steampipe database instances, shared dashboards, snapshots, and more. 95 | 96 | ## Developing 97 | 98 | Prerequisites: 99 | 100 | - [Steampipe](https://steampipe.io/downloads) 101 | - [Golang](https://golang.org/doc/install) 102 | 103 | Clone: 104 | 105 | ```sh 106 | git clone https://github.com/turbot/steampipe-plugin-prometheus.git 107 | cd steampipe-plugin-prometheus 108 | ``` 109 | 110 | Build, which automatically installs the new version to your `~/.steampipe/plugins` directory: 111 | 112 | ``` 113 | make 114 | ``` 115 | 116 | Configure the plugin: 117 | 118 | ``` 119 | cp config/* ~/.steampipe/config 120 | vi ~/.steampipe/config/prometheus.spc 121 | ``` 122 | 123 | Try it! 124 | 125 | ``` 126 | steampipe query 127 | > .inspect prometheus 128 | ``` 129 | 130 | Further reading: 131 | 132 | - [Writing plugins](https://steampipe.io/docs/develop/writing-plugins) 133 | - [Writing your first table](https://steampipe.io/docs/develop/writing-your-first-table) 134 | 135 | ## Open Source & Contributing 136 | 137 | This repository is published under the [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0) (source code) and [CC BY-NC-ND](https://creativecommons.org/licenses/by-nc-nd/2.0/) (docs) licenses. Please see our [code of conduct](https://github.com/turbot/.github/blob/main/CODE_OF_CONDUCT.md). We look forward to collaborating with you! 138 | 139 | [Steampipe](https://steampipe.io) is a product produced from this open source software, exclusively by [Turbot HQ, Inc](https://turbot.com). It is distributed under our commercial terms. Others are allowed to make their own distribution of the software, but cannot use any of the Turbot trademarks, cloud services, etc. You can learn more in our [Open Source FAQ](https://turbot.com/open-source). 140 | 141 | ## Get Involved 142 | 143 | **[Join #steampipe on Slack →](https://turbot.com/community/join)** 144 | 145 | Want to help but don't know where to start? Pick up one of the `help wanted` issues: 146 | 147 | - [Steampipe](https://github.com/turbot/steampipe/labels/help%20wanted) 148 | - [Prometheus Plugin](https://github.com/turbot/steampipe-plugin-prometheus/labels/help%20wanted) 149 | -------------------------------------------------------------------------------- /config/prometheus.spc: -------------------------------------------------------------------------------- 1 | connection "prometheus" { 2 | plugin = "prometheus" 3 | 4 | # The address of your Prometheus 5 | # Can also be set with the PROMETHEUS_URL environment variable 6 | # address = "http://localhost:9090" 7 | 8 | # List of metrics that will be considered for dynamic table creation 9 | # Please refer to https://prometheus.io/docs/prometheus/latest/querying/basics 10 | # for information about supported expressions 11 | # For example: 12 | # - ".+" matches all metrics 13 | # - "prometheus_http_request.*" matches metrics starting with "prometheus_http_request" 14 | # - ".*error.*" matches metrics containing the word "error" 15 | # metrics = [".+"] 16 | } 17 | -------------------------------------------------------------------------------- /docs/LICENSE: -------------------------------------------------------------------------------- 1 | Attribution-NonCommercial-NoDerivatives 4.0 International 2 | 3 | ======================================================================= 4 | 5 | Creative Commons Corporation ("Creative Commons") is not a law firm and 6 | does not provide legal services or legal advice. Distribution of 7 | Creative Commons public licenses does not create a lawyer-client or 8 | other relationship. Creative Commons makes its licenses and related 9 | information available on an "as-is" basis. Creative Commons gives no 10 | warranties regarding its licenses, any material licensed under their 11 | terms and conditions, or any related information. Creative Commons 12 | disclaims all liability for damages resulting from their use to the 13 | fullest extent possible. 14 | 15 | Using Creative Commons Public Licenses 16 | 17 | Creative Commons public licenses provide a standard set of terms and 18 | conditions that creators and other rights holders may use to share 19 | original works of authorship and other material subject to copyright 20 | and certain other rights specified in the public license below. The 21 | following considerations are for informational purposes only, are not 22 | exhaustive, and do not form part of our licenses. 23 | 24 | Considerations for licensors: Our public licenses are 25 | intended for use by those authorized to give the public 26 | permission to use material in ways otherwise restricted by 27 | copyright and certain other rights. Our licenses are 28 | irrevocable. Licensors should read and understand the terms 29 | and conditions of the license they choose before applying it. 30 | Licensors should also secure all rights necessary before 31 | applying our licenses so that the public can reuse the 32 | material as expected. Licensors should clearly mark any 33 | material not subject to the license. This includes other CC- 34 | licensed material, or material used under an exception or 35 | limitation to copyright. More considerations for licensors: 36 | wiki.creativecommons.org/Considerations_for_licensors 37 | 38 | Considerations for the public: By using one of our public 39 | licenses, a licensor grants the public permission to use the 40 | licensed material under specified terms and conditions. If 41 | the licensor's permission is not necessary for any reason--for 42 | example, because of any applicable exception or limitation to 43 | copyright--then that use is not regulated by the license. Our 44 | licenses grant only permissions under copyright and certain 45 | other rights that a licensor has authority to grant. Use of 46 | the licensed material may still be restricted for other 47 | reasons, including because others have copyright or other 48 | rights in the material. A licensor may make special requests, 49 | such as asking that all changes be marked or described. 50 | Although not required by our licenses, you are encouraged to 51 | respect those requests where reasonable. More considerations 52 | for the public: 53 | wiki.creativecommons.org/Considerations_for_licensees 54 | 55 | ======================================================================= 56 | 57 | Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 58 | International Public License 59 | 60 | By exercising the Licensed Rights (defined below), You accept and agree 61 | to be bound by the terms and conditions of this Creative Commons 62 | Attribution-NonCommercial-NoDerivatives 4.0 International Public 63 | License ("Public License"). To the extent this Public License may be 64 | interpreted as a contract, You are granted the Licensed Rights in 65 | consideration of Your acceptance of these terms and conditions, and the 66 | Licensor grants You such rights in consideration of benefits the 67 | Licensor receives from making the Licensed Material available under 68 | these terms and conditions. 69 | 70 | 71 | Section 1 -- Definitions. 72 | 73 | a. Adapted Material means material subject to Copyright and Similar 74 | Rights that is derived from or based upon the Licensed Material 75 | and in which the Licensed Material is translated, altered, 76 | arranged, transformed, or otherwise modified in a manner requiring 77 | permission under the Copyright and Similar Rights held by the 78 | Licensor. For purposes of this Public License, where the Licensed 79 | Material is a musical work, performance, or sound recording, 80 | Adapted Material is always produced where the Licensed Material is 81 | synched in timed relation with a moving image. 82 | 83 | b. Copyright and Similar Rights means copyright and/or similar rights 84 | closely related to copyright including, without limitation, 85 | performance, broadcast, sound recording, and Sui Generis Database 86 | Rights, without regard to how the rights are labeled or 87 | categorized. For purposes of this Public License, the rights 88 | specified in Section 2(b)(1)-(2) are not Copyright and Similar 89 | Rights. 90 | 91 | c. Effective Technological Measures means those measures that, in the 92 | absence of proper authority, may not be circumvented under laws 93 | fulfilling obligations under Article 11 of the WIPO Copyright 94 | Treaty adopted on December 20, 1996, and/or similar international 95 | agreements. 96 | 97 | d. Exceptions and Limitations means fair use, fair dealing, and/or 98 | any other exception or limitation to Copyright and Similar Rights 99 | that applies to Your use of the Licensed Material. 100 | 101 | e. Licensed Material means the artistic or literary work, database, 102 | or other material to which the Licensor applied this Public 103 | License. 104 | 105 | f. Licensed Rights means the rights granted to You subject to the 106 | terms and conditions of this Public License, which are limited to 107 | all Copyright and Similar Rights that apply to Your use of the 108 | Licensed Material and that the Licensor has authority to license. 109 | 110 | g. Licensor means the individual(s) or entity(ies) granting rights 111 | under this Public License. 112 | 113 | h. NonCommercial means not primarily intended for or directed towards 114 | commercial advantage or monetary compensation. For purposes of 115 | this Public License, the exchange of the Licensed Material for 116 | other material subject to Copyright and Similar Rights by digital 117 | file-sharing or similar means is NonCommercial provided there is 118 | no payment of monetary compensation in connection with the 119 | exchange. 120 | 121 | i. Share means to provide material to the public by any means or 122 | process that requires permission under the Licensed Rights, such 123 | as reproduction, public display, public performance, distribution, 124 | dissemination, communication, or importation, and to make material 125 | available to the public including in ways that members of the 126 | public may access the material from a place and at a time 127 | individually chosen by them. 128 | 129 | j. Sui Generis Database Rights means rights other than copyright 130 | resulting from Directive 96/9/EC of the European Parliament and of 131 | the Council of 11 March 1996 on the legal protection of databases, 132 | as amended and/or succeeded, as well as other essentially 133 | equivalent rights anywhere in the world. 134 | 135 | k. You means the individual or entity exercising the Licensed Rights 136 | under this Public License. Your has a corresponding meaning. 137 | 138 | 139 | Section 2 -- Scope. 140 | 141 | a. License grant. 142 | 143 | 1. Subject to the terms and conditions of this Public License, 144 | the Licensor hereby grants You a worldwide, royalty-free, 145 | non-sublicensable, non-exclusive, irrevocable license to 146 | exercise the Licensed Rights in the Licensed Material to: 147 | 148 | a. reproduce and Share the Licensed Material, in whole or 149 | in part, for NonCommercial purposes only; and 150 | 151 | b. produce and reproduce, but not Share, Adapted Material 152 | for NonCommercial purposes only. 153 | 154 | 2. Exceptions and Limitations. For the avoidance of doubt, where 155 | Exceptions and Limitations apply to Your use, this Public 156 | License does not apply, and You do not need to comply with 157 | its terms and conditions. 158 | 159 | 3. Term. The term of this Public License is specified in Section 160 | 6(a). 161 | 162 | 4. Media and formats; technical modifications allowed. The 163 | Licensor authorizes You to exercise the Licensed Rights in 164 | all media and formats whether now known or hereafter created, 165 | and to make technical modifications necessary to do so. The 166 | Licensor waives and/or agrees not to assert any right or 167 | authority to forbid You from making technical modifications 168 | necessary to exercise the Licensed Rights, including 169 | technical modifications necessary to circumvent Effective 170 | Technological Measures. For purposes of this Public License, 171 | simply making modifications authorized by this Section 2(a) 172 | (4) never produces Adapted Material. 173 | 174 | 5. Downstream recipients. 175 | 176 | a. Offer from the Licensor -- Licensed Material. Every 177 | recipient of the Licensed Material automatically 178 | receives an offer from the Licensor to exercise the 179 | Licensed Rights under the terms and conditions of this 180 | Public License. 181 | 182 | b. No downstream restrictions. You may not offer or impose 183 | any additional or different terms or conditions on, or 184 | apply any Effective Technological Measures to, the 185 | Licensed Material if doing so restricts exercise of the 186 | Licensed Rights by any recipient of the Licensed 187 | Material. 188 | 189 | 6. No endorsement. Nothing in this Public License constitutes or 190 | may be construed as permission to assert or imply that You 191 | are, or that Your use of the Licensed Material is, connected 192 | with, or sponsored, endorsed, or granted official status by, 193 | the Licensor or others designated to receive attribution as 194 | provided in Section 3(a)(1)(A)(i). 195 | 196 | b. Other rights. 197 | 198 | 1. Moral rights, such as the right of integrity, are not 199 | licensed under this Public License, nor are publicity, 200 | privacy, and/or other similar personality rights; however, to 201 | the extent possible, the Licensor waives and/or agrees not to 202 | assert any such rights held by the Licensor to the limited 203 | extent necessary to allow You to exercise the Licensed 204 | Rights, but not otherwise. 205 | 206 | 2. Patent and trademark rights are not licensed under this 207 | Public License. 208 | 209 | 3. To the extent possible, the Licensor waives any right to 210 | collect royalties from You for the exercise of the Licensed 211 | Rights, whether directly or through a collecting society 212 | under any voluntary or waivable statutory or compulsory 213 | licensing scheme. In all other cases the Licensor expressly 214 | reserves any right to collect such royalties, including when 215 | the Licensed Material is used other than for NonCommercial 216 | purposes. 217 | 218 | 219 | Section 3 -- License Conditions. 220 | 221 | Your exercise of the Licensed Rights is expressly made subject to the 222 | following conditions. 223 | 224 | a. Attribution. 225 | 226 | 1. If You Share the Licensed Material, You must: 227 | 228 | a. retain the following if it is supplied by the Licensor 229 | with the Licensed Material: 230 | 231 | i. identification of the creator(s) of the Licensed 232 | Material and any others designated to receive 233 | attribution, in any reasonable manner requested by 234 | the Licensor (including by pseudonym if 235 | designated); 236 | 237 | ii. a copyright notice; 238 | 239 | iii. a notice that refers to this Public License; 240 | 241 | iv. a notice that refers to the disclaimer of 242 | warranties; 243 | 244 | v. a URI or hyperlink to the Licensed Material to the 245 | extent reasonably practicable; 246 | 247 | b. indicate if You modified the Licensed Material and 248 | retain an indication of any previous modifications; and 249 | 250 | c. indicate the Licensed Material is licensed under this 251 | Public License, and include the text of, or the URI or 252 | hyperlink to, this Public License. 253 | 254 | For the avoidance of doubt, You do not have permission under 255 | this Public License to Share Adapted Material. 256 | 257 | 2. You may satisfy the conditions in Section 3(a)(1) in any 258 | reasonable manner based on the medium, means, and context in 259 | which You Share the Licensed Material. For example, it may be 260 | reasonable to satisfy the conditions by providing a URI or 261 | hyperlink to a resource that includes the required 262 | information. 263 | 264 | 3. If requested by the Licensor, You must remove any of the 265 | information required by Section 3(a)(1)(A) to the extent 266 | reasonably practicable. 267 | 268 | 269 | Section 4 -- Sui Generis Database Rights. 270 | 271 | Where the Licensed Rights include Sui Generis Database Rights that 272 | apply to Your use of the Licensed Material: 273 | 274 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right 275 | to extract, reuse, reproduce, and Share all or a substantial 276 | portion of the contents of the database for NonCommercial purposes 277 | only and provided You do not Share Adapted Material; 278 | 279 | b. if You include all or a substantial portion of the database 280 | contents in a database in which You have Sui Generis Database 281 | Rights, then the database in which You have Sui Generis Database 282 | Rights (but not its individual contents) is Adapted Material; and 283 | 284 | c. You must comply with the conditions in Section 3(a) if You Share 285 | all or a substantial portion of the contents of the database. 286 | 287 | For the avoidance of doubt, this Section 4 supplements and does not 288 | replace Your obligations under this Public License where the Licensed 289 | Rights include other Copyright and Similar Rights. 290 | 291 | 292 | Section 5 -- Disclaimer of Warranties and Limitation of Liability. 293 | 294 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE 295 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS 296 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF 297 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, 298 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, 299 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR 300 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, 301 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT 302 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT 303 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. 304 | 305 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE 306 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, 307 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, 308 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, 309 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR 310 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN 311 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR 312 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR 313 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. 314 | 315 | c. The disclaimer of warranties and limitation of liability provided 316 | above shall be interpreted in a manner that, to the extent 317 | possible, most closely approximates an absolute disclaimer and 318 | waiver of all liability. 319 | 320 | 321 | Section 6 -- Term and Termination. 322 | 323 | a. This Public License applies for the term of the Copyright and 324 | Similar Rights licensed here. However, if You fail to comply with 325 | this Public License, then Your rights under this Public License 326 | terminate automatically. 327 | 328 | b. Where Your right to use the Licensed Material has terminated under 329 | Section 6(a), it reinstates: 330 | 331 | 1. automatically as of the date the violation is cured, provided 332 | it is cured within 30 days of Your discovery of the 333 | violation; or 334 | 335 | 2. upon express reinstatement by the Licensor. 336 | 337 | For the avoidance of doubt, this Section 6(b) does not affect any 338 | right the Licensor may have to seek remedies for Your violations 339 | of this Public License. 340 | 341 | c. For the avoidance of doubt, the Licensor may also offer the 342 | Licensed Material under separate terms or conditions or stop 343 | distributing the Licensed Material at any time; however, doing so 344 | will not terminate this Public License. 345 | 346 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public 347 | License. 348 | 349 | 350 | Section 7 -- Other Terms and Conditions. 351 | 352 | a. The Licensor shall not be bound by any additional or different 353 | terms or conditions communicated by You unless expressly agreed. 354 | 355 | b. Any arrangements, understandings, or agreements regarding the 356 | Licensed Material not stated herein are separate from and 357 | independent of the terms and conditions of this Public License. 358 | 359 | 360 | Section 8 -- Interpretation. 361 | 362 | a. For the avoidance of doubt, this Public License does not, and 363 | shall not be interpreted to, reduce, limit, restrict, or impose 364 | conditions on any use of the Licensed Material that could lawfully 365 | be made without permission under this Public License. 366 | 367 | b. To the extent possible, if any provision of this Public License is 368 | deemed unenforceable, it shall be automatically reformed to the 369 | minimum extent necessary to make it enforceable. If the provision 370 | cannot be reformed, it shall be severed from this Public License 371 | without affecting the enforceability of the remaining terms and 372 | conditions. 373 | 374 | c. No term or condition of this Public License will be waived and no 375 | failure to comply consented to unless expressly agreed to by the 376 | Licensor. 377 | 378 | d. Nothing in this Public License constitutes or may be interpreted 379 | as a limitation upon, or waiver of, any privileges and immunities 380 | that apply to the Licensor or You, including from the legal 381 | processes of any jurisdiction or authority. 382 | 383 | ======================================================================= 384 | 385 | Creative Commons is not a party to its public 386 | licenses. Notwithstanding, Creative Commons may elect to apply one of 387 | its public licenses to material it publishes and in those instances 388 | will be considered the “Licensor.” The text of the Creative Commons 389 | public licenses is dedicated to the public domain under the CC0 Public 390 | Domain Dedication. Except for the limited purpose of indicating that 391 | material is shared under a Creative Commons public license or as 392 | otherwise permitted by the Creative Commons policies published at 393 | creativecommons.org/policies, Creative Commons does not authorize the 394 | use of the trademark "Creative Commons" or any other trademark or logo 395 | of Creative Commons without its prior written consent including, 396 | without limitation, in connection with any unauthorized modifications 397 | to any of its public licenses or any other arrangements, 398 | understandings, or agreements concerning use of licensed material. For 399 | the avoidance of doubt, this paragraph does not form part of the 400 | public licenses. 401 | 402 | Creative Commons may be contacted at creativecommons.org. -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | organization: Turbot 3 | category: ["software development"] 4 | icon_url: "/images/plugins/turbot/prometheus.svg" 5 | brand_color: "#E6522C" 6 | display_name: "Prometheus" 7 | short_name: "prometheus" 8 | description: "Steampipe plugin to query metrics, labels, alerts and more from Prometheus." 9 | og_description: "Query Prometheus with SQL! Open source CLI. No DB required." 10 | og_image: "/images/plugins/turbot/prometheus-social-graphic.png" 11 | engines: ["steampipe", "sqlite", "postgres", "export"] 12 | --- 13 | 14 | # Prometheus + Steampipe 15 | 16 | [Prometheus](https://prometheus.io) is an open-source monitoring system with a dimensional data model, flexible query language, efficient time series database and modern alerting approach. 17 | 18 | [Steampipe](https://steampipe.io) is an open-source zero-ETL engine to instantly query cloud APIs using SQL. 19 | 20 | Query all the labels in your prometheus metrics: 21 | 22 | ```sql 23 | select 24 | name, 25 | values 26 | from 27 | prometheus_label 28 | ``` 29 | 30 | ``` 31 | > select name, values from prometheus_label 32 | +---------------+----------------------------------------------+ 33 | | name | values | 34 | +---------------+----------------------------------------------+ 35 | | alertname | ["TotalRequests"] | 36 | | alertstate | ["firing"] | 37 | | reason | ["refused","resolution","timeout","unknown"] | 38 | | interval | ["10s"] | 39 | | version | ["2.30.3","go1.17.1"] | 40 | | code | ["200","302","400","500","503"] | 41 | +---------------+----------------------------------------------+ 42 | ``` 43 | 44 | Query data for a given metric (tables are dynamically created): 45 | 46 | ```sql 47 | select 48 | code, 49 | handler, 50 | value 51 | from 52 | prometheus_http_requests_total 53 | ``` 54 | 55 | ``` 56 | +------+----------------------------+-------+ 57 | | code | handler | value | 58 | +------+----------------------------+-------+ 59 | | 302 | / | 1 | 60 | | 200 | /-/ready | 1 | 61 | | 200 | /api/v1/alerts | 1 | 62 | | 200 | /api/v1/label/:name/values | 421 | 63 | | 200 | /api/v1/labels | 16 | 64 | | 200 | /graph | 1 | 65 | | 200 | /static/*filepath | 4 | 66 | +------+----------------------------+-------+ 67 | ``` 68 | 69 | ## Documentation 70 | 71 | - **[Table definitions & examples →](/plugins/turbot/prometheus/tables)** 72 | 73 | ## Get started 74 | 75 | ### Install 76 | 77 | Download and install the latest Prometheus plugin: 78 | 79 | ```bash 80 | steampipe plugin install prometheus 81 | ``` 82 | 83 | ### Configuration 84 | 85 | Installing the latest prometheus plugin will create a config file (`~/.steampipe/config/prometheus.spc`) with a single connection named `prometheus`: 86 | 87 | ```hcl 88 | connection "prometheus" { 89 | plugin = "prometheus" 90 | 91 | 92 | # The address of your Prometheus. 93 | # Can also be set with the PROMETHEUS_URL environment variable 94 | address = "http://localhost:9090" 95 | metrics = ["prometheus_http_requests_.*", ".*error.*"] 96 | } 97 | ``` 98 | 99 | - `address` - HTTP address of your prometheus server. Can also be set with the PROMETHEUS_URL environment variable. 100 | - `metrics` - List of metric expressions to be matched against while creating dynamic metric tables. 101 | 102 | 103 | -------------------------------------------------------------------------------- /docs/tables/prometheus_alert.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Steampipe Table: prometheus_alert - Query Prometheus Alerts using SQL" 3 | description: "Allows users to query Alerts in Prometheus, specifically the active alerts and their details, providing insights into system performance and potential issues." 4 | --- 5 | 6 | # Table: prometheus_alert - Query Prometheus Alerts using SQL 7 | 8 | Prometheus is an open-source systems monitoring and alerting toolkit. It provides a multi-dimensional data model with time series data identified by metric name and key/value pairs. Alerts in Prometheus sends notifications to external systems when certain conditions are observed in the monitored system. 9 | 10 | ## Table Usage Guide 11 | 12 | The `prometheus_alert` table provides insights into active alerts within Prometheus. As a system administrator or DevOps engineer, explore alert-specific details through this table, including alert name, severity, and associated metadata. Utilize it to uncover information about active alerts, such as those with high severity, the conditions that triggered the alerts, and the duration for which the alerts have been active. 13 | 14 | ## Examples 15 | 16 | ### List all alerts 17 | Gain insights into all the alerts currently active in your system. This can be useful for monitoring system health and responding quickly to any issues. 18 | 19 | ```sql+postgres 20 | select 21 | * 22 | from 23 | prometheus_alert; 24 | ``` 25 | 26 | ```sql+sqlite 27 | select 28 | * 29 | from 30 | prometheus_alert; 31 | ``` 32 | 33 | ### Alerts with a labeled severity of high 34 | Identify instances where alerts have been marked with a high severity level. This can be useful in prioritizing responses to potential issues within your system. 35 | 36 | ```sql+postgres 37 | select 38 | * 39 | from 40 | prometheus_alert 41 | where 42 | labels ->> 'severity' = 'high'; 43 | ``` 44 | 45 | ```sql+sqlite 46 | select 47 | * 48 | from 49 | prometheus_alert 50 | where 51 | json_extract(labels, '$.severity') = 'high'; 52 | ``` 53 | 54 | ### Alerts that became active in the last 5 mins 55 | Discover the alerts that have been activated recently, allowing you to respond promptly to any potential issues or threats. This can be particularly beneficial in real-time monitoring and incident response scenarios. 56 | 57 | ```sql+postgres 58 | select 59 | * 60 | from 61 | prometheus_alert 62 | where 63 | active_at > now() - interval '5 min'; 64 | ``` 65 | 66 | ```sql+sqlite 67 | select 68 | * 69 | from 70 | prometheus_alert 71 | where 72 | active_at > datetime('now', '-5 minutes'); 73 | ``` -------------------------------------------------------------------------------- /docs/tables/prometheus_label.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Steampipe Table: prometheus_label - Query Prometheus Labels using SQL" 3 | description: "Allows users to query Labels in Prometheus, specifically the metadata attached to timeseries data, providing insights into the metrics data and its dimensions." 4 | --- 5 | 6 | # Table: prometheus_label - Query Prometheus Labels using SQL 7 | 8 | Prometheus Labels are a type of metadata attached to timeseries data in Prometheus, a widely used open-source monitoring and alerting toolkit. Labels enable the identification of the metrics data and its dimensions, such as instance, job, etc. They play a crucial role in data querying, visualization, and aggregation. 9 | 10 | ## Table Usage Guide 11 | 12 | The `prometheus_label` table provides insights into the labels used in Prometheus. As a DevOps engineer or a system administrator, you can explore label-specific details through this table, including the key-value pairs that identify the timeseries data. Utilize it to uncover information about the metrics data, such as the instance it belongs to, the job it is associated with, and other dimensions that help in effective data querying and visualization. 13 | 14 | ## Examples 15 | 16 | ### List all labels names 17 | Explore all existing labels in your Prometheus monitoring system to understand the various classifications and groupings within your data. This can help in organizing and managing your system more effectively. 18 | 19 | ```sql+postgres 20 | select 21 | name 22 | from 23 | prometheus_label; 24 | ``` 25 | 26 | ```sql+sqlite 27 | select 28 | name 29 | from 30 | prometheus_label; 31 | ``` 32 | 33 | ### List all labels with their values 34 | Explore which labels are associated with specific values in your Prometheus data. This can help you categorize and better understand your data for more effective management and analysis. 35 | 36 | ```sql+postgres 37 | select 38 | ln.name as name, 39 | value 40 | from 41 | prometheus_label as ln, 42 | jsonb_array_elements_text(ln.values) as value 43 | order by 44 | name, 45 | value; 46 | ``` 47 | 48 | ```sql+sqlite 49 | select 50 | ln.name as name, 51 | value 52 | from 53 | prometheus_label as ln, 54 | json_each(ln.values) as value 55 | order by 56 | name, 57 | value.value; 58 | ``` -------------------------------------------------------------------------------- /docs/tables/prometheus_metric.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Steampipe Table: prometheus_metric - Query Prometheus Metrics using SQL" 3 | description: "Allows users to query Metrics in Prometheus, specifically the numerical data about the system's state, providing insights into system performance and potential anomalies." 4 | --- 5 | 6 | # Table: prometheus_metric - Query Prometheus Metrics using SQL 7 | 8 | Prometheus is an open-source systems monitoring and alerting toolkit. It collects numerical data about the state of a system at any point in time. This data is stored as a series of metrics, which can be queried and visualized to gain insights into system performance. 9 | 10 | ## Table Usage Guide 11 | 12 | The `prometheus_metric` table provides insights into the numerical data about the state of a system in Prometheus. As a system administrator or DevOps engineer, explore metric-specific details through this table, including metric names, labels, and values. Utilize it to monitor system performance, identify potential issues, and make data-driven decisions about system improvements. 13 | 14 | **Important Notes** 15 | - A `query` must be provided in all queries to this table. 16 | 17 | ## Examples 18 | 19 | ### Get current values for a metric 20 | Explore the current values for a specific metric to monitor the performance and health of your system. This could be particularly useful in identifying potential issues or bottlenecks in your system's operation. 21 | 22 | ```sql+postgres 23 | select 24 | * 25 | from 26 | prometheus_metric 27 | where 28 | query = 'prometheus_http_requests_total'; 29 | ``` 30 | 31 | ```sql+sqlite 32 | select 33 | * 34 | from 35 | prometheus_metric 36 | where 37 | query = 'prometheus_http_requests_total'; 38 | ``` 39 | 40 | ### Get current values for a metric with specific labels 41 | Explore the current values of a specific metric by identifying its unique labels. This can be beneficial in monitoring and analyzing the performance of your system based on certain parameters. 42 | 43 | ```sql+postgres 44 | select 45 | * 46 | from 47 | prometheus_metric 48 | where 49 | query = 'prometheus_http_requests_total{handler="/metrics"}'; 50 | ``` 51 | 52 | ```sql+sqlite 53 | select 54 | * 55 | from 56 | prometheus_metric 57 | where 58 | query = 'prometheus_http_requests_total{handler="/metrics"}'; 59 | ``` 60 | 61 | ### Get values from 24 hrs ago for a metric 62 | Analyze the metrics to understand the changes in HTTP requests over the past 24 hours. This is particularly useful for monitoring server performance and identifying potential issues or anomalies. 63 | 64 | ```sql+postgres 65 | select 66 | * 67 | from 68 | prometheus_metric 69 | where 70 | query = 'prometheus_http_requests_total' 71 | and timestamp = now() - interval '24 hrs'; 72 | ``` 73 | 74 | ```sql+sqlite 75 | select 76 | * 77 | from 78 | prometheus_metric 79 | where 80 | query = 'prometheus_http_requests_total' 81 | and timestamp = datetime('now', '-24 hours'); 82 | ``` 83 | 84 | ### Get metric values every 5 mins for the last hour 85 | Analyze the frequency of HTTP requests in the last hour, by obtaining metrics at 5-minute intervals. This can help monitor web traffic patterns and identify potential surges or dips in usage. 86 | 87 | ```sql+postgres 88 | select 89 | * 90 | from 91 | prometheus_metric 92 | where 93 | query = 'prometheus_http_requests_total' 94 | and timestamp > now() - interval '1 hrs' 95 | and step_seconds = 300 96 | order by 97 | timestamp; 98 | ``` 99 | 100 | ```sql+sqlite 101 | select 102 | * 103 | from 104 | prometheus_metric 105 | where 106 | query = 'prometheus_http_requests_total' 107 | and timestamp > datetime('now', '-1 hours') 108 | and step_seconds = 300 109 | order by 110 | timestamp; 111 | ``` -------------------------------------------------------------------------------- /docs/tables/prometheus_rule.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Steampipe Table: prometheus_rule - Query Prometheus Rules using SQL" 3 | description: "Allows users to query Prometheus Rules, specifically the rule configurations, providing insights into monitoring and alerting rules set in the Prometheus service." 4 | --- 5 | 6 | # Table: prometheus_rule - Query Prometheus Rules using SQL 7 | 8 | Prometheus is an open-source systems monitoring and alerting toolkit. It collects metrics from configured targets at given intervals, evaluates rule expressions, displays the results, and can trigger alerts if some condition is observed to be true. Prometheus's main features are a multi-dimensional data model with time series data identified by metric name and key/value pairs, a flexible query language to leverage this dimensionality, and no reliance on distributed storage. 9 | 10 | ## Table Usage Guide 11 | 12 | The `prometheus_rule` table provides insights into rules within the Prometheus service. As a DevOps engineer, explore rule-specific details through this table, including rule configurations, alerting rules, and associated metadata. Utilize it to uncover information about rules, such as those triggering certain alerts, the conditions set for each rule, and the verification of rule expressions. 13 | 14 | ## Examples 15 | 16 | ### List all rules 17 | Explore all the rules in your Prometheus setup, organized by group name and rule number. This can help you understand the structure and hierarchy of your rules for better management and troubleshooting. 18 | 19 | ```sql+postgres 20 | select 21 | * 22 | from 23 | prometheus_rule 24 | order by 25 | group_name, 26 | group_rule_num; 27 | ``` 28 | 29 | ```sql+sqlite 30 | select 31 | * 32 | from 33 | prometheus_rule 34 | order by 35 | group_name, 36 | group_rule_num; 37 | ``` 38 | 39 | ### Rules with a labeled severity of high 40 | This query is used to identify any rules within the Prometheus system that have been marked with a high severity label. This could be useful in prioritizing responses to system alerts or issues, by focusing on the most critical rules first. 41 | 42 | ```sql+postgres 43 | select 44 | name, 45 | labels, 46 | state 47 | from 48 | prometheus_rule 49 | where 50 | labels ->> 'severity' = 'high'; 51 | ``` 52 | 53 | ```sql+sqlite 54 | select 55 | name, 56 | labels, 57 | state 58 | from 59 | prometheus_rule 60 | where 61 | json_extract(labels, '$.severity') = 'high'; 62 | ``` 63 | 64 | ### Rules that are firing 65 | Explore which rules are currently active or 'firing' within your Prometheus monitoring system. This can aid in identifying potential issues or anomalies in your network or system that require immediate attention. 66 | 67 | ```sql+postgres 68 | select 69 | name, 70 | labels, 71 | state 72 | from 73 | prometheus_rule 74 | where 75 | state = 'firing'; 76 | ``` 77 | 78 | ```sql+sqlite 79 | select 80 | name, 81 | labels, 82 | state 83 | from 84 | prometheus_rule 85 | where 86 | state = 'firing'; 87 | ``` 88 | 89 | ### Slow running rules with evaluation time > 1 sec 90 | Analyze your system's performance by pinpointing rules that are running slowly, taking more than one second for evaluation. This can help you identify potential bottlenecks or areas for optimization to improve overall system efficiency. 91 | 92 | ```sql+postgres 93 | select 94 | name, 95 | labels, 96 | state 97 | from 98 | prometheus_rule 99 | where 100 | evaluation_time > 1; 101 | ``` 102 | 103 | ```sql+sqlite 104 | select 105 | name, 106 | labels, 107 | state 108 | from 109 | prometheus_rule 110 | where 111 | evaluation_time > 1; 112 | ``` -------------------------------------------------------------------------------- /docs/tables/prometheus_rule_group.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Steampipe Table: prometheus_rule_group - Query Prometheus Rule Groups using SQL" 3 | description: "Allows users to query Prometheus Rule Groups, specifically the configuration and status of each rule group, providing insights into monitoring and alerting rules." 4 | --- 5 | 6 | # Table: prometheus_rule_group - Query Prometheus Rule Groups using SQL 7 | 8 | Prometheus Rule Groups are a set of Prometheus rules that are evaluated together in a specific order. These rules can be either recording rules, which precompute frequently needed or computationally expensive expressions and save their result as a new set of time series, or alerting rules, which trigger alerts when certain conditions are observed to be true. Rule Groups are part of Prometheus' configuration and are used to define a list of alerts and recording rules. 9 | 10 | ## Table Usage Guide 11 | 12 | The `prometheus_rule_group` table provides insights into Rule Groups within Prometheus. As a DevOps engineer, explore group-specific details through this table, including the rules, their configuration, and their current status. Utilize it to monitor and manage your Prometheus rules, understand the current state of your alerting and recording rules, and quickly identify any potential issues. 13 | 14 | ## Examples 15 | 16 | ### List all rule groups 17 | Explore all the rule groups in your Prometheus monitoring system to understand how your rules are organized and to identify potential areas for optimization or reconfiguration. This can be especially beneficial for large-scale systems where efficient rule management is crucial. 18 | 19 | ```sql+postgres 20 | select 21 | * 22 | from 23 | prometheus_rule_group; 24 | ``` 25 | 26 | ```sql+sqlite 27 | select 28 | * 29 | from 30 | prometheus_rule_group; 31 | ``` -------------------------------------------------------------------------------- /docs/tables/prometheus_series.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Steampipe Table: prometheus_series - Query Prometheus Time Series Data using SQL" 3 | description: "Allows users to query Time Series Data in Prometheus, specifically the series of data points indexed by time, providing insights into system metrics over a period of time." 4 | --- 5 | 6 | # Table: prometheus_series - Query Prometheus Time Series Data using SQL 7 | 8 | Prometheus is an open-source system monitoring and alerting toolkit that collects and stores its metrics as time series data, i.e., metrics information that is output at a regular interval. This includes various system metrics such as CPU usage, memory utilization, disk I/O, network traffic, etc. It provides a multidimensional data model with time series data identified by metric name and key-value pairs. 9 | 10 | ## Table Usage Guide 11 | 12 | The `prometheus_series` table provides insights into time series data within Prometheus. As a System Administrator or DevOps engineer, you can explore specific metrics details through this table, including the metric name, labels, and timestamp. Utilize it to uncover information about system performance over time, identify trends, and aid in capacity planning. 13 | 14 | ## Examples 15 | 16 | ### List all current prometheus_http_requests_total series 17 | Explore the current series of Prometheus HTTP requests to understand the volume and nature of web traffic. This can be useful in monitoring server performance and identifying potential issues or areas for optimization. 18 | 19 | ```sql+postgres 20 | select 21 | * 22 | from 23 | prometheus_series 24 | where 25 | query = 'prometheus_http_requests_total'; 26 | ``` 27 | 28 | ```sql+sqlite 29 | select 30 | * 31 | from 32 | prometheus_series 33 | where 34 | query = 'prometheus_http_requests_total'; 35 | ``` 36 | 37 | ### List all prometheus_http_requests_total series present 24 hours ago 38 | Explore the total number of HTTP requests made to your Prometheus server 24 hours ago. This can help in identifying any unusual spikes or drops in traffic, assisting in network monitoring and troubleshooting. 39 | 40 | ```sql+postgres 41 | select 42 | * 43 | from 44 | prometheus_series 45 | where 46 | query = 'prometheus_http_requests_total' 47 | and timestamp = now() - interval '24 hrs'; 48 | ``` 49 | 50 | ```sql+sqlite 51 | select 52 | * 53 | from 54 | prometheus_series 55 | where 56 | query = 'prometheus_http_requests_total' 57 | and timestamp = datetime('now', '-24 hours'); 58 | ``` 59 | 60 | ### List all prometheus_http_requests_total series for /metrics present 24 hours ago 61 | Analyze the settings to understand the total number of HTTP requests made to the '/metrics' handler in Prometheus, exactly 24 hours ago. This can help in monitoring traffic patterns and identifying possible issues or anomalies in request volume. 62 | 63 | ```sql+postgres 64 | select 65 | * 66 | from 67 | prometheus_series 68 | where 69 | query = 'prometheus_http_requests_total{handler="/metrics"}' 70 | and timestamp = now() - interval '24 hrs'; 71 | ``` 72 | 73 | ```sql+sqlite 74 | select 75 | * 76 | from 77 | prometheus_series 78 | where 79 | query = 'prometheus_http_requests_total{handler="/metrics"}' 80 | and timestamp = datetime('now','-24 hours'); 81 | ``` 82 | 83 | ### List all prometheus_http_requests_total series on 31st Oct 2021 84 | Explore the total number of HTTP requests recorded by Prometheus on October 31st, 2021. This can help in analyzing the web traffic patterns and server load on that specific day. 85 | 86 | ```sql+postgres 87 | select 88 | * 89 | from 90 | prometheus_series 91 | where 92 | query = 'prometheus_http_requests_total' 93 | and timestamp > '2021-10-31' 94 | and timestamp < '2021-11-01'; 95 | ``` 96 | 97 | ```sql+sqlite 98 | select 99 | * 100 | from 101 | prometheus_series 102 | where 103 | query = 'prometheus_http_requests_total' 104 | and timestamp > '2021-10-31' 105 | and timestamp < '2021-11-01'; 106 | ``` -------------------------------------------------------------------------------- /docs/tables/prometheus_target.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Steampipe Table: prometheus_target - Query Prometheus Targets using SQL" 3 | description: "Allows users to query Prometheus Targets, specifically the targets' state, health, and scrape pool. This provides insights into the target's operational status and performance." 4 | --- 5 | 6 | # Table: prometheus_target - Query Prometheus Targets using SQL 7 | 8 | Prometheus Target is a resource within Prometheus that represents an individual node or endpoint that Prometheus instances are scraping. It provides a detailed view of the target's state, health, and scrape pool, which can be used to monitor the operational status and performance of the target. By querying Prometheus Targets, users can gain insights into the metrics being scraped from each target and the health of the scraping process. 9 | 10 | ## Table Usage Guide 11 | 12 | The `prometheus_target` table provides insights into individual nodes or endpoints within Prometheus. As a system administrator or a DevOps engineer, this table can be used to explore target-specific details, including its state, health, and scrape pool. Utilize it to uncover information about the performance of each target, the success or failure of the scraping process, and the metrics being collected from each target. 13 | 14 | ## Examples 15 | 16 | ### List all targets 17 | Discover all the monitoring targets in your Prometheus setup, helping you gain a comprehensive view of what metrics are being tracked across your systems. This can be particularly useful for auditing or troubleshooting purposes. 18 | 19 | ```sql+postgres 20 | select 21 | * 22 | from 23 | prometheus_target; 24 | ``` 25 | 26 | ```sql+sqlite 27 | select 28 | * 29 | from 30 | prometheus_target; 31 | ``` 32 | 33 | ### Targets that are not up 34 | Explore which targets in your Prometheus monitoring system are not currently operational. This can help you quickly identify and address any potential issues, improving system performance and reliability. 35 | 36 | ```sql+postgres 37 | select 38 | scrape_pool, 39 | scrape_url, 40 | health, 41 | last_scrape, 42 | last_error 43 | from 44 | prometheus_target 45 | where 46 | health != 'up'; 47 | ``` 48 | 49 | ```sql+sqlite 50 | select 51 | scrape_pool, 52 | scrape_url, 53 | health, 54 | last_scrape, 55 | last_error 56 | from 57 | prometheus_target 58 | where 59 | health != 'up'; 60 | ``` 61 | 62 | ### Targets whose last scrape was more than 24 hrs ago 63 | Identify instances where targets haven't been scanned in the last 24 hours. This is useful for maintaining up-to-date data and ensuring the health of your system. 64 | 65 | ```sql+postgres 66 | select 67 | scrape_pool, 68 | scrape_url, 69 | health, 70 | last_scrape, 71 | last_error 72 | from 73 | prometheus_target 74 | where 75 | last_scrape < now() - interval '24 hrs'; 76 | ``` 77 | 78 | ```sql+sqlite 79 | select 80 | scrape_pool, 81 | scrape_url, 82 | health, 83 | last_scrape, 84 | last_error 85 | from 86 | prometheus_target 87 | where 88 | datetime(last_scrape) < datetime('now', '-24 hours'); 89 | ``` -------------------------------------------------------------------------------- /docs/tables/{metric_name}.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Steampipe Table: {metric_name} - Query Prometheus metrics using SQL" 3 | description: "Allows users to query Proemtheus metrics, providing insights into specific metrics and potential anomalies." 4 | --- 5 | 6 | # Table: {metric_name} - Query Prometheus metrics using SQL 7 | 8 | Query data from the metric called `{metric_name}`. A table is automatically created to represent each metric. 9 | 10 | For instance, given the metric: 11 | 12 | ``` 13 | { 14 | "__name__": "prometheus_http_requests_total", 15 | "code": "302", 16 | "handler": "/", 17 | "instance": "localhost:9090", 18 | "job":"prometheus" 19 | } 20 | ``` 21 | 22 | And the connection configuration: 23 | ```hcl 24 | connection "prometheus" { 25 | plugin = "prometheus" 26 | address = "http://localhost:9090" 27 | metrics = ["prometheus_http_requests_total"] 28 | } 29 | ``` 30 | 31 | This plugin will automatically create a table called `prometheus_http_requests_total`: 32 | ``` 33 | > select * from prometheus_http_requests_total; 34 | +----------------------+-------+------+----------------------------+----------------+------------+--------------+ 35 | | timestamp | value | code | handler | instance | job | step_seconds | 36 | +----------------------+-------+------+----------------------------+----------------+------------+--------------+ 37 | | 2021-11-06T02:05:41Z | 308 | 200 | /api/v1/label/:name/values | localhost:9090 | prometheus | 60 | 38 | | 2021-11-06T01:43:41Z | 1 | 200 | /-/ready | localhost:9090 | prometheus | 60 | 39 | | 2021-11-06T01:56:41Z | 12 | 200 | /api/v1/labels | localhost:9090 | prometheus | 60 | 40 | +----------------------+-------+------+----------------------------+----------------+------------+--------------+ 41 | ``` 42 | 43 | Regular expressions can also be used to match metric names. For instance, if 44 | you want to create tables for all metrics starting with 45 | `prometheus_target_`, use the following configuration: 46 | 47 | ```hcl 48 | connection "prometheus" { 49 | plugin = "prometheus" 50 | address = "http://localhost:9090" 51 | metrics = ["prometheus_target_.*"] 52 | } 53 | ``` 54 | 55 | If you want to create tables for all metrics, use: 56 | 57 | ```hcl 58 | connection "prometheus" { 59 | plugin = "prometheus" 60 | address = "http://localhost:9090" 61 | metrics = [".+"] 62 | } 63 | ``` 64 | 65 | However, please note that this could be slow depending on how many metrics are 66 | in your environment. 67 | 68 | ## Examples 69 | 70 | ### Inspect the table structure 71 | 72 | Assuming your connection configuration is: 73 | ```hcl 74 | connection "prometheus" { 75 | plugin = "prometheus" 76 | address = "http://localhost:9090" 77 | metrics = ["prometheus_http_requests_total"] 78 | } 79 | ``` 80 | 81 | List all tables with: 82 | 83 | ```sql 84 | .inspect prometheus 85 | +--------------------------------+---------------------------------------------------+ 86 | | table | description | 87 | +--------------------------------+---------------------------------------------------+ 88 | | prometheus_alert | Alerts currently firing on the Prometheus server. | 89 | | ... | ... | 90 | | prometheus_http_requests_total | Metrics for prometheus_http_requests_total. | 91 | | ... | ... | 92 | +--------------------------------+---------------------------------------------------+ 93 | ``` 94 | 95 | To get details of a specific metric table, inspect it by name: 96 | 97 | ```sql 98 | > .inspect prometheus.prometheus_http_requests_total 99 | +--------------+--------------------------+----------------------------------------------------------------+ 100 | | column | type | description | 101 | +--------------+--------------------------+----------------------------------------------------------------+ 102 | | code | text | The code label. | 103 | | handler | text | The handler label. | 104 | | instance | text | The instance label. | 105 | | job | text | The job label. | 106 | | step_seconds | bigint | Interval in seconds between metric values. Default 60 seconds. | 107 | | timestamp | timestamp with time zone | Timestamp of the value. | 108 | | value | double precision | Value of the metric. | 109 | +--------------+--------------------------+----------------------------------------------------------------+ 110 | ``` 111 | 112 | ### Get current values for prometheus_http_requests_total 113 | 114 | ```sql+postgres 115 | select 116 | * 117 | from 118 | prometheus_http_requests_total; 119 | ``` 120 | 121 | ```sql+sqlite 122 | select 123 | * 124 | from 125 | prometheus_http_requests_total; 126 | ``` 127 | 128 | ### Get current values for a metric with specific labels 129 | 130 | ```sql+postgres 131 | select 132 | * 133 | from 134 | prometheus_http_requests_total 135 | where 136 | handler = '/metrics'; 137 | ``` 138 | 139 | ```sql+sqlite 140 | select 141 | * 142 | from 143 | prometheus_http_requests_total 144 | where 145 | handler = '/metrics'; 146 | ``` 147 | 148 | ### Get values from 24 hrs ago for a metric 149 | 150 | ```sql+postgres 151 | select 152 | timestamp, 153 | code, 154 | handler, 155 | value 156 | from 157 | prometheus_http_requests_total 158 | where 159 | timestamp = now() - interval '24 hrs'; 160 | ``` 161 | 162 | ```sql+sqlite 163 | select 164 | timestamp, 165 | code, 166 | handler, 167 | value 168 | from 169 | prometheus_http_requests_total 170 | where 171 | timestamp = datetime('now','-24 hours'); 172 | ``` 173 | 174 | ### Get metric values every 5 mins for the last hour 175 | 176 | ```sql+postgres 177 | select 178 | timestamp, 179 | code, 180 | handler, 181 | value 182 | from 183 | prometheus_http_requests_total 184 | where 185 | timestamp > now() - interval '1 hrs' 186 | and step_seconds = 300 187 | order by 188 | timestamp; 189 | ``` 190 | 191 | ```sql+sqlite 192 | select 193 | timestamp, 194 | code, 195 | handler, 196 | value 197 | from 198 | prometheus_http_requests_total 199 | where 200 | timestamp > datetime('now','-1 hours') 201 | and step_seconds = 300 202 | order by 203 | timestamp; 204 | ``` -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/turbot/steampipe-plugin-prometheus 2 | 3 | go 1.23.1 4 | 5 | toolchain go1.23.2 6 | 7 | require ( 8 | github.com/prometheus/client_golang v1.14.0 9 | github.com/prometheus/common v0.37.0 10 | github.com/turbot/steampipe-plugin-sdk/v5 v5.11.5 11 | ) 12 | 13 | require ( 14 | cloud.google.com/go v0.112.1 // indirect 15 | cloud.google.com/go/compute/metadata v0.3.0 // indirect 16 | cloud.google.com/go/iam v1.1.6 // indirect 17 | cloud.google.com/go/storage v1.38.0 // indirect 18 | github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect 19 | github.com/agext/levenshtein v1.2.3 // indirect 20 | github.com/allegro/bigcache/v3 v3.1.0 // indirect 21 | github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect 22 | github.com/aws/aws-sdk-go v1.44.183 // indirect 23 | github.com/beorn7/perks v1.0.1 // indirect 24 | github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect 25 | github.com/btubbs/datetime v0.1.1 // indirect 26 | github.com/cenkalti/backoff/v4 v4.3.0 // indirect 27 | github.com/cespare/xxhash/v2 v2.3.0 // indirect 28 | github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect 29 | github.com/dgraph-io/ristretto v0.2.0 // indirect 30 | github.com/dustin/go-humanize v1.0.1 // indirect 31 | github.com/eko/gocache/lib/v4 v4.1.6 // indirect 32 | github.com/eko/gocache/store/bigcache/v4 v4.2.1 // indirect 33 | github.com/eko/gocache/store/ristretto/v4 v4.2.1 // indirect 34 | github.com/fatih/color v1.17.0 // indirect 35 | github.com/felixge/httpsnoop v1.0.4 // indirect 36 | github.com/fsnotify/fsnotify v1.7.0 // indirect 37 | github.com/gertd/go-pluralize v0.2.1 // indirect 38 | github.com/ghodss/yaml v1.0.0 // indirect 39 | github.com/go-logr/logr v1.4.1 // indirect 40 | github.com/go-logr/stdr v1.2.2 // indirect 41 | github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect 42 | github.com/golang/mock v1.6.0 // indirect 43 | github.com/golang/protobuf v1.5.4 // indirect 44 | github.com/google/go-cmp v0.6.0 // indirect 45 | github.com/google/s2a-go v0.1.7 // indirect 46 | github.com/google/uuid v1.6.0 // indirect 47 | github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect 48 | github.com/googleapis/gax-go/v2 v2.12.3 // indirect 49 | github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect 50 | github.com/hashicorp/go-cleanhttp v0.5.2 // indirect 51 | github.com/hashicorp/go-getter v1.7.5 // indirect 52 | github.com/hashicorp/go-hclog v1.6.3 // indirect 53 | github.com/hashicorp/go-plugin v1.6.1 // indirect 54 | github.com/hashicorp/go-safetemp v1.0.0 // indirect 55 | github.com/hashicorp/go-version v1.7.0 // indirect 56 | github.com/hashicorp/hcl/v2 v2.20.1 // indirect 57 | github.com/hashicorp/yamux v0.1.1 // indirect 58 | github.com/iancoleman/strcase v0.3.0 // indirect 59 | github.com/jmespath/go-jmespath v0.4.0 // indirect 60 | github.com/json-iterator/go v1.1.12 // indirect 61 | github.com/klauspost/compress v1.17.2 // indirect 62 | github.com/mattn/go-colorable v0.1.13 // indirect 63 | github.com/mattn/go-isatty v0.0.20 // indirect 64 | github.com/mattn/go-runewidth v0.0.15 // indirect 65 | github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect 66 | github.com/mitchellh/go-homedir v1.1.0 // indirect 67 | github.com/mitchellh/go-testing-interface v1.14.1 // indirect 68 | github.com/mitchellh/go-wordwrap v1.0.0 // indirect 69 | github.com/mitchellh/mapstructure v1.5.0 // indirect 70 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect 71 | github.com/modern-go/reflect2 v1.0.2 // indirect 72 | github.com/oklog/run v1.0.0 // indirect 73 | github.com/olekukonko/tablewriter v0.0.5 // indirect 74 | github.com/pkg/errors v0.9.1 // indirect 75 | github.com/prometheus/client_model v0.3.0 // indirect 76 | github.com/prometheus/procfs v0.8.0 // indirect 77 | github.com/rivo/uniseg v0.2.0 // indirect 78 | github.com/sethvargo/go-retry v0.2.4 // indirect 79 | github.com/stevenle/topsort v0.2.0 // indirect 80 | github.com/tkrajina/go-reflector v0.5.6 // indirect 81 | github.com/turbot/go-kit v1.1.0 // indirect 82 | github.com/ulikunitz/xz v0.5.10 // indirect 83 | github.com/zclconf/go-cty v1.14.4 // indirect 84 | go.opencensus.io v0.24.0 // indirect 85 | go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect 86 | go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect 87 | go.opentelemetry.io/otel v1.26.0 // indirect 88 | go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.26.0 // indirect 89 | go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 // indirect 90 | go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.24.0 // indirect 91 | go.opentelemetry.io/otel/metric v1.26.0 // indirect 92 | go.opentelemetry.io/otel/sdk v1.26.0 // indirect 93 | go.opentelemetry.io/otel/sdk/metric v1.26.0 // indirect 94 | go.opentelemetry.io/otel/trace v1.26.0 // indirect 95 | go.opentelemetry.io/proto/otlp v1.2.0 // indirect 96 | golang.org/x/crypto v0.35.0 // indirect 97 | golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect 98 | golang.org/x/mod v0.19.0 // indirect 99 | golang.org/x/net v0.36.0 // indirect 100 | golang.org/x/oauth2 v0.21.0 // indirect 101 | golang.org/x/sync v0.11.0 // indirect 102 | golang.org/x/sys v0.30.0 // indirect 103 | golang.org/x/text v0.22.0 // indirect 104 | golang.org/x/time v0.5.0 // indirect 105 | golang.org/x/tools v0.23.0 // indirect 106 | google.golang.org/api v0.171.0 // indirect 107 | google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect 108 | google.golang.org/genproto/googleapis/api v0.0.0-20240604185151-ef581f913117 // indirect 109 | google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect 110 | google.golang.org/grpc v1.66.0 // indirect 111 | google.golang.org/protobuf v1.34.2 // indirect 112 | gopkg.in/yaml.v2 v2.4.0 // indirect 113 | ) 114 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/turbot/steampipe-plugin-prometheus/prometheus" 5 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin" 6 | ) 7 | 8 | func main() { 9 | plugin.Serve(&plugin.ServeOpts{PluginFunc: prometheus.Plugin}) 10 | } 11 | -------------------------------------------------------------------------------- /prometheus/connection_config.go: -------------------------------------------------------------------------------- 1 | package prometheus 2 | 3 | import ( 4 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin" 5 | ) 6 | 7 | type prometheusConfig struct { 8 | Address *string `hcl:"address"` 9 | Metrics []string `hcl:"metrics,optional"` 10 | } 11 | 12 | func ConfigInstance() interface{} { 13 | return &prometheusConfig{} 14 | } 15 | 16 | // GetConfig :: retrieve and cast connection config from query data 17 | func GetConfig(connection *plugin.Connection) prometheusConfig { 18 | if connection == nil || connection.Config == nil { 19 | return prometheusConfig{} 20 | } 21 | config, _ := connection.Config.(prometheusConfig) 22 | return config 23 | } 24 | -------------------------------------------------------------------------------- /prometheus/plugin.go: -------------------------------------------------------------------------------- 1 | package prometheus 2 | 3 | import ( 4 | "context" 5 | "path/filepath" 6 | "strings" 7 | "time" 8 | 9 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin" 10 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform" 11 | ) 12 | 13 | type ctxKey string 14 | 15 | func Plugin(ctx context.Context) *plugin.Plugin { 16 | p := &plugin.Plugin{ 17 | Name: "steampipe-plugin-prometheus", 18 | ConnectionConfigSchema: &plugin.ConnectionConfigSchema{ 19 | NewInstance: ConfigInstance, 20 | }, 21 | DefaultTransform: transform.FromGo(), 22 | SchemaMode: plugin.SchemaModeDynamic, 23 | TableMapFunc: pluginTableDefinitions, 24 | } 25 | return p 26 | } 27 | 28 | func pluginTableDefinitions(ctx context.Context, p *plugin.TableMapData) (map[string]*plugin.Table, error) { 29 | 30 | // Initialize tables 31 | tables := map[string]*plugin.Table{ 32 | "prometheus_alert": tablePrometheusAlert(ctx), 33 | "prometheus_label": tablePrometheusLabel(ctx), 34 | "prometheus_metric": tablePrometheusMetric(ctx), 35 | "prometheus_rule": tablePrometheusRule(ctx), 36 | "prometheus_rule_group": tablePrometheusRuleGroup(ctx), 37 | "prometheus_series": tablePrometheusSeries(ctx), 38 | "prometheus_target": tablePrometheusTarget(ctx), 39 | } 40 | 41 | // Get list of metrics to create tables for from config 42 | prometheusConfig := GetConfig(p.Connection) 43 | if prometheusConfig.Metrics == nil { 44 | return tables, nil 45 | } 46 | 47 | // Search for metrics to create as tables 48 | metricNames, err := metricNameList(ctx, p) 49 | 50 | if err != nil { 51 | // Return only the static tables when encountering an error 52 | return tables, err 53 | } 54 | 55 | for _, i := range metricNames { 56 | tableCtx := context.WithValue(ctx, ctxKey("metric_name"), i) 57 | base := filepath.Base(i) 58 | tableName := base[0 : len(base)-len(filepath.Ext(base))] 59 | // Add the table if it does not already exist, ensuring standard tables win 60 | if tables[tableName] == nil { 61 | tables[tableName] = tableDynamicMetric(tableCtx, p) 62 | } else { 63 | plugin.Logger(ctx).Error("prometheus.pluginTableDefinitions", "table_already_exists", tableName) 64 | } 65 | } 66 | 67 | return tables, nil 68 | } 69 | 70 | func metricNameList(ctx context.Context, p *plugin.TableMapData) ([]string, error) { 71 | startTime := time.Now().Add(-time.Hour) 72 | endTime := time.Now() 73 | 74 | // Get list of metrics to create tables for from config 75 | prometheusConfig := GetConfig(p.Connection) 76 | if prometheusConfig.Metrics == nil { 77 | return []string{}, nil 78 | } 79 | 80 | metrics := prometheusConfig.Metrics 81 | q := "{__name__=~\"" + strings.Join(metrics, "|") + "\"}" 82 | matches := []string{q} 83 | 84 | conn, err := connectRaw(ctx, p.Connection) 85 | if err != nil { 86 | plugin.Logger(ctx).Error("prometheus.metricNameList", "connection_error", err) 87 | return nil, err 88 | } 89 | 90 | result, warnings, err := conn.LabelValues(ctx, "__name__", matches, startTime, endTime) 91 | if err != nil { 92 | plugin.Logger(ctx).Error("prometheus.metricNameList", "query_error", err) 93 | return nil, err 94 | } 95 | 96 | names := []string{} 97 | for _, i := range result { 98 | names = append(names, string(i)) 99 | } 100 | 101 | // Log the warnings 102 | for _, i := range warnings { 103 | plugin.Logger(ctx).Error("prometheus.metricNameList", "warning", i) 104 | } 105 | return names, nil 106 | } 107 | -------------------------------------------------------------------------------- /prometheus/table_prometheus_alert.go: -------------------------------------------------------------------------------- 1 | package prometheus 2 | 3 | import ( 4 | "context" 5 | 6 | "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" 7 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin" 8 | ) 9 | 10 | func tablePrometheusAlert(ctx context.Context) *plugin.Table { 11 | return &plugin.Table{ 12 | Name: "prometheus_alert", 13 | Description: "Alerts currently firing on the Prometheus server.", 14 | List: &plugin.ListConfig{ 15 | Hydrate: listAlert, 16 | }, 17 | Columns: []*plugin.Column{ 18 | // Top columns 19 | {Name: "active_at", Type: proto.ColumnType_TIMESTAMP, Description: "Timestamp when the alert became active."}, 20 | {Name: "annotations", Type: proto.ColumnType_JSON, Description: "Annotations set on the alert rule."}, 21 | {Name: "labels", Type: proto.ColumnType_JSON, Description: "Labels set on the metric."}, 22 | {Name: "state", Type: proto.ColumnType_STRING, Description: "State of the metric, e.g. firing."}, 23 | {Name: "value", Type: proto.ColumnType_DOUBLE, Description: "Value of the metric."}, 24 | }, 25 | } 26 | } 27 | 28 | func listAlert(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { 29 | conn, err := connect(ctx, d) 30 | if err != nil { 31 | plugin.Logger(ctx).Error("prometheus_alert.listAlert", "connection_error", err) 32 | return nil, err 33 | } 34 | items, err := conn.Alerts(ctx) 35 | if err != nil { 36 | plugin.Logger(ctx).Error("prometheus_alert.listAlert", "query_error", err) 37 | return nil, err 38 | } 39 | for _, i := range items.Alerts { 40 | d.StreamListItem(ctx, i) 41 | } 42 | return nil, nil 43 | } 44 | -------------------------------------------------------------------------------- /prometheus/table_prometheus_dynamic_metric.go: -------------------------------------------------------------------------------- 1 | package prometheus 2 | 3 | import ( 4 | "context" 5 | "encoding/json" 6 | "fmt" 7 | "strings" 8 | "time" 9 | 10 | v1 "github.com/prometheus/client_golang/api/prometheus/v1" 11 | "github.com/prometheus/common/model" 12 | 13 | "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" 14 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin" 15 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform" 16 | ) 17 | 18 | func tableDynamicMetric(ctx context.Context, p *plugin.TableMapData) *plugin.Table { 19 | 20 | conn, err := connectRaw(ctx, p.Connection) 21 | if err != nil { 22 | plugin.Logger(ctx).Error("prometheus_dynamic_metric.tableDynamicMetric", "connection_error", err) 23 | return nil 24 | } 25 | 26 | // Get the query for the metric (required) 27 | metricName := ctx.Value(ctxKey("metric_name")).(string) 28 | metricNameBytes, _ := json.Marshal(metricName) 29 | q := fmt.Sprintf(`{__name__=%s}`, string(metricNameBytes)) 30 | 31 | // Query parameters. Default to results from the current point in time only. 32 | r := v1.Range{ 33 | Start: time.Now().Add(-time.Hour), 34 | End: time.Now(), 35 | Step: time.Minute * time.Duration(5), 36 | } 37 | 38 | result, warnings, err := conn.QueryRange(ctx, q, r) 39 | if err != nil { 40 | plugin.Logger(ctx).Error("prometheus_dynamic_metric.tableDynamicMetric", "query_error", err) 41 | return nil 42 | } 43 | 44 | // Top columns 45 | cols := []*plugin.Column{ 46 | {Name: "timestamp", Type: proto.ColumnType_TIMESTAMP, Transform: transform.FromField("Timestamp").Transform(transform.UnixMsToTimestamp), Description: "Timestamp of the value."}, 47 | {Name: "value", Type: proto.ColumnType_DOUBLE, Description: "Value of the metric."}, 48 | } 49 | 50 | // List of columns already seen 51 | seenCols := map[string]bool{"__name__": true, "labels": true, "step_seconds": true, "timestamp": true, "value": true} 52 | 53 | // Key columns for query filtering 54 | keyColumns := []*plugin.KeyColumn{ 55 | {Name: "step_seconds", Require: plugin.Optional}, 56 | {Name: "timestamp", Operators: []string{">", ">=", "=", "<", "<="}, Require: plugin.Optional}, 57 | } 58 | 59 | // If there are any results, check them for label data to build the columns 60 | for _, i := range result.(model.Matrix) { 61 | for k := range i.Metric { 62 | sk := string(k) 63 | if seenCols[sk] { 64 | continue 65 | } 66 | cols = append(cols, &plugin.Column{Name: sk, Type: proto.ColumnType_STRING, Transform: transform.FromField("Metric").TransformP(getMetricLabelFromMetric, sk), Description: fmt.Sprintf("The %s label.", sk)}) 67 | keyColumns = append(keyColumns, &plugin.KeyColumn{Name: sk, Require: plugin.Optional}) 68 | } 69 | // Feels silly, but a simple way to check the first result only... 70 | break 71 | } 72 | 73 | // Log the warnings 74 | for _, i := range warnings { 75 | plugin.Logger(ctx).Error("prometheus_dynamic_metric.tableDynamicMetric", "query_warning", i) 76 | } 77 | 78 | // If there were no labels, then return the full set in each row 79 | if len(cols) == 2 { 80 | cols = append(cols, &plugin.Column{Name: "labels", Type: proto.ColumnType_JSON, Transform: transform.FromField("Metric"), Description: "Map of all labels in the metric."}) 81 | } 82 | 83 | // Other columns added at the end 84 | cols = append(cols, &plugin.Column{Name: "step_seconds", Type: proto.ColumnType_INT, Transform: transform.FromQual("step_seconds").Transform(getStepSeconds), Description: "Interval in seconds between metric values. Default 60 seconds."}) 85 | 86 | return &plugin.Table{ 87 | Name: metricName, 88 | Description: fmt.Sprintf("Metrics for %s.", metricName), 89 | List: &plugin.ListConfig{ 90 | KeyColumns: keyColumns, 91 | Hydrate: listMetricWithName(metricName), 92 | }, 93 | Columns: cols, 94 | } 95 | } 96 | 97 | func listMetricWithName(metricName string) func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { 98 | return func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { 99 | 100 | conn, err := connect(ctx, d) 101 | if err != nil { 102 | plugin.Logger(ctx).Error("prometheus_query.listMetricWithName", "connection_error", err) 103 | return nil, err 104 | } 105 | 106 | // Query parameters. Default to results from the current point in time only. 107 | r := v1.Range{ 108 | Start: time.Now(), 109 | End: time.Now(), 110 | } 111 | 112 | // Allow the query to set a range to get values over time 113 | timestamp := time.Now() 114 | isRange := true 115 | if d.Quals["timestamp"] != nil { 116 | for _, tq := range d.Quals["timestamp"].Quals { 117 | ts := tq.Value.GetTimestampValue().AsTime() 118 | switch tq.Operator { 119 | case ">", ">=": 120 | r.Start = ts 121 | case "=": 122 | isRange = false 123 | timestamp = ts 124 | case "<=", "<": 125 | r.End = ts 126 | } 127 | } 128 | stepSeconds := (r.End.Sub(r.Start) / 1000).Round(time.Second) 129 | // Step has to be higher than 0 seconds 130 | if stepSeconds < 1 { 131 | stepSeconds = time.Second 132 | } 133 | r.Step = stepSeconds 134 | } else { 135 | isRange = false 136 | } 137 | 138 | // Allow user to change in the query 139 | if d.Quals["step_seconds"] != nil { 140 | r.Step = time.Second * time.Duration(d.EqualsQuals["step_seconds"].GetInt64Value()) 141 | } 142 | 143 | // Always filter results to the specific metric 144 | metricNameBytes, _ := json.Marshal(metricName) 145 | pairs := []string{fmt.Sprintf(`__name__=%s`, string(metricNameBytes))} 146 | // Add any other qualifier filters 147 | for k, v := range d.EqualsQuals { 148 | // Skip any non-label quals 149 | if k == "timestamp" || k == "step_seconds" { 150 | continue 151 | } 152 | // A convenient way to encode the quotes etc 153 | valueBytes, _ := json.Marshal(v.GetStringValue()) 154 | pairs = append(pairs, fmt.Sprintf(`%s=%s`, k, string(valueBytes))) 155 | } 156 | // Combine into a single PromQL query 157 | q := fmt.Sprintf("{%s}", strings.Join(pairs, ",")) 158 | 159 | plugin.Logger(ctx).Trace("prometheus_dynamic_metric.listMetricWithName", "q", q) 160 | plugin.Logger(ctx).Trace("prometheus_dynamic_metric.listMetricWithName", "r", r) 161 | 162 | if isRange { 163 | 164 | // PRE: query is for data over time 165 | 166 | result, warnings, err := conn.QueryRange(ctx, q, r) 167 | if err != nil { 168 | plugin.Logger(ctx).Error("prometheus_query.listMetricWithName", "query_error", err) 169 | return nil, err 170 | } 171 | 172 | // Stream the results 173 | for _, i := range result.(model.Matrix) { 174 | for _, v := range i.Values { 175 | row := model.Sample{ 176 | Metric: i.Metric, 177 | Timestamp: v.Timestamp, 178 | Value: v.Value, 179 | } 180 | d.StreamListItem(ctx, row) 181 | } 182 | } 183 | 184 | // Log the warnings 185 | for _, i := range warnings { 186 | plugin.Logger(ctx).Error("prometheus_query.listMetricWithName", "query_warning", i) 187 | } 188 | 189 | } else { 190 | 191 | // PRE: query is for a single point in time. 192 | 193 | result, warnings, err := conn.Query(ctx, q, timestamp) 194 | if err != nil { 195 | plugin.Logger(ctx).Error("prometheus_query.listMetricWithName", "query_error", err) 196 | return nil, err 197 | } 198 | switch result := result.(type) { 199 | case model.Vector: 200 | { 201 | // Stream the results 202 | for _, i := range result { 203 | d.StreamListItem(ctx, i) 204 | } 205 | } 206 | case model.Matrix: 207 | { 208 | // Stream the results 209 | for _, i := range result { 210 | for _, v := range i.Values { 211 | row := model.Sample{ 212 | Metric: i.Metric, 213 | Timestamp: v.Timestamp, 214 | Value: v.Value, 215 | } 216 | d.StreamListItem(ctx, row) 217 | } 218 | } 219 | } 220 | } 221 | 222 | // Log the warnings 223 | for _, i := range warnings { 224 | plugin.Logger(ctx).Error("prometheus_query.listMetricWithName", "query_warning", i) 225 | } 226 | 227 | } 228 | 229 | return nil, nil 230 | } 231 | } 232 | 233 | func getMetricLabelFromMetric(_ context.Context, d *transform.TransformData) (interface{}, error) { 234 | param := d.Param.(string) 235 | ls := d.Value.(model.Metric) 236 | return ls[model.LabelName(param)], nil 237 | } 238 | -------------------------------------------------------------------------------- /prometheus/table_prometheus_label.go: -------------------------------------------------------------------------------- 1 | package prometheus 2 | 3 | import ( 4 | "context" 5 | "time" 6 | 7 | "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" 8 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin" 9 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform" 10 | ) 11 | 12 | func tablePrometheusLabel(ctx context.Context) *plugin.Table { 13 | return &plugin.Table{ 14 | Name: "prometheus_label", 15 | Description: "Labels used in metrics in the Prometheus server.", 16 | List: &plugin.ListConfig{ 17 | KeyColumns: []*plugin.KeyColumn{ 18 | {Name: "query", Require: plugin.Optional}, 19 | {Name: "timestamp", Operators: []string{">", ">=", "=", "<=", "<"}, Require: plugin.Optional}, 20 | }, 21 | Hydrate: listLabel, 22 | }, 23 | Columns: []*plugin.Column{ 24 | // Top columns 25 | {Name: "timestamp", Type: proto.ColumnType_TIMESTAMP, Description: "Timestamp when the labels were found."}, 26 | {Name: "name", Type: proto.ColumnType_STRING, Description: "Name of the label."}, 27 | {Name: "values", Type: proto.ColumnType_JSON, Hydrate: listLabelValue, Transform: transform.FromValue(), Description: "Values for the label."}, 28 | {Name: "query", Type: proto.ColumnType_STRING, Transform: transform.FromQual("query"), Description: "Query used to filter the label search."}, 29 | }, 30 | } 31 | } 32 | 33 | type labelNameRow struct { 34 | Timestamp time.Time 35 | Name string 36 | } 37 | 38 | func listLabel(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { 39 | conn, err := connect(ctx, d) 40 | if err != nil { 41 | plugin.Logger(ctx).Error("prometheus_label.listLabel", "connection_error", err) 42 | return nil, err 43 | } 44 | 45 | startTime := time.Now().Add(-time.Hour) 46 | endTime := time.Now() 47 | if d.Quals["timestamp"] != nil { 48 | for _, q := range d.Quals["timestamp"].Quals { 49 | ts := q.Value.GetTimestampValue().AsTime() 50 | switch q.Operator { 51 | case ">", ">=": 52 | startTime = ts 53 | case "=": 54 | startTime = ts 55 | endTime = ts 56 | case "<=", "<": 57 | endTime = ts 58 | } 59 | } 60 | } 61 | 62 | ts := startTime.Add(endTime.Sub(startTime) / 2) 63 | 64 | q := []string{} 65 | if d.EqualsQuals["query"] != nil { 66 | qs := d.EqualsQuals["query"].GetStringValue() 67 | if qs != "" { 68 | q = append(q, qs) 69 | } 70 | } 71 | 72 | result, warnings, err := conn.LabelNames(ctx, q, startTime, endTime) 73 | if err != nil { 74 | plugin.Logger(ctx).Error("prometheus_label.listLabel", "label_error", err) 75 | return nil, err 76 | } 77 | 78 | for _, i := range result { 79 | r := labelNameRow{ 80 | Timestamp: ts, 81 | Name: i, 82 | } 83 | d.StreamListItem(ctx, r) 84 | } 85 | 86 | // Log the warnings 87 | for _, i := range warnings { 88 | plugin.Logger(ctx).Error("prometheus_label.listLabel", "label_warning", i) 89 | } 90 | 91 | return nil, nil 92 | } 93 | 94 | func listLabelValue(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { 95 | conn, err := connect(ctx, d) 96 | if err != nil { 97 | plugin.Logger(ctx).Error("prometheus_label_value.listLabelValue", "connection_error", err) 98 | return nil, err 99 | } 100 | 101 | startTime := time.Now().Add(-time.Hour) 102 | endTime := time.Now() 103 | if d.Quals["timestamp"] != nil { 104 | for _, q := range d.Quals["timestamp"].Quals { 105 | ts := q.Value.GetTimestampValue().AsTime() 106 | switch q.Operator { 107 | case ">", ">=": 108 | startTime = ts 109 | case "=": 110 | startTime = ts 111 | endTime = ts 112 | case "<=", "<": 113 | endTime = ts 114 | } 115 | } 116 | } 117 | 118 | q := []string{} 119 | if d.EqualsQuals["query"] != nil { 120 | qs := d.EqualsQuals["query"].GetStringValue() 121 | if qs != "" { 122 | q = append(q, qs) 123 | } 124 | } 125 | 126 | result, warnings, err := conn.LabelValues(ctx, h.Item.(labelNameRow).Name, q, startTime, endTime) 127 | if err != nil { 128 | plugin.Logger(ctx).Error("prometheus_label_value.listLabelValue", "labelValue_error", err) 129 | return nil, err 130 | } 131 | 132 | // Log the warnings 133 | for _, i := range warnings { 134 | plugin.Logger(ctx).Error("prometheus_label_value.listLabelValue", "labelValue_warning", i) 135 | } 136 | 137 | return result, nil 138 | } 139 | -------------------------------------------------------------------------------- /prometheus/table_prometheus_metric.go: -------------------------------------------------------------------------------- 1 | package prometheus 2 | 3 | import ( 4 | "context" 5 | "time" 6 | 7 | v1 "github.com/prometheus/client_golang/api/prometheus/v1" 8 | "github.com/prometheus/common/model" 9 | 10 | "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" 11 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin" 12 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform" 13 | ) 14 | 15 | func tablePrometheusMetric(ctx context.Context) *plugin.Table { 16 | return &plugin.Table{ 17 | Name: "prometheus_metric", 18 | Description: "Query metrics in the Prometheus server.", 19 | List: &plugin.ListConfig{ 20 | KeyColumns: []*plugin.KeyColumn{ 21 | {Name: "query"}, 22 | {Name: "step_seconds", Require: plugin.Optional}, 23 | {Name: "timestamp", Operators: []string{">", ">=", "=", "<", "<="}, Require: plugin.Optional}, 24 | }, 25 | Hydrate: listMetric, 26 | }, 27 | Columns: []*plugin.Column{ 28 | // Top columns 29 | {Name: "name", Type: proto.ColumnType_STRING, Transform: transform.FromField("Metric").Transform(getMetricNameFromMetric), Description: "Name of the metric."}, 30 | {Name: "timestamp", Type: proto.ColumnType_TIMESTAMP, Transform: transform.FromField("Timestamp").Transform(transform.UnixMsToTimestamp), Description: "Timestamp of the value."}, 31 | {Name: "value", Type: proto.ColumnType_DOUBLE, Description: "Value of the metric."}, 32 | // Other columns 33 | {Name: "labels", Type: proto.ColumnType_JSON, Transform: transform.FromField("Metric"), Description: "Labels for the metric."}, 34 | {Name: "query", Type: proto.ColumnType_STRING, Transform: transform.FromQual("query"), Description: "Query used to filter the metric data."}, 35 | {Name: "step_seconds", Type: proto.ColumnType_INT, Transform: transform.FromQual("step_seconds").Transform(getStepSeconds), Description: "Interval in seconds between metric values."}, 36 | }, 37 | } 38 | } 39 | 40 | func listMetric(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { 41 | 42 | conn, err := connect(ctx, d) 43 | if err != nil { 44 | plugin.Logger(ctx).Error("prometheus_query.listMetric", "connection_error", err) 45 | return nil, err 46 | } 47 | 48 | // Query parameters. Default to results from the current point in time only. 49 | r := v1.Range{ 50 | Start: time.Now(), 51 | End: time.Now(), 52 | } 53 | 54 | // Allow the query to set a range to get values over time 55 | timestamp := time.Now() 56 | isRange := true 57 | if d.Quals["timestamp"] != nil { 58 | for _, tq := range d.Quals["timestamp"].Quals { 59 | ts := tq.Value.GetTimestampValue().AsTime() 60 | switch tq.Operator { 61 | case ">", ">=": 62 | r.Start = ts 63 | case "=": 64 | isRange = false 65 | timestamp = ts 66 | case "<=", "<": 67 | r.End = ts 68 | } 69 | } 70 | stepSeconds := (r.End.Sub(r.Start) / 1000).Round(time.Second) 71 | // Step has to be higher than 0 seconds 72 | if stepSeconds < 1 { 73 | stepSeconds = time.Second 74 | } 75 | r.Step = stepSeconds 76 | } else { 77 | isRange = false 78 | } 79 | 80 | // Allow user to change in the query 81 | if d.Quals["step_seconds"] != nil { 82 | r.Step = time.Second * time.Duration(d.EqualsQuals["step_seconds"].GetInt64Value()) 83 | } 84 | 85 | // Get the query (required) 86 | q := d.EqualsQuals["query"].GetStringValue() 87 | 88 | if isRange { 89 | 90 | // PRE: query is for data over time 91 | 92 | result, warnings, err := conn.QueryRange(ctx, q, r) 93 | if err != nil { 94 | plugin.Logger(ctx).Error("prometheus_query.listMetric", "query_error", err) 95 | return nil, err 96 | } 97 | 98 | // Stream the results 99 | for _, i := range result.(model.Matrix) { 100 | for _, v := range i.Values { 101 | row := model.Sample{ 102 | Metric: i.Metric, 103 | Timestamp: v.Timestamp, 104 | Value: v.Value, 105 | } 106 | d.StreamListItem(ctx, row) 107 | } 108 | } 109 | 110 | // Log the warnings 111 | for _, i := range warnings { 112 | plugin.Logger(ctx).Error("prometheus_query.listMetric", "query_warning", i) 113 | } 114 | 115 | } else { 116 | 117 | // PRE: query is for a single point in time. 118 | 119 | result, warnings, err := conn.Query(ctx, q, timestamp) 120 | if err != nil { 121 | plugin.Logger(ctx).Error("prometheus_query.listMetric", "query_error", err) 122 | return nil, err 123 | } 124 | switch result := result.(type) { 125 | case model.Vector: 126 | { 127 | // Stream the results 128 | for _, i := range result { 129 | d.StreamListItem(ctx, i) 130 | } 131 | } 132 | case model.Matrix: 133 | { 134 | // Stream the results 135 | for _, i := range result { 136 | for _, v := range i.Values { 137 | row := model.Sample{ 138 | Metric: i.Metric, 139 | Timestamp: v.Timestamp, 140 | Value: v.Value, 141 | } 142 | d.StreamListItem(ctx, row) 143 | } 144 | } 145 | } 146 | } 147 | 148 | // Log the warnings 149 | for _, i := range warnings { 150 | plugin.Logger(ctx).Error("prometheus_query.listMetric", "query_warning", i) 151 | } 152 | 153 | } 154 | 155 | return nil, nil 156 | } 157 | 158 | func getMetricNameFromMetric(_ context.Context, d *transform.TransformData) (interface{}, error) { 159 | ls := d.Value.(model.Metric) 160 | return ls["__name__"], nil 161 | } 162 | 163 | func getStepSeconds(_ context.Context, d *transform.TransformData) (interface{}, error) { 164 | // Use the qual value if specified 165 | if d.Value != nil { 166 | return d.Value.(int64), nil 167 | } 168 | 169 | // Else calculate based on a time range (if available) or default to 1 second 170 | start := time.Now() 171 | end := time.Now() 172 | step := int64(1) 173 | 174 | if d.KeyColumnQuals["timestamp"] != nil { 175 | for _, tq := range d.KeyColumnQuals["timestamp"] { 176 | ts := tq.Value.GetTimestampValue().AsTime() 177 | switch tq.Operator { 178 | case ">", ">=": 179 | start = ts 180 | case "<=", "<": 181 | end = ts 182 | } 183 | } 184 | stepSeconds := (end.Sub(start) / 1000).Round(time.Second) 185 | // Step has to be higher than 0 seconds 186 | if stepSeconds < 1 { 187 | stepSeconds = time.Second 188 | } 189 | step = int64(stepSeconds.Seconds()) 190 | } 191 | 192 | return step, nil 193 | } 194 | -------------------------------------------------------------------------------- /prometheus/table_prometheus_rule.go: -------------------------------------------------------------------------------- 1 | package prometheus 2 | 3 | import ( 4 | "context" 5 | 6 | v1 "github.com/prometheus/client_golang/api/prometheus/v1" 7 | 8 | "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" 9 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin" 10 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform" 11 | ) 12 | 13 | func tablePrometheusRule(ctx context.Context) *plugin.Table { 14 | return &plugin.Table{ 15 | Name: "prometheus_rule", 16 | Description: "Rules in the Prometheus server.", 17 | List: &plugin.ListConfig{ 18 | ParentHydrate: listRuleGroup, 19 | Hydrate: listRule, 20 | }, 21 | Columns: []*plugin.Column{ 22 | // Top columns 23 | {Name: "name", Type: proto.ColumnType_STRING, Transform: transform.FromField("Rule.Name"), Description: "Name of the alert."}, 24 | {Name: "last_evaluation", Type: proto.ColumnType_TIMESTAMP, Transform: transform.FromField("Rule.LastEvaluation"), Description: "Time when the rule was last evaluated."}, 25 | {Name: "health", Type: proto.ColumnType_STRING, Transform: transform.FromField("Rule.Health"), Description: "Health of the rule, e.g. ok."}, 26 | {Name: "state", Type: proto.ColumnType_STRING, Transform: transform.FromField("Rule.State"), Description: "State of the alert for this rule, e.g. firing, inactive."}, 27 | // Other columns 28 | {Name: "alerts", Type: proto.ColumnType_JSON, Transform: transform.FromField("Rule.Alerts"), Description: "Alerts for the rule."}, 29 | {Name: "annotations", Type: proto.ColumnType_JSON, Transform: transform.FromField("Rule.Annotations"), Description: "Annotations to add to each alert."}, 30 | {Name: "duration", Type: proto.ColumnType_DOUBLE, Transform: transform.FromField("Rule.Duration"), Description: "Alerts are considered firing once they have been returned for this long. Alerts which have not yet fired for long enough are considered pending."}, 31 | {Name: "evaluation_time", Type: proto.ColumnType_DOUBLE, Transform: transform.FromField("Rule.EvaluationTime"), Description: "Time taken in seconds to run the rule evaluation."}, 32 | {Name: "group_file", Type: proto.ColumnType_STRING, Description: "Path to the file that defines the rule group for this rule."}, 33 | {Name: "group_interval", Type: proto.ColumnType_STRING, Description: "Interval between evaluations for rules in this rule group."}, 34 | {Name: "group_name", Type: proto.ColumnType_STRING, Description: "Name of the rule group that contains the rule."}, 35 | {Name: "group_rule_num", Type: proto.ColumnType_INT, Description: "Rule number within the group. Starts at 1."}, 36 | {Name: "labels", Type: proto.ColumnType_JSON, Transform: transform.FromField("Rule.Labels"), Description: "Labels to add or overwrite for each alert."}, 37 | {Name: "last_error", Type: proto.ColumnType_STRING, Transform: transform.FromField("Rule.LastError"), Description: "Last error message from the rule, if any."}, 38 | {Name: "query", Type: proto.ColumnType_STRING, Transform: transform.FromField("Rule.Query"), Description: "The PromQL expression to evaluate. Every evaluation cycle this is evaluated at the current time, and all resultant time series become pending/firing alerts."}, 39 | }, 40 | } 41 | } 42 | 43 | type ruleRow struct { 44 | GroupName string 45 | GroupFile string 46 | GroupInterval float64 47 | GroupRuleNum int 48 | Rule interface{} 49 | } 50 | 51 | func listRule(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { 52 | ruleGroup := h.Item.(v1.RuleGroup) 53 | for idx, i := range ruleGroup.Rules { 54 | r := ruleRow{ 55 | GroupName: ruleGroup.Name, 56 | GroupFile: ruleGroup.File, 57 | GroupInterval: ruleGroup.Interval, 58 | Rule: i, 59 | GroupRuleNum: idx + 1, 60 | } 61 | d.StreamListItem(ctx, r) 62 | } 63 | return nil, nil 64 | } 65 | -------------------------------------------------------------------------------- /prometheus/table_prometheus_rule_group.go: -------------------------------------------------------------------------------- 1 | package prometheus 2 | 3 | import ( 4 | "context" 5 | 6 | "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" 7 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin" 8 | ) 9 | 10 | func tablePrometheusRuleGroup(ctx context.Context) *plugin.Table { 11 | return &plugin.Table{ 12 | Name: "prometheus_rule_group", 13 | Description: "Rule groups in the Prometheus server.", 14 | List: &plugin.ListConfig{ 15 | Hydrate: listRuleGroup, 16 | }, 17 | Columns: []*plugin.Column{ 18 | // Top columns 19 | {Name: "name", Type: proto.ColumnType_STRING, Description: "The name of the group. Must be unique within a file."}, 20 | {Name: "file", Type: proto.ColumnType_STRING, Description: "Path to the rule group file definition."}, 21 | {Name: "interval", Type: proto.ColumnType_DOUBLE, Description: "How often rules in the group are evaluated in seconds."}, 22 | }, 23 | } 24 | } 25 | 26 | func listRuleGroup(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { 27 | conn, err := connect(ctx, d) 28 | if err != nil { 29 | plugin.Logger(ctx).Error("prometheus_rule_group.listRule", "connection_error", err) 30 | return nil, err 31 | } 32 | items, err := conn.Rules(ctx) 33 | if err != nil { 34 | plugin.Logger(ctx).Error("prometheus_rule_group.listRule", "query_error", err) 35 | return nil, err 36 | } 37 | for _, i := range items.Groups { 38 | d.StreamListItem(ctx, i) 39 | } 40 | return nil, nil 41 | } 42 | -------------------------------------------------------------------------------- /prometheus/table_prometheus_series.go: -------------------------------------------------------------------------------- 1 | package prometheus 2 | 3 | import ( 4 | "context" 5 | "time" 6 | 7 | "github.com/prometheus/common/model" 8 | 9 | "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" 10 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin" 11 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform" 12 | ) 13 | 14 | func tablePrometheusSeries(ctx context.Context) *plugin.Table { 15 | return &plugin.Table{ 16 | Name: "prometheus_series", 17 | Description: "Series in the Prometheus server.", 18 | List: &plugin.ListConfig{ 19 | KeyColumns: []*plugin.KeyColumn{ 20 | {Name: "query", Operators: []string{"="}}, 21 | {Name: "timestamp", Operators: []string{">", ">=", "=", "<=", "<"}, Require: plugin.Optional}, 22 | }, 23 | Hydrate: listSeries, 24 | }, 25 | Columns: []*plugin.Column{ 26 | // Top columns 27 | {Name: "timestamp", Type: proto.ColumnType_TIMESTAMP, Description: "Timestamp when the series was found."}, 28 | {Name: "name", Type: proto.ColumnType_STRING, Transform: transform.FromField("Metric").Transform(getMetricNameFromLabelSet), Description: "Name of the metric for the series."}, 29 | {Name: "metric", Type: proto.ColumnType_JSON, Description: "Metric details for the series."}, 30 | {Name: "query", Type: proto.ColumnType_STRING, Transform: transform.FromQual("query"), Description: "Query used to match the series."}, 31 | }, 32 | } 33 | } 34 | 35 | type seriesRow struct { 36 | Timestamp time.Time 37 | Metric model.LabelSet 38 | } 39 | 40 | func listSeries(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { 41 | conn, err := connect(ctx, d) 42 | if err != nil { 43 | plugin.Logger(ctx).Error("prometheus_target.listSeries", "connection_error", err) 44 | return nil, err 45 | } 46 | 47 | q := d.EqualsQuals["query"].GetStringValue() 48 | 49 | startTime := time.Now().Add(-time.Hour) 50 | endTime := time.Now() 51 | if d.Quals["timestamp"] != nil { 52 | for _, q := range d.Quals["timestamp"].Quals { 53 | ts := q.Value.GetTimestampValue().AsTime() 54 | switch q.Operator { 55 | case ">", ">=": 56 | startTime = ts 57 | case "=": 58 | startTime = ts 59 | endTime = ts 60 | case "<=", "<": 61 | endTime = ts 62 | } 63 | } 64 | } 65 | 66 | // We discover series from the timerange quals passed in. But, series do not 67 | // have an inherent timestamp associated with them, so we choose the halfway 68 | // point as the column return value. 69 | ts := startTime.Add(endTime.Sub(startTime) / 2) 70 | 71 | result, warnings, err := conn.Series(ctx, []string{q}, startTime, endTime) 72 | if err != nil { 73 | plugin.Logger(ctx).Error("prometheus_target.listSeries", "series_error", err) 74 | return nil, err 75 | } 76 | 77 | for _, i := range result { 78 | r := seriesRow{ 79 | Timestamp: ts, 80 | Metric: i, 81 | } 82 | d.StreamListItem(ctx, r) 83 | } 84 | 85 | // Log the warnings 86 | for _, i := range warnings { 87 | plugin.Logger(ctx).Error("prometheus_target.listSeries", "series_warning", i) 88 | } 89 | 90 | return nil, nil 91 | } 92 | 93 | func getMetricNameFromLabelSet(_ context.Context, d *transform.TransformData) (interface{}, error) { 94 | ls := d.Value.(model.LabelSet) 95 | return ls["__name__"], nil 96 | } 97 | -------------------------------------------------------------------------------- /prometheus/table_prometheus_target.go: -------------------------------------------------------------------------------- 1 | package prometheus 2 | 3 | import ( 4 | "context" 5 | 6 | "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" 7 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin" 8 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform" 9 | ) 10 | 11 | func tablePrometheusTarget(ctx context.Context) *plugin.Table { 12 | return &plugin.Table{ 13 | Name: "prometheus_target", 14 | Description: "Target scraped by the Prometheus server.", 15 | List: &plugin.ListConfig{ 16 | Hydrate: listTarget, 17 | }, 18 | Columns: []*plugin.Column{ 19 | // Top columns 20 | {Name: "scrape_pool", Type: proto.ColumnType_STRING, Transform: transform.FromField("Target.ScrapePool"), Description: "Name of the scrape pool this target belongs to."}, 21 | {Name: "scrape_url", Type: proto.ColumnType_STRING, Transform: transform.FromField("Target.ScrapeURL"), Description: "URL to be scraped."}, 22 | {Name: "state", Type: proto.ColumnType_STRING, Description: "State of the target, e.g. active, dropped."}, 23 | {Name: "health", Type: proto.ColumnType_STRING, Transform: transform.FromField("Target.Health"), Description: "Health of the target, e.g. up."}, 24 | {Name: "last_scrape", Type: proto.ColumnType_TIMESTAMP, Transform: transform.FromField("Target.LastScrape"), Description: "Time when the last scrape occurred."}, 25 | {Name: "labels", Type: proto.ColumnType_JSON, Transform: transform.FromField("Target.Labels"), Description: "Label set after relabelling has occurred."}, 26 | // Other columns 27 | {Name: "discovered_labels", Type: proto.ColumnType_JSON, Transform: transform.FromField("Target.DiscoveredLabels"), Description: "Unmodified labels retrieved during service discovery before relabelling has occurred."}, 28 | {Name: "global_url", Type: proto.ColumnType_STRING, Transform: transform.FromField("Target.GlobalURL"), Description: "Global URL to be scraped."}, 29 | {Name: "last_error", Type: proto.ColumnType_STRING, Transform: transform.FromField("Target.LastError"), Description: "Last error message, if any."}, 30 | {Name: "last_scrape_duration", Type: proto.ColumnType_DOUBLE, Transform: transform.FromField("Target.LastScrapeDuration"), Description: "Time in seconds the last scrape took to run."}, 31 | }, 32 | } 33 | } 34 | 35 | type targetRow struct { 36 | State string 37 | Target interface{} 38 | } 39 | 40 | func listTarget(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { 41 | conn, err := connect(ctx, d) 42 | if err != nil { 43 | plugin.Logger(ctx).Error("prometheus_target.listTarget", "connection_error", err) 44 | return nil, err 45 | } 46 | items, err := conn.Targets(ctx) 47 | if err != nil { 48 | plugin.Logger(ctx).Error("prometheus_target.listTarget", "query_error", err) 49 | return nil, err 50 | } 51 | for _, i := range items.Active { 52 | tr := targetRow{"active", i} 53 | d.StreamListItem(ctx, tr) 54 | } 55 | for _, i := range items.Dropped { 56 | tr := targetRow{"dropped", i} 57 | d.StreamListItem(ctx, tr) 58 | } 59 | return nil, nil 60 | } 61 | -------------------------------------------------------------------------------- /prometheus/utils.go: -------------------------------------------------------------------------------- 1 | package prometheus 2 | 3 | import ( 4 | "context" 5 | "os" 6 | 7 | "github.com/prometheus/client_golang/api" 8 | v1 "github.com/prometheus/client_golang/api/prometheus/v1" 9 | 10 | "github.com/turbot/steampipe-plugin-sdk/v5/memoize" 11 | "github.com/turbot/steampipe-plugin-sdk/v5/plugin" 12 | ) 13 | 14 | func connect(ctx context.Context, d *plugin.QueryData) (v1.API, error) { 15 | api, err := getConnectionMemoize(ctx, d, nil) 16 | if err != nil { 17 | return nil, err 18 | } 19 | return api.(v1.API), nil 20 | } 21 | 22 | var getConnectionMemoize = plugin.HydrateFunc(getConnectionUncached).Memoize(memoize.WithCacheKeyFunction(getConnectionCackeKey)) 23 | 24 | func getConnectionUncached(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { 25 | return connectRaw(ctx, d.Connection) 26 | } 27 | 28 | func getConnectionCackeKey(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { 29 | key := "connectPrometheus" 30 | return key, nil 31 | } 32 | 33 | func connectRaw(ctx context.Context, c *plugin.Connection) (v1.API, error) { 34 | 35 | var address string 36 | // Prefer config settings 37 | prometheusConfig := GetConfig(c) 38 | 39 | address = os.Getenv("PROMETHEUS_URL") 40 | if prometheusConfig.Address != nil { 41 | address = *prometheusConfig.Address 42 | } 43 | 44 | // Error if the minimum config is not set 45 | if address == "" { 46 | // Panic since we cannot create a valid empty API to return 47 | panic("address must be configured") 48 | } 49 | 50 | client, err := api.NewClient(api.Config{ 51 | Address: address, 52 | }) 53 | 54 | if err != nil { 55 | plugin.Logger(ctx).Error("connectRaw", "client connection error", err) 56 | return nil, err 57 | } 58 | 59 | conn := v1.NewAPI(client) 60 | 61 | if err != nil { 62 | plugin.Logger(ctx).Error("connectRaw", "cache-set", err) 63 | return conn, err 64 | } 65 | 66 | return conn, nil 67 | } 68 | --------------------------------------------------------------------------------