dubbo.apache.org/dubbo-go/v3@v3.1.1/metrics/config_center/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 metrics
    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  	"dubbo.apache.org/dubbo-go/v3/remoting"
    25  )
    26  
    27  const eventType = constant.MetricsConfigCenter
    28  
    29  var ch = make(chan metrics.MetricsEvent, 10)
    30  var info = metrics.NewMetricKey("dubbo_configcenter_total", "Config Changed Total")
    31  
    32  func init() {
    33  	metrics.AddCollector("config_center", func(mr metrics.MetricRegistry, url *common.URL) {
    34  		if url.GetParamBool(constant.ConfigCenterEnabledKey, true) {
    35  			c := &configCenterCollector{r: mr}
    36  			c.start()
    37  		}
    38  	})
    39  }
    40  
    41  type configCenterCollector struct {
    42  	r metrics.MetricRegistry
    43  }
    44  
    45  func (c *configCenterCollector) start() {
    46  	metrics.Subscribe(eventType, ch)
    47  	go func() {
    48  		for e := range ch {
    49  			if event, ok := e.(*ConfigCenterMetricEvent); ok {
    50  				c.handleDataChange(event)
    51  			}
    52  		}
    53  	}()
    54  }
    55  
    56  func (c *configCenterCollector) handleDataChange(event *ConfigCenterMetricEvent) {
    57  	id := metrics.NewMetricId(info, metrics.NewConfigCenterLevel(event.key, event.group, event.configCenter, event.getChangeType()))
    58  	c.r.Counter(id).Add(event.size)
    59  }
    60  
    61  const (
    62  	Nacos     = "nacos"
    63  	Apollo    = "apollo"
    64  	Zookeeper = "zookeeper"
    65  )
    66  
    67  type ConfigCenterMetricEvent struct {
    68  	// Name  MetricName
    69  	key          string
    70  	group        string
    71  	configCenter string
    72  	changeType   remoting.EventType
    73  	size         float64
    74  }
    75  
    76  func (e *ConfigCenterMetricEvent) getChangeType() string {
    77  	switch e.changeType {
    78  	case remoting.EventTypeAdd:
    79  		return "added"
    80  	case remoting.EventTypeDel:
    81  		return "deleted"
    82  	case remoting.EventTypeUpdate:
    83  		return "modified"
    84  	default:
    85  		return ""
    86  	}
    87  }
    88  
    89  func (*ConfigCenterMetricEvent) Type() string {
    90  	return eventType
    91  }
    92  
    93  func NewIncMetricEvent(key, group string, changeType remoting.EventType, c string) *ConfigCenterMetricEvent {
    94  	return &ConfigCenterMetricEvent{key: key, group: group, changeType: changeType, configCenter: c, size: 1}
    95  }