- fix how tls phase is computed
authorkgersen <kgersen@hotmail.com>
Sun, 14 Mar 2021 17:38:43 +0000 (18:38 +0100)
committerkgersen <kgersen@hotmail.com>
Sun, 14 Mar 2021 17:55:57 +0000 (18:55 +0100)
Signed-off-by: kgersen <kgersen@hotmail.com>
prober/http.go

index 41182d67c2cb3a0c7dee789058e40667f6a362eb..c1f3433679b542b4bffd63319050112a2625e61d 100644 (file)
@@ -15,6 +15,7 @@ package prober
 
 import (
        "context"
+       "crypto/tls"
        "errors"
        "fmt"
        "io"
@@ -119,6 +120,8 @@ type roundTripTrace struct {
        gotConn       time.Time
        responseStart time.Time
        end           time.Time
+       tlsStart      time.Time
+       tlsDone       time.Time
 }
 
 // transport is a custom transport keeping traces for each HTTP roundtrip.
@@ -202,6 +205,16 @@ func (t *transport) GotFirstResponseByte() {
        defer t.mu.Unlock()
        t.current.responseStart = time.Now()
 }
+func (t *transport) TLSHandshakeStart() {
+       t.mu.Lock()
+       defer t.mu.Unlock()
+       t.current.tlsStart = time.Now()
+}
+func (t *transport) TLSHandshakeDone(_ tls.ConnectionState, _ error) {
+       t.mu.Lock()
+       defer t.mu.Unlock()
+       t.current.tlsDone = time.Now()
+}
 
 // byteCounter implements an io.ReadCloser that keeps track of the total
 // number of bytes it has read.
@@ -411,6 +424,8 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
                ConnectDone:          tt.ConnectDone,
                GotConn:              tt.GotConn,
                GotFirstResponseByte: tt.GotFirstResponseByte,
+               TLSHandshakeStart:    tt.TLSHandshakeStart,
+               TLSHandshakeDone:     tt.TLSHandshakeDone,
        }
        request = request.WithContext(httptrace.WithClientTrace(request.Context(), trace))
 
@@ -521,6 +536,8 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
                        "connectDone", trace.connectDone,
                        "gotConn", trace.gotConn,
                        "responseStart", trace.responseStart,
+                       "tlsStart", trace.tlsStart,
+                       "tlsDone", trace.tlsDone,
                        "end", trace.end,
                )
                // We get the duration for the first request from chooseProtocol.
@@ -532,13 +549,12 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
                        continue
                }
                if trace.tls {
-                       // dnsDone must be set if gotConn was set.
-                       durationGaugeVec.WithLabelValues("connect").Add(trace.connectDone.Sub(trace.dnsDone).Seconds())
-                       durationGaugeVec.WithLabelValues("tls").Add(trace.gotConn.Sub(trace.dnsDone).Seconds())
-               } else {
-                       durationGaugeVec.WithLabelValues("connect").Add(trace.gotConn.Sub(trace.dnsDone).Seconds())
+                       durationGaugeVec.WithLabelValues("tls").Add(trace.tlsDone.Sub(trace.tlsStart).Seconds())
                }
 
+               // actual connection - we could add a new phase between connectDone and gotConn
+               durationGaugeVec.WithLabelValues("connect").Add(trace.gotConn.Sub(trace.dnsDone).Seconds())
+
                // Continue here if we never got a response from the server.
                if trace.responseStart.IsZero() {
                        continue