simplify notification.Start

This commit is contained in:
Elio Bischof
2023-09-26 16:44:39 +02:00
parent e9869f383e
commit e2119d1617
2 changed files with 53 additions and 30 deletions

View File

@@ -3,18 +3,63 @@ package notification
import (
"context"
"github.com/zitadel/logging"
"github.com/zitadel/zitadel/internal/notification/channels/smtp"
"github.com/zitadel/zitadel/internal/notification/channels/twilio"
"github.com/zitadel/zitadel/internal/notification/channels/webhook"
"github.com/zitadel/zitadel/internal/notification/handlers"
"github.com/zitadel/zitadel/internal/notification/senders"
"github.com/zitadel/zitadel/internal/notification/types"
"github.com/zitadel/zitadel/internal/telemetry/metrics"
)
var _ types.ChannelChains = (*channels)(nil)
type counters struct {
success deliveryMetrics
failed deliveryMetrics
}
type deliveryMetrics struct {
email string
sms string
json string
}
type channels struct {
q *handlers.NotificationQueries
q *handlers.NotificationQueries
counters counters
}
func newChannels(q *handlers.NotificationQueries) *channels {
c := &channels{
q: q,
counters: counters{
success: deliveryMetrics{
email: "successful_deliveries_email",
sms: "successful_deliveries_sms",
json: "successful_deliveries_json",
},
failed: deliveryMetrics{
email: "failed_deliveries_email",
sms: "failed_deliveries_sms",
json: "failed_deliveries_json",
},
},
}
registerCounter(c.counters.success.email, "Successfully delivered emails")
registerCounter(c.counters.failed.email, "Failed email deliveries")
registerCounter(c.counters.success.sms, "Successfully delivered SMS")
registerCounter(c.counters.failed.sms, "Failed SMS deliveries")
registerCounter(c.counters.success.json, "Successfully delivered JSON messages")
registerCounter(c.counters.failed.json, "Failed JSON message deliveries")
return c
}
func registerCounter(counter, desc string) {
err := metrics.RegisterCounter(counter, desc)
logging.WithFields("metric", counter).OnError(err).Panic("unable to register counter")
}
func (c *channels) Email(ctx context.Context) (*senders.Chain, *smtp.Config, error) {
@@ -27,8 +72,8 @@ func (c *channels) Email(ctx context.Context) (*senders.Chain, *smtp.Config, err
smtpCfg,
c.q.GetFileSystemProvider,
c.q.GetLogProvider,
metricSuccessfulDeliveriesEmail,
metricFailedDeliveriesEmail,
c.counters.success.email,
c.counters.failed.email,
)
return chain, smtpCfg, err
}
@@ -43,8 +88,8 @@ func (c *channels) SMS(ctx context.Context) (*senders.Chain, *twilio.Config, err
twilioCfg,
c.q.GetFileSystemProvider,
c.q.GetLogProvider,
metricSuccessfulDeliveriesSMS,
metricFailedDeliveriesSMS,
c.counters.success.sms,
c.counters.failed.sms,
)
return chain, twilioCfg, err
}
@@ -55,7 +100,7 @@ func (c *channels) Webhook(ctx context.Context, cfg webhook.Config) (*senders.Ch
cfg,
c.q.GetFileSystemProvider,
c.q.GetLogProvider,
metricSuccessfulDeliveriesJSON,
metricFailedDeliveriesJSON,
c.counters.success.json,
c.counters.failed.json,
)
}

View File

@@ -13,16 +13,6 @@ import (
_ "github.com/zitadel/zitadel/internal/notification/statik"
"github.com/zitadel/zitadel/internal/query"
"github.com/zitadel/zitadel/internal/query/projection"
"github.com/zitadel/zitadel/internal/telemetry/metrics"
)
const (
metricSuccessfulDeliveriesEmail = "successful_deliveries_email"
metricFailedDeliveriesEmail = "failed_deliveries_email"
metricSuccessfulDeliveriesSMS = "successful_deliveries_sms"
metricFailedDeliveriesSMS = "failed_deliveries_sms"
metricSuccessfulDeliveriesJSON = "successful_deliveries_json"
metricFailedDeliveriesJSON = "failed_deliveries_json"
)
func Start(
@@ -41,20 +31,8 @@ func Start(
) {
statikFS, err := statik_fs.NewWithNamespace("notification")
logging.OnError(err).Panic("unable to start listener")
err = metrics.RegisterCounter(metricSuccessfulDeliveriesEmail, "Successfully delivered emails")
logging.WithFields("metric", metricSuccessfulDeliveriesEmail).OnError(err).Panic("unable to register counter")
err = metrics.RegisterCounter(metricFailedDeliveriesEmail, "Failed email deliveries")
logging.WithFields("metric", metricFailedDeliveriesEmail).OnError(err).Panic("unable to register counter")
err = metrics.RegisterCounter(metricSuccessfulDeliveriesSMS, "Successfully delivered SMS")
logging.WithFields("metric", metricSuccessfulDeliveriesSMS).OnError(err).Panic("unable to register counter")
err = metrics.RegisterCounter(metricFailedDeliveriesSMS, "Failed SMS deliveries")
logging.WithFields("metric", metricFailedDeliveriesSMS).OnError(err).Panic("unable to register counter")
err = metrics.RegisterCounter(metricSuccessfulDeliveriesJSON, "Successfully delivered JSON messages")
logging.WithFields("metric", metricSuccessfulDeliveriesJSON).OnError(err).Panic("unable to register counter")
err = metrics.RegisterCounter(metricFailedDeliveriesJSON, "Failed JSON message deliveries")
logging.WithFields("metric", metricFailedDeliveriesJSON).OnError(err).Panic("unable to register counter")
q := handlers.NewNotificationQueries(queries, es, externalDomain, externalPort, externalSecure, fileSystemPath, userEncryption, smtpEncryption, smsEncryption, statikFS)
c := &channels{q: q}
c := newChannels(q)
handlers.NewUserNotifier(ctx, projection.ApplyCustomConfig(userHandlerCustomConfig), commands, q, c, otpEmailTmpl).Start()
handlers.NewQuotaNotifier(ctx, projection.ApplyCustomConfig(quotaHandlerCustomConfig), commands, q, c).Start()
if telemetryCfg.Enabled {