TUN-4811: Publish quick tunnels' hostname in /metrics under `userHostname` for backwards-compatibility

This commit is contained in:
Nuno Diegues 2021-07-28 09:27:05 +01:00
parent 67a3be5b7a
commit 0924549efd
3 changed files with 33 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strings"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
@ -51,9 +52,14 @@ func RunQuickTunnel(sc *subcommandContext) error {
TunnelName: data.Result.Name, TunnelName: data.Result.Name,
} }
url := data.Result.Hostname
if !strings.HasPrefix(url, "https://") {
url = "https://" + url
}
for _, line := range connection.AsciiBox([]string{ for _, line := range connection.AsciiBox([]string{
"Your Quick Tunnel has been created! Visit it at:", "Your Quick Tunnel has been created! Visit it at:",
data.Result.Hostname, url,
}, 2) { }, 2) {
sc.log.Info().Msg(line) sc.log.Info().Msg(line)
} }

View File

@ -111,6 +111,13 @@ func (o *Observer) sendConnectedEvent(connIndex uint8, location string) {
func (o *Observer) sendURL(url string) { func (o *Observer) sendURL(url string) {
o.sendEvent(Event{EventType: SetURL, URL: url}) o.sendEvent(Event{EventType: SetURL, URL: url})
if !strings.HasPrefix(url, "https://") {
// We add https:// in the prefix for backwards compatibility as we used to do that with the old free tunnels
// and some tools (like `wrangler tail`) are regexp-ing for that specifically.
url = "https://" + url
}
o.metrics.userHostnamesCounts.WithLabelValues(url).Inc()
} }
func (o *Observer) SendReconnect(connIndex uint8) { func (o *Observer) SendReconnect(connIndex uint8) {

View File

@ -6,9 +6,28 @@ import (
"testing" "testing"
"time" "time"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestSendUrl(t *testing.T) {
observer := NewObserver(&log, &log, false)
observer.sendURL("my-url.com")
assert.Equal(t, 1.0, getCounterValue(t, observer.metrics.userHostnamesCounts, "https://my-url.com"))
observer.sendURL("https://another-long-one.com")
assert.Equal(t, 1.0, getCounterValue(t, observer.metrics.userHostnamesCounts, "https://another-long-one.com"))
}
func getCounterValue(t *testing.T, metric *prometheus.CounterVec, val string) float64 {
var m = &dto.Metric{}
err := metric.WithLabelValues(val).Write(m)
assert.NoError(t, err)
return m.Counter.GetValue()
}
func TestRegisterServerLocation(t *testing.T) { func TestRegisterServerLocation(t *testing.T) {
m := newTunnelMetrics() m := newTunnelMetrics()
tunnels := 20 tunnels := 20