samber / slog-gin

🚨 Gin middleware for slog logger
https://pkg.go.dev/github.com/samber/slog-gin
MIT License
89 stars 13 forks source link

fix: latency measurement #8

Closed frzifus closed 8 months ago

codecov-commenter commented 8 months ago

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (26cd833) 0.00% compared to head (a10c93a) 0.00%.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #8 +/- ## ===================================== Coverage 0.00% 0.00% ===================================== Files 3 3 Lines 327 328 +1 ===================================== - Misses 327 328 +1 ``` | [Flag](https://app.codecov.io/gh/samber/slog-gin/pull/8/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Berthe) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/samber/slog-gin/pull/8/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Berthe) | `0.00% <0.00%> (ø)` | | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Berthe#carryforward-flags-in-the-pull-request-comment) to find out more. | [Files](https://app.codecov.io/gh/samber/slog-gin/pull/8?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Berthe) | Coverage Δ | | |---|---|---| | [middleware.go](https://app.codecov.io/gh/samber/slog-gin/pull/8?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Berthe#diff-bWlkZGxld2FyZS5nbw==) | `0.00% <0.00%> (ø)` | |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

samber commented 8 months ago

Can you explain ?

frzifus commented 8 months ago

Sure, my understanding is that we want to measure the latency of the wrapped handler call and add it to the log line.

The follow up handler is called using c.Next(). ~To measure the execution we should get the timestamp before and after to calculate the difference.~

~If this middleware is placed before a handler func like this:~

func test(c *gin.Context) {
    time.Sleep(time.Minute)
}

~I would expect an execution time of close to 1min.~

~Before this proposed change, it would as minimal as it can be.~

update

Ups, forget about it. I noticed that start is actually generated at the beginning of the function. :facepalm:

samber commented 8 months ago

I don't think so ^^

start := time.Now()

// ...

c.Next()

end := time.Now()
latency := end.Sub(start)