Skip to content

Commit aa354b7

Browse files
authored
metricsreader, observer: read metrics from backend HTTP API (#597)
1 parent 390b8ab commit aa354b7

File tree

13 files changed

+1315
-126
lines changed

13 files changed

+1315
-126
lines changed

lib/config/proxy.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ package config
55

66
import (
77
"bytes"
8+
"net"
89
"os"
910
"path/filepath"
11+
"strings"
1012
"time"
1113

1214
"github.com/BurntSushi/toml"
1315
"github.com/pingcap/tiproxy/lib/util/errors"
16+
"github.com/pingcap/tiproxy/lib/util/sys"
1417
)
1518

1619
var (
@@ -201,3 +204,30 @@ func (cfg *Config) ToBytes() ([]byte, error) {
201204
err := toml.NewEncoder(b).Encode(cfg)
202205
return b.Bytes(), errors.WithStack(err)
203206
}
207+
208+
func (cfg *Config) GetIPPort() (ip, port, statusPort string, err error) {
209+
addrs := strings.Split(cfg.Proxy.Addr, ",")
210+
ip, port, err = net.SplitHostPort(addrs[0])
211+
if err != nil {
212+
err = errors.WithStack(err)
213+
return
214+
}
215+
_, statusPort, err = net.SplitHostPort(cfg.API.Addr)
216+
if err != nil {
217+
err = errors.WithStack(err)
218+
return
219+
}
220+
// AdvertiseAddr may be a DNS in k8s and certificate SAN typically contains DNS but not IP.
221+
if len(cfg.Proxy.AdvertiseAddr) > 0 {
222+
ip = cfg.Proxy.AdvertiseAddr
223+
} else {
224+
// reporting a non unicast IP makes no sense, try to find one
225+
// loopback/linklocal-unicast are not global unicast IP, but are valid local unicast IP
226+
if pip := net.ParseIP(ip); ip == "" || pip.Equal(net.IPv4bcast) || pip.IsUnspecified() || pip.IsMulticast() {
227+
if v := sys.GetGlobalUnicastIP(); v != "" {
228+
ip = v
229+
}
230+
}
231+
}
232+
return
233+
}

lib/config/proxy_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
package config
55

66
import (
7+
"net"
78
"os"
89
"path/filepath"
910
"testing"
1011

1112
"github.com/BurntSushi/toml"
13+
"github.com/pingcap/tiproxy/lib/util/sys"
1214
"github.com/stretchr/testify/require"
1315
)
1416

@@ -125,3 +127,46 @@ func TestProxyCheck(t *testing.T) {
125127
tc.post(t, &cfg)
126128
}
127129
}
130+
131+
func TestGetIPPort(t *testing.T) {
132+
for _, cas := range []struct {
133+
addr string
134+
advertiseAddr string
135+
port string
136+
nonUnicast bool
137+
}{
138+
{":34", "", "34", true},
139+
{"0.0.0.0:34", "", "34", true},
140+
{"255.255.255.255:34", "", "34", true},
141+
{"239.255.255.255:34", "", "34", true},
142+
{"[FF02::1:FF47]:34", "", "34", true},
143+
{"127.0.0.1:34", "", "34", false},
144+
{"[F02::1:FF47]:34", "", "34", false},
145+
{"192.0.0.1:6049", "", "6049", false},
146+
{"0.0.0.0:1000", "tc-tiproxy-0.tc-tiproxy-peer.ns.svc", "1000", false},
147+
} {
148+
cfg := &Config{
149+
Proxy: ProxyServer{
150+
Addr: cas.addr,
151+
AdvertiseAddr: cas.advertiseAddr,
152+
},
153+
API: API{
154+
Addr: cas.addr,
155+
},
156+
}
157+
ip, port, statusPort, err := cfg.GetIPPort()
158+
require.NoError(t, err)
159+
160+
expectedIP := cas.advertiseAddr
161+
if len(expectedIP) == 0 {
162+
expectedIP, _, err = net.SplitHostPort(cas.addr)
163+
require.NoError(t, err)
164+
if cas.nonUnicast {
165+
expectedIP = sys.GetGlobalUnicastIP()
166+
}
167+
}
168+
require.Equal(t, expectedIP, ip)
169+
require.Equal(t, cas.port, port)
170+
require.Equal(t, cas.port, statusPort)
171+
}
172+
}

0 commit comments

Comments
 (0)