Skip to content

Commit

Permalink
customize alert add dingtalk_work_notice and delete vms (erda-project…
Browse files Browse the repository at this point in the history
…#3177) (erda-project#3197)

* customize alert add dingtalk_work_notice and delete vms

* Optimize logical judgment

* Optimize logical judgment

* Optimize logical judgment

* Optimize logical judgment

* Optimize logical judgment

* Optimize logical judgment

Co-authored-by: panjiayao <44628544+Counterflowwind@users.noreply.github.com>
  • Loading branch information
erda-bot and Counterflowwind authored Nov 26, 2021
1 parent 5c77588 commit 279633a
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 21 deletions.
1 change: 1 addition & 0 deletions cmd/monitor/monitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/erda-project/erda/pkg/common"

// modules
_ "github.com/erda-project/erda-proto-go/core/services/notify/channel/client"
_ "github.com/erda-project/erda/modules/core/monitor/alert/alert-apis"
_ "github.com/erda-project/erda/modules/core/monitor/alert/details-apis"
_ "github.com/erda-project/erda/modules/core/monitor/dataview"
Expand Down
2 changes: 1 addition & 1 deletion conf/monitor/monitor/i18n/alert.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ zh:
alert: 告警
recover: 恢复
# notify_target
dingding: 钉钉
dingding: 钉钉群
email: 邮箱
vms: 电话
sms: 短信
Expand Down
4 changes: 4 additions & 0 deletions conf/monitor/monitor/monitor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,7 @@ monitor-monitoring:
usage_sync_interval:
metric: ${MONITOR_MONITORING_METRIC_SYNC_INTERVAL:1h}
log: ${MONITOR_MONITORING_LOG_SYNC_INTERVAL:30m}

grpc-client@erda.core.services.notify.channel:
addr: "${CORE_SERVICES_GRPC_ADDR:core-services:9537}"
erda.core.services.notify.channel-client:
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,10 @@ func (s *notifyChannelService) GetNotifyChannelsEnabled(ctx context.Context, req
if err != nil {
return nil, err
}
targetList := []string{"dingding", "webhook", "email", "mbox", "ticket"}
for _, v := range targetList {
result.Data[v] = true
}
return result, nil
}

Expand Down
21 changes: 10 additions & 11 deletions modules/core/monitor/alert/alert-apis/adapt/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ const (
HostIP = "host_ip"
)

const (
SMS = "sms"
shortMessage = "short_message"
dingtalkWorkNotice = "dingtalk_work_notice"
)

type (
// DisplayKey .
DisplayKey struct {
Expand Down Expand Up @@ -195,18 +201,11 @@ func (a *Adapt) AggregatorKeysSet() map[string]bool {
}

// NotifyTargetsKeys .
func (a *Adapt) NotifyTargetsKeys(code i18n.LanguageCodes, orgId string) []*pb.DisplayKey {
func (a *Adapt) NotifyTargetsKeys(code i18n.LanguageCodes, config map[string]bool) []*pb.DisplayKey {
var keys []*pb.DisplayKey
for _, item := range notifyTargets {

if item == "vms" || item == "sms" {
config, err := a.bdl.GetNotifyConfig(orgId, "")
if err != nil {
continue
}
if !config.Config.EnableMS {
continue
}
for item := range config {
if item == shortMessage {
keys = append(keys, &pb.DisplayKey{Key: SMS, Display: a.t.Text(code, SMS)})
}
keys = append(keys, &pb.DisplayKey{Key: item, Display: a.t.Text(code, item)})
}
Expand Down
16 changes: 13 additions & 3 deletions modules/core/monitor/alert/alert-apis/alert.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/erda-project/erda-proto-go/core/monitor/alert/pb"
metricpb "github.com/erda-project/erda-proto-go/core/monitor/metric/pb"
channelpb "github.com/erda-project/erda-proto-go/core/services/notify/channel/pb"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/adapt"
"github.com/erda-project/erda/modules/monitor/utils"
Expand Down Expand Up @@ -183,7 +184,12 @@ func (m *alertService) QueryCustomizeNotifyTarget(ctx context.Context, request *
Data: &pb.QueryCustomizeNotifyTargetData{},
}
lang := apis.Language(ctx)
data := m.p.a.NotifyTargetsKeys(lang, apis.GetOrgID(ctx))
context := utils.NewContextWithHeader(ctx)
config, err := m.p.NotifyChannel.GetNotifyChannelsEnabled(context, &channelpb.GetNotifyChannelsEnabledRequest{})
if err != nil {
return nil, errors.NewInternalServerError(err)
}
data := m.p.a.NotifyTargetsKeys(lang, config.Data)
result.Data.Targets = data
return result, nil
}
Expand All @@ -193,8 +199,12 @@ func (m *alertService) QueryOrgCustomizeNotifyTarget(ctx context.Context, reques
Data: &pb.QueryCustomizeNotifyTargetData{},
}
lang := apis.Language(ctx)
orgID := apis.GetOrgID(ctx)
data := m.p.a.NotifyTargetsKeys(lang, orgID)
context := utils.NewContextWithHeader(ctx)
config, err := m.p.NotifyChannel.GetNotifyChannelsEnabled(context, &channelpb.GetNotifyChannelsEnabledRequest{})
if err != nil {
return nil, errors.NewInternalServerError(err)
}
data := m.p.a.NotifyTargetsKeys(lang, config.Data)
result.Data.Targets = data
return result, nil
}
Expand Down
4 changes: 2 additions & 2 deletions modules/core/monitor/alert/alert-apis/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ func (p *provider) CustomizeMetrics(lang i18n.LanguageCodes, scope, scopeID stri
return p.a.CustomizeMetrics(lang, scope, scopeID, names)
}

func (p *provider) NotifyTargetsKeys(lang i18n.LanguageCodes, orgId string) []*pb.DisplayKey {
return p.a.NotifyTargetsKeys(lang, orgId)
func (p *provider) NotifyTargetsKeys(lang i18n.LanguageCodes, config map[string]bool) []*pb.DisplayKey {
return p.a.NotifyTargetsKeys(lang, config)
}

func (p *provider) CustomizeAlerts(lang i18n.LanguageCodes, scope, scopeID string, pageNo, pageSize int) ([]*pb.CustomizeAlertOverview, int, error) {
Expand Down
10 changes: 6 additions & 4 deletions modules/core/monitor/alert/alert-apis/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/erda-project/erda-infra/providers/mysql"
"github.com/erda-project/erda-proto-go/core/monitor/alert/pb"
metricpb "github.com/erda-project/erda-proto-go/core/monitor/metric/pb"
channelpb "github.com/erda-project/erda-proto-go/core/services/notify/channel/pb"
"github.com/erda-project/erda/bundle"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/adapt"
"github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/cql"
Expand Down Expand Up @@ -72,10 +73,11 @@ type provider struct {
microServiceOtherFilterTags map[string]bool
alertConditions []*AlertConditions

Register transport.Register `autowired:"service-register" optional:"true"`
Metric metricpb.MetricServiceServer `autowired:"erda.core.monitor.metric.MetricService"`
Perm perm.Interface `autowired:"permission"`
alertService *alertService
Register transport.Register `autowired:"service-register" optional:"true"`
Metric metricpb.MetricServiceServer `autowired:"erda.core.monitor.metric.MetricService"`
Perm perm.Interface `autowired:"permission"`
alertService *alertService
NotifyChannel channelpb.NotifyChannelServiceServer `autowired:"erda.core.services.notify.channel.NotifyChannelService"`
}

func (p *provider) Init(ctx servicehub.Context) error {
Expand Down

0 comments on commit 279633a

Please sign in to comment.