pangolin_exporter
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
14package collector
15
16import (
17"context"
18"database/sql"
19"math"
20
21"github.com/go-kit/log"
22"github.com/prometheus/client_golang/prometheus"
23)
24
25const InfoPangolinSubsystem = ""
26
27func init() {
28registerCollector(InfoPangolinSubsystem, defaultEnabled, NewPGInfoPangolinCollector)
29}
30
31type PGInfoPangolinCollector struct {
32log log.Logger
33}
34
35func NewPGInfoPangolinCollector(config collectorConfig) (Collector, error) {
36return &PGInfoPangolinCollector{log: config.logger}, nil
37}
38
39var (
40InfoPangolinVersion = prometheus.NewDesc(
41prometheus.BuildFQName(namespace_pangolin, InfoPangolinSubsystem, "version"),
42"Pangolin version",
43[]string{"product_version"},
44prometheus.Labels{},
45)
46statPgProfile = prometheus.NewDesc(
47prometheus.BuildFQName(namespace_pangolin, InfoPangolinSubsystem, "pg_profile_enable"),
48"PG_Profile enable",
49[]string{},
50prometheus.Labels{},
51)
52statPerformanceInsights = prometheus.NewDesc(
53prometheus.BuildFQName(namespace_pangolin, InfoPangolinSubsystem, "performance_insights_enable"),
54"Performance Insights enable",
55[]string{},
56prometheus.Labels{},
57)
58
59pgInfoPangolinQuery = `select product_version from product_version();`
60pgProfileQuery = `select coalesce(nullif(current_setting( 'pg_profile.is_enable', true ),''), 'False') as on,
61coalesce((select EXTRACT(EPOCH FROM clock_timestamp() - sample_time)::text as sample_old
62from pgse_profile.samples order by sample_time desc limit 1),'0')::float;`
63PerformanceInsightsQuery = `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
66func (c PGInfoPangolinCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
67
68db := instance.getDB()
69rows, err := db.QueryContext(ctx, pgInfoPangolinQuery)
70if err != nil {
71return err
72}
73
74defer rows.Close()
75for rows.Next() {
76var product_version sql.NullString
77
78if err := rows.Scan(&product_version); err != nil {
79return err
80}
81
82product_versionLabel := "unknown"
83upMetric := 0.0
84if product_version.Valid {
85product_versionLabel = product_version.String
86upMetric = 1
87} else {
88upMetric = 0
89}
90
91ch <- prometheus.MustNewConstMetric(
92InfoPangolinVersion,
93prometheus.CounterValue,
94upMetric,
95product_versionLabel,
96)
97
98}
99
100rows, err = db.QueryContext(ctx, pgProfileQuery)
101if err != nil {
102return err
103}
104
105defer rows.Close()
106for rows.Next() {
107var sample_old sql.NullFloat64
108var pg_profile_on sql.NullString
109
110if err := rows.Scan(&pg_profile_on, &sample_old); err != nil {
111return err
112}
113
114sample_oldLabel := math.NaN()
115if pg_profile_on.String == "On" || pg_profile_on.String == "on" ||
116pg_profile_on.String == "True" || pg_profile_on.String == "true" {
117sample_oldLabel = float64(sample_old.Float64)
118} else {
119sample_oldLabel = -1
120}
121
122ch <- prometheus.MustNewConstMetric(
123statPgProfile,
124prometheus.CounterValue,
125sample_oldLabel,
126)
127
128}
129rows, err = db.QueryContext(ctx, PerformanceInsightsQuery)
130if err != nil {
131return err
132}
133
134defer rows.Close()
135for rows.Next() {
136var sample_old sql.NullFloat64
137var performance_insights_on sql.NullString
138
139if err := rows.Scan(&performance_insights_on, &sample_old); err != nil {
140return err
141}
142
143sample_oldLabel := math.NaN()
144if performance_insights_on.String == "On" || performance_insights_on.String == "on" ||
145performance_insights_on.String == "True" || performance_insights_on.String == "true" {
146sample_oldLabel = float64(sample_old.Float64)
147} else {
148sample_oldLabel = -1
149}
150
151ch <- prometheus.MustNewConstMetric(
152statPerformanceInsights,
153prometheus.CounterValue,
154sample_oldLabel,
155)
156
157}
158
159if err := rows.Err(); err != nil {
160return err
161}
162return nil
163}
164