@@ -2,6 +2,7 @@ package instance
22
33import (
44 "fmt"
5+ "github.com/tencentyun/tencentcloud-exporter/pkg/util"
56 "reflect"
67)
78
@@ -16,6 +17,9 @@ type TcInstance interface {
1617 // 根据字段名称获取该字段的值, 由各个产品接口具体实现
1718 GetFieldValueByName (string ) (string , error )
1819
20+ // 根据字段名称获取该字段的值, 由各个产品接口具体实现
21+ GetFieldValuesByName (string ) (map [string ][]string , error )
22+
1923 // 获取实例raw元数据, 每个实例类型不一样
2024 GetMeta () interface {}
2125}
@@ -36,7 +40,7 @@ func (ins *baseTcInstance) GetMonitorQueryKey() string {
3640func (ins * baseTcInstance ) GetFieldValueByName (name string ) (val string , err error ) {
3741 defer func () {
3842 if err := recover (); err != nil {
39- //nothing ignore err
43+ // nothing ignore err
4044 }
4145 }()
4246 v := ins .value .FieldByName (name )
@@ -45,3 +49,44 @@ func (ins *baseTcInstance) GetFieldValueByName(name string) (val string, err err
4549 }
4650 return fmt .Sprintf ("%v" , v .Interface ()), nil
4751}
52+
53+ func (ins * baseTcInstance ) GetFieldValuesByName (name string ) (val map [string ][]string , err error ) {
54+ defer func () {
55+ if err := recover (); err != nil {
56+ // nothing ignore err
57+ }
58+ }()
59+ v := ins .value .FieldByName (name )
60+ if v .Kind () == reflect .Ptr {
61+ v = reflect .Indirect (v )
62+ }
63+ valueMap := make (map [string ][]string )
64+ if v .Kind () == reflect .Slice {
65+ for i := 0 ; i < v .Len (); i ++ {
66+ if v .Index (i ).Elem ().Kind () == reflect .String {
67+ valueMap [name ] = append (val [name ], fmt .Sprintf ("%v" , v .Index (i ).Elem ().Interface ()))
68+ } else if v .Index (i ).Elem ().Kind () == reflect .Struct {
69+ var tagKey , tagValue reflect.Value
70+ if v .Index (i ).Elem ().FieldByName ("TagKey" ).IsValid () && v .Index (i ).Elem ().FieldByName ("TagValue" ).IsValid () {
71+ tagKey = v .Index (i ).Elem ().FieldByName ("TagKey" )
72+ tagValue = v .Index (i ).Elem ().FieldByName ("TagValue" )
73+ } else if v .Index (i ).Elem ().FieldByName ("Key" ).IsValid () && v .Index (i ).Elem ().FieldByName ("Value" ).IsValid () {
74+ tagKey = v .Index (i ).Elem ().FieldByName ("Key" )
75+ tagValue = v .Index (i ).Elem ().FieldByName ("Value" )
76+ }
77+ if tagKey .Kind () == reflect .Ptr {
78+ tagKey = reflect .Indirect (tagKey )
79+ }
80+ if tagValue .Kind () == reflect .Ptr {
81+ tagValue = reflect .Indirect (tagValue )
82+ }
83+ if util .IsValidTagKey (tagKey .String ()) {
84+ valueMap [tagKey .String ()] = append (val [tagKey .String ()], tagValue .String ())
85+ }
86+ }
87+ }
88+ } else {
89+ valueMap [name ] = append (val [name ], fmt .Sprintf ("%v" , v .Interface ()))
90+ }
91+ return valueMap , nil
92+ }
0 commit comments