add mutex for prom transport

This commit is contained in:
Ulric Qin 2022-05-22 12:45:25 +08:00
parent 6ba93527ba
commit 6a7b543ad6
5 changed files with 52 additions and 51 deletions

View File

@ -2,7 +2,7 @@
NOW = $(shell date -u '+%Y%m%d%I%M%S') NOW = $(shell date -u '+%Y%m%d%I%M%S')
RELEASE_VERSION = 5.8.0 RELEASE_VERSION = 5.8.1
APP = n9e APP = n9e
SERVER_BIN = $(APP) SERVER_BIN = $(APP)

View File

@ -10,6 +10,12 @@ MetricsYamlFile = "./etc/metrics.yaml"
BuiltinAlertsDir = "./etc/alerts" BuiltinAlertsDir = "./etc/alerts"
BuiltinDashboardsDir = "./etc/dashboards" BuiltinDashboardsDir = "./etc/dashboards"
# config | api
ClustersFrom = "config"
# using when ClustersFrom = "api"
ClustersFromAPIs = []
[[NotifyChannels]] [[NotifyChannels]]
Label = "邮箱" Label = "邮箱"
# do not change Key # do not change Key
@ -172,14 +178,7 @@ BasicAuthUser = ""
BasicAuthPass = "" BasicAuthPass = ""
# timeout settings, unit: ms # timeout settings, unit: ms
Timeout = 30000 Timeout = 30000
DialTimeout = 10000 DialTimeout = 3000
TLSHandshakeTimeout = 30000
ExpectContinueTimeout = 1000
IdleConnTimeout = 90000
# time duration, unit: ms
KeepAlive = 30000
MaxConnsPerHost = 0
MaxIdleConns = 100
MaxIdleConnsPerHost = 100 MaxIdleConnsPerHost = 100
[Ibex] [Ibex]

View File

@ -15,7 +15,6 @@ import (
"github.com/didi/nightingale/v5/src/pkg/oidcc" "github.com/didi/nightingale/v5/src/pkg/oidcc"
"github.com/didi/nightingale/v5/src/pkg/ormx" "github.com/didi/nightingale/v5/src/pkg/ormx"
"github.com/didi/nightingale/v5/src/storage" "github.com/didi/nightingale/v5/src/storage"
"github.com/didi/nightingale/v5/src/webapi/prom"
) )
var ( var (
@ -82,6 +81,8 @@ type Config struct {
MetricsYamlFile string MetricsYamlFile string
BuiltinAlertsDir string BuiltinAlertsDir string
BuiltinDashboardsDir string BuiltinDashboardsDir string
ClustersFrom string
ClustersFromAPIs []string
ContactKeys []LabelAndKey ContactKeys []LabelAndKey
NotifyChannels []LabelAndKey NotifyChannels []LabelAndKey
Log logx.Config Log logx.Config
@ -91,12 +92,26 @@ type Config struct {
AnonymousAccess AnonymousAccess AnonymousAccess AnonymousAccess
LDAP ldapx.LdapSection LDAP ldapx.LdapSection
Redis storage.RedisConfig Redis storage.RedisConfig
DB ormx.DBConfig DB ormx.DBConfig
Clusters []prom.Options Clusters []ClusterOptions
Ibex Ibex Ibex Ibex
OIDC oidcc.Config OIDC oidcc.Config
} }
type ClusterOptions struct {
Name string
Prom string
BasicAuthUser string
BasicAuthPass string
Timeout int64
DialTimeout int64
KeepAlive int64
MaxIdleConnsPerHost int
}
type LabelAndKey struct { type LabelAndKey struct {
Label string `json:"label"` Label string `json:"label"`
Key string `json:"key"` Key string `json:"key"`

View File

@ -3,55 +3,48 @@ package prom
import ( import (
"net" "net"
"net/http" "net/http"
"sync"
"time" "time"
"github.com/didi/nightingale/v5/src/webapi/config"
) )
type Options struct {
Name string
Prom string
BasicAuthUser string
BasicAuthPass string
Timeout int64
DialTimeout int64
TLSHandshakeTimeout int64
ExpectContinueTimeout int64
IdleConnTimeout int64
KeepAlive int64
MaxConnsPerHost int
MaxIdleConns int
MaxIdleConnsPerHost int
}
type ClusterType struct { type ClusterType struct {
Opts Options Opts config.ClusterOptions
Transport *http.Transport Transport *http.Transport
} }
type ClustersType struct { type ClustersType struct {
M map[string]ClusterType datas map[string]ClusterType
} mutex *sync.RWMutex
func NewClusters() ClustersType {
return ClustersType{
M: make(map[string]ClusterType),
}
} }
func (cs *ClustersType) Put(name string, cluster ClusterType) { func (cs *ClustersType) Put(name string, cluster ClusterType) {
cs.M[name] = cluster cs.mutex.Lock()
cs.datas[name] = cluster
cs.mutex.Unlock()
} }
func (cs *ClustersType) Get(name string) (ClusterType, bool) { func (cs *ClustersType) Get(name string) (ClusterType, bool) {
c, has := cs.M[name] cs.mutex.RLock()
defer cs.mutex.RUnlock()
c, has := cs.datas[name]
return c, has return c, has
} }
var Clusters = NewClusters() var Clusters = ClustersType{
datas: make(map[string]ClusterType),
mutex: new(sync.RWMutex),
}
func Init() error {
if config.C.ClustersFrom != "" && config.C.ClustersFrom != "config" {
return nil
}
opts := config.C.Clusters
func Init(opts []Options) error {
for i := 0; i < len(opts); i++ { for i := 0; i < len(opts); i++ {
cluster := ClusterType{ cluster := ClusterType{
Opts: opts[i], Opts: opts[i],
@ -59,16 +52,10 @@ func Init(opts []Options) error {
// TLSClientConfig: tlsConfig, // TLSClientConfig: tlsConfig,
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{ DialContext: (&net.Dialer{
Timeout: time.Duration(opts[i].DialTimeout) * time.Millisecond, Timeout: time.Duration(opts[i].DialTimeout) * time.Millisecond,
KeepAlive: time.Duration(opts[i].KeepAlive) * time.Millisecond,
}).DialContext, }).DialContext,
ResponseHeaderTimeout: time.Duration(opts[i].Timeout) * time.Millisecond, ResponseHeaderTimeout: time.Duration(opts[i].Timeout) * time.Millisecond,
TLSHandshakeTimeout: time.Duration(opts[i].TLSHandshakeTimeout) * time.Millisecond,
ExpectContinueTimeout: time.Duration(opts[i].ExpectContinueTimeout) * time.Millisecond,
MaxConnsPerHost: opts[i].MaxConnsPerHost,
MaxIdleConns: opts[i].MaxIdleConns,
MaxIdleConnsPerHost: opts[i].MaxIdleConnsPerHost, MaxIdleConnsPerHost: opts[i].MaxIdleConnsPerHost,
IdleConnTimeout: time.Duration(opts[i].IdleConnTimeout) * time.Millisecond,
}, },
} }
Clusters.Put(opts[i].Name, cluster) Clusters.Put(opts[i].Name, cluster)

View File

@ -115,7 +115,7 @@ func (a Webapi) initialize() (func(), error) {
models.InitRoot() models.InitRoot()
// init prometheus proxy config // init prometheus proxy config
if err = prom.Init(config.C.Clusters); err != nil { if err = prom.Init(); err != nil {
return nil, err return nil, err
} }