Compare commits

..

1 Commits

Author SHA1 Message Date
Xu Bin
c74014d53c refactor: global webhook add env proxy (#2367) 2024-12-20 14:20:52 +08:00
8 changed files with 224 additions and 295 deletions

9
go.mod
View File

@@ -41,21 +41,18 @@ require (
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/yaml.v2 v2.4.0
gorm.io/driver/mysql v1.4.4
gorm.io/driver/postgres v1.5.11
gorm.io/driver/postgres v1.4.5
gorm.io/driver/sqlite v1.5.5
gorm.io/gorm v1.25.12
gorm.io/gorm v1.25.7
)
require (
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/glebarez/go-sqlite v1.21.2 // indirect
github.com/jackc/pgx/v5 v5.7.1 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/yuin/gopher-lua v1.1.1 // indirect
golang.org/x/sync v0.10.0 // indirect
modernc.org/libc v1.22.5 // indirect
modernc.org/mathutil v1.5.0 // indirect
modernc.org/memory v1.5.0 // indirect
@@ -90,7 +87,7 @@ require (
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/jackc/pgx/v4 v4.17.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect

11
go.sum
View File

@@ -166,8 +166,6 @@ github.com/jackc/pgproto3/v2 v2.3.1 h1:nwj7qwf0S+Q7ISFfBndqeLwSwxs+4DPsbRFjECT1Y
github.com/jackc/pgproto3/v2 v2.3.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b h1:C8S2+VttkHFdOOCXJe+YGfa4vHYwlt4Zx+IVXQ97jYg=
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg=
github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCMSo2DXpzsoWOAfFkdEtEJpPbVLq8eE+mc=
github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw=
@@ -180,15 +178,10 @@ github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQ
github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs=
github.com/jackc/pgx/v4 v4.17.2 h1:0Ut0rpeKwvIVbMQ1KbMBU4h6wxehBI535LK6Flheh8E=
github.com/jackc/pgx/v4 v4.17.2/go.mod h1:lcxIZN44yMIrWI78a5CpucdD14hX0SBDbNRvjDBItsw=
github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs=
github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA=
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.3.0 h1:eHK/5clGOatcjX3oWGBO/MpxpbHzSwud5EWTSCI+MX0=
github.com/jackc/puddle v1.3.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
@@ -520,16 +513,12 @@ gorm.io/driver/mysql v1.4.4 h1:MX0K9Qvy0Na4o7qSC/YI7XxqUw5KDw01umqgID+svdQ=
gorm.io/driver/mysql v1.4.4/go.mod h1:BCg8cKI+R0j/rZRQxeKis/forqRwRSYOR8OM3Wo6hOM=
gorm.io/driver/postgres v1.4.5 h1:mTeXTTtHAgnS9PgmhN2YeUbazYpLhUI1doLnw42XUZc=
gorm.io/driver/postgres v1.4.5/go.mod h1:GKNQYSJ14qvWkvPwXljMGehpKrhlDNsqYRr5HnYGncg=
gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314=
gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
gorm.io/driver/sqlite v1.5.5 h1:7MDMtUZhV065SilG62E0MquljeArQZNfJnjd9i9gx3E=
gorm.io/driver/sqlite v1.5.5/go.mod h1:6NgQ7sQWAIFsPrJJl1lSNSu2TABh0ZZ/zm5fosATavE=
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.24.1-0.20221019064659-5dd2bb482755/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.25.7 h1:VsD6acwRjz2zFxGO50gPO6AkNs7KKnvfzUjHQhZDz/A=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE=
modernc.org/libc v1.22.5/go.mod h1:jj+Z7dTNX8fBScMVNRAYZ/jF91K8fdT2hYMThc3YjBY=

View File

@@ -8,13 +8,14 @@ import (
"sync"
"time"
"github.com/ccfos/nightingale/v6/pkg/tplx"
"github.com/BurntSushi/toml"
"github.com/ccfos/nightingale/v6/alert/aconf"
"github.com/ccfos/nightingale/v6/dumper"
"github.com/ccfos/nightingale/v6/models"
"github.com/ccfos/nightingale/v6/pkg/ctx"
"github.com/ccfos/nightingale/v6/pkg/poster"
"github.com/ccfos/nightingale/v6/pkg/tplx"
"github.com/BurntSushi/toml"
"github.com/toolkits/pkg/logger"
)
@@ -114,13 +115,18 @@ func (w *NotifyConfigCacheType) syncNotifyConfigs() error {
}
if webhooks[i].Client == nil {
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: webhooks[i].SkipVerify},
}
if poster.UseProxy(webhooks[i].Url) {
transport.Proxy = http.ProxyFromEnvironment
}
webhooks[i].Client = &http.Client{
Timeout: time.Second * time.Duration(webhooks[i].Timeout),
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: webhooks[i].SkipVerify},
},
Timeout: time.Second * time.Duration(webhooks[i].Timeout),
Transport: transport,
}
}
newWebhooks[webhooks[i].Url] = webhooks[i]
}

