Skip to content

Commit 0924549

Browse files
committed
TUN-4811: Publish quick tunnels' hostname in /metrics under userHostname for backwards-compatibility
1 parent 67a3be5 commit 0924549

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

cmd/cloudflared/tunnel/quick_tunnel.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7+
"strings"
78
"time"
89

910
"github.com/google/uuid"
@@ -51,9 +52,14 @@ func RunQuickTunnel(sc *subcommandContext) error {
5152
TunnelName: data.Result.Name,
5253
}
5354

55+
url := data.Result.Hostname
56+
if !strings.HasPrefix(url, "https://") {
57+
url = "https://" + url
58+
}
59+
5460
for _, line := range connection.AsciiBox([]string{
5561
"Your Quick Tunnel has been created! Visit it at:",
56-
data.Result.Hostname,
62+
url,
5763
}, 2) {
5864
sc.log.Info().Msg(line)
5965
}

connection/observer.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ func (o *Observer) sendConnectedEvent(connIndex uint8, location string) {
111111

112112
func (o *Observer) sendURL(url string) {
113113
o.sendEvent(Event{EventType: SetURL, URL: url})
114+
115+
if !strings.HasPrefix(url, "https://") {
116+
// We add https:// in the prefix for backwards compatibility as we used to do that with the old free tunnels
117+
// and some tools (like `wrangler tail`) are regexp-ing for that specifically.
118+
url = "https://" + url
119+
}
120+
o.metrics.userHostnamesCounts.WithLabelValues(url).Inc()
114121
}
115122

116123
func (o *Observer) SendReconnect(connIndex uint8) {

connection/observer_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,28 @@ import (
66
"testing"
77
"time"
88

9+
"github.com/prometheus/client_golang/prometheus"
10+
dto "github.com/prometheus/client_model/go"
911
"github.com/stretchr/testify/assert"
1012
)
1113

14+
func TestSendUrl(t *testing.T) {
15+
observer := NewObserver(&log, &log, false)
16+
17+
observer.sendURL("my-url.com")
18+
assert.Equal(t, 1.0, getCounterValue(t, observer.metrics.userHostnamesCounts, "https://my-url.com"))
19+
20+
observer.sendURL("https://another-long-one.com")
21+
assert.Equal(t, 1.0, getCounterValue(t, observer.metrics.userHostnamesCounts, "https://another-long-one.com"))
22+
}
23+
24+
func getCounterValue(t *testing.T, metric *prometheus.CounterVec, val string) float64 {
25+
var m = &dto.Metric{}
26+
err := metric.WithLabelValues(val).Write(m)
27+
assert.NoError(t, err)
28+
return m.Counter.GetValue()
29+
}
30+
1231
func TestRegisterServerLocation(t *testing.T) {
1332
m := newTunnelMetrics()
1433
tunnels := 20

0 commit comments

Comments
 (0)