Skip to content
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

Merged
merged 6 commits into from
Aug 22, 2024
Merged

Conversation

alexander-akhmetov
Copy link
Contributor

@alexander-akhmetov alexander-akhmetov commented Jul 30, 2024

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):

$ cat mosquitto.conf
listener 1883 0.0.0.0
allow_anonymous true  # set to false to disallow anonymous users

Start the server:

docker run -it -p 127.0.0.1:1883:1883 -v ./mosquitto.conf:/mosquitto/config/mosquitto.conf eclipse-mosquitto

usernames and passwords

Install mosquitto for mosquitto_passwd tool. Create the passwords file:

mosquitto_passwd -c passwords.txt user1

Add the following to the mosquitto.conf:

password_file /mosquitto/config/passwords.txt

Start the server:

docker run -it -p 127.0.0.1:1883:1883 -v ./mosquitto.conf:/mosquitto/config/mosquitto.conf -v ./passwords.txt:/mosquitto/config/passwords.txt eclipse-mosquitto

Screenshots

image image

@CLAassistant
Copy link

CLAassistant commented Jul 30, 2024

CLA assistant check
All committers have signed the CLA.

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 {
Copy link
Contributor Author

@alexander-akhmetov alexander-akhmetov Aug 15, 2024

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)
}

Copy link
Contributor

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.

Copy link
Contributor Author

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...)
Copy link
Contributor

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

Copy link
Contributor Author

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.

receivers/mqtt/mqtt.go Outdated Show resolved Hide resolved
Copy link
Contributor

@yuri-tceretian yuri-tceretian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

receivers/mqtt/config.go Outdated Show resolved Hide resolved
receivers/mqtt/config.go Outdated Show resolved Hide resolved
receivers/mqtt/testing.go Show resolved Hide resolved
@alexander-akhmetov alexander-akhmetov merged commit 9daa623 into main Aug 22, 2024
3 checks passed
@alexander-akhmetov alexander-akhmetov deleted the alexander-akhmetov/mqtt-receiver branch August 22, 2024 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Add MQTT as an notification channel
3 participants