dubbo.apache.org/dubbo-go/v3@v3.1.1/cluster/metrics/local_metrics.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 "fmt" 22 "sync" 23 ) 24 25 import ( 26 "dubbo.apache.org/dubbo-go/v3/common" 27 ) 28 29 var LocalMetrics Metrics 30 31 func init() { 32 LocalMetrics = newLocalMetrics() 33 } 34 35 var _ Metrics = (*localMetrics)(nil) 36 37 type localMetrics struct { 38 // protect metrics 39 lock *sync.RWMutex 40 metrics map[string]interface{} 41 } 42 43 func newLocalMetrics() *localMetrics { 44 return &localMetrics{ 45 lock: new(sync.RWMutex), 46 metrics: make(map[string]interface{}), 47 } 48 } 49 50 func (m *localMetrics) GetMethodMetrics(url *common.URL, methodName, key string) (interface{}, error) { 51 m.lock.RLock() 52 defer m.lock.RUnlock() 53 metricsKey := fmt.Sprintf("%s.%s.%s.%s", getInstanceKey(url), getInvokerKey(url), methodName, key) 54 if metrics, ok := m.metrics[metricsKey]; ok { 55 return metrics, nil 56 } 57 return nil, ErrMetricsNotFound 58 } 59 60 func (m *localMetrics) SetMethodMetrics(url *common.URL, methodName, key string, value interface{}) error { 61 m.lock.Lock() 62 defer m.lock.Unlock() 63 metricsKey := fmt.Sprintf("%s.%s.%s.%s", getInstanceKey(url), getInvokerKey(url), methodName, key) 64 m.metrics[metricsKey] = value 65 return nil 66 } 67 68 func (m *localMetrics) GetInvokerMetrics(url *common.URL, key string) (interface{}, error) { 69 panic("implement me") 70 } 71 72 func (m *localMetrics) SetInvokerMetrics(url *common.URL, key string, value interface{}) error { 73 panic("implement me") 74 } 75 76 func (m *localMetrics) GetInstanceMetrics(url *common.URL, key string) (interface{}, error) { 77 panic("implement me") 78 } 79 80 func (m *localMetrics) SetInstanceMetrics(url *common.URL, key string, value interface{}) error { 81 panic("implement me") 82 }