-
Notifications
You must be signed in to change notification settings - Fork 39
feat: implement slack notifier for demo organization creation event #2041
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d7b77c1
stash change
hvn2k1 a4dde4a
Merge branch 'main' into notify-demo-org-create
hvn2k1 2c2fcb9
implement slack notifier for demo organization creation event
hvn2k1 b0bb22e
fix lint header
hvn2k1 838f6bb
refactor slack notifier config for more flexibility
hvn2k1 e9c8871
rename webURL to consoleEndpoint
hvn2k1 3f12079
use existed web url
hvn2k1 fafa3a6
acknowledge metric
hvn2k1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
186 changes: 186 additions & 0 deletions
186
pkg/subscriber/processor/demo_organization_creation_notifier.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| // Copyright 2025 The Bucketeer Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package processor | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "go.uber.org/zap" | ||
| "google.golang.org/protobuf/proto" | ||
|
|
||
| "github.com/bucketeer-io/bucketeer/pkg/notification/sender/notifier" | ||
| "github.com/bucketeer-io/bucketeer/pkg/pubsub/puller" | ||
| "github.com/bucketeer-io/bucketeer/pkg/pubsub/puller/codes" | ||
| "github.com/bucketeer-io/bucketeer/pkg/subscriber" | ||
| domainevent "github.com/bucketeer-io/bucketeer/proto/event/domain" | ||
| domaineventproto "github.com/bucketeer-io/bucketeer/proto/event/domain" | ||
| notificationproto "github.com/bucketeer-io/bucketeer/proto/notification" | ||
| senderproto "github.com/bucketeer-io/bucketeer/proto/notification/sender" | ||
| ) | ||
|
|
||
| type DemoOrganizationCreationNotifierConfig struct { | ||
| Notifier NotifierConfig `json:"notifier"` | ||
| } | ||
|
|
||
| type NotifierConfig struct { | ||
| Slack SlackNotifierConfig `json:"slack"` | ||
| } | ||
|
|
||
| type SlackNotifierConfig struct { | ||
| WebHookURL string `json:"webHookURL"` | ||
| } | ||
|
|
||
| type demoOrganizationCreationNotifier struct { | ||
| slackNotifier notifier.Notifier | ||
| demoOrganizationCreationNotifierConfig DemoOrganizationCreationNotifierConfig | ||
| logger *zap.Logger | ||
| } | ||
|
|
||
| func NewDemoOrganizationCreationNotifier( | ||
| config interface{}, | ||
| webURL string, | ||
| logger *zap.Logger, | ||
| ) subscriber.PubSubProcessor { | ||
| jsonConfigMap, ok := config.(map[string]interface{}) | ||
| if !ok { | ||
| logger.Error("demoOrganizationCreationNotifier: invalid config type, expected map[string]interface{}") | ||
| return nil | ||
| } | ||
| configBytes, err := json.Marshal(jsonConfigMap) | ||
| if err != nil { | ||
| logger.Error("demoOrganizationCreationNotifier: failed to marshal config", zap.Error(err)) | ||
| return nil | ||
| } | ||
| var notifierConfig DemoOrganizationCreationNotifierConfig | ||
| if err := json.Unmarshal(configBytes, ¬ifierConfig); err != nil { | ||
| logger.Error("demoOrganizationCreationNotifier: failed to unmarshal config", zap.Error(err)) | ||
| return nil | ||
| } | ||
| slackNotifier := notifier.NewSlackNotifier(webURL) | ||
|
|
||
| return &demoOrganizationCreationNotifier{ | ||
| slackNotifier: slackNotifier, | ||
| demoOrganizationCreationNotifierConfig: notifierConfig, | ||
| logger: logger, | ||
| } | ||
| } | ||
|
|
||
| func (d demoOrganizationCreationNotifier) Process(ctx context.Context, msgChan <-chan *puller.Message) error { | ||
| for { | ||
| select { | ||
| case msg, ok := <-msgChan: | ||
| if !ok { | ||
| d.logger.Error("demoOrganizationCreationNotifier: message channel closed") | ||
| return nil | ||
| } | ||
| subscriberReceivedCounter.WithLabelValues(subscriberDemoOrganizationEvent).Inc() | ||
| d.handleMessage(msg) | ||
| case <-ctx.Done(): | ||
| d.logger.Debug("subscriber context done, stopped processing messages") | ||
| return nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (d demoOrganizationCreationNotifier) handleMessage(msg *puller.Message) { | ||
| if id := msg.Attributes["id"]; id == "" { | ||
| msg.Ack() | ||
| subscriberHandledCounter.WithLabelValues(subscriberDemoOrganizationEvent, codes.MissingID.String()).Inc() | ||
| return | ||
| } | ||
| domainEvent, err := d.unmarshalMessage(msg) | ||
| if err != nil { | ||
| d.logger.Error("Failed to unmarshal message", | ||
| zap.Error(err), | ||
| zap.String("msgID", msg.ID), | ||
| zap.String("attributes", fmt.Sprintf("%+v", msg.Attributes)), | ||
| ) | ||
| subscriberHandledCounter.WithLabelValues(subscriberDemoOrganizationEvent, codes.BadMessage.String()).Inc() | ||
| msg.Ack() | ||
| return | ||
| } | ||
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
| defer cancel() | ||
|
|
||
| if domainEvent.Type != domainevent.Event_DEMO_ORGANIZATION_CREATED { | ||
| subscriberHandledCounter.WithLabelValues(subscriberDemoOrganizationEvent, codes.OK.String()).Inc() | ||
| msg.Ack() | ||
| return | ||
| } | ||
|
|
||
| var organizationCreatedEvent domaineventproto.OrganizationCreatedEvent | ||
| if err := domainEvent.Data.UnmarshalTo(&organizationCreatedEvent); err != nil { | ||
| d.logger.Error("Failed to unmarshal OrganizationCreatedEvent", | ||
| zap.String("event id", domainEvent.Id), | ||
| zap.Error(err), | ||
| ) | ||
| subscriberHandledCounter.WithLabelValues( | ||
| subscriberDemoOrganizationEvent, | ||
| codes.NonRepeatableError.String(), | ||
| ).Inc() | ||
| msg.Ack() | ||
| return | ||
| } | ||
|
|
||
| recipient := ¬ificationproto.Recipient{ | ||
| Type: notificationproto.Recipient_SlackChannel, | ||
| Language: notificationproto.Recipient_ENGLISH, | ||
| SlackChannelRecipient: ¬ificationproto.SlackChannelRecipient{ | ||
| WebhookUrl: d.demoOrganizationCreationNotifierConfig.Notifier.Slack.WebHookURL, | ||
| }, | ||
| } | ||
|
|
||
| err = d.slackNotifier.Notify(ctx, &senderproto.Notification{ | ||
| Type: senderproto.Notification_DemoOrganizationCreation, | ||
| DemoOrganizationCreationNotification: &senderproto.DemoOrganizationCreationNotification{ | ||
| OwnerEmail: organizationCreatedEvent.OwnerEmail, | ||
| OrganizationId: organizationCreatedEvent.Id, | ||
| OrganizationName: organizationCreatedEvent.Name, | ||
| }, | ||
| }, recipient, recipient.Language) | ||
| if err != nil { | ||
| d.logger.Error("Failed to send notification", | ||
| zap.Error(err), | ||
| zap.String("event id", domainEvent.Id), | ||
| zap.String("webhookURL", d.demoOrganizationCreationNotifierConfig.Notifier.Slack.WebHookURL), | ||
| zap.String("organizationId", organizationCreatedEvent.Id), | ||
| ) | ||
| subscriberHandledCounter.WithLabelValues( | ||
| subscriberDemoOrganizationEvent, | ||
| codes.NonRepeatableError.String(), | ||
| ).Inc() | ||
| msg.Ack() | ||
| return | ||
| } | ||
|
|
||
| subscriberHandledCounter.WithLabelValues( | ||
| subscriberDemoOrganizationEvent, | ||
| codes.OK.String(), | ||
| ).Inc() | ||
| msg.Ack() | ||
| } | ||
|
|
||
| func (d demoOrganizationCreationNotifier) unmarshalMessage(msg *puller.Message) (*domainevent.Event, error) { | ||
| event := &domaineventproto.Event{} | ||
| err := proto.Unmarshal(msg.Data, event) | ||
| if err != nil { | ||
| d.logger.Error("Failed to unmarshal message", zap.Error(err), zap.String("msgID", msg.ID)) | ||
| return nil, err | ||
| } | ||
| return event, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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.
In this case, is there no need for logs or
subscriberHandledCounter.Inc()?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.
Yeah, I think we should ignore events that aren't relevant
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.
I also think that unrelated events should be ignored.
In that case, isn't it necessary to collect logs or call
subscriberHandledCounter.Inc()?When I check the code for other errors or irregular cases, I see that logs are collected and
subscriberHandledCounter.Inc()is called.https://github.com/bucketeer-io/bucketeer/blob/notify-demo-org-create/pkg/subscriber/processor/demo_organization_creation_notifier.go#L107-L116
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.
Oh that error case means that something already went wrong with the message and we don't know if that message is relevant or not
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.
The
subscriberHandledCounter.Inc()should be called whenever the processing fails or not.It means that the processor tried to handle the messages.
We also have another metric that reports what happened.
E.g.
It could OK, MissingID, etc
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.
Got it, I've added the subscriberHandledCounter.Inc() for this