Skip to content

Commit

Permalink
Added TLS Handshake metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad H. Shahin committed Apr 15, 2023
1 parent 6140156 commit c08c27b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions artemis.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func (t *Tracer) RequestWithTracer(request *http.Request) *http.Request {
DNSDone: httpTracer.DNSDone,
ConnectStart: httpTracer.ConnStart,
ConnectDone: httpTracer.ConnDone,
TLSHandshakeStart: httpTracer.TLSHandshakeStart,
TLSHandshakeDone: httpTracer.TLSHandshakeDone,
WroteHeaders: httpTracer.WroteHeaders,
WroteRequest: httpTracer.WroteRequest,
}
Expand Down
13 changes: 13 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type TracingMetrics struct {
ConnectionHandshakeDurationSeconds *prometheus.HistogramVec
HeaderWriteDrurationSeconds *prometheus.HistogramVec
RequestWriteDurationSeconds *prometheus.HistogramVec
TLSHandshakeDurationSeconds *prometheus.HistogramVec
}

var (
Expand Down Expand Up @@ -68,6 +69,12 @@ func NewTracingMetrics(namespace string) *TracingMetrics {
Help: "HTTP Connection Handshake Duration",
Buckets: SmallDurationBuckets,
}, []string{MethodLabel, UrlLabel}),
TLSHandshakeDurationSeconds: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Name: "http_tls_handshake_duration_seconds",
Help: "HTTP TLS Handshake Duration",
Buckets: SmallDurationBuckets,
}, []string{MethodLabel, UrlLabel}),
HeaderWriteDrurationSeconds: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Name: "http_header_write_duration_seconds",
Expand Down Expand Up @@ -115,6 +122,11 @@ func (tm *TracingMetrics) ConnectionHandshakeDurationSecondsMetric(connStartTime
tm.ConnectionHandshakeDurationSeconds.With(prometheus.Labels{MethodLabel: method, UrlLabel: url}).Observe(handshakeDuration.Seconds())
}

func (tm *TracingMetrics) TLSHandshakeDurationSecondsMetric(connStartTime time.Time, method, url string) {
handshakeDuration := time.Since(connStartTime)
tm.TLSHandshakeDurationSeconds.With(prometheus.Labels{MethodLabel: method, UrlLabel: url}).Observe(handshakeDuration.Seconds())
}

func (tm *TracingMetrics) HeaderWriteDrurationSecondsMetric(startTime time.Time, method, url string) {
headerWriteDuration := time.Since(startTime)
tm.HeaderWriteDrurationSeconds.With(prometheus.Labels{MethodLabel: method, UrlLabel: url}).Observe(headerWriteDuration.Seconds())
Expand All @@ -133,6 +145,7 @@ func (tm *TracingMetrics) GetCollectors() []prometheus.Collector {
tm.DNSLookupDurationSeconds,
tm.DNSCoalesced,
tm.ConnectionHandshakeDurationSeconds,
tm.TLSHandshakeDurationSeconds,
tm.HeaderWriteDrurationSeconds,
tm.RequestWriteDurationSeconds,
}
Expand Down
18 changes: 14 additions & 4 deletions tracer.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package artemis

import (
"crypto/tls"
"net/http/httptrace"
"time"
)

type HttpTracer struct {
RequestStartTime time.Time

GetConnTime time.Time
DNSStartTime time.Time
DNSHost string
ConnectStartTime time.Time
GetConnTime time.Time
DNSStartTime time.Time
DNSHost string
ConnectStartTime time.Time
TLSHandshakeStartTime time.Time

Metrics *TracingMetrics

Expand Down Expand Up @@ -69,6 +71,14 @@ func (ht *HttpTracer) ConnDone(network, addr string, err error) {
ht.Metrics.ConnectionHandshakeDurationSecondsMetric(ht.ConnectStartTime, ht.ReqMethod, ht.ReqURL)
}

func (ht *HttpTracer) TLSHandshakeStart() {
ht.TLSHandshakeStartTime = time.Now()
}

func (ht *HttpTracer) TLSHandshakeDone(tls.ConnectionState, error) {
ht.Metrics.TLSHandshakeDurationSecondsMetric(ht.TLSHandshakeStartTime, ht.ReqMethod, ht.ReqURL)
}

func (ht *HttpTracer) WroteHeaders() {
ht.Metrics.HeaderWriteDrurationSecondsMetric(ht.RequestStartTime, ht.ReqMethod, ht.ReqURL)
}
Expand Down

0 comments on commit c08c27b

Please sign in to comment.