View File

@@ -115,74 +115,53 @@ func BusiGroupExists(ctx *ctx.Context, where string, args ...interface{}) (bool,
return num > 0, err
}
// RegisterGroupDelCheckEntries 提供给外部注册删除 group 时需要检查的表
func RegisterGroupDelCheckEntries(e []CheckEntry) {
entries = append(entries, e...)
}
type CheckEntry struct {
Entry interface{}
ErrorMessage string
FieldName string
}
var entries = []CheckEntry{
var entries = []struct {
entry interface{}
errorMessage string
}{
{
Entry: &AlertRule{},
ErrorMessage: "Some alert rules still in the BusiGroup",
FieldName: "group_id",
entry: &AlertRule{},
errorMessage: "Some alert rules still in the BusiGroup",
},
{
Entry: &AlertMute{},
ErrorMessage: "Some alert mutes still in the BusiGroup",
FieldName: "group_id",
entry: &AlertMute{},
errorMessage: "Some alert mutes still in the BusiGroup",
},
{
Entry: &AlertSubscribe{},
ErrorMessage: "Some alert subscribes still in the BusiGroup",
FieldName: "group_id",
entry: &AlertSubscribe{},
errorMessage: "Some alert subscribes still in the BusiGroup",
},
{
Entry: &Board{},
ErrorMessage: "Some Board still in the BusiGroup",
FieldName: "group_id",
entry: &Target{},
errorMessage: "Some targets still in the BusiGroup",
},
{
Entry: &Target{},
ErrorMessage: "Some targets still in the BusiGroup",
FieldName: "group_id",
entry: &RecordingRule{},
errorMessage: "Some recording rules still in the BusiGroup",
},
{
Entry: &RecordingRule{},
ErrorMessage: "Some recording rules still in the BusiGroup",
FieldName: "group_id",
entry: &TaskTpl{},
errorMessage: "Some recovery scripts still in the BusiGroup",
},
{
Entry: &TaskTpl{},
ErrorMessage: "Some recovery scripts still in the BusiGroup",
FieldName: "group_id",
entry: &TaskRecord{},
errorMessage: "Some Task Record records still in the BusiGroup",
},
{
Entry: &TaskRecord{},
ErrorMessage: "Some Task Record records still in the BusiGroup",
FieldName: "group_id",
},
{
Entry: &TargetBusiGroup{},
ErrorMessage: "Some target busigroups still in the BusiGroup",
FieldName: "group_id",
entry: &TargetBusiGroup{},
errorMessage: "Some target busigroups still in the BusiGroup",
},
}
func (bg *BusiGroup) Del(ctx *ctx.Context) error {
for _, e := range entries {
has, err := Exists(DB(ctx).Model(e.Entry).Where(fmt.Sprintf("%s=?", e.FieldName), bg.Id))
has, err := Exists(DB(ctx).Model(e.entry).Where("group_id=?", bg.Id))
if err != nil {
return err
}
if has {
return errors.New(e.ErrorMessage)
return errors.New(e.errorMessage)
}
}

View File

@@ -340,43 +340,42 @@ func (InitChartShare) TableOptions() string {
}
type InitAlertRule struct {
ID uint64 `gorm:"primaryKey;autoIncrement"`
GroupID uint64 `gorm:"not null;default:0;comment:busi group id;index"`
Cate string `gorm:"size:128;not null"`
DatasourceIDs string `gorm:"size:255;not null;default:'';comment:datasource ids"`
Cluster string `gorm:"size:128;not null"`
Name string `gorm:"size:255;not null"`
Note string `gorm:"size:1024;not null;default:''"`
Prod string `gorm:"size:255;not null;default:''"`
Algorithm string `gorm:"size:255;not null;default:''"`
AlgoParams string `gorm:"size:255"`
Delay int32 `gorm:"not null;default:0"`
Severity int16 `gorm:"type:tinyint(1);not null;comment:1:Emergency 2:Warning 3:Notice"`
Disabled bool `gorm:"type:tinyint(1);not null;comment:0:enabled 1:disabled"`
PromForDuration int32 `gorm:"not null;comment:prometheus for, unit:s"`
RuleConfig string `gorm:"type:text;not null;comment:rule_config"`
PromQL string `gorm:"type:text;not null;comment:promql"`
PromEvalInterval int32 `gorm:"not null;comment:evaluate interval"`
EnableStime string `gorm:"size:255;not null;default:'00:00'"`
EnableEtime string `gorm:"size:255;not null;default:'23:59'"`
EnableDaysOfWeek string `gorm:"size:255;not null;default:'';comment:split by space: 0 1 2 3 4 5 6"`
EnableInBg bool `gorm:"type:tinyint(1);not null;default:0;comment:1: only this bg 0: global"`
NotifyRecovered bool `gorm:"type:tinyint(1);not null;comment:whether notify when recovery"`
NotifyChannels string `gorm:"size:255;not null;default:'';comment:split by space: sms voice email dingtalk wecom"`
NotifyGroups string `gorm:"size:255;not null;default:'';comment:split by space: 233 43"`
NotifyRepeatStep int32 `gorm:"not null;default:0;comment:unit: min"`
NotifyMaxNumber int32 `gorm:"not null;default:0"`
RecoverDuration int32 `gorm:"not null;default:0;comment:unit: s"`
Callbacks string `gorm:"size:4096;not null;default:'';comment:split by space: http://a.com/api/x http://a.com/api/y"`
RunbookURL string `gorm:"size:4096"`
AppendTags string `gorm:"size:255;not null;default:'';comment:split by space: service=n9e mod=api"`
Annotations string `gorm:"type:text;not null;comment:annotations"`
ExtraConfig string `gorm:"type:text;not null;comment:extra_config"`
CreateAt int64 `gorm:"not null;default:0"`
CreateBy string `gorm:"size:64;not null;default:''"`
UpdateAt int64 `gorm:"not null;default:0;index"`
UpdateBy string `gorm:"size:64;not null;default:''"`
DatasourceQueries string `gorm:"type:text"`
ID uint64 `gorm:"primaryKey;autoIncrement"`
GroupID uint64 `gorm:"not null;default:0;comment:busi group id;index"`
Cate string `gorm:"size:128;not null"`
DatasourceIDs string `gorm:"size:255;not null;default:'';comment:datasource ids"`
Cluster string `gorm:"size:128;not null"`
Name string `gorm:"size:255;not null"`
Note string `gorm:"size:1024;not null;default:''"`
Prod string `gorm:"size:255;not null;default:''"`
Algorithm string `gorm:"size:255;not null;default:''"`
AlgoParams string `gorm:"size:255"`
Delay int32 `gorm:"not null;default:0"`
Severity int16 `gorm:"type:tinyint(1);not null;comment:1:Emergency 2:Warning 3:Notice"`
Disabled bool `gorm:"type:tinyint(1);not null;comment:0:enabled 1:disabled"`
PromForDuration int32 `gorm:"not null;comment:prometheus for, unit:s"`
RuleConfig string `gorm:"type:text;not null;comment:rule_config"`
PromQL string `gorm:"type:text;not null;comment:promql"`
PromEvalInterval int32 `gorm:"not null;comment:evaluate interval"`
EnableStime string `gorm:"size:255;not null;default:'00:00'"`
EnableEtime string `gorm:"size:255;not null;default:'23:59'"`
EnableDaysOfWeek string `gorm:"size:255;not null;default:'';comment:split by space: 0 1 2 3 4 5 6"`
EnableInBg bool `gorm:"type:tinyint(1);not null;default:0;comment:1: only this bg 0: global"`
NotifyRecovered bool `gorm:"type:tinyint(1);not null;comment:whether notify when recovery"`
NotifyChannels string `gorm:"size:255;not null;default:'';comment:split by space: sms voice email dingtalk wecom"`
NotifyGroups string `gorm:"size:255;not null;default:'';comment:split by space: 233 43"`
NotifyRepeatStep int32 `gorm:"not null;default:0;comment:unit: min"`
NotifyMaxNumber int32 `gorm:"not null;default:0"`
RecoverDuration int32 `gorm:"not null;default:0;comment:unit: s"`
Callbacks string `gorm:"size:4096;not null;default:'';comment:split by space: http://a.com/api/x http://a.com/api/y"`
RunbookURL string `gorm:"size:4096"`
AppendTags string `gorm:"size:255;not null;default:'';comment:split by space: service=n9e mod=api"`
Annotations string `gorm:"type:text;not null;comment:annotations"`
ExtraConfig string `gorm:"type:text;not null;comment:extra_config"`
CreateAt int64 `gorm:"not null;default:0"`
CreateBy string `gorm:"size:64;not null;default:''"`
UpdateAt int64 `gorm:"not null;default:0;index"`
UpdateBy string `gorm:"size:64;not null;default:''"`
}
func (InitAlertRule) TableName() string {
@@ -388,43 +387,42 @@ func (InitAlertRule) TableOptions() string {
}
type InitPostgresAlertRule struct {
ID uint64 `gorm:"primaryKey;autoIncrement"`
GroupID uint64 `gorm:"not null;default:0;comment:busi group id;index"`
Cate string `gorm:"size:128;not null"`
DatasourceIDs string `gorm:"size:255;not null;default:'';comment:datasource ids"`
Cluster string `gorm:"size:128;not null"`
Name string `gorm:"size:255;not null"`
Note string `gorm:"size:1024;not null;default:''"`
Prod string `gorm:"size:255;not null;default:''"`
Algorithm string `gorm:"size:255;not null;default:''"`
AlgoParams string `gorm:"size:255"`
Delay int32 `gorm:"not null;default:0"`
Severity int16 `gorm:"type:smallint;not null;comment:1:Emergency 2:Warning 3:Notice"`
Disabled int16 `gorm:"type:smallint;not null;comment:0:enabled 1:disabled"`
PromForDuration int32 `gorm:"not null;comment:prometheus for, unit:s"`
RuleConfig string `gorm:"type:text;not null;comment:rule_config"`
PromQL string `gorm:"type:text;not null;comment:promql"`
PromEvalInterval int32 `gorm:"not null;comment:evaluate interval"`
EnableStime string `gorm:"size:255;not null;default:'00:00'"`
EnableEtime string `gorm:"size:255;not null;default:'23:59'"`
EnableDaysOfWeek string `gorm:"size:255;not null;default:'';comment:split by space: 0 1 2 3 4 5 6"`
EnableInBg int16 `gorm:"type:smallint;not null;default:0;comment:1: only this bg 0: global"`
NotifyRecovered int16 `gorm:"type:smallint;not null;comment:whether notify when recovery"`
NotifyChannels string `gorm:"size:255;not null;default:'';comment:split by space: sms voice email dingtalk wecom"`
NotifyGroups string `gorm:"size:255;not null;default:'';comment:split by space: 233 43"`
NotifyRepeatStep int32 `gorm:"not null;default:0;comment:unit: min"`
NotifyMaxNumber int32 `gorm:"not null;default:0"`
RecoverDuration int32 `gorm:"not null;default:0;comment:unit: s"`
Callbacks string `gorm:"size:4096;not null;default:'';comment:split by space: http://a.com/api/x http://a.com/api/y"`
RunbookURL string `gorm:"size:4096"`
AppendTags string `gorm:"size:255;not null;default:'';comment:split by space: service=n9e mod=api"`
Annotations string `gorm:"type:text;not null;comment:annotations"`
ExtraConfig string `gorm:"type:text;not null;comment:extra_config"`
CreateAt int64 `gorm:"not null;default:0"`
CreateBy string `gorm:"size:64;not null;default:''"`
UpdateAt int64 `gorm:"not null;default:0;index"`
UpdateBy string `gorm:"size:64;not null;default:''"`
DatasourceQueries string `gorm:"type:text"`
ID uint64 `gorm:"primaryKey;autoIncrement"`
GroupID uint64 `gorm:"not null;default:0;comment:busi group id;index"`
Cate string `gorm:"size:128;not null"`
DatasourceIDs string `gorm:"size:255;not null;default:'';comment:datasource ids"`
Cluster string `gorm:"size:128;not null"`
Name string `gorm:"size:255;not null"`
Note string `gorm:"size:1024;not null;default:''"`
Prod string `gorm:"size:255;not null;default:''"`
Algorithm string `gorm:"size:255;not null;default:''"`
AlgoParams string `gorm:"size:255"`
Delay int32 `gorm:"not null;default:0"`
Severity int16 `gorm:"type:smallint;not null;comment:1:Emergency 2:Warning 3:Notice"`
Disabled int16 `gorm:"type:smallint;not null;comment:0:enabled 1:disabled"`
PromForDuration int32 `gorm:"not null;comment:prometheus for, unit:s"`
RuleConfig string `gorm:"type:text;not null;comment:rule_config"`
PromQL string `gorm:"type:text;not null;comment:promql"`
PromEvalInterval int32 `gorm:"not null;comment:evaluate interval"`
EnableStime string `gorm:"size:255;not null;default:'00:00'"`
EnableEtime string `gorm:"size:255;not null;default:'23:59'"`
EnableDaysOfWeek string `gorm:"size:255;not null;default:'';comment:split by space: 0 1 2 3 4 5 6"`
EnableInBg int16 `gorm:"type:smallint;not null;default:0;comment:1: only this bg 0: global"`
NotifyRecovered int16 `gorm:"type:smallint;not null;comment:whether notify when recovery"`
NotifyChannels string `gorm:"size:255;not null;default:'';comment:split by space: sms voice email dingtalk wecom"`
NotifyGroups string `gorm:"size:255;not null;default:'';comment:split by space: 233 43"`
NotifyRepeatStep int32 `gorm:"not null;default:0;comment:unit: min"`
NotifyMaxNumber int32 `gorm:"not null;default:0"`
RecoverDuration int32 `gorm:"not null;default:0;comment:unit: s"`
Callbacks string `gorm:"size:4096;not null;default:'';comment:split by space: http://a.com/api/x http://a.com/api/y"`
RunbookURL string `gorm:"size:4096"`
AppendTags string `gorm:"size:255;not null;default:'';comment:split by space: service=n9e mod=api"`
Annotations string `gorm:"type:text;not null;comment:annotations"`
ExtraConfig string `gorm:"type:text;not null;comment:extra_config"`
CreateAt int64 `gorm:"not null;default:0"`
CreateBy string `gorm:"size:64;not null;default:''"`
UpdateAt int64 `gorm:"not null;default:0;index"`
UpdateBy string `gorm:"size:64;not null;default:''"`
}
func (InitPostgresAlertRule) TableName() string {
@@ -614,23 +612,22 @@ func (InitPostgresMetricView) TableName() string {
}
type InitRecordingRule struct {
ID uint64 `gorm:"primaryKey;autoIncrement"`
GroupID uint64 `gorm:"not null;default:0;comment:group_id;index"`
DatasourceIDs string `gorm:"size:255;not null;default:'';comment:datasource ids"`
Cluster string `gorm:"size:128;not null"`
Name string `gorm:"size:255;not null;comment:new metric name"`
Note string `gorm:"size:255;not null;comment:rule note"`
Disabled bool `gorm:"type:tinyint(1);not null;default:0;comment:0:enabled 1:disabled"`
PromQL string `gorm:"size:8192;not null;comment:promql"`
PromEvalInterval int32 `gorm:"not null;comment:evaluate interval"`
CronPattern string `gorm:"size:255;default:'';comment:cron pattern"`
AppendTags string `gorm:"size:255;default:'';comment:split by space: service=n9e mod=api"`
QueryConfigs string `gorm:"type:text;not null;comment:query configs"`
CreateAt int64 `gorm:"default:0"`
CreateBy string `gorm:"size:64;default:''"`
UpdateAt int64 `gorm:"default:0;index"`
UpdateBy string `gorm:"size:64;default:''"`
DatasourceQueries string `gorm:"type:text"`
ID uint64 `gorm:"primaryKey;autoIncrement"`
GroupID uint64 `gorm:"not null;default:0;comment:group_id;index"`
DatasourceIDs string `gorm:"size:255;not null;default:'';comment:datasource ids"`
Cluster string `gorm:"size:128;not null"`
Name string `gorm:"size:255;not null;comment:new metric name"`
Note string `gorm:"size:255;not null;comment:rule note"`
Disabled bool `gorm:"type:tinyint(1);not null;default:0;comment:0:enabled 1:disabled"`
PromQL string `gorm:"size:8192;not null;comment:promql"`
PromEvalInterval int32 `gorm:"not null;comment:evaluate interval"`
CronPattern string `gorm:"size:255;default:'';comment:cron pattern"`
AppendTags string `gorm:"size:255;default:'';comment:split by space: service=n9e mod=api"`
QueryConfigs string `gorm:"type:text;not null;comment:query configs"`
CreateAt int64 `gorm:"default:0"`
CreateBy string `gorm:"size:64;default:''"`
UpdateAt int64 `gorm:"default:0;index"`
UpdateBy string `gorm:"size:64;default:''"`
}
func (InitRecordingRule) TableName() string {
@@ -642,23 +639,22 @@ func (InitRecordingRule) TableOptions() string {
}
type InitPostgresRecordingRule struct {
ID uint64 `gorm:"primaryKey;autoIncrement"`
GroupID uint64 `gorm:"not null;default:0;comment:group_id;index"`
DatasourceIDs string `gorm:"size:255;not null;default:'';comment:datasource ids"`
Cluster string `gorm:"size:128;not null"`
Name string `gorm:"size:255;not null;comment:new metric name"`
Note string `gorm:"size:255;not null;comment:rule note"`
Disabled int16 `gorm:"type:smallint;not null;default:0;comment:0:enabled 1:disabled"`
PromQL string `gorm:"size:8192;not null;comment:promql"`
PromEvalInterval int32 `gorm:"not null;comment:evaluate interval"`
CronPattern string `gorm:"size:255;default:'';comment:cron pattern"`
AppendTags string `gorm:"size:255;default:'';comment:split by space: service=n9e mod=api"`
QueryConfigs string `gorm:"type:text;not null;comment:query configs"`
CreateAt int64 `gorm:"default:0"`
CreateBy string `gorm:"size:64;default:''"`
UpdateAt int64 `gorm:"default:0;index"`
UpdateBy string `gorm:"size:64;default:''"`
DatasourceQueries string `gorm:"type:text"`
ID uint64 `gorm:"primaryKey;autoIncrement"`
GroupID uint64 `gorm:"not null;default:0;comment:group_id;index"`
DatasourceIDs string `gorm:"size:255;not null;default:'';comment:datasource ids"`
Cluster string `gorm:"size:128;not null"`
Name string `gorm:"size:255;not null;comment:new metric name"`
Note string `gorm:"size:255;not null;comment:rule note"`
Disabled int16 `gorm:"type:smallint;not null;default:0;comment:0:enabled 1:disabled"`
PromQL string `gorm:"size:8192;not null;comment:promql"`
PromEvalInterval int32 `gorm:"not null;comment:evaluate interval"`
CronPattern string `gorm:"size:255;default:'';comment:cron pattern"`
AppendTags string `gorm:"size:255;default:'';comment:split by space: service=n9e mod=api"`
QueryConfigs string `gorm:"type:text;not null;comment:query configs"`
CreateAt int64 `gorm:"default:0"`
CreateBy string `gorm:"size:64;default:''"`
UpdateAt int64 `gorm:"default:0;index"`
UpdateBy string `gorm:"size:64;default:''"`
}
func (InitPostgresRecordingRule) TableName() string {
@@ -1211,7 +1207,6 @@ type InitBuiltinMetric struct {
CreatedBy string `gorm:"size:191;not null;default:'';comment:creator"`
UpdatedAt int64 `gorm:"not null;default:0;comment:update time"`
UpdatedBy string `gorm:"size:191;not null;default:'';comment:updater"`
UUID int64 `gorm:"not null;default:0;comment:'uuid'"`
}
func (InitBuiltinMetric) TableName() string {
@@ -1235,7 +1230,6 @@ type InitSqliteBuiltinMetric struct {
CreatedBy string `gorm:"size:191;not null;default:'';comment:creator"`
UpdatedAt int64 `gorm:"not null;default:0;comment:update time"`
UpdatedBy string `gorm:"size:191;not null;default:'';comment:updater"`
UUID int64 `gorm:"not null;default:0;comment:'uuid'"`
}
func (InitSqliteBuiltinMetric) TableName() string {

View File

@@ -1,7 +1,6 @@
package ormx
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
@@ -11,43 +10,6 @@ import (
"gorm.io/gorm"
)
func TestCheckPostgresDatabaseExist(t *testing.T) {
tests := []struct {
name string
config DBConfig
}{
{
name: "MySQL",
config: DBConfig{
DBType: "mysql",
DSN: "root:1234@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local&allowNativePasswords=true",
},
},
{
name: "Postgres",
config: DBConfig{
DBType: "postgres",
DSN: "host=127.0.0.1 port=5432 user=root dbname=n9e_v6 password=1234 sslmode=disable",
},
},
{
name: "SQLite",
config: DBConfig{
DBType: "sqlite",
DSN: "./test.db",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exist, err := checkPostgresDatabaseExist(tt.config)
fmt.Printf("exitst: %v", exist)
assert.NoError(t, err)
})
}
}
func TestDataBaseInit(t *testing.T) {
tests := []struct {
name string

View File

@@ -7,8 +7,8 @@ import (
"strings"
"time"
"github.com/glebarez/sqlite"
tklog "github.com/toolkits/pkg/logger"
"github.com/glebarez/sqlite"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
@@ -91,7 +91,7 @@ func createSqliteDatabase(dsn string, gconfig *gorm.Config) error {
if err != nil {
return fmt.Errorf("failed to open temporary connection: %v", err)
}
fmt.Println("sqlite file created")
return nil
@@ -110,7 +110,7 @@ func createPostgresDatabase(dsn string, gconfig *gorm.Config) error {
}
}
createDBQuery := fmt.Sprintf("CREATE DATABASE %s ENCODING='UTF8' LC_COLLATE='en_US.utf8' LC_CTYPE='en_US.utf8';", dbName)
createDBQuery := fmt.Sprintf("CREATE DATABASE %s ENCODING='UTF8' LC_COLLATE='en_US.UTF-8' LC_CTYPE='en_US.UTF-8';", dbName)
tempDialector := postgres.Open(connectionWithoutDB)
@@ -187,16 +187,18 @@ func checkSqliteDatabaseExist(c DBConfig) (bool, error) {
func checkPostgresDatabaseExist(c DBConfig) (bool, error) {
dsnParts := strings.Split(c.DSN, " ")
dbName := ""
dbpair := ""
for _, part := range dsnParts {
if strings.HasPrefix(part, "dbname=") {
dbName = part[strings.Index(part, "=")+1:]
dbpair = part
}
}
connectionStr := strings.Replace(c.DSN, dbpair, "dbname=postgres", 1)
dialector := postgres.Open(connectionStr)
dbName := ""
connectionWithoutDB := ""
for _, part := range dsnParts {
if strings.HasPrefix(part, "dbname=") {
dbName = part[strings.Index(part, "=")+1:]
} else {
connectionWithoutDB += part
connectionWithoutDB += " "
}
}
dialector := postgres.Open(connectionWithoutDB)
gconfig := &gorm.Config{
NamingStrategy: schema.NamingStrategy{
@@ -211,89 +213,89 @@ func checkPostgresDatabaseExist(c DBConfig) (bool, error) {
return false, fmt.Errorf("failed to open database: %v", err)
}
var databases []string
query := genQuery(c)
if err := db.Raw(query).Scan(&databases).Error; err != nil {
return false, fmt.Errorf("failed to query: %v", err)
}
var databases []string
query := genQuery(c)
if err := db.Raw(query).Scan(&databases).Error; err != nil {
return false, fmt.Errorf("failed to query: %v", err)
}
for _, database := range databases {
if database == dbName {
for _, database := range databases {
if database == dbName {
fmt.Println("Database exist")
return true, nil
}
}
return true, nil
}
}
return false, nil
return false, nil
}
func checkMysqlDatabaseExist(c DBConfig) (bool, error) {
dsnParts := strings.SplitN(c.DSN, "/", 2)
if len(dsnParts) != 2 {
return false, fmt.Errorf("failed to parse DSN: %s", c.DSN)
}
if len(dsnParts) != 2 {
return false, fmt.Errorf("failed to parse DSN: %s", c.DSN)
}
connectionInfo := dsnParts[0]
dbInfo := dsnParts[1]
dbName := dbInfo
connectionInfo := dsnParts[0]
dbInfo := dsnParts[1]
dbName := dbInfo
queryIndex := strings.Index(dbInfo, "?")
if queryIndex != -1 {
dbName = dbInfo[:queryIndex]
} else {
return false, fmt.Errorf("failed to parse database name from DSN: %s", c.DSN)
}
queryIndex := strings.Index(dbInfo, "?")
if queryIndex != -1 {
dbName = dbInfo[:queryIndex]
} else {
return false, fmt.Errorf("failed to parse database name from DSN: %s", c.DSN)
}
connectionWithoutDB := connectionInfo + "/?" + dbInfo[queryIndex+1:]
connectionWithoutDB := connectionInfo + "/?" + dbInfo[queryIndex+1:]
var dialector gorm.Dialector
switch strings.ToLower(c.DBType) {
case "mysql":
dialector = mysql.Open(connectionWithoutDB)
case "postgres":
dialector = postgres.Open(connectionWithoutDB)
default:
return false, fmt.Errorf("unsupported database type: %s", c.DBType)
}
var dialector gorm.Dialector
switch strings.ToLower(c.DBType) {
case "mysql":
dialector = mysql.Open(connectionWithoutDB)
case "postgres":
dialector = postgres.Open(connectionWithoutDB)
default:
return false, fmt.Errorf("unsupported database type: %s", c.DBType)
}
gconfig := &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: c.TablePrefix,
SingularTable: true,
},
Logger: gormLogger,
}
gconfig := &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: c.TablePrefix,
SingularTable: true,
},
Logger: gormLogger,
}
db, err := gorm.Open(dialector, gconfig)
if err != nil {
return false, fmt.Errorf("failed to open database: %v", err)
}
db, err := gorm.Open(dialector, gconfig)
if err != nil {
return false, fmt.Errorf("failed to open database: %v", err)
}
var databases []string
query := genQuery(c)
if err := db.Raw(query).Scan(&databases).Error; err != nil {
return false, fmt.Errorf("failed to query: %v", err)
}
var databases []string
query := genQuery(c)
if err := db.Raw(query).Scan(&databases).Error; err != nil {
return false, fmt.Errorf("failed to query: %v", err)
}
for _, database := range databases {
if database == dbName {
return true, nil
}
}
for _, database := range databases {
if database == dbName {
return true, nil
}
}
return false, nil
return false, nil
}
func genQuery(c DBConfig) string {
switch strings.ToLower(c.DBType) {
case "mysql":
return "SHOW DATABASES"
case "postgres":
return "SELECT datname FROM pg_database"
case "sqlite":
return ""
default:
return ""
case "mysql":
return "SHOW DATABASES"
case "postgres":
return "SELECT datname FROM pg_database"
case "sqlite":
return ""
default:
return ""
}
}

View File

@@ -63,7 +63,7 @@ func GetByUrl[T any](url string, cfg conf.CenterApi) (T, error) {
Timeout: time.Duration(cfg.Timeout) * time.Millisecond,
}
if useProxy(url) {
if UseProxy(url) {
client.Transport = ProxyTransporter
}
@@ -147,7 +147,7 @@ func PostByUrl[T any](url string, cfg conf.CenterApi, v interface{}) (t T, err e
Timeout: time.Duration(cfg.Timeout) * time.Millisecond,
}
if useProxy(url) {
if UseProxy(url) {
client.Transport = ProxyTransporter
}
@@ -195,7 +195,7 @@ var ProxyTransporter = &http.Transport{
Proxy: http.ProxyFromEnvironment,
}
func useProxy(url string) bool {
func UseProxy(url string) bool {
// N9E_PROXY_URL=oapi.dingtalk.com,feishu.com
patterns := os.Getenv("N9E_PROXY_URL")
if patterns != "" {
@@ -228,7 +228,7 @@ func PostJSON(url string, timeout time.Duration, v interface{}, retries ...int)
Timeout: timeout,
}
if useProxy(url) {
if UseProxy(url) {
client.Transport = ProxyTransporter
}