-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add MQTT receiver #227
Add MQTT receiver #227
Conversation
1b1e213
to
92d508e
Compare
8294123
to
7e445a6
Compare
receivers/mqtt/mqtt.go
Outdated
return false, fmt.Errorf("Failed to connect to MQTT broker: %w", token.Error()) | ||
} | ||
|
||
if token := n.client.Publish(n.settings.Topic, 0, false, string(msg)); token.Wait() && token.Error() != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Publish accepts QoS
and retained
which are set to 0
and false
here. I started adding them too, but I'll do separate PRs later, as the current PRs (to Grafana, alerting and the terraform provider) are becoming large.
n.log.Error("Failed to template MQTT message", "error", tmplErr) | ||
return "", fmt.Errorf("Failed to template MQTT message: %w", tmplErr) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we usually do not fail on errors caused by expanding templates. We log it as a warning and proceed. Let's keep the same logic here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated: 95ac686
func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) { | ||
n.log.Debug("Sending an MQTT message") | ||
|
||
msg, err := n.buildMessage(ctx, as...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
although we validate Message while parsing config, it does not guarantee that it cannot be empty here. Should we check the result for emptiness?
It looks like MQTT does not put any requirements on this, though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we can send even an empty message to a topic, not sure that we should check the result here.
084a220
to
29e10bb
Compare
29e10bb
to
88fb445
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Adds an MQTT (v3.1) receiver.
Fixes grafana/grafana#16858
How to start an MQTT broker for testing
mosquitto can be used as a local MQTT broker for testing. Create the config file (full config file reference):
Start the server:
usernames and passwords
Install mosquitto for
mosquitto_passwd
tool. Create the passwords file:Add the following to the
mosquitto.conf
:Start the server:
Screenshots