0
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-07-19 12:50:52 +02:00

add database histogram

This commit is contained in:
TheFox0x7 2025-07-08 11:08:25 +02:00
parent d310cd08f8
commit 93116f5d47
No known key found for this signature in database
GPG Key ID: 6CA33903484AF7C2

View File

@ -10,6 +10,8 @@ import (
"code.gitea.io/gitea/modules/gtprof"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"xorm.io/xorm/contexts"
)
@ -21,12 +23,22 @@ type EngineHook struct {
var _ contexts.Hook = (*EngineHook)(nil)
// follows: https://opentelemetry.io/docs/specs/semconv/database/database-metrics/#metric-dbclientoperationduration
var durationHistogram = promauto.NewHistogram(prometheus.HistogramOpts{
Namespace: "db",
Subsystem: "client",
Name: "operation.duration",
Help: "Duration of database client operations.",
// ConstLabels: prometheus.Labels{"db.system.name": BuilderDialect()}, //TODO: add type of database per spec.
})
func (*EngineHook) BeforeProcess(c *contexts.ContextHook) (context.Context, error) {
ctx, _ := gtprof.GetTracer().Start(c.Ctx, gtprof.TraceSpanDatabase)
return ctx, nil
}
func (h *EngineHook) AfterProcess(c *contexts.ContextHook) error {
durationHistogram.Observe(c.ExecuteTime.Seconds())
span := gtprof.GetContextSpan(c.Ctx)
if span != nil {
// Do not record SQL parameters here: