pangolin_exporter

Форк
0
/
pg_long_running_transactions.go 
93 строки · 2.5 Кб
1
// Copyright 2023 The Prometheus Authors
2
// Licensed under the Apache License, Version 2.0 (the "License");
3
// you may not use this file except in compliance with the License.
4
// You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13

14
package collector
15

16
import (
17
	"context"
18

19
	"github.com/go-kit/log"
20
	"github.com/prometheus/client_golang/prometheus"
21
)
22

23
const longRunningTransactionsSubsystem = "long_running_transactions"
24

25
func init() {
26
	registerCollector(longRunningTransactionsSubsystem, defaultDisabled, NewPGLongRunningTransactionsCollector)
27
}
28

29
type PGLongRunningTransactionsCollector struct {
30
	log log.Logger
31
}
32

33
func NewPGLongRunningTransactionsCollector(config collectorConfig) (Collector, error) {
34
	return &PGLongRunningTransactionsCollector{log: config.logger}, nil
35
}
36

37
var (
38
	longRunningTransactionsCount = prometheus.NewDesc(
39
		"pg_long_running_transactions",
40
		"Current number of long running transactions",
41
		[]string{},
42
		prometheus.Labels{},
43
	)
44

45
	longRunningTransactionsAgeInSeconds = prometheus.NewDesc(
46
		prometheus.BuildFQName(namespace, longRunningTransactionsSubsystem, "oldest_timestamp_seconds"),
47
		"The current maximum transaction age in seconds",
48
		[]string{},
49
		prometheus.Labels{},
50
	)
51

52
	longRunningTransactionsQuery = `
53
	SELECT
54
		COUNT(*) as transactions,
55
   		MAX(EXTRACT(EPOCH FROM clock_timestamp())) AS oldest_timestamp_seconds
56
    FROM pg_catalog.pg_stat_activity
57
    WHERE state is distinct from 'idle' AND query not like 'autovacuum:%'
58
	`
59
)
60

61
func (PGLongRunningTransactionsCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
62
	db := instance.getDB()
63
	rows, err := db.QueryContext(ctx,
64
		longRunningTransactionsQuery)
65

66
	if err != nil {
67
		return err
68
	}
69
	defer rows.Close()
70

71
	for rows.Next() {
72
		var transactions, ageInSeconds float64
73

74
		if err := rows.Scan(&transactions, &ageInSeconds); err != nil {
75
			return err
76
		}
77

78
		ch <- prometheus.MustNewConstMetric(
79
			longRunningTransactionsCount,
80
			prometheus.GaugeValue,
81
			transactions,
82
		)
83
		ch <- prometheus.MustNewConstMetric(
84
			longRunningTransactionsAgeInSeconds,
85
			prometheus.GaugeValue,
86
			ageInSeconds,
87
		)
88
	}
89
	if err := rows.Err(); err != nil {
90
		return err
91
	}
92
	return nil
93
}
94

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.