Skip to content

Commit

Permalink
Merge pull request #19 from askuy/feature/slowlog20240625
Browse files Browse the repository at this point in the history
slowlog
  • Loading branch information
askuy authored Jun 26, 2024
2 parents a984e44 + 8c47846 commit c0f85f1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
34 changes: 21 additions & 13 deletions interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ func metricInterceptor(compName string, dsn *manager.DSN, op string, config *con

loggerKeys := transport.CustomContextKeys()

var fields = make([]elog.Field, 0, 15+len(loggerKeys))
var fields = make([]elog.Field, 0, 15+transport.CustomContextKeysLength())
event := "normal"
fields = append(fields,
elog.FieldMethod(op),
elog.FieldName(dsn.DBName+"."+db.Statement.Table), elog.FieldCost(cost))
elog.FieldName(dsn.DBName+"."+db.Statement.Table),
elog.FieldCost(cost),
)
if config.EnableAccessInterceptorReq {
fields = append(fields, elog.String("req", logSQL(db, config.EnableDetailSQL)))
}
Expand All @@ -84,7 +87,7 @@ func metricInterceptor(compName string, dsn *manager.DSN, op string, config *con
}

// 开启了链路,那么就记录链路id
if config.EnableTraceInterceptor && etrace.IsGlobalTracerRegistered() {
if etrace.IsGlobalTracerRegistered() {
fields = append(fields, elog.FieldTid(etrace.ExtractTraceID(db.Statement.Context)))
}

Expand All @@ -97,15 +100,18 @@ func metricInterceptor(compName string, dsn *manager.DSN, op string, config *con

// 记录监控耗时
emetric.ClientHandleHistogram.WithLabelValues(emetric.TypeGorm, compName, dsn.DBName+"."+db.Statement.Table, dsn.Addr).Observe(cost.Seconds())

// 如果有慢日志,就记录
isSlowLog := false
if config.SlowLogThreshold > time.Duration(0) && config.SlowLogThreshold < cost {
logger.Warn("slow", fields...)
event = "slow"
isSlowLog = true
}

// 如果有错误,记录错误信息
if db.Error != nil {
fields = append(fields, elog.FieldEvent("error"), elog.FieldErr(db.Error))
fields = append(fields,
elog.FieldEvent(event),
elog.FieldErr(db.Error),
)
if errors.Is(db.Error, ErrRecordNotFound) {
logger.Warn("access", fields...)
emetric.ClientHandleCounter.Inc(emetric.TypeGorm, compName, dsn.DBName+"."+db.Statement.Table, dsn.Addr, "Empty")
Expand All @@ -117,13 +123,16 @@ func metricInterceptor(compName string, dsn *manager.DSN, op string, config *con
}

emetric.ClientHandleCounter.Inc(emetric.TypeGorm, compName, dsn.DBName+"."+db.Statement.Table, dsn.Addr, "OK")
// 开启了记录日志信息,那么就记录access
// event normal和error,代表全部access的请求数
if config.EnableAccessInterceptor {

if config.EnableAccessInterceptor || isSlowLog {
fields = append(fields,
elog.FieldEvent("normal"),
elog.FieldEvent(event),
)
logger.Info("access", fields...)
if isSlowLog {
logger.Warn("access", fields...)
} else {
logger.Info("access", fields...)
}
}
}
}
Expand Down Expand Up @@ -153,7 +162,6 @@ func traceInterceptor(compName string, dsn *manager.DSN, _ string, options *conf
if len(db.Statement.BuildClauses) > 0 {
operation += strings.ToLower(db.Statement.BuildClauses[0])
}

_, span := tracer.Start(db.Statement.Context, operation, nil, trace.WithAttributes(attrs...))
defer span.End()
comment := fmt.Sprintf("tid=%s", span.SpanContext().TraceID().String())
Expand Down
20 changes: 10 additions & 10 deletions stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ func monitor() {
elog.EgoLogger.With(elog.FieldComponent(PackageName)).Panic("monitor db error", elog.FieldErr(err))
return false
}
stats := sqlDB.Stats()
sqlStats := sqlDB.Stats()
// Gauge指标
emetric.ClientStatsGauge.Set(float64(stats.MaxOpenConnections), emetric.TypeGorm, name, "max_open_connections")
emetric.ClientStatsGauge.Set(float64(stats.OpenConnections), emetric.TypeGorm, name, "open_connections")
emetric.ClientStatsGauge.Set(float64(stats.InUse), emetric.TypeGorm, name, "in_use")
emetric.ClientStatsGauge.Set(float64(stats.Idle), emetric.TypeGorm, name, "idle")
emetric.ClientStatsGauge.Set(float64(stats.MaxIdleClosed), emetric.TypeGorm, name, "max_idle_closed")
emetric.ClientStatsGauge.Set(float64(stats.MaxIdleTimeClosed), emetric.TypeGorm, name, "max_idle_time_closed")
emetric.ClientStatsGauge.Set(float64(stats.MaxLifetimeClosed), emetric.TypeGorm, name, "max_lifetime_closed")
emetric.ClientStatsGauge.Set(float64(sqlStats.MaxOpenConnections), emetric.TypeGorm, name, "max_open_connections")
emetric.ClientStatsGauge.Set(float64(sqlStats.OpenConnections), emetric.TypeGorm, name, "open_connections")
emetric.ClientStatsGauge.Set(float64(sqlStats.InUse), emetric.TypeGorm, name, "in_use")
emetric.ClientStatsGauge.Set(float64(sqlStats.Idle), emetric.TypeGorm, name, "idle")
emetric.ClientStatsGauge.Set(float64(sqlStats.MaxIdleClosed), emetric.TypeGorm, name, "max_idle_closed")
emetric.ClientStatsGauge.Set(float64(sqlStats.MaxIdleTimeClosed), emetric.TypeGorm, name, "max_idle_time_closed")
emetric.ClientStatsGauge.Set(float64(sqlStats.MaxLifetimeClosed), emetric.TypeGorm, name, "max_lifetime_closed")
// 以下数据为db里的累加值,如果要看瞬时的,需要在metric里使用irate
emetric.ClientStatsGauge.Set(float64(stats.WaitCount), emetric.TypeGorm, name, "wait_count")
emetric.ClientStatsGauge.Set(float64(stats.WaitDuration.Milliseconds()/1000), emetric.TypeGorm, name, "wait_duration")
emetric.ClientStatsGauge.Set(float64(sqlStats.WaitCount), emetric.TypeGorm, name, "wait_count")
emetric.ClientStatsGauge.Set(float64(sqlStats.WaitDuration.Milliseconds()/1000), emetric.TypeGorm, name, "wait_duration")
return true
})
time.Sleep(time.Second * 10)
Expand Down

0 comments on commit c0f85f1

Please sign in to comment.