github.com/polarismesh/polaris@v1.17.8/plugin/statis.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 plugin 19 20 import ( 21 "os" 22 "sync" 23 24 "github.com/polarismesh/polaris/common/metrics" 25 ) 26 27 var ( 28 statisOnce sync.Once 29 _statis Statis 30 ) 31 32 // Statis Statistical plugin interface 33 type Statis interface { 34 Plugin 35 // ReportCallMetrics report call metrics info 36 ReportCallMetrics(metric metrics.CallMetric) 37 // ReportDiscoveryMetrics report discovery metrics 38 ReportDiscoveryMetrics(metric ...metrics.DiscoveryMetric) 39 // ReportConfigMetrics report config_center metrics 40 ReportConfigMetrics(metric ...metrics.ConfigMetrics) 41 // ReportDiscoverCall report discover service times 42 ReportDiscoverCall(service, namespace string, ttMill int64) 43 } 44 45 // compositeStatis is used to receive discover events from the agent 46 type compositeStatis struct { 47 chain []Statis 48 options []ConfigEntry 49 } 50 51 func (c *compositeStatis) Name() string { 52 return "compositeStatis" 53 } 54 55 func (c *compositeStatis) Initialize(config *ConfigEntry) error { 56 for i := range c.options { 57 entry := c.options[i] 58 item, exist := pluginSet[entry.Name] 59 if !exist { 60 log.Errorf("plugin Statis not found target: %s", entry.Name) 61 continue 62 } 63 64 statis, ok := item.(Statis) 65 if !ok { 66 log.Errorf("plugin target: %s not Statis", entry.Name) 67 continue 68 } 69 70 if err := statis.Initialize(&entry); err != nil { 71 return err 72 } 73 c.chain = append(c.chain, statis) 74 } 75 return nil 76 } 77 78 func (c *compositeStatis) Destroy() error { 79 for i := range c.chain { 80 if err := c.chain[i].Destroy(); err != nil { 81 return err 82 } 83 } 84 return nil 85 } 86 87 // ReportCallMetrics report call metrics info 88 func (c *compositeStatis) ReportCallMetrics(metric metrics.CallMetric) { 89 for i := range c.chain { 90 c.chain[i].ReportCallMetrics(metric) 91 } 92 } 93 94 // ReportDiscoveryMetrics report discovery metrics 95 func (c *compositeStatis) ReportDiscoveryMetrics(metric ...metrics.DiscoveryMetric) { 96 for i := range c.chain { 97 c.chain[i].ReportDiscoveryMetrics(metric...) 98 } 99 } 100 101 // ReportConfigMetrics report config_center metrics 102 func (c *compositeStatis) ReportConfigMetrics(metric ...metrics.ConfigMetrics) { 103 for i := range c.chain { 104 c.chain[i].ReportConfigMetrics(metric...) 105 } 106 } 107 108 // ReportDiscoverCall report discover service times 109 func (c *compositeStatis) ReportDiscoverCall(service, namespace string, ttMill int64) { 110 for i := range c.chain { 111 c.chain[i].ReportDiscoverCall(service, namespace, ttMill) 112 } 113 } 114 115 // GetStatis Get statistical plugin 116 func GetStatis() Statis { 117 if _statis != nil { 118 return _statis 119 } 120 121 statisOnce.Do(func() { 122 var ( 123 entries []ConfigEntry 124 defaultEntries = []ConfigEntry{ 125 { 126 Name: "local", 127 }, 128 { 129 Name: "prometheus", 130 }, 131 } 132 ) 133 134 if len(config.Statis.Entries) != 0 { 135 entries = append(entries, config.Statis.Entries...) 136 } else { 137 if config.Statis.Name == "local" { 138 entries = defaultEntries 139 } else { 140 entries = append(entries, ConfigEntry{ 141 Name: config.Statis.Name, 142 Option: config.Statis.Option, 143 }) 144 } 145 } 146 147 _statis = &compositeStatis{ 148 chain: []Statis{}, 149 options: entries, 150 } 151 if err := _statis.Initialize(nil); err != nil { 152 log.Errorf("Statis plugin init err: %s", err.Error()) 153 os.Exit(-1) 154 } 155 }) 156 157 return _statis 158 }