github.com/polarismesh/polaris@v1.17.8/cache/config/config_file_metrics.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package config 19 20 import ( 21 "time" 22 23 "github.com/prometheus/client_golang/prometheus" 24 "go.uber.org/zap" 25 26 "github.com/polarismesh/polaris/common/metrics" 27 "github.com/polarismesh/polaris/common/utils" 28 "github.com/polarismesh/polaris/plugin" 29 ) 30 31 func (fc *fileCache) reportMetricsInfo() { 32 lastReportTime := fc.lastReportTime.Load() 33 if time.Since(lastReportTime) <= time.Minute { 34 return 35 } 36 defer func() { 37 fc.lastReportTime.Store(time.Now()) 38 }() 39 40 metricValues := make([]metrics.ConfigMetrics, 0, 64) 41 42 configFiles, err := fc.storage.CountConfigFileEachGroup() 43 if err != nil { 44 log.Error("[Cache][ConfigFile] report metrics for config_file each group", zap.Error(err)) 45 return 46 } 47 tmpGroup := map[string]map[string]struct{}{} 48 for ns, groups := range configFiles { 49 if _, ok := tmpGroup[ns]; !ok { 50 tmpGroup[ns] = map[string]struct{}{} 51 } 52 for group := range groups { 53 tmpGroup[ns][group] = struct{}{} 54 } 55 } 56 cleanExpireConfigFileMetricLabel(fc.preMetricsFiles.Load(), tmpGroup) 57 fc.preMetricsFiles.Store(tmpGroup) 58 59 for ns, groups := range configFiles { 60 for group, total := range groups { 61 metricValues = append(metricValues, metrics.ConfigMetrics{ 62 Type: metrics.FileMetric, 63 Total: total, 64 Labels: map[string]string{ 65 metrics.LabelNamespace: ns, 66 metrics.LabelGroup: group, 67 }, 68 }) 69 } 70 } 71 72 fc.metricsReleaseCount.Range(func(namespace string, groups *utils.SyncMap[string, uint64]) bool { 73 groups.Range(func(groupName string, count uint64) bool { 74 metricValues = append(metricValues, metrics.ConfigMetrics{ 75 Type: metrics.ReleaseFileMetric, 76 Total: int64(count), 77 Labels: map[string]string{ 78 metrics.LabelNamespace: namespace, 79 metrics.LabelGroup: groupName, 80 }, 81 }) 82 return true 83 }) 84 return true 85 }) 86 87 plugin.GetStatis().ReportConfigMetrics(metricValues...) 88 } 89 90 func cleanExpireConfigFileMetricLabel(pre, curr map[string]map[string]struct{}) { 91 if len(pre) == 0 { 92 return 93 } 94 95 var ( 96 removeNs = map[string]struct{}{} 97 remove = map[string]map[string]struct{}{} 98 ) 99 100 for ns, groups := range pre { 101 if _, ok := curr[ns]; !ok { 102 removeNs[ns] = struct{}{} 103 } 104 if _, ok := remove[ns]; !ok { 105 remove[ns] = map[string]struct{}{} 106 } 107 for group := range groups { 108 if _, ok := curr[ns][group]; !ok { 109 remove[ns][group] = struct{}{} 110 } 111 } 112 } 113 114 for ns := range removeNs { 115 metrics.GetConfigGroupTotal().Delete(prometheus.Labels{ 116 metrics.LabelNamespace: ns, 117 }) 118 } 119 120 for ns, groups := range remove { 121 for group := range groups { 122 metrics.GetConfigFileTotal().Delete(prometheus.Labels{ 123 metrics.LabelNamespace: ns, 124 metrics.LabelGroup: group, 125 }) 126 metrics.GetReleaseConfigFileTotal().Delete(prometheus.Labels{ 127 metrics.LabelNamespace: ns, 128 metrics.LabelGroup: group, 129 }) 130 metrics.GetConfigFileTotal().Delete(prometheus.Labels{ 131 metrics.LabelNamespace: ns, 132 metrics.LabelGroup: group, 133 }) 134 } 135 } 136 137 }