grafana/pkg/infra/log/databaseCounter.go
Emil Tullstedt b287047052
Chore: Upgrade Go to 1.19.1 (#54902)
* WIP

* Set public_suffix to a pre Ruby 2.6 version

* we don't need to install python

* Stretch->Buster

* Bump versions in lib.star

* Manually update linter

Sort of messy, but the .mod-file need to contain all dependencies that
use 1.16+ features, otherwise they're assumed to be compiled with
-lang=go1.16 and cannot access generics et al.

Bingo doesn't seem to understand that, but it's possible to manually
update things to get Bingo happy.

* undo reformatting

* Various lint improvements

* More from the linter

* goimports -w ./pkg/

* Disable gocritic

* Add/modify linter exceptions

* lint + flatten nested list

Go 1.19 doesn't support nested lists, and there wasn't an obvious workaround.
https://go.dev/doc/comment#lists
2022-09-12 12:03:49 +02:00

45 lines
941 B
Go

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
}