pangolin_exporter

Форк
0
/
pangolin_info.go 
163 строки · 4.4 Кб
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
	"database/sql"
19
	"math"
20

21
	"github.com/go-kit/log"
22
	"github.com/prometheus/client_golang/prometheus"
23
)
24

25
const InfoPangolinSubsystem = ""
26

27
func init() {
28
	registerCollector(InfoPangolinSubsystem, defaultEnabled, NewPGInfoPangolinCollector)
29
}
30

31
type PGInfoPangolinCollector struct {
32
	log log.Logger
33
}
34

35
func NewPGInfoPangolinCollector(config collectorConfig) (Collector, error) {
36
	return &PGInfoPangolinCollector{log: config.logger}, nil
37
}
38

39
var (
40
	InfoPangolinVersion = prometheus.NewDesc(
41
		prometheus.BuildFQName(namespace_pangolin, InfoPangolinSubsystem, "version"),
42
		"Pangolin version",
43
		[]string{"product_version"},
44
		prometheus.Labels{},
45
	)
46
	statPgProfile = prometheus.NewDesc(
47
		prometheus.BuildFQName(namespace_pangolin, InfoPangolinSubsystem, "pg_profile_enable"),
48
		"PG_Profile enable",
49
		[]string{},
50
		prometheus.Labels{},
51
	)
52
	statPerformanceInsights = prometheus.NewDesc(
53
		prometheus.BuildFQName(namespace_pangolin, InfoPangolinSubsystem, "performance_insights_enable"),
54
		"Performance Insights enable",
55
		[]string{},
56
		prometheus.Labels{},
57
	)
58

59
	pgInfoPangolinQuery = `select product_version from product_version();`
60
	pgProfileQuery      = `select coalesce(nullif(current_setting( 'pg_profile.is_enable', true ),''), 'False') as on,
61
								coalesce((select EXTRACT(EPOCH FROM clock_timestamp() - sample_time)::text as sample_old
62
							from pgse_profile.samples order by sample_time desc limit 1),'0')::float;`
63
	PerformanceInsightsQuery = `select coalesce(nullif(current_setting( 'performance_insights.enable', true ),''), 'False'), coalesce((select EXTRACT(EPOCH FROM clock_timestamp() - sample_time)::text as sample_old from pg_stat_get_activity_history_last(NULL)),'0')::float;`
64
)
65

66
func (c PGInfoPangolinCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
67

68
	db := instance.getDB()
69
	rows, err := db.QueryContext(ctx, pgInfoPangolinQuery)
70
	if err != nil {
71
		return err
72
	}
73

74
	defer rows.Close()
75
	for rows.Next() {
76
		var product_version sql.NullString
77

78
		if err := rows.Scan(&product_version); err != nil {
79
			return err
80
		}
81

82
		product_versionLabel := "unknown"
83
		upMetric := 0.0
84
		if product_version.Valid {
85
			product_versionLabel = product_version.String
86
			upMetric = 1
87
		} else {
88
			upMetric = 0
89
		}
90

91
		ch <- prometheus.MustNewConstMetric(
92
			InfoPangolinVersion,
93
			prometheus.CounterValue,
94
			upMetric,
95
			product_versionLabel,
96
		)
97

98
	}
99

100
	rows, err = db.QueryContext(ctx, pgProfileQuery)
101
	if err != nil {
102
		return err
103
	}
104

105
	defer rows.Close()
106
	for rows.Next() {
107
		var sample_old sql.NullFloat64
108
		var pg_profile_on sql.NullString
109

110
		if err := rows.Scan(&pg_profile_on, &sample_old); err != nil {
111
			return err
112
		}
113

114
		sample_oldLabel := math.NaN()
115
		if pg_profile_on.String == "On" || pg_profile_on.String == "on" ||
116
			pg_profile_on.String == "True" || pg_profile_on.String == "true" {
117
			sample_oldLabel = float64(sample_old.Float64)
118
		} else {
119
			sample_oldLabel = -1
120
		}
121

122
		ch <- prometheus.MustNewConstMetric(
123
			statPgProfile,
124
			prometheus.CounterValue,
125
			sample_oldLabel,
126
		)
127

128
	}
129
	rows, err = db.QueryContext(ctx, PerformanceInsightsQuery)
130
	if err != nil {
131
		return err
132
	}
133

134
	defer rows.Close()
135
	for rows.Next() {
136
		var sample_old sql.NullFloat64
137
		var performance_insights_on sql.NullString
138

139
		if err := rows.Scan(&performance_insights_on, &sample_old); err != nil {
140
			return err
141
		}
142

143
		sample_oldLabel := math.NaN()
144
		if performance_insights_on.String == "On" || performance_insights_on.String == "on" ||
145
			performance_insights_on.String == "True" || performance_insights_on.String == "true" {
146
			sample_oldLabel = float64(sample_old.Float64)
147
		} else {
148
			sample_oldLabel = -1
149
		}
150

151
		ch <- prometheus.MustNewConstMetric(
152
			statPerformanceInsights,
153
			prometheus.CounterValue,
154
			sample_oldLabel,
155
		)
156

157
	}
158

159
	if err := rows.Err(); err != nil {
160
		return err
161
	}
162
	return nil
163
}
164

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

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

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

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