Skip to content

Commit

Permalink
feat: add label
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien ZAGABE committed Jul 26, 2023
1 parent b23d04f commit 35e93f3
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions collector/pg_top_queries_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
)

Expand All @@ -59,14 +59,16 @@ 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,
max_time / 1000.0 as max_time_seconds,
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 {
Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -101,6 +113,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
prometheus.CounterValue,
totalSecondsMetric,
queryIdLabel,
queryLabel,
userLabel,
)

meanTimeSecondsMetric := 0.0
Expand All @@ -112,6 +126,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
prometheus.CounterValue,
meanTimeSecondsMetric,
queryIdLabel,
queryLabel,
userLabel,
)

minTimeSecondsMetric := 0.0
Expand All @@ -123,6 +139,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
prometheus.CounterValue,
minTimeSecondsMetric,
queryIdLabel,
queryLabel,
userLabel,
)

maxTimeSecondsMetric := 0.0
Expand All @@ -134,6 +152,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
prometheus.CounterValue,
maxTimeSecondsMetric,
queryIdLabel,
queryLabel,
userLabel,
)

callsMetric := 0.0
Expand All @@ -146,6 +166,8 @@ func (PGStatTopQueriesExecutionTime) Update(ctx context.Context, instance *insta
prometheus.CounterValue,
callsMetric,
queryIdLabel,
queryLabel,
userLabel,
)
}
return nil
Expand Down

0 comments on commit 35e93f3

Please sign in to comment.