diff --git a/components/playground/instance/instance.go b/components/playground/instance/instance.go index 0676c75910..620f2a502d 100644 --- a/components/playground/instance/instance.go +++ b/components/playground/instance/instance.go @@ -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 @@ -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 } diff --git a/components/playground/instance/tiproxy.go b/components/playground/instance/tiproxy.go index b39f5f01de..95b6b13cd8 100644 --- a/components/playground/instance/tiproxy.go +++ b/components/playground/instance/tiproxy.go @@ -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) diff --git a/components/playground/monitor.go b/components/playground/monitor.go index f0cfd2bd45..3d17a7edae 100644 --- a/components/playground/monitor.go +++ b/components/playground/monitor.go @@ -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") diff --git a/components/playground/playground.go b/components/playground/playground.go index a63bb4b5ed..4df08b60af 100644 --- a/components/playground/playground.go +++ b/components/playground/playground.go @@ -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 })