github.com/polarismesh/polaris@v1.17.8/plugin/statis/logger/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 logger 19 20 import ( 21 "context" 22 "fmt" 23 "time" 24 25 commonlog "github.com/polarismesh/polaris/common/log" 26 "github.com/polarismesh/polaris/common/metrics" 27 commontime "github.com/polarismesh/polaris/common/time" 28 "github.com/polarismesh/polaris/plugin" 29 "github.com/polarismesh/polaris/plugin/statis/base" 30 ) 31 32 const ( 33 PluginName = "local" 34 ) 35 36 // init 注册统计插件 37 func init() { 38 s := &StatisWorker{} 39 plugin.RegisterPlugin(s.Name(), s) 40 } 41 42 // StatisWorker 本地统计插件 43 type StatisWorker struct { 44 *base.BaseWorker 45 cancel context.CancelFunc 46 } 47 48 // Name 获取统计插件名称 49 func (s *StatisWorker) Name() string { 50 return PluginName 51 } 52 53 // Initialize 初始化统计插件 54 func (s *StatisWorker) Initialize(conf *plugin.ConfigEntry) error { 55 ctx, cancel := context.WithCancel(context.Background()) 56 baseWorker, err := base.NewBaseWorker(ctx, s.metricsHandle) 57 if err != nil { 58 cancel() 59 return err 60 } 61 62 s.cancel = cancel 63 s.BaseWorker = baseWorker 64 65 // 设置统计打印周期 66 interval, _ := conf.Option["interval"].(int) 67 if interval == 0 { 68 interval = 60 69 } 70 71 go s.Run(ctx, time.Duration(interval)*time.Second) 72 return nil 73 } 74 75 // Destroy 销毁统计插件 76 func (s *StatisWorker) Destroy() error { 77 if s.cancel != nil { 78 s.cancel() 79 } 80 return nil 81 } 82 83 // ReportCallMetrics report call metrics info 84 func (s *StatisWorker) ReportCallMetrics(metric metrics.CallMetric) { 85 s.BaseWorker.ReportCallMetrics(metric) 86 } 87 88 // ReportDiscoveryMetrics report discovery metrics 89 func (s *StatisWorker) ReportDiscoveryMetrics(metric ...metrics.DiscoveryMetric) { 90 } 91 92 // ReportConfigMetrics report config_center metrics 93 func (s *StatisWorker) ReportConfigMetrics(metric ...metrics.ConfigMetrics) { 94 } 95 96 // ReportDiscoverCall report discover service times 97 func (s *StatisWorker) ReportDiscoverCall(service, namespace string, ttMill int64) { 98 99 } 100 101 func (a *StatisWorker) metricsHandle(mt metrics.CallMetricType, start time.Time, 102 statics []*base.APICallStatisItem) { 103 startStr := commontime.Time2String(start) 104 if len(statics) == 0 { 105 log.Info(fmt.Sprintf("Statis %s: No API Call\n", startStr)) 106 return 107 } 108 109 scope := commonlog.GetScopeOrDefaultByName(string(mt)) 110 if scope.Name() == commonlog.DefaultLoggerName { 111 scope = log 112 } 113 114 header := fmt.Sprintf("Statis %s:\n", startStr) 115 116 header += fmt.Sprintf( 117 "%-48v|%12v|%12v|%12v|%12v|%12v|\n", "", "Code", "Count", "Min(ms)", "Max(ms)", "Avg(ms)") 118 119 var msg string 120 for i := range statics { 121 msg += statics[i].String() 122 } 123 if len(msg) == 0 { 124 log.Info(fmt.Sprintf("Statis %s: No API Call\n", startStr)) 125 return 126 } 127 128 log.Info(header + msg) 129 }