go-server-timing
go-server-timing copied to clipboard
Increment metric duration
This change allows for incrementing the metric duration. Good use case for aggregating compute duration for specific methods in a recursive environment. Which is what I use it for.
func recusiveThing(ctx context.Context, id string) {
//... many other compute
timing := servertiming.FromContext(ctx)
dt := timing.NewMetric("some_compute").Start()
doSomeComputeForThisThing(ctx, id)
dt.End()
subThings := findSubThingsForThisThing(ctx, id)
for i := range subThings {
recursiveThing(ctx, subThings[i].id)
}
}
// how much time did we spend with doSomeComputeForThisThing in total?
Please consider if you want to alter the functionality before merging, as it may be a breaking change for some who rely upon the current implementation logic.