TUN-4067: Reformat code for consistent import order, grouping, and fix formatting. Added goimports target to the Makefile to make this easier in the future.
parent
027168c23a
commit
da4d0b2bae
@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e -o pipefail
|
||||
|
||||
OUTPUT=$(for d in $(go list -mod=vendor -f '{{.Dir}}' -a ./... | fgrep -v tunnelrpc) ; do goimports -format-only -local github.com/cloudflare/cloudflared -d $d ; done)
|
||||
|
||||
if [ -n "$OUTPUT" ] ; then
|
||||
PAGER=$(which colordiff || echo cat)
|
||||
echo
|
||||
echo "Code formatting issues found, use 'goimports -format-only -local github.com/cloudflare/cloudflared' to correct them"
|
||||
echo
|
||||
echo "$OUTPUT" | $PAGER
|
||||
exit 1
|
||||
fi
|
@ -1,47 +1,48 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"time"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
// Timer assumes the metrics is partitioned by one label
|
||||
type Timer struct {
|
||||
startTime map[string]time.Time
|
||||
metrics *prometheus.HistogramVec
|
||||
measureUnit time.Duration
|
||||
labelKey string
|
||||
startTime map[string]time.Time
|
||||
metrics *prometheus.HistogramVec
|
||||
measureUnit time.Duration
|
||||
labelKey string
|
||||
}
|
||||
|
||||
func NewTimer(metrics *prometheus.HistogramVec, unit time.Duration, labelKey string) *Timer {
|
||||
return &Timer{
|
||||
startTime: make(map[string]time.Time),
|
||||
measureUnit: unit,
|
||||
metrics: metrics,
|
||||
labelKey: labelKey,
|
||||
}
|
||||
return &Timer{
|
||||
startTime: make(map[string]time.Time),
|
||||
measureUnit: unit,
|
||||
metrics: metrics,
|
||||
labelKey: labelKey,
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Timer) Start(labelVal string) {
|
||||
i.startTime[labelVal] = time.Now()
|
||||
i.startTime[labelVal] = time.Now()
|
||||
}
|
||||
|
||||
func (i *Timer) End(labelVal string) time.Duration {
|
||||
if start, ok := i.startTime[labelVal]; ok {
|
||||
return Latency(start, time.Now())
|
||||
}
|
||||
return 0
|
||||
if start, ok := i.startTime[labelVal]; ok {
|
||||
return Latency(start, time.Now())
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (i *Timer) Observe(measurement time.Duration, labelVal string) {
|
||||
metricsLabels := prometheus.Labels{i.labelKey: labelVal}
|
||||
i.metrics.With(metricsLabels).Observe(float64(measurement / i.measureUnit))
|
||||
metricsLabels := prometheus.Labels{i.labelKey: labelVal}
|
||||
i.metrics.With(metricsLabels).Observe(float64(measurement / i.measureUnit))
|
||||
}
|
||||
|
||||
func (i *Timer) EndAndObserve(labelVal string) {
|
||||
i.Observe(i.End(labelVal), labelVal)
|
||||
i.Observe(i.End(labelVal), labelVal)
|
||||
}
|
||||
|
||||
func Latency(startTime, endTime time.Time) time.Duration {
|
||||
return endTime.Sub(startTime)
|
||||
return endTime.Sub(startTime)
|
||||
}
|
||||
|
@ -1,24 +1,24 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestEnd(t *testing.T) {
|
||||
m := prometheus.NewHistogramVec(
|
||||
prometheus.HistogramOpts{
|
||||
Namespace: "TestCallLatencyWithoutMeasurement",
|
||||
Name: "Latency",
|
||||
Buckets: prometheus.LinearBuckets(0, 50, 100),
|
||||
},
|
||||
[]string{"key"},
|
||||
)
|
||||
timer := NewTimer(m, time.Millisecond, "key")
|
||||
assert.Equal(t, time.Duration(0), timer.End("dne"))
|
||||
timer.Start("test")
|
||||
assert.NotEqual(t, time.Duration(0), timer.End("test"))
|
||||
m := prometheus.NewHistogramVec(
|
||||
prometheus.HistogramOpts{
|
||||
Namespace: "TestCallLatencyWithoutMeasurement",
|
||||
Name: "Latency",
|
||||
Buckets: prometheus.LinearBuckets(0, 50, 100),
|
||||
},
|
||||
[]string{"key"},
|
||||
)
|
||||
timer := NewTimer(m, time.Millisecond, "key")
|
||||
assert.Equal(t, time.Duration(0), timer.End("dne"))
|
||||
timer.Start("test")
|
||||
assert.NotEqual(t, time.Duration(0), timer.End("test"))
|
||||
}
|
||||
|