dubbo.apache.org/dubbo-go/v3@v3.1.1/metrics/registry/collector.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package registry 19 20 import ( 21 "dubbo.apache.org/dubbo-go/v3/common" 22 "dubbo.apache.org/dubbo-go/v3/common/constant" 23 "dubbo.apache.org/dubbo-go/v3/metrics" 24 ) 25 26 var ( 27 registryChan = make(chan metrics.MetricsEvent, 128) 28 ) 29 30 func init() { 31 metrics.AddCollector("registry", func(m metrics.MetricRegistry, url *common.URL) { 32 if url.GetParamBool(constant.RegistryEnabledKey, true) { 33 rc := ®istryCollector{metrics.BaseCollector{R: m}} 34 go rc.start() 35 } 36 }) 37 } 38 39 // registryCollector is the registry's metrics collector 40 type registryCollector struct { 41 metrics.BaseCollector 42 } 43 44 func (rc *registryCollector) start() { 45 metrics.Subscribe(constant.MetricsRegistry, registryChan) 46 for event := range registryChan { 47 if registryEvent, ok := event.(*RegistryMetricsEvent); ok { 48 switch registryEvent.Name { 49 case Reg: 50 rc.regHandler(registryEvent) 51 case Sub: 52 rc.subHandler(registryEvent) 53 case Notify: 54 rc.notifyHandler(registryEvent) 55 case ServerReg: 56 rc.serverRegHandler(registryEvent) 57 case ServerSub: 58 rc.serverSubHandler(registryEvent) 59 default: 60 } 61 } 62 } 63 } 64 65 // regHandler handles register metrics 66 func (rc *registryCollector) regHandler(event *RegistryMetricsEvent) { 67 // Event is converted to metrics 68 // Save metrics to the MetricRegistry 69 level := metrics.GetApplicationLevel() 70 rc.StateCount(RegisterMetricRequests, RegisterMetricRequestsSucceed, RegisterMetricRequestsFailed, level, event.Succ) 71 rc.R.Rt(metrics.NewMetricId(RegisterRt, level), &metrics.RtOpts{}).Observe(event.CostMs()) 72 } 73 74 // subHandler handles subscribe metrics 75 func (rc *registryCollector) subHandler(event *RegistryMetricsEvent) { 76 // Event is converted to metrics 77 // Save metrics to the MetricRegistry 78 level := metrics.GetApplicationLevel() 79 rc.StateCount(SubscribeMetricNum, SubscribeMetricNumSucceed, SubscribeMetricNumFailed, level, event.Succ) 80 } 81 82 // notifyHandler handles notify metrics 83 func (rc *registryCollector) notifyHandler(event *RegistryMetricsEvent) { 84 // Event is converted to metrics 85 // Save metrics to the MetricRegistry 86 level := metrics.GetApplicationLevel() 87 rc.R.Counter(metrics.NewMetricId(NotifyMetricRequests, level)).Inc() 88 rc.R.Gauge(metrics.NewMetricId(NotifyMetricNumLast, level)).Set(event.CostMs()) 89 rc.R.Rt(metrics.NewMetricId(NotifyRt, level), &metrics.RtOpts{}).Observe(event.CostMs()) 90 } 91 92 // directoryHandler handles directory metrics 93 func (rc *registryCollector) directoryHandler(event *RegistryMetricsEvent) { 94 // Event is converted to metrics 95 // Save metrics to the MetricRegistry 96 level := metrics.GetApplicationLevel() 97 typ := event.Attachment["DirTyp"] 98 switch typ { 99 case NumAllInc: 100 rc.R.Counter(metrics.NewMetricId(DirectoryMetricNumAll, level)).Inc() 101 case NumAllDec: 102 rc.R.Counter(metrics.NewMetricId(DirectoryMetricNumAll, level)).Add(-1) 103 case NumDisableTotal: 104 rc.R.Counter(metrics.NewMetricId(DirectoryMetricNumDisable, level)).Inc() 105 case NumToReconnectTotal: 106 rc.R.Counter(metrics.NewMetricId(DirectoryMetricNumToReconnect, level)).Inc() 107 case NumValidTotal: 108 rc.R.Counter(metrics.NewMetricId(DirectoryMetricNumValid, level)).Inc() 109 default: 110 } 111 112 } 113 114 // serverRegHandler handles server register metrics 115 func (rc *registryCollector) serverRegHandler(event *RegistryMetricsEvent) { 116 // Event is converted to metrics 117 // Save metrics to the MetricRegistry 118 level := metrics.GetApplicationLevel() 119 rc.StateCount(ServiceRegisterMetricRequests, ServiceRegisterMetricRequestsSucceed, ServiceRegisterMetricRequestsFailed, level, event.Succ) 120 rc.R.Rt(metrics.NewMetricId(RegisterServiceRt, level), &metrics.RtOpts{}).Observe(event.CostMs()) 121 } 122 123 // serverSubHandler handles server subscribe metrics 124 func (rc *registryCollector) serverSubHandler(event *RegistryMetricsEvent) { 125 // Event is converted to metrics 126 // Save metrics to the MetricRegistry 127 level := metrics.GetApplicationLevel() 128 rc.StateCount(ServiceSubscribeMetricNum, ServiceSubscribeMetricNumSucceed, ServiceSubscribeMetricNumFailed, level, event.Succ) 129 }