Skip to content

Commit

Permalink
playground: fix tiproxy metrics addr (pingcap#2299)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhebox authored Nov 3, 2023
1 parent 721b065 commit a07c4d7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
14 changes: 10 additions & 4 deletions components/playground/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ type instance struct {
BinPath string
}

// MetricAddr will be used by prometheus scrape_configs.
type MetricAddr struct {
Targets []string `json:"targets"`
Labels map[string]string `json:"labels"`
}

// Instance represent running component
type Instance interface {
Pid() int
Expand All @@ -59,16 +65,16 @@ type Instance interface {
LogFile() string
// Uptime show uptime.
Uptime() string
// StatusAddrs return the address to pull metrics.
StatusAddrs() []string
// MetricAddr return the address to pull metrics.
MetricAddr() MetricAddr
// Wait Should only call this if the instance is started successfully.
// The implementation should be safe to call Wait multi times.
Wait() error
}

func (inst *instance) StatusAddrs() (addrs []string) {
func (inst *instance) MetricAddr() (r MetricAddr) {
if inst.Host != "" && inst.StatusPort != 0 {
addrs = append(addrs, utils.JoinHostPort(inst.Host, inst.StatusPort))
r.Targets = append(r.Targets, utils.JoinHostPort(inst.Host, inst.StatusPort))
}
return
}
Expand Down
9 changes: 9 additions & 0 deletions components/playground/instance/tiproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ func NewTiProxy(binPath string, dir, host, configPath string, id int, port int,
return tiproxy
}

// MetricAddr implements Instance interface.
func (c *TiProxy) MetricAddr() (r MetricAddr) {
r.Targets = append(r.Targets, utils.JoinHostPort(c.Host, c.StatusPort))
r.Labels = map[string]string{
"__metrics_path__": "/api/metrics",
}
return
}

// Start implements Instance interface.
func (c *TiProxy) Start(ctx context.Context, version utils.Version) error {
endpoints := pdEndpoints(c.pds, true)
Expand Down
26 changes: 11 additions & 15 deletions components/playground/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,20 @@ import (
)

// ref: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#file_sd_config
func (m *monitor) renderSDFile(cid2targets map[string][]string) error {
type Item struct {
Targets []string `json:"targets"`
Labels map[string]string `json:"labels"`
}

cid2targets["prometheus"] = []string{utils.JoinHostPort(m.host, m.port)}
func (m *monitor) renderSDFile(cid2targets map[string]instance.MetricAddr) error {
cid2targets["prometheus"] = instance.MetricAddr{Targets: []string{utils.JoinHostPort(m.host, m.port)}}

var items []Item
var items []instance.MetricAddr

for id, targets := range cid2targets {
item := Item{
Targets: targets,
Labels: map[string]string{
"job": id,
},
for id, t := range cid2targets {
it := instance.MetricAddr{
Targets: t.Targets,
Labels: map[string]string{"job": id},
}
for k, v := range t.Labels {
it.Labels[k] = v
}
items = append(items, item)
items = append(items, it)
}

data, err := json.MarshalIndent(&items, "", "\t")
Expand Down
6 changes: 2 additions & 4 deletions components/playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -1289,12 +1289,10 @@ func (p *Playground) renderSDFile() error {
return nil
}

cid2targets := make(map[string][]string)
cid2targets := make(map[string]instance.MetricAddr)

_ = p.WalkInstances(func(cid string, inst instance.Instance) error {
targets := cid2targets[cid]
targets = append(targets, inst.StatusAddrs()...)
cid2targets[cid] = targets
cid2targets[cid] = inst.MetricAddr()
return nil
})

Expand Down

0 comments on commit a07c4d7

Please sign in to comment.