Skip to content
forked from TwiN/gatus

Commit

Permalink
feat(core): do not include DNS query time in HTTP response time
Browse files Browse the repository at this point in the history
  • Loading branch information
Exagone313 committed Jul 16, 2023
1 parent a725e7e commit bfe25f7
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions core/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"net"
"net/http"
"net/http/httptrace"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand All @@ -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
}

Expand Down

0 comments on commit bfe25f7

Please sign in to comment.