Skip to content

Commit

Permalink
fix: 修复test包命名冲突问题 (#3)
Browse files Browse the repository at this point in the history
* fix: eureka同步时候,出现性能超时问题

* 删除batch逻辑

* feat: 调整更新事件,增加时间的对象成功更新

* Refactor test dir (#995) (#2)

Co-authored-by: liaochuntao <[email protected]>

* fix:包命名调整

---------

Co-authored-by: liaochuntao <[email protected]>
  • Loading branch information
andrewshan and chuntaojun committed Mar 15, 2023
1 parent e0a89be commit 248df93
Show file tree
Hide file tree
Showing 27 changed files with 1,486 additions and 1,147 deletions.
6 changes: 3 additions & 3 deletions apiserver/eurekaserver/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import (
"github.com/stretchr/testify/assert"

api "github.com/polarismesh/polaris/common/api/v1"
"github.com/polarismesh/polaris/service"
testsuit "github.com/polarismesh/polaris/test/suit"
)

func createEurekaServerForTest(
discoverSuit *service.DiscoverTestSuit, options map[string]interface{}) (*EurekaServer, error) {
discoverSuit *testsuit.DiscoverTestSuit, options map[string]interface{}) (*EurekaServer, error) {
eurekaSrv := &EurekaServer{
namingServer: discoverSuit.DiscoverServer(),
healthCheckServer: discoverSuit.HealthCheckServer(),
Expand Down Expand Up @@ -142,7 +142,7 @@ func checkInstanceAction(t *testing.T, applications *Applications, appName strin

// 测试新建实例
func TestCreateInstance(t *testing.T) {
discoverSuit := &service.DiscoverTestSuit{}
discoverSuit := &testsuit.DiscoverTestSuit{}
if err := discoverSuit.Initialize(); err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions apiserver/eurekaserver/replicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
"github.com/stretchr/testify/assert"

api "github.com/polarismesh/polaris/common/api/v1"
"github.com/polarismesh/polaris/service"
testsuit "github.com/polarismesh/polaris/test/suit"
)

func TestDispatchHeartbeat(t *testing.T) {
discoverSuit := &service.DiscoverTestSuit{}
discoverSuit := &testsuit.DiscoverTestSuit{}
if err := discoverSuit.Initialize(); err != nil {
t.Fatal(err)
}
Expand Down
66 changes: 65 additions & 1 deletion cache/config_file_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"time"

"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"

"github.com/polarismesh/polaris/common/metrics"
Expand All @@ -30,9 +31,14 @@ import (
func (fc *fileCache) reportMetricsInfo(ctx context.Context) {
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()

var preGroup map[string]map[string]struct{}

for {
select {
case <-ticker.C:
tmpGroup := map[string]map[string]struct{}{}

configGroups, err := fc.storage.CountGroupEachNamespace()
if err != nil {
log.Error("[Cache][ConfigFile] report metrics for config_group each namespace", zap.Error(err))
Expand All @@ -50,6 +56,14 @@ func (fc *fileCache) reportMetricsInfo(ctx context.Context) {
log.Error("[Cache][ConfigFile] report metrics for release config_file each group", zap.Error(err))
continue
}
for ns, groups := range configFiles {
if _, ok := tmpGroup[ns]; !ok {
tmpGroup[ns] = map[string]struct{}{}
}
for group := range groups {
tmpGroup[ns][group] = struct{}{}
}
}

metricValues := make([]metrics.ConfigMetrics, 0, 64)

Expand Down Expand Up @@ -89,10 +103,60 @@ func (fc *fileCache) reportMetricsInfo(ctx context.Context) {
})
}
}

cleanExpireConfigFileMetricLabel(preGroup, tmpGroup)
preGroup = tmpGroup
plugin.GetStatis().ReportConfigMetrics(metricValues...)
case <-ctx.Done():
return
}
}
}

func cleanExpireConfigFileMetricLabel(pre, curr map[string]map[string]struct{}) {
if len(pre) == 0 {
return
}

var (
removeNs = map[string]struct{}{}
remove = map[string]map[string]struct{}{}
)

for ns, groups := range pre {
if _, ok := curr[ns]; !ok {
removeNs[ns] = struct{}{}
}
if _, ok := remove[ns]; !ok {
remove[ns] = map[string]struct{}{}
}
for group := range groups {
if _, ok := curr[ns][group]; !ok {
remove[ns][group] = struct{}{}
}
}
}

for ns := range removeNs {
metrics.GetConfigGroupTotal().Delete(prometheus.Labels{
metrics.LabelNamespace: ns,
})
}

for ns, groups := range remove {
for group := range groups {
metrics.GetConfigFileTotal().Delete(prometheus.Labels{
metrics.LabelNamespace: ns,
metrics.LabelGroup: group,
})
metrics.GetReleaseConfigFileTotal().Delete(prometheus.Labels{
metrics.LabelNamespace: ns,
metrics.LabelGroup: group,
})
metrics.GetConfigFileTotal().Delete(prometheus.Labels{
metrics.LabelNamespace: ns,
metrics.LabelGroup: group,
})
}
}

}
75 changes: 75 additions & 0 deletions cache/instance_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@
package cache

import (
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"

"github.com/polarismesh/polaris/common/metrics"
"github.com/polarismesh/polaris/common/model"
"github.com/polarismesh/polaris/plugin"
)

var preServiceInfos = map[string]map[string]struct{}{}

func (ic *instanceCache) reportMetricsInfo() {
cacheMgr, err := GetCacheManager()
if err != nil {
log.Warn("[Cache][Instance] report metrics get cache manager, but impossible", zap.Error(err))
return
}

tmpServiceInfos := map[string]map[string]struct{}{}

allServices := map[string]map[string]struct{}{}
onlineService := map[string]map[string]struct{}{}
offlineService := map[string]map[string]struct{}{}
Expand All @@ -49,6 +54,12 @@ func (ic *instanceCache) reportMetricsInfo() {
log.Debug("[Cache][Instance] report metrics get service not found", zap.String("svc-id", serviceID))
return true
}

if _, ok := tmpServiceInfos[svc.Namespace]; !ok {
tmpServiceInfos[svc.Namespace] = map[string]struct{}{}
}
tmpServiceInfos[svc.Namespace][svc.Name] = struct{}{}

if _, ok := allServices[svc.Namespace]; !ok {
allServices[svc.Namespace] = map[string]struct{}{}
}
Expand Down Expand Up @@ -101,5 +112,69 @@ func (ic *instanceCache) reportMetricsInfo() {
})
}

cleanExpireServiceMetricLabel(preServiceInfos, tmpServiceInfos)
preServiceInfos = tmpServiceInfos
plugin.GetStatis().ReportDiscoveryMetrics(metricValues...)
}

func cleanExpireServiceMetricLabel(pre, curr map[string]map[string]struct{}) {
if len(pre) == 0 {
return
}

var (
removeNs = map[string]struct{}{}
remove = map[string]map[string]struct{}{}
)

for ns, services := range pre {
if _, ok := curr[ns]; !ok {
removeNs[ns] = struct{}{}
}
if _, ok := remove[ns]; !ok {
remove[ns] = map[string]struct{}{}
}
for service := range services {
if _, ok := curr[ns][service]; !ok {
remove[ns][service] = struct{}{}
}
}
}

for ns := range removeNs {
metrics.GetServiceCount().Delete(prometheus.Labels{
metrics.LabelNamespace: ns,
})
metrics.GetServiceOfflineCountl().Delete(prometheus.Labels{
metrics.LabelNamespace: ns,
})
metrics.GetServiceOnlineCountl().Delete(prometheus.Labels{
metrics.LabelNamespace: ns,
})
metrics.GetServiceAbnormalCountl().Delete(prometheus.Labels{
metrics.LabelNamespace: ns,
})
}

for ns, services := range remove {
for service := range services {
metrics.GetInstanceCount().Delete(prometheus.Labels{
metrics.LabelNamespace: ns,
metrics.LabelService: service,
})
metrics.GetInstanceAbnormalCountl().Delete(prometheus.Labels{
metrics.LabelNamespace: ns,
metrics.LabelService: service,
})
metrics.GetInstanceIsolateCountl().Delete(prometheus.Labels{
metrics.LabelNamespace: ns,
metrics.LabelService: service,
})
metrics.GetInstanceOnlineCountl().Delete(prometheus.Labels{
metrics.LabelNamespace: ns,
metrics.LabelService: service,
})
}
}

}
70 changes: 70 additions & 0 deletions common/metrics/config_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package metrics

import (
"github.com/prometheus/client_golang/prometheus"

"github.com/polarismesh/polaris/common/utils"
)

func registerConfigFileMetrics() {
GetRegistry().MustRegister(
configGroupTotal,
configFileTotal,
releaseConfigFileTotal,
)
}

var (
configGroupTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "config_group_count",
Help: "polaris config group total number",
ConstLabels: map[string]string{
LabelServerNode: utils.LocalHost,
},
}, []string{LabelNamespace})

configFileTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "config_file_count",
Help: "total number of config_file each config group",
ConstLabels: map[string]string{
LabelServerNode: utils.LocalHost,
},
}, []string{LabelNamespace, LabelGroup})

releaseConfigFileTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "config_release_file_count",
Help: "total number of config_release_file each config group",
ConstLabels: map[string]string{
LabelServerNode: utils.LocalHost,
},
}, []string{LabelNamespace, LabelGroup})
)

func GetConfigGroupTotal() *prometheus.GaugeVec {
return configGroupTotal
}

func GetConfigFileTotal() *prometheus.GaugeVec {
return configFileTotal
}

func GetReleaseConfigFileTotal() *prometheus.GaugeVec {
return releaseConfigFileTotal
}
2 changes: 2 additions & 0 deletions common/metrics/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ func GetHttpHandler() http.Handler {
func InitMetrics() {
registerSysMetrics()
registerClientMetrics()
registerConfigFileMetrics()
registerDiscoveryMetrics()
}
Loading

0 comments on commit 248df93

Please sign in to comment.