feat(core): do not include DNS query time in HTTP response time

Fix #49
This commit is contained in:
Elouan Martinet 2023-07-16 13:57:12 +02:00
parent a725e7e770
commit bfe25f71e9

View File

@ -9,6 +9,7 @@ import (
"io" "io"
"net" "net"
"net/http" "net/http"
"net/http/httptrace"
"net/url" "net/url"
"strings" "strings"
"time" "time"
@ -310,10 +311,10 @@ func (endpoint *Endpoint) call(result *Result) {
var err error var err error
var certificate *x509.Certificate var certificate *x509.Certificate
endpointType := endpoint.Type() endpointType := endpoint.Type()
if endpointType == EndpointTypeHTTP {
request = endpoint.buildHTTPRequest()
}
startTime := time.Now() startTime := time.Now()
if endpointType == EndpointTypeHTTP {
request = endpoint.buildHTTPRequest(&startTime)
}
if endpointType == EndpointTypeDNS { if endpointType == EndpointTypeDNS {
endpoint.DNS.query(endpoint.URL, result) endpoint.DNS.query(endpoint.URL, result)
result.Duration = time.Since(startTime) result.Duration = time.Since(startTime)
@ -364,7 +365,7 @@ func (endpoint *Endpoint) call(result *Result) {
} }
} }
func (endpoint *Endpoint) buildHTTPRequest() *http.Request { func (endpoint *Endpoint) buildHTTPRequest(startTime *time.Time) *http.Request {
var bodyBuffer *bytes.Buffer var bodyBuffer *bytes.Buffer
if endpoint.GraphQL { if endpoint.GraphQL {
graphQlBody := map[string]string{ graphQlBody := map[string]string{
@ -382,6 +383,13 @@ func (endpoint *Endpoint) buildHTTPRequest() *http.Request {
request.Host = v request.Host = v
} }
} }
clientTrace := &httptrace.ClientTrace{
DNSDone: func(info httptrace.DNSDoneInfo) {
*startTime = time.Now()
},
}
clientTraceCtx := httptrace.WithClientTrace(request.Context(), clientTrace)
request = request.WithContext(clientTraceCtx)
return request return request
} }