Skip to content
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

[ISSUE #127] 合并prometheus类型的监控插件 #124

Merged
merged 10 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ backup/
polaris/log/*

angevil_test/
test/other/other_test_suit.go
*.log

/vendor/

.vscode/

style_tool/
goimports-reviser
goimports-reviser
7 changes: 6 additions & 1 deletion api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,12 @@ func getSelfIP(cfg config.Configuration) {

conn, _ := net.Dial("tcp", address[0])
if conn != nil {
cfg.GetGlobal().GetAPI().SetBindIP(conn.LocalAddr().String())
localAddr := conn.LocalAddr().String()
colonIdx := strings.LastIndex(localAddr, ":")
if colonIdx > 0 {
localAddr = localAddr[:colonIdx]
}
cfg.GetGlobal().GetAPI().SetBindIP(localAddr)
_ = conn.Close()
}
}
Expand Down
4 changes: 1 addition & 3 deletions examples/circuitbreaker/consumer/polaris.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ global:
# - pushgateway
plugin:
prometheus:
type: pull
metricPort: 0
# pushgateway:
# address: 127.0.0.1:9091
# pushInterval: 10s
6 changes: 2 additions & 4 deletions examples/circuitbreaker/provider/polaris.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ global:
# - pushgateway
plugin:
prometheus:
metricPort: 0
# pushgateway:
# address: 127.0.0.1:9091
# pushInterval: 10s
type: pull
metricPort: 0
4 changes: 1 addition & 3 deletions examples/quickstart/consumer/polaris.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ global:
# - pushgateway
plugin:
prometheus:
type: pull
metricPort: 0
# pushgateway:
# address: 127.0.0.1:9091
# pushInterval: 10s
5 changes: 2 additions & 3 deletions examples/quickstart/provider/polaris.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ global:
# - pushgateway
plugin:
prometheus:
type: pull
metricPort: 0
# pushgateway:
# address: 127.0.0.1:9091
# pushInterval: 10s

4 changes: 1 addition & 3 deletions examples/ratelimit/consumer/polaris.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ global:
# - pushgateway
plugin:
prometheus:
type: pull
metricPort: 0
# pushgateway:
# address: 127.0.0.1:9091
# pushInterval: 10s
4 changes: 1 addition & 3 deletions examples/ratelimit/provider/polaris.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ global:
# - pushgateway
plugin:
prometheus:
type: pull
metricPort: 0
# pushgateway:
# address: 127.0.0.1:9091
# pushInterval: 10s
4 changes: 1 addition & 3 deletions examples/route/dynamic/consumer/polaris.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ global:
# - pushgateway
plugin:
prometheus:
type: pull
metricPort: 0
# pushgateway:
# address: 127.0.0.1:9091
# pushInterval: 10s
4 changes: 1 addition & 3 deletions examples/route/dynamic/provider/polaris.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ global:
# - pushgateway
plugin:
prometheus:
type: pull
metricPort: 0
# pushgateway:
# address: 127.0.0.1:9091
# pushInterval: 10s
4 changes: 1 addition & 3 deletions examples/route/nearby/consumer/polaris.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ global:
# - pushgateway
plugin:
prometheus:
type: pull
metricPort: 0
# pushgateway:
# address: 127.0.0.1:9091
# pushInterval: 10s
consumer:
serviceRouter:
# 服务路由链
Expand Down
4 changes: 1 addition & 3 deletions examples/route/nearby/provider/polaris.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ global:
# - pushgateway
plugin:
prometheus:
type: pull
metricPort: 0
# pushgateway:
# address: 127.0.0.1:9091
# pushInterval: 10s
4 changes: 1 addition & 3 deletions examples/watchinstance/polaris.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ global:
# - pushgateway
plugin:
prometheus:
type: pull
metricPort: 0
# pushgateway:
# address: 127.0.0.1:9091
# pushInterval: 10s
4 changes: 3 additions & 1 deletion pkg/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ const (
// DefaultServiceRouteReporter .
DefaultServiceRouteReporter string = "serviceRoute"
// DefaultStatReportEnabled .
DefaultStatReportEnabled bool = false
DefaultStatReportEnabled bool = true
chuntaojun marked this conversation as resolved.
Show resolved Hide resolved
// DefaultMetricsChain .
DefaultMetricsChain string = "prometheus"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/statreporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s *StatReporterConfigImpl) SetDefault() {
s.Enable = &enable
}
if len(s.Chain) == 0 {
s.Chain = []string{}
s.Chain = []string{DefaultMetricsChain}
}
s.Plugin.SetDefault(common.TypeStatReporter)
}
Expand Down
1 change: 0 additions & 1 deletion pkg/plugin/register/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import (
_ "github.com/polarismesh/polaris-go/plugin/location"
_ "github.com/polarismesh/polaris-go/plugin/logger/zaplog"
_ "github.com/polarismesh/polaris-go/plugin/metrics/prometheus"
_ "github.com/polarismesh/polaris-go/plugin/metrics/pushgateway"
_ "github.com/polarismesh/polaris-go/plugin/ratelimiter/reject"
_ "github.com/polarismesh/polaris-go/plugin/ratelimiter/unirate"
_ "github.com/polarismesh/polaris-go/plugin/serverconnector/grpc"
Expand Down
20 changes: 14 additions & 6 deletions plugin/metrics/prometheus/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ const (

// Config prometheus 的配置
type Config struct {
IP string `yaml:"metricHost"`
PortStr string `yaml:"metricPort"`
Port int `yaml:"-"`
Type string `yaml:"type"`
IP string `yaml:"metricHost"`
PortStr string `yaml:"metricPort"`
port int `yaml:"-"`
Interval time.Duration `yaml:"interval"`
Address string `yaml:"address"`
}

// Verify verify config
Expand All @@ -46,11 +49,16 @@ func (c *Config) Verify() error {

// SetDefault Setting defaults
func (c *Config) SetDefault() {
if c.Type == "" {
c.Type = "pull"
}
if c.PortStr == "" {
c.Port = defaultMetricPort
c.port = defaultMetricPort
return
}

if c.Interval == 0 {
c.Interval = 15 * time.Second
}
port, _ := strconv.ParseInt(c.PortStr, 10, 64)
c.Port = int(port)
c.port = int(port)
}
81 changes: 81 additions & 0 deletions plugin/metrics/prometheus/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ package prometheus

import (
"fmt"
"net/http"
"strings"
"sync"

"github.com/prometheus/client_golang/prometheus"

"github.com/polarismesh/polaris-go/pkg/model"
"github.com/polarismesh/polaris-go/plugin/metrics/prometheus/addons"
)

// MetricsType 指标类型,对应 Prometheus 提供的 Collector 类型.
Expand Down Expand Up @@ -231,6 +236,9 @@ var (
// 主调方相关信息
CallerLabels: func(args interface{}) string {
val := args.(*model.ServiceCallResult)
if val.SourceService == nil || len(val.SourceService.Metadata) == 0 {
return ""
}
labels := val.SourceService.Metadata
var ret []string
for k, v := range labels {
Expand Down Expand Up @@ -323,3 +331,76 @@ func formatLabelsToStr(arguments []model.Argument) string {

return strings.Join(s, "|")
}

func buildMetrics() ([]prometheus.Collector, map[string]prometheus.Collector) {
var (
metricVecCaches = map[string]prometheus.Collector{}
collectors = make([]prometheus.Collector, 0, len(metrcisDesces))
)

for _, desc := range metrcisDesces {
var collector prometheus.Collector
switch desc.MetricType {
case TypeForGaugeVec:
collector = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: desc.Name,
Help: desc.Help,
}, desc.LabelNames)
case TypeForCounterVec:
collector = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: desc.Name,
Help: desc.Help,
}, desc.LabelNames)
case TypeForMaxGaugeVec:
collector = addons.NewMaxGaugeVec(prometheus.GaugeOpts{
Name: desc.Name,
Help: desc.Help,
}, desc.LabelNames)
case TypeForHistogramVec:
collector = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: desc.Name,
Help: desc.Help,
}, desc.LabelNames)
}
collectors = append(collectors, collector)
metricVecCaches[desc.Name] = collector
}
return collectors, metricVecCaches
}

type metricsHttpHandler struct {
promeHttpHandler http.Handler
lock *sync.RWMutex
chuntaojun marked this conversation as resolved.
Show resolved Hide resolved
}

// ServeHTTP 提供 prometheus http 服务.
func (p *metricsHttpHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
p.lock.RLock()
defer p.lock.RUnlock()
p.promeHttpHandler.ServeHTTP(writer, request)
}

func convertInsGaugeToLabels(val *model.ServiceCallResult, bindIP string) map[string]string {
labels := make(map[string]string)
for label, supplier := range InstanceGaugeLabelOrder {
labels[label] = supplier(val)
}
labels[CallerIP] = bindIP
return labels
}

func convertRateLimitGaugeToLabels(val *model.RateLimitGauge) map[string]string {
labels := make(map[string]string)
for label, supplier := range RateLimitGaugeLabelOrder {
labels[label] = supplier(val)
}
return labels
}

func convertCircuitBreakGaugeToLabels(val *model.CircuitBreakGauge) map[string]string {
labels := make(map[string]string)
for label, supplier := range CircuitBreakerGaugeLabelOrder {
labels[label] = supplier(val)
}
return labels
}
Loading