func NewHTTPTraceLogger(ctx context.Context, fileUrl string, fragmentID, attempt int) context.Context {traceStart := time.Now()var dnsStart, connectStart, tlsStart, gotConnTime time.Timetrace := &httptrace.ClientTrace{DNSStart: func(info httptrace.DNSStartInfo) {dnsStart = time.Now()},DNSDone: func(info httptrace.DNSDoneInfo) {g.Log().Infof(ctx, "[trace] fileUrl=%s fragment=%d attempt=%d DNSDone: %+v cost=%v",fileUrl, fragmentID, attempt, info, time.Since(dnsStart))},ConnectStart: func(network, addr string) {connectStart = time.Now()},ConnectDone: func(network, addr string, err error) {g.Log().Infof(ctx, "[trace] fileUrl=%s fragment=%d attempt=%d ConnectDone: %s %s err=%v cost=%v",fileUrl, fragmentID, attempt, network, addr, err, time.Since(connectStart))},TLSHandshakeStart: func() {tlsStart = time.Now()},TLSHandshakeDone: func(cs tls.ConnectionState, err error) {g.Log().Infof(ctx, "[trace] fileUrl=%s fragment=%d attempt=%d TLSHandshakeDone err=%v cost=%v",fileUrl, fragmentID, attempt, err, time.Since(tlsStart))},GotConn: func(connInfo httptrace.GotConnInfo) {gotConnTime = time.Now()total := time.Since(traceStart)g.Log().Infof(ctx, "[trace] fileUrl=%s fragment=%d attempt=%d GotConn reused=%v idleTime=%v total=%v",fileUrl, fragmentID, attempt, connInfo.Reused, connInfo.IdleTime, total)},GotFirstResponseByte: func() {ttfb := time.Since(gotConnTime)g.Log().Infof(ctx, "[trace] fileUrl=%s fragment=%d attempt=%d GotFirstResponseByte TTFB=%v",fileUrl, fragmentID, attempt, ttfb)},}return httptrace.WithClientTrace(ctx, trace) }
使用:
func NewHTTPTraceLogger(ctx context.Context, fileUrl string, fragmentID, attempt int) context.Context {traceStart := time.Now()var dnsStart, connectStart, tlsStart, gotConnTime time.Timetrace := &httptrace.ClientTrace{DNSStart: func(info httptrace.DNSStartInfo) {dnsStart = time.Now()},DNSDone: func(info httptrace.DNSDoneInfo) {g.Log().Infof(ctx, "[trace] fileUrl=%s fragment=%d attempt=%d DNSDone: %+v cost=%v",fileUrl, fragmentID, attempt, info, time.Since(dnsStart))},ConnectStart: func(network, addr string) {connectStart = time.Now()},ConnectDone: func(network, addr string, err error) {g.Log().Infof(ctx, "[trace] fileUrl=%s fragment=%d attempt=%d ConnectDone: %s %s err=%v cost=%v",fileUrl, fragmentID, attempt, network, addr, err, time.Since(connectStart))},TLSHandshakeStart: func() {tlsStart = time.Now()},TLSHandshakeDone: func(cs tls.ConnectionState, err error) {g.Log().Infof(ctx, "[trace] fileUrl=%s fragment=%d attempt=%d TLSHandshakeDone err=%v cost=%v",fileUrl, fragmentID, attempt, err, time.Since(tlsStart))},GotConn: func(connInfo httptrace.GotConnInfo) {gotConnTime = time.Now()total := time.Since(traceStart)g.Log().Infof(ctx, "[trace] fileUrl=%s fragment=%d attempt=%d GotConn reused=%v idleTime=%v total=%v",fileUrl, fragmentID, attempt, connInfo.Reused, connInfo.IdleTime, total)},GotFirstResponseByte: func() {ttfb := time.Since(gotConnTime)g.Log().Infof(ctx, "[trace] fileUrl=%s fragment=%d attempt=%d GotFirstResponseByte TTFB=%v",fileUrl, fragmentID, attempt, ttfb)},}return httptrace.WithClientTrace(ctx, trace) }