github.com/polarismesh/polaris@v1.17.8/plugin/statis/base/base_worker.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 base 19 20 import ( 21 "context" 22 "time" 23 24 "github.com/polarismesh/polaris/common/log" 25 "github.com/polarismesh/polaris/common/metrics" 26 "github.com/polarismesh/polaris/store" 27 ) 28 29 type BaseWorker struct { 30 apiStatis *ComponentStatics 31 redisStatis *ComponentStatics 32 innerStatis *ComponentStatics 33 cacheStatis *CacheCallStatis 34 } 35 36 func NewBaseWorker(ctx context.Context, handler MetricsHandler) (*BaseWorker, error) { 37 cacheStatis, err := NewCacheCallStatis(ctx) 38 if err != nil { 39 return nil, err 40 } 41 return &BaseWorker{ 42 apiStatis: NewComponentStatics(ctx, metrics.ServerCallMetric, handler), 43 redisStatis: NewComponentStatics(ctx, metrics.RedisCallMetric, handler), 44 innerStatis: NewComponentStatics(ctx, metrics.SystemCallMetric, handler), 45 cacheStatis: cacheStatis, 46 }, nil 47 } 48 49 // ReportCallMetrics report call metrics info 50 func (s *BaseWorker) ReportCallMetrics(metric metrics.CallMetric) { 51 switch metric.Type { 52 case metrics.ServerCallMetric: 53 item := &APICall{ 54 Api: metric.API, 55 Protocol: metric.Protocol, 56 Code: metric.Code, 57 Duration: int64(metric.Duration.Nanoseconds()), 58 Component: metric.Type, 59 } 60 s.apiStatis.Add(item) 61 case metrics.SystemCallMetric: 62 item := &APICall{ 63 Api: metric.API, 64 Protocol: metric.Protocol, 65 Code: metric.Code, 66 Duration: int64(metric.Duration.Nanoseconds()), 67 Component: metric.Type, 68 } 69 s.innerStatis.Add(item) 70 case metrics.RedisCallMetric: 71 item := &APICall{ 72 Api: metric.API, 73 Protocol: metric.Protocol, 74 Code: metric.Code, 75 Duration: int64(metric.Duration.Nanoseconds()), 76 Component: metric.Type, 77 } 78 s.redisStatis.Add(item) 79 case metrics.ProtobufCacheCallMetric: 80 s.cacheStatis.Add(metric) 81 } 82 } 83 84 // Run 主流程 85 func (s *BaseWorker) Run(ctx context.Context, interval time.Duration) { 86 getStore, err := store.GetStore() 87 if err != nil { 88 log.Errorf("[APICall] get store error, %v", err) 89 return 90 } 91 92 nowSeconds, err := getStore.GetUnixSecond(time.Second) 93 if err != nil { 94 log.Errorf("[APICall] get now second from store error, %v", err) 95 return 96 } 97 if nowSeconds == 0 { 98 nowSeconds = time.Now().Unix() 99 } 100 dest := nowSeconds 101 dest += 60 102 dest = dest - (dest % 60) 103 diff := dest - nowSeconds 104 105 log.Infof("[APICall] base stats need sleep %ds", diff) 106 time.Sleep(time.Duration(diff) * time.Second) 107 108 ticker := time.NewTicker(interval) 109 defer ticker.Stop() 110 111 for { 112 select { 113 case <-ctx.Done(): 114 return 115 case <-ticker.C: 116 s.apiStatis.deal() 117 s.redisStatis.deal() 118 s.innerStatis.deal() 119 s.cacheStatis.deal() 120 } 121 } 122 }