Skip to content

Commit

Permalink
Added URL and Method labels to metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
mhshahin committed Nov 26, 2022
1 parent 4b93f13 commit 6140156
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion artemis.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewTracer(namespace string) *Tracer {
func (t *Tracer) RequestWithTracer(request *http.Request) *http.Request {
requestStart := time.Now()

httpTracer := NewHttpTracer(requestStart, t.Metrics)
httpTracer := NewHttpTracer(requestStart, t.Metrics, request.Method, request.URL.Host)

clientTrace := &httptrace.ClientTrace{
GetConn: httpTracer.GetConn,
Expand Down
36 changes: 19 additions & 17 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ type TracingMetrics struct {
var (
SmallDurationBuckets = []float64{.0001, .0005, .001, .002, .005, .01, .05, .1, 1, 2.5, 5, 10}

HttpLabels prometheus.Labels
HostLabel = "host"
HttpLabels prometheus.Labels
HostLabel = "host"
MethodLabel = "method"
UrlLabel = "url"
)

func NewTracingMetrics(namespace string) *TracingMetrics {
Expand All @@ -32,7 +34,7 @@ func NewTracingMetrics(namespace string) *TracingMetrics {
Name: "http_get_connection_duration_seconds",
Help: "HTTP Get Connection Duration",
Buckets: SmallDurationBuckets,
}, []string{}),
}, []string{MethodLabel, UrlLabel}),
ReuseConnections: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "http_reuse_connections",
Expand All @@ -48,7 +50,7 @@ func NewTracingMetrics(namespace string) *TracingMetrics {
Name: "http_first_byte_response_duration_seconds",
Help: "HTTP Duration of Getting First Response Bytes",
Buckets: SmallDurationBuckets,
}, []string{}),
}, []string{MethodLabel, UrlLabel}),
DNSLookupDurationSeconds: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Name: "http_dns_lookup_duration_seconds",
Expand All @@ -65,25 +67,25 @@ func NewTracingMetrics(namespace string) *TracingMetrics {
Name: "http_connection_handshake_duration_seconds",
Help: "HTTP Connection Handshake Duration",
Buckets: SmallDurationBuckets,
}, []string{}),
}, []string{MethodLabel, UrlLabel}),
HeaderWriteDrurationSeconds: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Name: "http_header_write_duration_seconds",
Help: "HTTP Header Write Duration",
Buckets: SmallDurationBuckets,
}, []string{}),
}, []string{MethodLabel, UrlLabel}),
RequestWriteDurationSeconds: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Name: "http_request_write_duration_seconds",
Help: "HTTP Request Write Duration",
Buckets: SmallDurationBuckets,
}, []string{}),
}, []string{MethodLabel, UrlLabel}),
}
}

