github.com/matrixorigin/matrixone@v1.2.0/pkg/pb/metric/pbutil.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package metric 16 17 import ( 18 "fmt" 19 20 dto "github.com/prometheus/client_model/go" 21 ) 22 23 // P2M means prometheus to matrixone 24 25 func P2MMetricFamilies(mfs []*dto.MetricFamily) []*MetricFamily { 26 moMfs := make([]*MetricFamily, 0, len(mfs)) 27 for _, mf := range mfs { 28 if mf.GetType() == dto.MetricType_UNTYPED { 29 // Untyped is used as a placeholder, it has no data 30 continue 31 } 32 moMf := &MetricFamily{ 33 Name: mf.GetName(), 34 Help: mf.GetHelp(), 35 } 36 switch mf.GetType() { 37 case dto.MetricType_COUNTER: 38 moMf.Type = MetricType_COUNTER 39 moMf.Metric = P2MCounters(mf.Metric) 40 case dto.MetricType_GAUGE: 41 moMf.Type = MetricType_GAUGE 42 moMf.Metric = P2MGauges(mf.Metric) 43 default: 44 panic(fmt.Sprintf("unsupported metric type in mo %v", mf.GetType())) 45 } 46 moMfs = append(moMfs, moMf) 47 } 48 return moMfs 49 } 50 51 func P2MCounters(ms []*dto.Metric) []*Metric { 52 moMetrics := make([]*Metric, len(ms)) 53 for i, m := range ms { 54 moMetrics[i] = &Metric{ 55 Label: P2MLabelPairs(m.Label), 56 Counter: &Counter{Value: m.Counter.GetValue()}, 57 } 58 } 59 return moMetrics 60 } 61 62 func P2MGauges(ms []*dto.Metric) []*Metric { 63 moMetrics := make([]*Metric, len(ms)) 64 for i, m := range ms { 65 moMetrics[i] = &Metric{ 66 Label: P2MLabelPairs(m.Label), 67 Gauge: &Gauge{Value: m.Gauge.GetValue()}, 68 } 69 } 70 return moMetrics 71 } 72 73 func P2MLabelPairs(lbls []*dto.LabelPair) []*LabelPair { 74 moLbls := make([]*LabelPair, len(lbls)) 75 for i, lbl := range lbls { 76 moLbls[i] = &LabelPair{ 77 Name: lbl.GetName(), 78 Value: lbl.GetValue(), 79 } 80 } 81 return moLbls 82 }