Skip to content

Commit b067070

Browse files
xiaochaoren1SongZhen0704
authored andcommitted
feat: profile support has no app_service
1 parent e175dd6 commit b067070

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

server/querier/profile/model/model.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package model
1919
import "context"
2020

2121
type Profile struct {
22-
AppService string `json:"app_service" binding:"required"`
22+
AppService string `json:"app_service"`
2323
ProfileEventType string `json:"profile_event_type" binding:"required"`
2424
ProfileLanguageType string `json:"profile_language_type" binding:"required"`
2525
TagFilter string `json:"tag_filter"`

server/querier/profile/service/grafana_profile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func GrafanaProfile(args model.Profile, cfg *config.QuerierConfig, where string)
2525
result = &model.GrafanaProfileValue{}
2626
result.Columns = []string{"level", "function", "self_value", "total_value"}
2727

28-
tree, generateDebug, err := GenerateProfile(args, cfg, where)
28+
tree, generateDebug, err := GenerateProfile(args, cfg, where, model.ProfileDebug{})
2929
debug = generateDebug
3030
if err != nil {
3131
return

server/querier/profile/service/profile.go

+55-5
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,72 @@ const (
4545
initNodeCapacity = 8192
4646
)
4747

48-
func Profile(args model.Profile, cfg *config.QuerierConfig) (result model.ProfileTree, debug interface{}, err error) {
48+
func GetAppService(args model.Profile, where string, debugs model.ProfileDebug) ([]string, model.ProfileDebug, error) {
49+
sql := fmt.Sprintf(
50+
"SELECT app_service FROM %s WHERE %s GROUP BY app_service LIMIT 11",
51+
common.TABLE_PROFILE, where,
52+
)
53+
ckEngine := &clickhouse.CHEngine{DB: common.DATABASE_PROFILE}
54+
ckEngine.Init()
55+
querierArgs := querier_common.QuerierParams{
56+
DB: common.DATABASE_PROFILE,
57+
Sql: sql,
58+
Debug: strconv.FormatBool(args.Debug),
59+
Context: args.Context,
60+
ORGID: args.OrgID,
61+
}
62+
63+
querierResult, querierDebug, err := ckEngine.ExecuteQuery(&querierArgs)
64+
profileDebug := NewProfileDebug(sql, querierDebug)
65+
debugs.QuerierDebug = append(debugs.QuerierDebug, profileDebug)
66+
appServices := []string{}
67+
if err != nil {
68+
log.Errorf("ExecuteQuery failed: %v", querierDebug, err)
69+
return appServices, debugs, err
70+
}
71+
values := querierResult.Values
72+
for _, value := range values {
73+
switch valueSlice := value.(type) {
74+
case []interface{}:
75+
appService, ok := valueSlice[0].(string)
76+
if ok {
77+
appServices = append(appServices, appService)
78+
}
79+
}
80+
}
81+
return appServices, debugs, nil
82+
}
83+
84+
func Profile(args model.Profile, cfg *config.QuerierConfig) (model.ProfileTree, interface{}, error) {
4985
whereSlice := []string{}
5086
whereSlice = append(whereSlice, fmt.Sprintf(" time>=%d", args.TimeStart))
5187
whereSlice = append(whereSlice, fmt.Sprintf(" time<=%d", args.TimeEnd))
52-
whereSlice = append(whereSlice, fmt.Sprintf(" app_service='%s'", args.AppService))
88+
if args.AppService != "" {
89+
whereSlice = append(whereSlice, fmt.Sprintf(" app_service='%s'", args.AppService))
90+
}
5391
whereSlice = append(whereSlice, fmt.Sprintf(" profile_language_type='%s'", args.ProfileLanguageType))
5492
whereSlice = append(whereSlice, fmt.Sprintf(" profile_event_type='%s'", args.ProfileEventType))
5593
if args.TagFilter != "" {
5694
whereSlice = append(whereSlice, " ("+args.TagFilter+")")
5795
}
5896
whereSql := strings.Join(whereSlice, " AND")
59-
return GenerateProfile(args, cfg, whereSql)
97+
debugs := model.ProfileDebug{}
98+
if args.AppService == "" {
99+
appServices, appDebugs, err := GetAppService(args, whereSql, debugs)
100+
debugs = appDebugs
101+
if err != nil {
102+
log.Errorf("GetAppService failed: %v", err)
103+
return model.ProfileTree{}, debugs, err
104+
}
105+
if len(appServices) < 1 || len(appServices) > 10 {
106+
return model.ProfileTree{}, debugs, fmt.Errorf("app_service count %d is invalid", len(appServices))
107+
}
108+
args.AppService = strings.Join(appServices, ", ")
109+
}
110+
return GenerateProfile(args, cfg, whereSql, debugs)
60111
}
61112

62-
func GenerateProfile(args model.Profile, cfg *config.QuerierConfig, where string) (result model.ProfileTree, debug interface{}, err error) {
63-
debugs := model.ProfileDebug{}
113+
func GenerateProfile(args model.Profile, cfg *config.QuerierConfig, where string, debugs model.ProfileDebug) (result model.ProfileTree, debug interface{}, err error) {
64114
limitSql := cfg.Profile.FlameQueryLimit
65115
sql := fmt.Sprintf(
66116
"SELECT %s, %s FROM %s WHERE %s LIMIT %d",

0 commit comments

Comments
 (0)