Skip to content

Commit

Permalink
Improve canary middleware access log and transport.
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Mar 29, 2020
1 parent d67f681 commit 114d5f5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
22 changes: 13 additions & 9 deletions pkg/middlewares/canary/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,6 @@ func (c *Canary) GetTracingInformation() (string, ext.SpanKindEnum) {
func (c *Canary) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
c.processRequestID(rw, req)
c.processCanary(rw, req)

if logData := accesslog.GetLogData(req); logData != nil {
if requestID := req.Header.Get(headerXRequestID); requestID != "" {
logData.Core["XRequestID"] = requestID
}
if xCanary := req.Header.Values(headerXCanary); len(xCanary) > 0 {
logData.Core["XCanary"] = xCanary
}
}
c.next.ServeHTTP(rw, req)
}

Expand All @@ -102,6 +93,10 @@ func (c *Canary) processRequestID(rw http.ResponseWriter, req *http.Request) {
req.Header.Set(headerXRequestID, requestID)
}
rw.Header().Set(headerXRequestID, requestID)

if logData := accesslog.GetLogData(req); logData != nil {
logData.Core["XRequestID"] = requestID
}
}
}

Expand Down Expand Up @@ -135,6 +130,15 @@ func (c *Canary) processCanary(rw http.ResponseWriter, req *http.Request) {
if c.canaryResponseHeader {
info.intoHeader(rw.Header())
}

if logData := accesslog.GetLogData(req); logData != nil {
if info.uid != "" {
logData.Core["UID"] = info.uid
}
if xCanary := req.Header.Values(headerXCanary); len(xCanary) > 0 {
logData.Core["XCanary"] = xCanary
}
}
}

type userInfo struct {
Expand Down
16 changes: 13 additions & 3 deletions pkg/middlewares/canary/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"runtime"
Expand All @@ -29,13 +30,22 @@ func init() {
var userAgent string

var tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
ForceAttemptHTTP2: true,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DialContext: (&net.Dialer{
Timeout: 3 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 20,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 3 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}

var client = &http.Client{
Transport: tr,
Timeout: time.Second,
Timeout: time.Second * 3,
}

type labelsRes struct {
Expand Down

0 comments on commit 114d5f5

Please sign in to comment.