Skip to content

Commit

Permalink
Put the IPKey metrics behind a flag for now.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbruens committed Mar 18, 2024
1 parent 6613942 commit f5e3cf5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
20 changes: 11 additions & 9 deletions cmd/outline-ss-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,21 +227,23 @@ func readConfig(filename string) (*Config, error) {

func main() {
var flags struct {
ConfigFile string
MetricsAddr string
IPCountryDB string
IPASNDB string
natTimeout time.Duration
replayHistory int
Verbose bool
Version bool
ConfigFile string
MetricsAddr string
IPCountryDB string
IPASNDB string
natTimeout time.Duration
replayHistory int
EnableIPKeyConnectivityMetrics bool
Verbose bool
Version bool
}
flag.StringVar(&flags.ConfigFile, "config", "", "Configuration filename")
flag.StringVar(&flags.MetricsAddr, "metrics", "", "Address for the Prometheus metrics")
flag.StringVar(&flags.IPCountryDB, "ip_country_db", "", "Path to the ip-to-country mmdb file")
flag.StringVar(&flags.IPASNDB, "ip_asn_db", "", "Path to the ip-to-ASN mmdb file")
flag.DurationVar(&flags.natTimeout, "udptimeout", defaultNatTimeout, "UDP tunnel timeout")
flag.IntVar(&flags.replayHistory, "replay_history", 0, "Replay buffer size (# of handshakes)")
flag.BoolVar(&flags.EnableIPKeyConnectivityMetrics, "enable_ip_key_connectivity_metrics", false, "Enables the collection and reporting of IPKey connectivity metrics")
flag.BoolVar(&flags.Verbose, "verbose", false, "Enables verbose logging output")
flag.BoolVar(&flags.Version, "version", false, "The version of the server")

Expand Down Expand Up @@ -284,7 +286,7 @@ func main() {
}
defer ip2info.Close()

m := newPrometheusOutlineMetrics(ip2info, prometheus.DefaultRegisterer)
m := newPrometheusOutlineMetrics(ip2info, prometheus.DefaultRegisterer, flags.EnableIPKeyConnectivityMetrics)
m.SetBuildInfo(version)
_, err = RunSSServer(flags.ConfigFile, flags.natTimeout, m, flags.replayHistory)
if err != nil {
Expand Down
25 changes: 18 additions & 7 deletions cmd/outline-ss-server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var since = time.Since

type outlineMetrics struct {
ipinfo.IPInfoMap
activeIPKeyTracker
*activeIPKeyTracker

buildInfo *prometheus.GaugeVec
accessKeys prometheus.Gauge
Expand Down Expand Up @@ -154,7 +154,7 @@ func newActiveIPKeyTracker(callback func(IPKey, time.Duration)) *activeIPKeyTrac
// `ip2info` to convert IP addresses to countries, and reports all
// metrics to Prometheus via `registerer`. `ip2info` may be nil, but
// `registerer` must not be.
func newPrometheusOutlineMetrics(ip2info ipinfo.IPInfoMap, registerer prometheus.Registerer) *outlineMetrics {
func newPrometheusOutlineMetrics(ip2info ipinfo.IPInfoMap, registerer prometheus.Registerer, enableIPKeyConnectivity bool) *outlineMetrics {
m := &outlineMetrics{
IPInfoMap: ip2info,
buildInfo: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Expand Down Expand Up @@ -256,7 +256,10 @@ func newPrometheusOutlineMetrics(ip2info ipinfo.IPInfoMap, registerer prometheus
Help: "Entries removed from the UDP NAT table",
}),
}
m.activeIPKeyTracker = *newActiveIPKeyTracker(m.reportIPKeyActivity)
if enableIPKeyConnectivity {
m.activeIPKeyTracker = newActiveIPKeyTracker(m.reportIPKeyActivity)
}
logger.Debugf("tracker: %v", m.activeIPKeyTracker)

// TODO: Is it possible to pass where to register the collectors?
registerer.MustRegister(m.buildInfo, m.accessKeys, m.ports, m.tcpProbes, m.tcpOpenConnections, m.tcpClosedConnections, m.tcpConnectionDurationMs,
Expand Down Expand Up @@ -295,7 +298,9 @@ func (m *outlineMetrics) reportIPKeyActivity(ipKey IPKey, duration time.Duration
}

func (m *outlineMetrics) AddAuthenticatedTCPConnection(addr net.Addr, accessKey string) {
m.activeIPKeyTracker.startConnection(addr, accessKey)
if m.activeIPKeyTracker != nil {
m.activeIPKeyTracker.startConnection(addr, accessKey)
}
}

// addIfNonZero helps avoid the creation of series that are always zero.
Expand Down Expand Up @@ -329,7 +334,9 @@ func (m *outlineMetrics) AddClosedTCPConnection(addr net.Addr, accessKey, status
addIfNonZero(data.ProxyClient, m.dataBytes, "c<p", "tcp", accessKey)
addIfNonZero(data.ProxyClient, m.dataBytesPerLocation, "c<p", "tcp", clientInfo.CountryCode.String(), asnLabel(clientInfo.ASN))

m.activeIPKeyTracker.stopConnection(addr, accessKey)
if m.activeIPKeyTracker != nil {
m.activeIPKeyTracker.stopConnection(addr, accessKey)
}
}

func (m *outlineMetrics) AddUDPPacketFromClient(clientInfo ipinfo.IPInfo, accessKey, status string, clientProxyBytes, proxyTargetBytes int) {
Expand All @@ -350,13 +357,17 @@ func (m *outlineMetrics) AddUDPPacketFromTarget(clientInfo ipinfo.IPInfo, access
func (m *outlineMetrics) AddUDPNatEntry(addr net.Addr, accessKey string) {
m.udpAddedNatEntries.Inc()

m.activeIPKeyTracker.startConnection(addr, accessKey)
if m.activeIPKeyTracker != nil {
m.activeIPKeyTracker.startConnection(addr, accessKey)
}
}

func (m *outlineMetrics) RemoveUDPNatEntry(addr net.Addr, accessKey string) {
m.udpRemovedNatEntries.Inc()

m.activeIPKeyTracker.stopConnection(addr, accessKey)
if m.activeIPKeyTracker != nil {
m.activeIPKeyTracker.stopConnection(addr, accessKey)
}
}

func (m *outlineMetrics) AddTCPProbe(status, drainResult string, port int, clientProxyBytes int64) {
Expand Down
20 changes: 10 additions & 10 deletions cmd/outline-ss-server/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (a fakeAddr) String() string { return string(a) }
func (a fakeAddr) Network() string { return "" }

func TestMethodsDontPanic(t *testing.T) {
ssMetrics := newPrometheusOutlineMetrics(nil, prometheus.NewPedanticRegistry())
ssMetrics := newPrometheusOutlineMetrics(nil, prometheus.NewPedanticRegistry(), true)
proxyMetrics := metrics.ProxyMetrics{
ClientProxy: 1,
ProxyTarget: 2,
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestASNLabel(t *testing.T) {
func TestIPKeyActivityPerKeyDoesNotReportUnlessAllConnectionsClosed(t *testing.T) {
since = func(time.Time) time.Duration { return 3 * time.Second }
reg := prometheus.NewPedanticRegistry()
ssMetrics := newPrometheusOutlineMetrics(nil, reg)
ssMetrics := newPrometheusOutlineMetrics(nil, reg, true)
accessKey := "key-1"
status := "OK"
data := metrics.ProxyMetrics{}
Expand All @@ -89,7 +89,7 @@ func TestIPKeyActivityPerKeyDoesNotReportUnlessAllConnectionsClosed(t *testing.T
func TestIPKeyActivityPerKey(t *testing.T) {
since = func(time.Time) time.Duration { return 3 * time.Second }
reg := prometheus.NewPedanticRegistry()
ssMetrics := newPrometheusOutlineMetrics(nil, reg)
ssMetrics := newPrometheusOutlineMetrics(nil, reg, true)
accessKey := "key-1"
status := "OK"
data := metrics.ProxyMetrics{}
Expand All @@ -116,7 +116,7 @@ func TestIPKeyActivityPerKey(t *testing.T) {
func TestIPKeyActivityPerLocation(t *testing.T) {
since = func(time.Time) time.Duration { return 5 * time.Second }
reg := prometheus.NewPedanticRegistry()
ssMetrics := newPrometheusOutlineMetrics(&noopMap{}, reg)
ssMetrics := newPrometheusOutlineMetrics(&noopMap{}, reg, true)
accessKey := "key-1"
status := "OK"
data := metrics.ProxyMetrics{}
Expand All @@ -139,15 +139,15 @@ func TestIPKeyActivityPerLocation(t *testing.T) {
}

func BenchmarkOpenTCP(b *testing.B) {
ssMetrics := newPrometheusOutlineMetrics(nil, prometheus.NewRegistry())
ssMetrics := newPrometheusOutlineMetrics(nil, prometheus.NewRegistry(), false)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ssMetrics.AddOpenTCPConnection(fakeAddr("127.0.0.1:9"))
}
}

func BenchmarkCloseTCP(b *testing.B) {
ssMetrics := newPrometheusOutlineMetrics(nil, prometheus.NewRegistry())
ssMetrics := newPrometheusOutlineMetrics(nil, prometheus.NewRegistry(), false)
addr := fakeAddr("127.0.0.1:9")
accessKey := "key 1"
status := "OK"
Expand All @@ -162,7 +162,7 @@ func BenchmarkCloseTCP(b *testing.B) {
}

func BenchmarkProbe(b *testing.B) {
ssMetrics := newPrometheusOutlineMetrics(nil, prometheus.NewRegistry())
ssMetrics := newPrometheusOutlineMetrics(nil, prometheus.NewRegistry(), false)
status := "ERR_REPLAY"
drainResult := "other"
port := 12345
Expand All @@ -174,7 +174,7 @@ func BenchmarkProbe(b *testing.B) {
}

func BenchmarkClientUDP(b *testing.B) {
ssMetrics := newPrometheusOutlineMetrics(nil, prometheus.NewRegistry())
ssMetrics := newPrometheusOutlineMetrics(nil, prometheus.NewRegistry(), false)
clientInfo := ipinfo.IPInfo{CountryCode: "ZZ", ASN: 100}
accessKey := "key 1"
status := "OK"
Expand All @@ -188,7 +188,7 @@ func BenchmarkClientUDP(b *testing.B) {
}

func BenchmarkTargetUDP(b *testing.B) {
ssMetrics := newPrometheusOutlineMetrics(nil, prometheus.NewRegistry())
ssMetrics := newPrometheusOutlineMetrics(nil, prometheus.NewRegistry(), false)
clientInfo := ipinfo.IPInfo{CountryCode: "ZZ", ASN: 100}
accessKey := "key 1"
status := "OK"
Expand All @@ -200,7 +200,7 @@ func BenchmarkTargetUDP(b *testing.B) {
}

func BenchmarkNAT(b *testing.B) {
ssMetrics := newPrometheusOutlineMetrics(nil, prometheus.NewRegistry())
ssMetrics := newPrometheusOutlineMetrics(nil, prometheus.NewRegistry(), false)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ssMetrics.AddUDPNatEntry(fakeAddr("127.0.0.1:9"), "key-0")
Expand Down
2 changes: 1 addition & 1 deletion cmd/outline-ss-server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

func TestRunSSServer(t *testing.T) {
m := newPrometheusOutlineMetrics(nil, prometheus.DefaultRegisterer)
m := newPrometheusOutlineMetrics(nil, prometheus.DefaultRegisterer, false)
server, err := RunSSServer("config_example.yml", 30*time.Second, m, 10000)
if err != nil {
t.Fatalf("RunSSServer() error = %v", err)
Expand Down

0 comments on commit f5e3cf5

Please sign in to comment.