From bfe25f71e9e8d9327cdc443e2cbaa31856e31c56 Mon Sep 17 00:00:00 2001 From: Elouan Martinet Date: Sun, 16 Jul 2023 13:57:12 +0200 Subject: [PATCH] feat(core): do not include DNS query time in HTTP response time Fix #49 --- core/endpoint.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/core/endpoint.go b/core/endpoint.go index 1214a6e81..d2c214860 100644 --- a/core/endpoint.go +++ b/core/endpoint.go @@ -9,6 +9,7 @@ import ( "io" "net" "net/http" + "net/http/httptrace" "net/url" "strings" "time" @@ -310,10 +311,10 @@ func (endpoint *Endpoint) call(result *Result) { var err error var certificate *x509.Certificate endpointType := endpoint.Type() + startTime := time.Now() if endpointType == EndpointTypeHTTP { - request = endpoint.buildHTTPRequest() + request = endpoint.buildHTTPRequest(&startTime) } - startTime := time.Now() if endpointType == EndpointTypeDNS { endpoint.DNS.query(endpoint.URL, result) 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 if endpoint.GraphQL { graphQlBody := map[string]string{ @@ -382,6 +383,13 @@ func (endpoint *Endpoint) buildHTTPRequest() *http.Request { 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 }