From bfe2a76742a636e414d61e2cd96761262aee8181 Mon Sep 17 00:00:00 2001 From: Adrien ZAGABE Date: Tue, 25 Jul 2023 11:19:36 +0200 Subject: [PATCH 01/28] adding top queries execution collector --- collector/pg_top_queries_execution.go | 165 ++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 collector/pg_top_queries_execution.go diff --git a/collector/pg_top_queries_execution.go b/collector/pg_top_queries_execution.go new file mode 100644 index 000000000..1ac043a15 --- /dev/null +++ b/collector/pg_top_queries_execution.go @@ -0,0 +1,165 @@ +package collector + +import ( + "context" + "database/sql" + + "github.com/go-kit/log" + "github.com/prometheus/client_golang/prometheus" +) + +const statTopQueriesExecutionTime = "stat_top_queries" + +func init() { + registerCollector(statTopQueriesExecutionTime, true, NewPGStatTopQueriesExecutionTime) +} + +type PGStatTopQueriesExecutionTime struct { + log log.Logger +} + +func NewPGStatTopQueriesExecutionTime(config collectorConfig) (Collector, error) { + return &PGStatTopQueriesExecutionTime{log: config.logger}, nil +} + +var ( + statTotalExecutionTime = prometheus.NewDesc( + prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "total_seconds"), + "Total time spent in the statement, in milliseconds", + []string{"queryid", "datname", "user"}, + prometheus.Labels{}, + ) + statMinimumExecutionTime = prometheus.NewDesc( + prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "min_time_seconds"), + "Minimum time spent in the statement, in milliseconds", + []string{"queryid", "datname", "user"}, + prometheus.Labels{}, + ) + + statMaximumExecutionTime = prometheus.NewDesc( + prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "max_time_seconds"), + "Maximum time spent in the statement, in milliseconds", + []string{"queryid", "datname", "user"}, + prometheus.Labels{}, + ) + + statMeanExecutionTime = prometheus.NewDesc( + prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "mean_time"), + "Mean time spent in the statement, in milliseconds", + []string{"queryid", "datname", "user"}, + prometheus.Labels{}, + ) + + statTopQueryExecutionQuery = `SELECT + pg_get_userbyid(userid) as user, + pg_database.datname, + pg_stat_statements.queryid, + pg_stat_statements.mean_time / 1000.0 as mean_time, + pg_stat_statements.total_time / 1000.0 as total_seconds, + pg_stat_statements.min_time / 1000.0 as min_time_seconds, + pg_stat_statements.max_time / 1000.0 as max_time_seconds, + pg_stat_statements.calls + FROM pg_stat_statements + JOIN pg_database + ON pg_database.id = pg_stat_statements.dbid + WHERE + total_time > ( + SELECT percentile_cont(0.1) + WITHIN GROUP (ORDER BY total_time) + FROM pg_stat_statements + ) + ORDER BY total_seconds DESC + LIMIT 100;` +) + +func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error { + db := instance.getDB() + rows, err := db.QueryContext(ctx, statTopQueryExecutionQuery) + + if err != nil { + return err + } + defer rows.Close() + + for rows.Next() { + var user, datName, queryid sql.NullString + var meanTime, totalSeconds, minTimeSeconds, maxTimeSeconds sql.NullFloat64 + var calls sql.NullInt64 + + if err := rows.Scan(&user, &datName, &meanTime, &totalSeconds, &minTimeSeconds, &maxTimeSeconds, &calls); err != nil { + return err + } + + userLabel := "unknown" + if user.Valid { + userLabel = user.String + } + + datnameLabel := "unknown" + if datName.Valid { + datnameLabel = datName.String + } + + queryIdLabel := "unknown" + if queryid.Valid { + queryIdLabel = queryid.String + } + + totalSecondsMetric := 0.0 + if totalSeconds.Valid { + totalSecondsMetric = totalSeconds.Float64 + } + ch <- prometheus.MustNewConstMetric( + statTotalExecutionTime, + prometheus.CounterValue, + totalSecondsMetric, + userLabel, datnameLabel, queryIdLabel, + ) + + meanTimeSecondsMetric := 0.0 + if meanTime.Valid { + meanTimeSecondsMetric = meanTime.Float64 + } + ch <- prometheus.MustNewConstMetric( + statMeanExecutionTime, + prometheus.CounterValue, + meanTimeSecondsMetric, + userLabel, datnameLabel, queryIdLabel, + ) + + minTimeSecondsMetric := 0.0 + if minTimeSeconds.Valid { + minTimeSecondsMetric = minTimeSeconds.Float64 + } + ch <- prometheus.MustNewConstMetric( + statMinimumExecutionTime, + prometheus.CounterValue, + minTimeSecondsMetric, + userLabel, datnameLabel, queryIdLabel, + ) + + maxTimeSecondsMetric := 0.0 + if maxTimeSeconds.Valid { + maxTimeSecondsMetric = maxTimeSeconds.Float64 + } + ch <- prometheus.MustNewConstMetric( + statMaximumExecutionTime, + prometheus.CounterValue, + maxTimeSecondsMetric, + userLabel, datnameLabel, queryIdLabel, + ) + + callsMetric := 0.0 + if calls.Valid { + callsMetric = float64(calls.Int64) + } + + ch <- prometheus.MustNewConstMetric( + statTotalExecutionTime, + prometheus.CounterValue, + callsMetric, + userLabel, datnameLabel, queryIdLabel, + ) + } + return nil +} From 68d33c40343cdc10c1455547e71482d2600b8438 Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Tue, 25 Jul 2023 12:37:52 +0200 Subject: [PATCH 02/28] Create go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .github/workflows/go.yml diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 000000000..5058f039f --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,25 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Build projet + +on: + workflow_dispatch: + +jobs: + + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.19' + + - name: Build + run: go build -v ./... + + - name: Test + run: go test -v ./... From 9febca2f0c6d8017a75e50a03e53add248143322 Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Tue, 25 Jul 2023 14:42:45 +0200 Subject: [PATCH 03/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 5058f039f..2d556f5cc 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -23,3 +23,22 @@ jobs: - name: Test run: go test -v ./... + + release: + name: Release + needs: build + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v2 + + - name: Create Release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ github.run_id }} + release_name: Release ${{ github.run_id }} + draft: false + prerelease: false From bc0149da5630c6413b0e0a1314d5b62b36aa2c66 Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Tue, 25 Jul 2023 15:01:40 +0200 Subject: [PATCH 04/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 2d556f5cc..e5848effa 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -7,30 +7,28 @@ on: workflow_dispatch: jobs: - build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: '1.19' + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.19' - - name: Build - run: go build -v ./... + - name: Build + run: go build -v ./... - - name: Test - run: go test -v ./... + - name: Test + run: go test -v ./... release: name: Release needs: build runs-on: ubuntu-latest steps: - - name: Check out code - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Create Release id: create_release @@ -38,7 +36,7 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: - tag_name: ${{ github.run_id }} - release_name: Release ${{ github.run_id }} + tag_name: 'run-${{ github.run_id }}' + release_name: Release run-${{ github.run_id }} draft: false prerelease: false From fc03b9f7bf0ba4187652e7302528404e6da4a162 Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Tue, 25 Jul 2023 15:14:26 +0200 Subject: [PATCH 05/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index e5848effa..34e1f965c 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -19,6 +19,12 @@ jobs: - name: Build run: go build -v ./... + + - name: Install make + run: apt-get install make + + - name: make + run: make build - name: Test run: go test -v ./... From d99c0864f77ea09343bc87d000443dfaaa0f8a70 Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Tue, 25 Jul 2023 15:15:50 +0200 Subject: [PATCH 06/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 34e1f965c..6c699ad9b 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -21,7 +21,7 @@ jobs: run: go build -v ./... - name: Install make - run: apt-get install make + run: sudo apt-get install make -y - name: make run: make build From 8bcfc2900fb99ab8a5ba1f90c0cf7704efa2d217 Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Tue, 25 Jul 2023 15:22:25 +0200 Subject: [PATCH 07/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 6c699ad9b..68ec40b32 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -46,3 +46,16 @@ jobs: release_name: Release run-${{ github.run_id }} draft: false prerelease: false + + - name: Upload Release Asset + id: upload-release-asset + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./postgres_exporter + # Vous devez également spécifier le type de contenu (Content-Type) de l'asset. + # Pour un binaire, vous pouvez généralement utiliser application/octet-stream. + asset_content_type: application/octet-stream + asset_name: postgres_exporter From 9c3f5672bcef9eb2550f8829cd22b07eed943321 Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Tue, 25 Jul 2023 15:36:25 +0200 Subject: [PATCH 08/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 68ec40b32..4de4e281e 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -21,13 +21,17 @@ jobs: run: go build -v ./... - name: Install make - run: sudo apt-get install make -y + run: sudo apt-get install make locate -y - name: make run: make build - name: Test run: go test -v ./... + + - name: locate postgres_exporter + run: sudo updatedb && sudo locate postgres_exporter + release: name: Release From 5c0db4b47cec7f5feb0df880f442ce2d09a2abf7 Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Tue, 25 Jul 2023 15:39:46 +0200 Subject: [PATCH 09/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 4de4e281e..fe6c96f94 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -21,17 +21,13 @@ jobs: run: go build -v ./... - name: Install make - run: sudo apt-get install make locate -y + run: sudo apt-get install make -y - name: make run: make build - name: Test run: go test -v ./... - - - name: locate postgres_exporter - run: sudo updatedb && sudo locate postgres_exporter - release: name: Release @@ -58,7 +54,7 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./postgres_exporter + asset_path: ./github.com/prometheus-community/postgres_exporter/cmd/postgres_exporter # Vous devez également spécifier le type de contenu (Content-Type) de l'asset. # Pour un binaire, vous pouvez généralement utiliser application/octet-stream. asset_content_type: application/octet-stream From a88587ef855bfa52b969ac1f5dc10a54ba85f059 Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Tue, 25 Jul 2023 15:50:59 +0200 Subject: [PATCH 10/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index fe6c96f94..8dfe3877b 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -54,7 +54,7 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./github.com/prometheus-community/postgres_exporter/cmd/postgres_exporter + asset_path: /go/src/app/postgres_exporter # Vous devez également spécifier le type de contenu (Content-Type) de l'asset. # Pour un binaire, vous pouvez généralement utiliser application/octet-stream. asset_content_type: application/octet-stream From 98bc34848f07bbf802133cac79b131ed7fde9b60 Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Tue, 25 Jul 2023 15:55:01 +0200 Subject: [PATCH 11/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 8dfe3877b..d54970d7f 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -26,6 +26,10 @@ jobs: - name: make run: make build + - name: Move binary + run: mv /go/src/app/postgres_exporter ./ + + - name: Test run: go test -v ./... @@ -54,8 +58,6 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: /go/src/app/postgres_exporter - # Vous devez également spécifier le type de contenu (Content-Type) de l'asset. - # Pour un binaire, vous pouvez généralement utiliser application/octet-stream. + asset_path: ./postgres_exporter asset_content_type: application/octet-stream asset_name: postgres_exporter From 23cab02f9fad32a73ce322082e652dbdecc6d8cb Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Tue, 25 Jul 2023 15:57:55 +0200 Subject: [PATCH 12/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index d54970d7f..47f5530c6 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -27,9 +27,8 @@ jobs: run: make build - name: Move binary - run: mv /go/src/app/postgres_exporter ./ - - + run: mv /home/runner/work/postgres_exporter/postgres_exporter/postgres_exporter ./ + - name: Test run: go test -v ./... From d7e8290a56296dd4bc95432f2dfeda7a66c9445c Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Tue, 25 Jul 2023 16:00:57 +0200 Subject: [PATCH 13/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 47f5530c6..7a302888a 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -25,6 +25,9 @@ jobs: - name: make run: make build + + - name: pwd + run: pwd - name: Move binary run: mv /home/runner/work/postgres_exporter/postgres_exporter/postgres_exporter ./ @@ -57,6 +60,6 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./postgres_exporter + asset_path: /home/runner/work/postgres_exporter/postgres_exporter/postgres_exporter asset_content_type: application/octet-stream asset_name: postgres_exporter From 2c9dbe7d709efc736cc057b34dd3af4615ecd9a7 Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Tue, 25 Jul 2023 16:02:46 +0200 Subject: [PATCH 14/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 7a302888a..94826e647 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -26,12 +26,6 @@ jobs: - name: make run: make build - - name: pwd - run: pwd - - - name: Move binary - run: mv /home/runner/work/postgres_exporter/postgres_exporter/postgres_exporter ./ - - name: Test run: go test -v ./... From 23115ec2bc175230f5a1aaa9e6dc53378cd63060 Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Tue, 25 Jul 2023 16:06:00 +0200 Subject: [PATCH 15/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 94826e647..2cd95f5f8 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -25,6 +25,9 @@ jobs: - name: make run: make build + + - name: mv + run: mv /home/runner/work/postgres_exporter/postgres_exporter/postgres_exporter /home/runner/ - name: Test run: go test -v ./... @@ -54,6 +57,6 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: /home/runner/work/postgres_exporter/postgres_exporter/postgres_exporter + asset_path: /home/runner/postgres_exporter asset_content_type: application/octet-stream asset_name: postgres_exporter From f39edef5b3d8412de1f0cbd25a8bdf740d9d7911 Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Tue, 25 Jul 2023 16:09:29 +0200 Subject: [PATCH 16/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 2cd95f5f8..0639ab55c 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -26,8 +26,12 @@ jobs: - name: make run: make build - - name: mv - run: mv /home/runner/work/postgres_exporter/postgres_exporter/postgres_exporter /home/runner/ + - name: Upload binary + uses: actions/upload-artifact@v2 + with: + name: postgres_exporter + path: /home/runner/work/postgres_exporter/postgres_exporter/postgres_exporter + - name: Test run: go test -v ./... @@ -39,6 +43,11 @@ jobs: steps: - uses: actions/checkout@v3 + - name: Download binary + uses: actions/download-artifact@v2 + with: + name: postgres_exporter + - name: Create Release id: create_release uses: actions/create-release@v1 From 3d8da169d4fc2dbeaeb1e95d781f301ade5e7199 Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Tue, 25 Jul 2023 16:11:56 +0200 Subject: [PATCH 17/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 0639ab55c..9d5785f25 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -66,6 +66,6 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: /home/runner/postgres_exporter + asset_path: ./postgres_exporter asset_content_type: application/octet-stream asset_name: postgres_exporter From 7e078bddb896bca594a1a72e8891609caafae2a4 Mon Sep 17 00:00:00 2001 From: Adrien ZAGABE Date: Tue, 25 Jul 2023 17:37:07 +0200 Subject: [PATCH 18/28] fix: remove where filter top queries --- collector/pg_top_queries_execution.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/collector/pg_top_queries_execution.go b/collector/pg_top_queries_execution.go index 1ac043a15..b87000620 100644 --- a/collector/pg_top_queries_execution.go +++ b/collector/pg_top_queries_execution.go @@ -62,12 +62,6 @@ var ( FROM pg_stat_statements JOIN pg_database ON pg_database.id = pg_stat_statements.dbid - WHERE - total_time > ( - SELECT percentile_cont(0.1) - WITHIN GROUP (ORDER BY total_time) - FROM pg_stat_statements - ) ORDER BY total_seconds DESC LIMIT 100;` ) From 25a4d762b85b43bbe0cbc63586d47d304e0a1c73 Mon Sep 17 00:00:00 2001 From: Adrien ZAGABE Date: Tue, 25 Jul 2023 18:01:02 +0200 Subject: [PATCH 19/28] fix: remove unexisting label --- collector/pg_top_queries_execution.go | 32 ++++++++------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/collector/pg_top_queries_execution.go b/collector/pg_top_queries_execution.go index b87000620..fc42495d7 100644 --- a/collector/pg_top_queries_execution.go +++ b/collector/pg_top_queries_execution.go @@ -26,33 +26,31 @@ var ( statTotalExecutionTime = prometheus.NewDesc( prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "total_seconds"), "Total time spent in the statement, in milliseconds", - []string{"queryid", "datname", "user"}, + []string{"queryid"}, prometheus.Labels{}, ) statMinimumExecutionTime = prometheus.NewDesc( prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "min_time_seconds"), "Minimum time spent in the statement, in milliseconds", - []string{"queryid", "datname", "user"}, + []string{"queryid"}, prometheus.Labels{}, ) statMaximumExecutionTime = prometheus.NewDesc( prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "max_time_seconds"), "Maximum time spent in the statement, in milliseconds", - []string{"queryid", "datname", "user"}, + []string{"queryid"}, prometheus.Labels{}, ) statMeanExecutionTime = prometheus.NewDesc( prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "mean_time"), "Mean time spent in the statement, in milliseconds", - []string{"queryid", "datname", "user"}, + []string{"queryid"}, prometheus.Labels{}, ) statTopQueryExecutionQuery = `SELECT - pg_get_userbyid(userid) as user, - pg_database.datname, pg_stat_statements.queryid, pg_stat_statements.mean_time / 1000.0 as mean_time, pg_stat_statements.total_time / 1000.0 as total_seconds, @@ -60,8 +58,6 @@ var ( pg_stat_statements.max_time / 1000.0 as max_time_seconds, pg_stat_statements.calls FROM pg_stat_statements - JOIN pg_database - ON pg_database.id = pg_stat_statements.dbid ORDER BY total_seconds DESC LIMIT 100;` ) @@ -84,16 +80,6 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta return err } - userLabel := "unknown" - if user.Valid { - userLabel = user.String - } - - datnameLabel := "unknown" - if datName.Valid { - datnameLabel = datName.String - } - queryIdLabel := "unknown" if queryid.Valid { queryIdLabel = queryid.String @@ -107,7 +93,7 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta statTotalExecutionTime, prometheus.CounterValue, totalSecondsMetric, - userLabel, datnameLabel, queryIdLabel, + queryIdLabel, ) meanTimeSecondsMetric := 0.0 @@ -118,7 +104,7 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta statMeanExecutionTime, prometheus.CounterValue, meanTimeSecondsMetric, - userLabel, datnameLabel, queryIdLabel, + queryIdLabel, ) minTimeSecondsMetric := 0.0 @@ -129,7 +115,7 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta statMinimumExecutionTime, prometheus.CounterValue, minTimeSecondsMetric, - userLabel, datnameLabel, queryIdLabel, + queryIdLabel, ) maxTimeSecondsMetric := 0.0 @@ -140,7 +126,7 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta statMaximumExecutionTime, prometheus.CounterValue, maxTimeSecondsMetric, - userLabel, datnameLabel, queryIdLabel, + queryIdLabel, ) callsMetric := 0.0 @@ -152,7 +138,7 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta statTotalExecutionTime, prometheus.CounterValue, callsMetric, - userLabel, datnameLabel, queryIdLabel, + queryIdLabel, ) } return nil From 4214b458f93038d82cfa76754cacc69aa0561cef Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Wed, 26 Jul 2023 09:37:00 +0200 Subject: [PATCH 20/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 9d5785f25..ed9e071fa 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -4,7 +4,9 @@ name: Build projet on: - workflow_dispatch: + push: + tags: + - 'v*.*.*.*' jobs: build: From efc0c54495d836d9c58ee1a572d629f8f41b870c Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Wed, 26 Jul 2023 09:47:07 +0200 Subject: [PATCH 21/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index ed9e071fa..5376d967c 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -56,8 +56,8 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: - tag_name: 'run-${{ github.run_id }}' - release_name: Release run-${{ github.run_id }} + tag_name: ${{ github.ref }} + release_name: Release ${{ github.ref }} draft: false prerelease: false From 03faea5aa34e47873b44a74bc5cae48925ddb381 Mon Sep 17 00:00:00 2001 From: Adrien ZAGABE Date: Wed, 26 Jul 2023 10:12:17 +0200 Subject: [PATCH 22/28] fix: scan rows query --- .github/workflows/golangci-lint.yml | 32 --------------------------- collector/pg_top_queries_execution.go | 18 +++++++-------- 2 files changed, 9 insertions(+), 41 deletions(-) delete mode 100644 .github/workflows/golangci-lint.yml diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml deleted file mode 100644 index 433f71b88..000000000 --- a/.github/workflows/golangci-lint.yml +++ /dev/null @@ -1,32 +0,0 @@ ---- -# This action is synced from https://github.com/prometheus/prometheus -name: golangci-lint -on: - push: - paths: - - "go.sum" - - "go.mod" - - "**.go" - - "scripts/errcheck_excludes.txt" - - ".github/workflows/golangci-lint.yml" - - ".golangci.yml" - pull_request: - -jobs: - golangci: - name: lint - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v3 - - name: install Go - uses: actions/setup-go@v3 - with: - go-version: 1.20.x - - name: Install snmp_exporter/generator dependencies - run: sudo apt-get update && sudo apt-get -y install libsnmp-dev - if: github.repository == 'prometheus/snmp_exporter' - - name: Lint - uses: golangci/golangci-lint-action@v3.4.0 - with: - version: v1.53.3 diff --git a/collector/pg_top_queries_execution.go b/collector/pg_top_queries_execution.go index fc42495d7..861350863 100644 --- a/collector/pg_top_queries_execution.go +++ b/collector/pg_top_queries_execution.go @@ -11,7 +11,7 @@ import ( const statTopQueriesExecutionTime = "stat_top_queries" func init() { - registerCollector(statTopQueriesExecutionTime, true, NewPGStatTopQueriesExecutionTime) + registerCollector(statTopQueriesExecutionTime, defaultEnabled, NewPGStatTopQueriesExecutionTime) } type PGStatTopQueriesExecutionTime struct { @@ -51,12 +51,12 @@ var ( ) statTopQueryExecutionQuery = `SELECT - pg_stat_statements.queryid, - pg_stat_statements.mean_time / 1000.0 as mean_time, - pg_stat_statements.total_time / 1000.0 as total_seconds, - pg_stat_statements.min_time / 1000.0 as min_time_seconds, - pg_stat_statements.max_time / 1000.0 as max_time_seconds, - pg_stat_statements.calls + queryid, + mean_time / 1000.0 as mean_time, + total_time / 1000.0 as total_seconds, + min_time / 1000.0 as min_time_seconds, + max_time / 1000.0 as max_time_seconds, + calls FROM pg_stat_statements ORDER BY total_seconds DESC LIMIT 100;` @@ -72,11 +72,11 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta defer rows.Close() for rows.Next() { - var user, datName, queryid sql.NullString + var queryid sql.NullString var meanTime, totalSeconds, minTimeSeconds, maxTimeSeconds sql.NullFloat64 var calls sql.NullInt64 - if err := rows.Scan(&user, &datName, &meanTime, &totalSeconds, &minTimeSeconds, &maxTimeSeconds, &calls); err != nil { + if err := rows.Scan(&queryid, &meanTime, &totalSeconds, &minTimeSeconds, &maxTimeSeconds, &calls); err != nil { return err } From 6734294ae145ad908e39993fbe16e8087845c0c0 Mon Sep 17 00:00:00 2001 From: Adrien ZAGABE Date: Wed, 26 Jul 2023 10:50:55 +0200 Subject: [PATCH 23/28] fix: callsMetric --- collector/pg_top_queries_execution.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/collector/pg_top_queries_execution.go b/collector/pg_top_queries_execution.go index 861350863..affd296b7 100644 --- a/collector/pg_top_queries_execution.go +++ b/collector/pg_top_queries_execution.go @@ -50,6 +50,13 @@ var ( prometheus.Labels{}, ) + statExecutedCalls = prometheus.NewDesc( + prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "calls"), + "Number of times executed", + []string{"queryid"}, + prometheus.Labels{}, + ) + statTopQueryExecutionQuery = `SELECT queryid, mean_time / 1000.0 as mean_time, @@ -135,7 +142,7 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta } ch <- prometheus.MustNewConstMetric( - statTotalExecutionTime, + statExecutedCalls, prometheus.CounterValue, callsMetric, queryIdLabel, From b61f80e340710dd09a63a03e75e9a0c4e980fbeb Mon Sep 17 00:00:00 2001 From: Thomas Rodriguez Date: Wed, 26 Jul 2023 11:00:26 +0200 Subject: [PATCH 24/28] fix: set limit to 1 --- collector/pg_top_queries_execution.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collector/pg_top_queries_execution.go b/collector/pg_top_queries_execution.go index affd296b7..88c27fb8a 100644 --- a/collector/pg_top_queries_execution.go +++ b/collector/pg_top_queries_execution.go @@ -66,7 +66,7 @@ var ( calls FROM pg_stat_statements ORDER BY total_seconds DESC - LIMIT 100;` + LIMIT 1;` ) func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error { From b23d04f837ede16f00b2a1f6b4f34264eeaf1a5c Mon Sep 17 00:00:00 2001 From: trodsre <125902721+trodsre@users.noreply.github.com> Date: Wed, 26 Jul 2023 11:09:09 +0200 Subject: [PATCH 25/28] Update go.yml Signed-off-by: trodsre <125902721+trodsre@users.noreply.github.com> --- .github/workflows/go.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 5376d967c..3270a05db 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -4,6 +4,7 @@ name: Build projet on: + workflow_dispatch: push: tags: - 'v*.*.*.*' From 35e93f31672ee7bf442a3102860574fd6d0f39aa Mon Sep 17 00:00:00 2001 From: Adrien ZAGABE Date: Wed, 26 Jul 2023 12:21:04 +0200 Subject: [PATCH 26/28] feat: add label --- collector/pg_top_queries_execution.go | 36 +++++++++++++++++++++------ 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/collector/pg_top_queries_execution.go b/collector/pg_top_queries_execution.go index 88c27fb8a..8540633df 100644 --- a/collector/pg_top_queries_execution.go +++ b/collector/pg_top_queries_execution.go @@ -26,27 +26,27 @@ var ( statTotalExecutionTime = prometheus.NewDesc( prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "total_seconds"), "Total time spent in the statement, in milliseconds", - []string{"queryid"}, + []string{"queryid", "query", "user"}, prometheus.Labels{}, ) statMinimumExecutionTime = prometheus.NewDesc( prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "min_time_seconds"), "Minimum time spent in the statement, in milliseconds", - []string{"queryid"}, + []string{"queryid", "query", "user"}, prometheus.Labels{}, ) statMaximumExecutionTime = prometheus.NewDesc( prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "max_time_seconds"), "Maximum time spent in the statement, in milliseconds", - []string{"queryid"}, + []string{"queryid", "query", "user"}, prometheus.Labels{}, ) statMeanExecutionTime = prometheus.NewDesc( prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "mean_time"), "Mean time spent in the statement, in milliseconds", - []string{"queryid"}, + []string{"queryid", "query", "user"}, prometheus.Labels{}, ) @@ -59,6 +59,8 @@ var ( statTopQueryExecutionQuery = `SELECT queryid, + query + pg_get_userbyid(userid) as user, mean_time / 1000.0 as mean_time, total_time / 1000.0 as total_seconds, min_time / 1000.0 as min_time_seconds, @@ -66,7 +68,7 @@ var ( calls FROM pg_stat_statements ORDER BY total_seconds DESC - LIMIT 1;` + LIMIT 10;` ) func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error { @@ -79,11 +81,11 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta defer rows.Close() for rows.Next() { - var queryid sql.NullString + var queryid, query, user sql.NullString var meanTime, totalSeconds, minTimeSeconds, maxTimeSeconds sql.NullFloat64 var calls sql.NullInt64 - if err := rows.Scan(&queryid, &meanTime, &totalSeconds, &minTimeSeconds, &maxTimeSeconds, &calls); err != nil { + if err := rows.Scan(&queryid, &query, &user, &meanTime, &totalSeconds, &minTimeSeconds, &maxTimeSeconds, &calls); err != nil { return err } @@ -92,6 +94,16 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta queryIdLabel = queryid.String } + queryLabel := "unknown" + if query.Valid { + queryIdLabel = query.String + } + + userLabel := "unknown" + if user.Valid { + userLabel = user.String + } + totalSecondsMetric := 0.0 if totalSeconds.Valid { totalSecondsMetric = totalSeconds.Float64 @@ -101,6 +113,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta prometheus.CounterValue, totalSecondsMetric, queryIdLabel, + queryLabel, + userLabel, ) meanTimeSecondsMetric := 0.0 @@ -112,6 +126,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta prometheus.CounterValue, meanTimeSecondsMetric, queryIdLabel, + queryLabel, + userLabel, ) minTimeSecondsMetric := 0.0 @@ -123,6 +139,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta prometheus.CounterValue, minTimeSecondsMetric, queryIdLabel, + queryLabel, + userLabel, ) maxTimeSecondsMetric := 0.0 @@ -134,6 +152,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta prometheus.CounterValue, maxTimeSecondsMetric, queryIdLabel, + queryLabel, + userLabel, ) callsMetric := 0.0 @@ -146,6 +166,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta prometheus.CounterValue, callsMetric, queryIdLabel, + queryLabel, + userLabel, ) } return nil From 4d928461227e3702ce03f70fc429a593140fc4b0 Mon Sep 17 00:00:00 2001 From: Adrien ZAGABE Date: Wed, 26 Jul 2023 12:22:27 +0200 Subject: [PATCH 27/28] feat: add label v2 --- VERSION | 2 +- collector/pg_top_queries_execution.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/VERSION b/VERSION index c317a9189..883dcff5e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.13.1 +0.13.1.0 diff --git a/collector/pg_top_queries_execution.go b/collector/pg_top_queries_execution.go index 8540633df..a99b75b73 100644 --- a/collector/pg_top_queries_execution.go +++ b/collector/pg_top_queries_execution.go @@ -53,13 +53,13 @@ var ( statExecutedCalls = prometheus.NewDesc( prometheus.BuildFQName(namespace, statTopQueriesExecutionTime, "calls"), "Number of times executed", - []string{"queryid"}, + []string{"queryid", "query", "user"}, prometheus.Labels{}, ) statTopQueryExecutionQuery = `SELECT queryid, - query + query, pg_get_userbyid(userid) as user, mean_time / 1000.0 as mean_time, total_time / 1000.0 as total_seconds, From 57217e2aaca2f0a6eef4eba283ddc92533e49262 Mon Sep 17 00:00:00 2001 From: Adrien ZAGABE Date: Wed, 26 Jul 2023 12:28:03 +0200 Subject: [PATCH 28/28] fix: reverse --- VERSION | 2 +- collector/pg_top_queries_execution.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index 883dcff5e..2f7bb426d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.13.1.0 +0.13.1.1 diff --git a/collector/pg_top_queries_execution.go b/collector/pg_top_queries_execution.go index a99b75b73..9758d7c50 100644 --- a/collector/pg_top_queries_execution.go +++ b/collector/pg_top_queries_execution.go @@ -96,7 +96,7 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta queryLabel := "unknown" if query.Valid { - queryIdLabel = query.String + queryLabel = query.String } userLabel := "unknown"