Skip to content

Commit

Permalink
httptrace
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmuth committed Sep 2, 2017
1 parent 7c0e828 commit c99fc93
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

func main() {

log.SetFormatter(&log.JSONFormatter{TimestampFormat:time.RFC3339Nano})

config, err := LoadAppConfig()
if err != nil {
log.Error("Error loading config", err.Error())
Expand Down
2 changes: 1 addition & 1 deletion locust/locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def api(l):
l.client.get("/api")

class UserBehavior(TaskSet):
tasks = {api: 5}
tasks = {api: 10}

class WebsiteUser(HttpLocust):
task_set = UserBehavior
Expand Down
56 changes: 51 additions & 5 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"errors"
log "github.com/sirupsen/logrus"
"net/http/httptrace"
)

// Service sends requests to a remote HTTP API
Expand All @@ -18,14 +18,60 @@ type Service struct {
}

type HttpClient interface {
Post(url string, bodyType string, body io.Reader) (*http.Response, error)
Do(r *http.Request) (*http.Response, error)
}

func (svc Service) Call(serviceRequest ServiceRequest) (serviceResponse ServiceResponse, err error) {
serviceRequestURL := fmt.Sprintf("%s", svc.BaseURL)

trace := &httptrace.ClientTrace{
GetConn: func(hostPort string) {
log.Info("About to get connection")
},
PutIdleConn: func(err error) {
if err != nil {
log.Info(fmt.Sprintf("Put idle connection failed. err=%v", err))
} else {
log.Info("Put idle connection succeeded.")
}
},
Got100Continue : func() {
log.Info("Got 100 Continue")
},
GotConn: func(connInfo httptrace.GotConnInfo) {
log.Info("Got connection")
},
ConnectStart: func(network, addr string) {
log.Info("Dial start")
},
DNSStart: func(info httptrace.DNSStartInfo) {
log.Info("DNS start", info.Host)
},
DNSDone: func(info httptrace.DNSDoneInfo) {
log.Info("DNS done")
},
ConnectDone: func(network, addr string, err error) {
log.Info("Dial done")
},
GotFirstResponseByte: func() {
log.Info("First response byte!")
},
WroteHeaders: func() {
log.Info("Wrote headers")
},
WroteRequest: func(wr httptrace.WroteRequestInfo) {
log.Info("Wrote request")
},
}
var resp *http.Response
log.Info(fmt.Sprintf("About to send request: POST %s with body %s", serviceRequestURL, serviceRequest.String()))
resp, err = svc.HttpClient.Post(serviceRequestURL, "application/json", strings.NewReader(serviceRequest.String()))
log.Info(fmt.Sprintf("About to send request: POST %s with body %s", svc.BaseURL, serviceRequest.String()))
req, err := http.NewRequest("POST", svc.BaseURL, strings.NewReader(serviceRequest.String()))
if err != nil {
log.Error("Error creating request to service", err)
return
}
req.Header.Set("Content-type", "application/json")
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
resp, err = svc.HttpClient.Do(req)
if err != nil {
log.Error("Error sending request to service", err)
return
Expand Down

0 comments on commit c99fc93

Please sign in to comment.