Skip to content

Commit 07ae1a4

Browse files
committed
chore: fix case error
Signed-off-by: Rory Z <[email protected]>
1 parent 88a1a9f commit 07ae1a4

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@ func (sc *SafeConfig) ReloadConfig(confFile string) (err error) {
173173
if probe.ClientID == "" {
174174
hostname, _ := os.Hostname()
175175
hostname = strings.Replace(hostname, ".", "-", -1)
176-
probe.ClientID = "emqx-exporter-probe-" + hostname + fmt.Sprintf("%d", index)
176+
probe.ClientID = fmt.Sprintf("emqx-exporter-probe-%s-%d", hostname, index)
177177
}
178178
if probe.Topic == "" {
179179
hostname, _ := os.Hostname()
180180
hostname = strings.Replace(hostname, ".", "-", -1)
181-
probe.Topic = "emqx-exporter-probe/" + hostname + "/" + fmt.Sprintf("%d", index)
181+
probe.Topic = fmt.Sprintf("emqx-exporter-probe/%s/%d", hostname, index)
182182
}
183183
if probe.KeepAlive == 0 {
184184
probe.KeepAlive = 30

main_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ dashboard {
460460
}
461461
}
462462
}
463+
log.console.level = debug
463464
listeners {
464465
tcp.fake{
465466
bind = 11883
@@ -590,7 +591,7 @@ rule_engine {
590591

591592
mqttxSubResp, err := cli.ContainerCreate(ctx, &container.Config{
592593
Image: mqttxContainer.image,
593-
Cmd: []string{"mqttx", "bench", "sub", "-t", "test", "-h", emqxInfo.NetworkSettings.IPAddress},
594+
Cmd: []string{"mqttx", "sub", "-t", "test", "-h", emqxInfo.NetworkSettings.IPAddress},
594595
}, nil, nil, nil, "mqttx-sub")
595596
if err != nil {
596597
panic(err)
@@ -602,7 +603,7 @@ rule_engine {
602603

603604
mqttxPubResp, err := cli.ContainerCreate(ctx, &container.Config{
604605
Image: mqttxContainer.image,
605-
Cmd: []string{"mqttx", "bench", "pub", "-c", "1", "-t", "test", "-h", emqxInfo.NetworkSettings.IPAddress},
606+
Cmd: []string{"mqttx", "pub", "-c", "1", "-t", "test", "-h", emqxInfo.NetworkSettings.IPAddress},
606607
}, nil, nil, nil, "mqttx-pub")
607608
if err != nil {
608609
panic(err)

prober/mqtt.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package prober
33
import (
44
"context"
55
"emqx-exporter/config"
6+
"fmt"
67
"sync"
78
"time"
89

@@ -49,6 +50,7 @@ func init() {
4950
}
5051

5152
func initMQTTProbe(probe config.Probe, logger log.Logger) (*MQTTProbe, error) {
53+
var isReady = make(chan struct{})
5254
var msgChan = make(chan mqtt.Message)
5355

5456
opt := mqtt.NewClientOptions().AddBroker(probe.Scheme + "://" + probe.Target)
@@ -60,7 +62,8 @@ func initMQTTProbe(probe config.Probe, logger log.Logger) (*MQTTProbe, error) {
6062
opt.SetTLSConfig(probe.TLSClientConfig.ToTLSConfig())
6163
}
6264
opt.SetOnConnectHandler(func(c mqtt.Client) {
63-
level.Info(logger).Log("msg", "Connected to MQTT broker", "target", probe.Target)
65+
optReader := c.OptionsReader()
66+
level.Info(logger).Log("msg", "Connected to MQTT broker", "target", probe.Target, "client_id", optReader.ClientID())
6467
token := c.Subscribe(probe.Topic, probe.QoS, func(c mqtt.Client, m mqtt.Message) {
6568
msgChan <- m
6669
})
@@ -69,6 +72,7 @@ func initMQTTProbe(probe config.Probe, logger log.Logger) (*MQTTProbe, error) {
6972
level.Error(logger).Log("msg", "Failed to subscribe to MQTT topic", "target", probe.Target, "topic", probe.Topic, "qos", probe.QoS, "err", token.Error())
7073
return
7174
}
75+
isReady <- struct{}{}
7276
level.Info(logger).Log("msg", "Subscribed to MQTT topic", "target", probe.Target, "topic", probe.Topic, "qos", probe.QoS)
7377
})
7478
opt.SetConnectionLostHandler(func(c mqtt.Client, err error) {
@@ -80,6 +84,12 @@ func initMQTTProbe(probe config.Probe, logger log.Logger) (*MQTTProbe, error) {
8084
return nil, token.Error()
8185
}
8286

87+
select {
88+
case <-isReady:
89+
case <-time.After(60 * time.Second):
90+
return nil, fmt.Errorf("MQTT probe connect timeout")
91+
}
92+
8393
return &MQTTProbe{
8494
Client: c,
8595
MsgChan: msgChan,
@@ -102,6 +112,7 @@ func ProbeMQTT(probe config.Probe, logger log.Logger) bool {
102112
return false
103113
}
104114

115+
level.Info(logger).Log("msg", "Publishing MQTT message", "target", probe.Target, "topic", probe.Topic, "qos", probe.QoS)
105116
if token := mqttProbe.Client.Publish(probe.Topic, probe.QoS, false, "hello world"); token.Wait() && token.Error() != nil {
106117
return false
107118
}

0 commit comments

Comments
 (0)