drone-plugin-lib/trace/http.go
Thomas Boerger a20f13fdb0
Restructure the code accross more files
I have split the code into various files and beside that I have also
used more flat package structure. I have removed the whole constants for
flag names and environment variables, since nobody got to write these
flags manually anyway as all these variables are assigned to structs
all can benefit form code completion while this lib stays more readable.

Signed-off-by: Thomas Boerger <thomas@webhippie.de>
2019-12-21 21:55:01 +01:00

118 lines
3.3 KiB
Go

// Copyright (c) 2019, the Drone Plugins project authors.
// Please see the AUTHORS file for details. All rights reserved.
// Use of this source code is governed by an Apache 2.0 license that can be
// found in the LICENSE file.
package trace
import (
"context"
"crypto/tls"
"net/http/httptrace"
"net/textproto"
"github.com/sirupsen/logrus"
)
// HTTP uses httptrace to log all network activity for HTTP requests.
func HTTP(ctx context.Context) context.Context {
return httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
GetConn: func(hostPort string) {
logrus.WithField("host-port", hostPort).Trace("ClientTrace.GetConn")
},
GotConn: func(connInfo httptrace.GotConnInfo) {
logrus.WithFields(logrus.Fields{
"local-address": connInfo.Conn.LocalAddr(),
"remote-address": connInfo.Conn.RemoteAddr(),
"reused": connInfo.Reused,
"was-idle": connInfo.WasIdle,
"idle-time": connInfo.IdleTime,
}).Trace("ClientTrace.GoConn")
},
PutIdleConn: func(err error) {
logrus.WithField("error", err).Trace("ClientTrace.GoConn")
},
GotFirstResponseByte: func() {
logrus.Trace("ClientTrace.GotFirstResponseByte")
},
Got100Continue: func() {
logrus.Trace("ClientTrace.Got100Continue")
},
Got1xxResponse: func(code int, header textproto.MIMEHeader) error {
logrus.WithFields(logrus.Fields{
"code": code,
"header": header,
}).Trace("ClientTrace.Got1xxxResponse")
return nil
},
DNSStart: func(dnsInfo httptrace.DNSStartInfo) {
logrus.WithField("host", dnsInfo.Host).Trace("ClientTrace.DNSStart")
},
DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
logrus.WithFields(logrus.Fields{
"addresses": dnsInfo.Addrs,
"error": dnsInfo.Err,
"coalesced": dnsInfo.Coalesced,
}).Trace("ClientTrace.DNSDone")
},
ConnectStart: func(network, addr string) {
logrus.WithFields(logrus.Fields{
"network": network,
"address": addr,
}).Trace("ClientTrace.ConnectStart")
},
ConnectDone: func(network, addr string, err error) {
logrus.WithFields(logrus.Fields{
"network": network,
"address": addr,
"error": err,
}).Trace("ClientTrace.ConnectDone")
},
TLSHandshakeStart: func() {
logrus.Trace("ClientTrace.TLSHandshakeStart")
},
TLSHandshakeDone: func(cs tls.ConnectionState, err error) {
logrus.WithFields(logrus.Fields{
"version": cs.Version,
"handshake-complete": cs.HandshakeComplete,
"did-resume": cs.DidResume,
"cipher-suite": cs.CipherSuite,
"negotiated-protocol": cs.NegotiatedProtocol,
"negotiated-protocol-is-mutual": cs.NegotiatedProtocolIsMutual,
"server-name": cs.ServerName,
"error": err,
}).Trace("ClientTrace.TLSHandshakeDone")
},
WroteHeaderField: func(key string, value []string) {
logrus.WithFields(logrus.Fields{
"key": key,
"values": value,
}).Trace("ClientTrace.WroteHeaderField")
},
WroteHeaders: func() {
logrus.Trace("ClientTrace.WroteHeaders")
},
Wait100Continue: func() {
logrus.Trace("ClientTrace.Wait100Continue")
},
WroteRequest: func(reqInfo httptrace.WroteRequestInfo) {
logrus.WithField("error", reqInfo.Err).Trace("ClientTrace.WroteRequest")
},
})
}