Slide 40
Slide 40 text
© DMM
type Store struct {
UserID string
TraceID string
TotalExternalDuration time.Duration
}
type storeKeyType struct{}
var storeKey = storeKeyType{}
40
func MiddlewareDuration(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
start := time.Now()
store := &Store{}
ctx := context.WithValue(ctx, storeKey, store)
next.ServeHTTP(rw, r.WithContext(ctx))
wholeDuration := time.Now().Sub(start)
internalDuration := wholeDuration - store.TotalExternalDuration
if span, ok := tracer.SpanFromContext(r.Context()); ok {
span.SetTag("internal.duration", internalDuration.Microseconds())
}
})
}
ミドルウェアで全体処理時間の計測を開始
①Contextに格納して伝搬する値
をまとめた構造体と
そのKeyを定義する
⚠可変の値として外部処理時間の総
計が含まれる
②全体処理時間の計測開始
④ContextにValue(*Store)を
セット
③Storeのポインタを生成