diff --git a/collector/app/handle.go b/collector/app/handle.go index ffd76de..183984e 100644 --- a/collector/app/handle.go +++ b/collector/app/handle.go @@ -25,10 +25,10 @@ import ( func initHttpServer(a *App) observer.Handler { return func(coll map[info.Info]*config.Config) { - a.Logger().Info("about to start server") - dataID := serverconfig.DataID + a.Logger().Info(fmt.Sprintf("start to get config: group = %s, dataID = %s", a.ACMGroup(), dataID)) + cc, ok := coll[info.Info{ Group: a.ACMGroup(), DataID: dataID, @@ -77,6 +77,8 @@ func initTablestoreClient(a *App) observer.Handler { return func(coll map[info.Info]*config.Config) { dataID := tablestoreconfig.DataID + a.Logger().Info(fmt.Sprintf("start to get config: group = %s, dataID = %s", a.ACMGroup(), dataID)) + cc, ok := coll[info.Info{ Group: a.ACMGroup(), DataID: dataID, diff --git a/collector/server/controller/profile/controller.go b/collector/server/controller/profile/controller.go index fa7c256..20ff791 100644 --- a/collector/server/controller/profile/controller.go +++ b/collector/server/controller/profile/controller.go @@ -7,6 +7,7 @@ import ( "fmt" "io/ioutil" "net/http" + "reflect" "strconv" "time" @@ -466,28 +467,26 @@ func searchProfile(tb *env.TablestoreClient, req MergeProfileReq, offset int32) */ func unMarshalProfileRow(row *tablestore.Row) *profilemodel.Model { result := new(profilemodel.Model) - for _, v := range row.Columns { - switch v.ColumnName { - case profilemodel.CreateTime: - result.CreateTime = v.Value.(int64) - case profilemodel.GoVersion: - result.GoVersion = v.Value.(string) - case profilemodel.Host: - result.Host = v.Value.(string) - case profilemodel.IP: - result.IP = v.Value.(string) - case profilemodel.ProfileType: - result.ProfileType = v.Value.(string) - case profilemodel.SendTime: - result.SendTime = v.Value.(int64) - case profilemodel.Service: - result.Service = v.Value.(string) - case profilemodel.ServiceVersion: - result.ServiceVersion = v.Value.(string) - case profilemodel.ObjectName: - result.ObjectName = v.Value.(string) - case profilemodel.Size: - result.Size = v.Value.(int64) + ve := reflect.ValueOf(result).Elem() + te := reflect.TypeOf(result).Elem() + ret := make(map[string]reflect.Value) + for i := 0; i < te.NumField(); i++ { + tag := te.Field(i).Tag.Get("ots") + ret[tag] = ve.Field(i) + } + for _, column := range row.Columns { + v, ok := ret[column.ColumnName] + if !ok { + continue + } + if !v.IsValid() || !v.CanSet() { + continue + } + switch v.Kind() { + case reflect.String: + v.SetString(column.Value.(string)) + case reflect.Int64: + v.SetInt(column.Value.(int64)) } } return result diff --git a/collector/server/controller/profile/controller_test.go b/collector/server/controller/profile/controller_test.go index bfb5086..79fe0cc 100644 --- a/collector/server/controller/profile/controller_test.go +++ b/collector/server/controller/profile/controller_test.go @@ -1,7 +1,23 @@ package profile -import "testing" +import ( + "testing" + + "github.com/aliyun/aliyun-tablestore-go-sdk/v5/tablestore" +) func TestUploadPath(t *testing.T) { t.Logf(UploadPath("abc", "bcf", "cpu", "efg")) } + +func TestUnMarshalProfileRow(t *testing.T) { + row := new(tablestore.Row) + row.Columns = append(row.Columns, &tablestore.AttributeColumn{ + ColumnName: "profile_id", + Value: "dfdfdkfmdkfkdfkdm", + }, &tablestore.AttributeColumn{ + ColumnName: "size", + Value: int64(64), + }) + unMarshalProfileRow(row) +} diff --git a/collector/server/model/profilemodel/model.go b/collector/server/model/profilemodel/model.go index b13394d..7a0962a 100644 --- a/collector/server/model/profilemodel/model.go +++ b/collector/server/model/profilemodel/model.go @@ -15,15 +15,15 @@ const ( ) type Model struct { - ProfileId string `json:"profile_id"` - Service string `json:"service"` - ServiceVersion string `json:"service_version"` - Host string `json:"host"` - IP string `json:"ip"` - GoVersion string `json:"go_version"` - ProfileType string `json:"profile_type"` - SendTime int64 `json:"send_time"` - CreateTime int64 `json:"create_time"` - ObjectName string `json:"object_name"` - Size int64 `json:"size"` + ProfileId string `ots:"profile_id"` + Service string `ots:"service"` + ServiceVersion string `ots:"service_version"` + Host string `ots:"host"` + IP string `ots:"ip"` + GoVersion string `ots:"go_version"` + ProfileType string `ots:"profile_type"` + SendTime int64 `ots:"send_time"` + CreateTime int64 `ots:"create_time"` + ObjectName string `ots:"object_name"` + Size int64 `ots:"size"` } diff --git a/script/tablestore_test.go b/script/tablestore_test.go new file mode 100644 index 0000000..4759160 --- /dev/null +++ b/script/tablestore_test.go @@ -0,0 +1 @@ +package script