func (tm *TracingMetrics) GetConnectionDurationSecondsMetric(getConnTime time.Time) {
func (tm *TracingMetrics) GetConnectionDurationSecondsMetric(getConnTime time.Time, method, url string) {
getConnDuration := time.Since(getConnTime)
tm.GetConnectionDurationSeconds.With(HttpLabels).Observe(getConnDuration.Seconds())
tm.GetConnectionDurationSeconds.With(prometheus.Labels{MethodLabel: method, UrlLabel: url}).Observe(getConnDuration.Seconds())
}

func (tm *TracingMetrics) ReuseConnectionsMetric() {
Expand All @@ -94,9 +96,9 @@ func (tm *TracingMetrics) ReuseIdleConnectionsMetric() {
tm.ReuseIdleConnections.With(HttpLabels).Inc()
}

func (tm *TracingMetrics) FirstByteReceiveDurationSecondsMetric(startTime time.Time) {
func (tm *TracingMetrics) FirstByteReceiveDurationSecondsMetric(startTime time.Time, method, url string) {
getFirstByteDuration := time.Since(startTime)
tm.FirstByteReceiveDurationSeconds.With(HttpLabels).Observe(getFirstByteDuration.Seconds())
tm.FirstByteReceiveDurationSeconds.With(prometheus.Labels{MethodLabel: method, UrlLabel: url}).Observe(getFirstByteDuration.Seconds())
}

func (tm *TracingMetrics) DNSLookupDurationSecondsMetric(dnsStartTime time.Time, dnsHost string) {
Expand All @@ -108,19 +110,19 @@ func (tm *TracingMetrics) DNSCoalescedMetric(dnsHost string) {
tm.DNSCoalesced.With(prometheus.Labels{HostLabel: dnsHost}).Inc()
}

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

func (tm *TracingMetrics) HeaderWriteDrurationSecondsMetric(startTime time.Time) {
func (tm *TracingMetrics) HeaderWriteDrurationSecondsMetric(startTime time.Time, method, url string) {
headerWriteDuration := time.Since(startTime)
tm.HeaderWriteDrurationSeconds.With(HttpLabels).Observe(headerWriteDuration.Seconds())
tm.HeaderWriteDrurationSeconds.With(prometheus.Labels{MethodLabel: method, UrlLabel: url}).Observe(headerWriteDuration.Seconds())
}

func (tm *TracingMetrics) RequestWriteDurationSecondsMetric(startTime time.Time) {
func (tm *TracingMetrics) RequestWriteDurationSecondsMetric(startTime time.Time, method, url string) {
requestWriteDuration := time.Since(startTime)
tm.RequestWriteDurationSeconds.With(HttpLabels).Observe(requestWriteDuration.Seconds())
tm.RequestWriteDurationSeconds.With(prometheus.Labels{MethodLabel: method, UrlLabel: url}).Observe(requestWriteDuration.Seconds())
}

func (tm *TracingMetrics) GetCollectors() []prometheus.Collector {
Expand Down
17 changes: 11 additions & 6 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ type HttpTracer struct {
ConnectStartTime time.Time

Metrics *TracingMetrics

ReqMethod string
ReqURL string
}

func NewHttpTracer(requestStart time.Time, metrics *TracingMetrics) *HttpTracer {
func NewHttpTracer(requestStart time.Time, metrics *TracingMetrics, method, reqUrl string) *HttpTracer {
return &HttpTracer{
RequestStartTime: requestStart,
Metrics: metrics,
ReqMethod: method,
ReqURL: reqUrl,
}
}

Expand All @@ -28,7 +33,7 @@ func (ht *HttpTracer) GetConn(hostPort string) {
}

func (ht *HttpTracer) GotConn(info httptrace.GotConnInfo) {
ht.Metrics.GetConnectionDurationSecondsMetric(ht.GetConnTime)
ht.Metrics.GetConnectionDurationSecondsMetric(ht.GetConnTime, ht.ReqMethod, ht.ReqURL)

if info.Reused {
ht.Metrics.ReuseConnectionsMetric()
Expand All @@ -40,7 +45,7 @@ func (ht *HttpTracer) GotConn(info httptrace.GotConnInfo) {
}

func (ht *HttpTracer) GotFirstResponseByte() {
ht.Metrics.FirstByteReceiveDurationSecondsMetric(ht.RequestStartTime)
ht.Metrics.FirstByteReceiveDurationSecondsMetric(ht.RequestStartTime, ht.ReqMethod, ht.ReqURL)
}

func (ht *HttpTracer) DNSStart(info httptrace.DNSStartInfo) {
Expand All @@ -61,13 +66,13 @@ func (ht *HttpTracer) ConnStart(network, addr string) {
}

func (ht *HttpTracer) ConnDone(network, addr string, err error) {
ht.Metrics.ConnectionHandshakeDurationSecondsMetric(ht.ConnectStartTime)
ht.Metrics.ConnectionHandshakeDurationSecondsMetric(ht.ConnectStartTime, ht.ReqMethod, ht.ReqURL)
}

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

func (ht *HttpTracer) WroteRequest(info httptrace.WroteRequestInfo) {
ht.Metrics.RequestWriteDurationSecondsMetric(ht.RequestStartTime)
ht.Metrics.RequestWriteDurationSecondsMetric(ht.RequestStartTime, ht.ReqMethod, ht.ReqURL)
}

0 comments on commit 6140156

Please sign in to comment.