Skip to content

Commit acf5121

Browse files
authored
Merge pull request #53 from vishal-dt/main
oss sync
2 parents 2a59eac + 80b6631 commit acf5121

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

internal/middleware/instrument.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ var (
3333
}, []string{"path", "method", "status"})
3434
)
3535

36+
var PgQueryDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
37+
Name: "pg_query_duration_seconds",
38+
Help: "Duration of PG queries",
39+
}, []string{"label"})
40+
3641
var CdDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
3742
Name: "cd_duration_seconds",
3843
Help: "Duration of CD process",

pkg/sql/connection.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package sql
1919

2020
import (
21+
"github.com/devtron-labs/devtron/internal/middleware"
2122
"go.uber.org/zap"
2223
"reflect"
2324
"time"
@@ -35,6 +36,7 @@ type Config struct {
3536
ApplicationName string `env:"APP" envDefault:"orchestrator"`
3637
LogQuery bool `env:"PG_LOG_QUERY" envDefault:"true"`
3738
LogAllQuery bool `env:"PG_LOG_ALL_QUERY" envDefault:"false"`
39+
ExportPromMetrics bool `env:"PG_EXPORT_PROM_METRICS" envDefault:"false"`
3840
QueryDurationThreshold int64 `env:"PG_QUERY_DUR_THRESHOLD" envDefault:"5000"`
3941
}
4042

@@ -69,14 +71,19 @@ func NewDbConnection(cfg *Config, logger *zap.SugaredLogger) (*pg.DB, error) {
6971

7072
dbConnection.OnQueryProcessed(func(event *pg.QueryProcessedEvent) {
7173
query, err := event.FormattedQuery()
74+
queryDuration := time.Since(event.StartTime)
75+
76+
// Expose prom metrics
77+
if cfg.ExportPromMetrics {
78+
middleware.PgQueryDuration.WithLabelValues("value").Observe(queryDuration.Seconds())
79+
}
80+
7281
if err != nil {
7382
logger.Errorw("Error formatting query",
7483
"err", err)
7584
return
7685
}
7786

78-
queryDuration := time.Since(event.StartTime)
79-
8087
if cfg.LogAllQuery || queryDuration.Milliseconds() > cfg.QueryDurationThreshold {
8188
logger.Debugw("query time",
8289
"duration", queryDuration.Seconds(),

0 commit comments

Comments
 (0)