/
githubmirror
/
grafana
Обзор
Документация
Войти
/
githubmirror
/
grafana
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
pkg/infra/log/databaseCounter.go
44 строки
941 B
Emil Tullstedt
Chore: Upgrade Go to 1.19.1 (#54902)
12 сен 2022, 13:03
Не верифицирован
12 сен 2022, 13:03
b287047
Код
Авторство
О чём код?
package log import ( "context" "sync/atomic" ) type contextKey struct{} var dbCallCounterNameKey = contextKey{} // InitCounter creates a pointer on the context that can be incremented later func InitCounter(ctx context.Context) context.Context { var ptr = new(int64) return context.WithValue(ctx, dbCallCounterNameKey, ptr) } // IncDBCallCounter increments the database counter on the context. func IncDBCallCounter(ctx context.Context) context.Context { if val := ctx.Value(dbCallCounterNameKey); val == nil { ctx = InitCounter(ctx) } if val := ctx.Value(dbCallCounterNameKey); val != nil { v2, ok := val.(*int64) if ok { atomic.AddInt64(v2, 1) } } return ctx } // TotalDBCallCount returns the total number of requests for the context func TotalDBCallCount(ctx context.Context) int64 { if val := ctx.Value(dbCallCounterNameKey); val != nil { v2, ok := val.(*int64) if ok { return *v2 } } return 0 }