dubbo.apache.org/dubbo-go/v3@v3.1.1/metrics/metadata/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 metadata
    19  
    20  import (
    21  	"time"
    22  )
    23  
    24  import (
    25  	"dubbo.apache.org/dubbo-go/v3/common"
    26  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    27  	"dubbo.apache.org/dubbo-go/v3/metrics"
    28  )
    29  
    30  const eventType = constant.MetricsMetadata
    31  
    32  var ch = make(chan metrics.MetricsEvent, 10)
    33  
    34  func init() {
    35  	metrics.AddCollector("metadata", func(mr metrics.MetricRegistry, url *common.URL) {
    36  		if url.GetParamBool(constant.MetadataEnabledKey, true) {
    37  			l := &MetadataMetricCollector{metrics.BaseCollector{R: mr}}
    38  			l.start()
    39  		}
    40  	})
    41  }
    42  
    43  type MetadataMetricCollector struct {
    44  	metrics.BaseCollector
    45  }
    46  
    47  func (c *MetadataMetricCollector) start() {
    48  	metrics.Subscribe(eventType, ch)
    49  	go func() {
    50  		for e := range ch {
    51  			if event, ok := e.(*MetadataMetricEvent); ok {
    52  				switch event.Name {
    53  				case StoreProvider:
    54  					c.handleStoreProvider(event)
    55  				case MetadataPush:
    56  					c.handleMetadataPush(event)
    57  				case MetadataSub:
    58  					c.handleMetadataSub(event)
    59  				case SubscribeServiceRt:
    60  					c.handleSubscribeService(event)
    61  				default:
    62  				}
    63  			}
    64  		}
    65  	}()
    66  }
    67  
    68  func (c *MetadataMetricCollector) handleMetadataPush(event *MetadataMetricEvent) {
    69  	level := metrics.GetApplicationLevel()
    70  	c.StateCount(metadataPushNum, metadataPushSucceed, metadataPushFailed, level, event.Succ)
    71  	c.R.Rt(metrics.NewMetricId(pushRt, level), &metrics.RtOpts{}).Observe(event.CostMs())
    72  }
    73  
    74  func (c *MetadataMetricCollector) handleMetadataSub(event *MetadataMetricEvent) {
    75  	level := metrics.GetApplicationLevel()
    76  	c.StateCount(metadataSubNum, metadataSubSucceed, metadataSubFailed, level, event.Succ)
    77  	c.R.Rt(metrics.NewMetricId(subscribeRt, level), &metrics.RtOpts{}).Observe(event.CostMs())
    78  }
    79  
    80  func (c *MetadataMetricCollector) handleStoreProvider(event *MetadataMetricEvent) {
    81  	level := metrics.NewServiceMetric(event.Attachment[constant.InterfaceKey])
    82  	c.StateCount(metadataStoreProviderNum, metadataStoreProviderSucceed, metadataStoreProviderFailed, level, event.Succ)
    83  	c.R.Rt(metrics.NewMetricId(storeProviderInterfaceRt, level), &metrics.RtOpts{}).Observe(event.CostMs())
    84  }
    85  
    86  func (c *MetadataMetricCollector) handleSubscribeService(event *MetadataMetricEvent) {
    87  	level := metrics.NewServiceMetric(event.Attachment[constant.InterfaceKey])
    88  	c.R.Rt(metrics.NewMetricId(subscribeServiceRt, level), &metrics.RtOpts{}).Observe(event.CostMs())
    89  }
    90  
    91  type MetadataMetricEvent struct {
    92  	Name       MetricName
    93  	Succ       bool
    94  	Start      time.Time
    95  	End        time.Time
    96  	Attachment map[string]string
    97  }
    98  
    99  func (*MetadataMetricEvent) Type() string {
   100  	return eventType
   101  }
   102  
   103  func (e *MetadataMetricEvent) CostMs() float64 {
   104  	return float64(e.End.Sub(e.Start)) / float64(time.Millisecond)
   105  }
   106  
   107  func NewMetadataMetricTimeEvent(n MetricName) *MetadataMetricEvent {
   108  	return &MetadataMetricEvent{Name: n, Start: time.Now(), Attachment: make(map[string]string)}
   109  }