[ENHANCEMENT] Notifier: GCP PubSub #3537
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why
This enhancement adds an ability to notify alerts to a GCP PubSub topic.
We found several examples implementing this by adding components/services like a cloud function or local server to "proxy" the alerts into the GCP PubSub topic. (for instance, https://modules.prosody.im/mod_pubsub_alertmanager.html).
We want to avoid unnecessary additional components in that process.
This use case can benefit more developers and projects and add more opportunities to use the Alert Manager system for their needs.
How
There is more than one way to implement this integration:
Implementing the REST API way was chosen to reduce the dependencies that will added to the code base for the GCP PubSub notifier support. Also, to re-use the existing code base for Webhook integration.
Do we need a new notifier (and not using existing Webhook)?
The exiting
webhooknotifier is missing two required particulars that GCP PubSub REST API is needed:google oauth2with the ability to refresh the token on the fly.:publishendpoint is strict with a different body schema, i.e.:{ "messages": [ { "data": "the message content", "attributes": {"key", "value"}, "orderingKey": "ordering_key" } ] }Prerequisite
roles/pubsub.publisherConfiguration
The receiver config also supported a global configuration
google_service_account_fileas a shared configuration for all thegoogle_pubsubreceiver instances (We can use the global config GCP service account for future GCP-related integration)An example for
alertmanager.ymlconfiguration:Implementation
On receiver initials, the receiver reads the service account file from the disk and constructs a new
TokenSourcebased on the provided service account with the required scopehttps://www.googleapis.com/auth/pubsubusinggoogle oauth2lib.The receiver creates a new
HttpClientattached to the newTokenSourceand stores the new client on the Notifier config.The receiver generates the target URL based on the URL template, Project, and Topic configuration, and stores the new client on the Notifier config.
When the notifier receives a new group event, we construct a new PubSub message with a single message instance inside that contains the "original" Alert Manager group message.
The notifier fills the PubSub message with all available
attributes(key values struct) of the source (GroupKey,GroupLabels,CommonLabels, andCommonAnnotations)The notifier fills the PubSub Message
orderingKeywith theGroupKeyvalueThe notifier sends the message using the
httpClientfrom the notifier config.The notifier handles the response based on the status code family (same as Webhook does):
On success, the
:publishendpoint returns the created PubSub message ID, and we print it to the log.