go-server-timing icon indicating copy to clipboard operation
go-server-timing copied to clipboard

Increment metric duration

Open bondepike opened this issue 1 year ago • 0 comments

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.

bondepike avatar Dec 08 '23 11:12 bondepike