Skip to content

Commit 93116f5

Browse files
committed
add database histogram
1 parent d310cd0 commit 93116f5

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

models/db/engine_hook.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"code.gitea.io/gitea/modules/gtprof"
1111
"code.gitea.io/gitea/modules/log"
1212
"code.gitea.io/gitea/modules/setting"
13+
"github.com/prometheus/client_golang/prometheus"
14+
"github.com/prometheus/client_golang/prometheus/promauto"
1315

1416
"xorm.io/xorm/contexts"
1517
)
@@ -21,12 +23,22 @@ type EngineHook struct {
2123

2224
var _ contexts.Hook = (*EngineHook)(nil)
2325

26+
// follows: https://opentelemetry.io/docs/specs/semconv/database/database-metrics/#metric-dbclientoperationduration
27+
var durationHistogram = promauto.NewHistogram(prometheus.HistogramOpts{
28+
Namespace: "db",
29+
Subsystem: "client",
30+
Name: "operation.duration",
31+
Help: "Duration of database client operations.",
32+
// ConstLabels: prometheus.Labels{"db.system.name": BuilderDialect()}, //TODO: add type of database per spec.
33+
})
34+
2435
func (*EngineHook) BeforeProcess(c *contexts.ContextHook) (context.Context, error) {
2536
ctx, _ := gtprof.GetTracer().Start(c.Ctx, gtprof.TraceSpanDatabase)
2637
return ctx, nil
2738
}
2839

2940
func (h *EngineHook) AfterProcess(c *contexts.ContextHook) error {
41+
durationHistogram.Observe(c.ExecuteTime.Seconds())
3042
span := gtprof.GetContextSpan(c.Ctx)
3143
if span != nil {
3244
// Do not record SQL parameters here:

0 commit comments

Comments
 (0)