Skip to content

Commit

Permalink
feat: 修复 vm 非单指标单表指标查询使用异常问题 #1010158081115702891 (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
shamcleren authored Dec 28, 2023
1 parent 3997aa2 commit 715ff98
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 46 deletions.
36 changes: 18 additions & 18 deletions pkg/unify-query/metadata/reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestCheckVmQuery(t *testing.T) {
Field: "usage",
IsSingleMetric: false,
VmRt: "100147_ieod_system_net_raw",
VmCondition: `result_table_id="100147_ieod_system_net_raw", __name__="cpu_detail_usage"`,
VmCondition: `result_table_id="100147_ieod_system_net_raw", __name__="usage_value"`,
AggregateMethodList: []AggrMethod{
{
Name: "sum",
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestCheckVmQuery(t *testing.T) {
VmRt: "100147_ieod_system_net_raw",
Condition: "(bk_inst_id='test' and bk_obj_id='demo') or bk_biz_id='test-1'",
AggregateMethodList: []AggrMethod{},
VmCondition: `result_table_id="100147_ieod_system_net_raw", __name__="cpu_detail_usage", bk_inst_id="test", bk_obj_id="demo" or result_table_id="100147_ieod_system_net_raw", __name__="cpu_detail_usage", bk_biz_id="test-1"`,
VmCondition: `result_table_id="100147_ieod_system_net_raw", __name__="usage_value", bk_inst_id="test", bk_obj_id="demo" or result_table_id="100147_ieod_system_net_raw", __name__="usage_value", bk_biz_id="test-1"`,
},
},
ReferenceName: refNameA,
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestCheckVmQuery(t *testing.T) {
Field: "usage",
IsSingleMetric: false,
VmRt: "100147_ieod_system_net_raw",
VmCondition: `result_table_id="100147_ieod_system_net_raw", __name__="cpu_detail_usage"`,
VmCondition: `result_table_id="100147_ieod_system_net_raw", __name__="usage_value"`,
AggregateMethodList: []AggrMethod{
{
Name: "sum",
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestCheckVmQuery(t *testing.T) {
Field: "usage",
IsSingleMetric: false,
VmRt: "100147_ieod_system_detail_raw",
VmCondition: `result_table_id="100147_ieod_system_detail_raw", __name__="cpu_detail_usage"`,
VmCondition: `result_table_id="100147_ieod_system_detail_raw", __name__="usage_value"`,
AggregateMethodList: []AggrMethod{
{
Name: "sum",
Expand All @@ -210,7 +210,7 @@ func TestCheckVmQuery(t *testing.T) {
IsSingleMetric: false,
VmRt: "100147_ieod_system_summary_raw",
Condition: "bk_obj_id = '1' and bk_inst_id = '2'",
VmCondition: `result_table_id="100147_ieod_system_summary_raw", __name__="cpu_summary_usage",bk_obj_id="1", bk_inst_id="2"`,
VmCondition: `result_table_id="100147_ieod_system_summary_raw", __name__="usage_value",bk_obj_id="1", bk_inst_id="2"`,
AggregateMethodList: []AggrMethod{
{
Name: "sum",
Expand Down Expand Up @@ -245,7 +245,7 @@ func TestCheckVmQuery(t *testing.T) {
Field: "usage",
IsSingleMetric: false,
VmRt: "100147_ieod_system_detail_raw",
VmCondition: `result_table_id="100147_ieod_system_detail_raw", __name__="cpu_detail_usage"`,
VmCondition: `result_table_id="100147_ieod_system_detail_raw", __name__="usage_value"`,
AggregateMethodList: []AggrMethod{
{
Name: "sum",
Expand All @@ -261,7 +261,7 @@ func TestCheckVmQuery(t *testing.T) {
Field: "usage",
IsSingleMetric: false,
VmRt: "100147_ieod_system_summary_raw",
VmCondition: `result_table_id="100147_ieod_system_summary_raw", __name__="cpu_summary_usage"`,
VmCondition: `result_table_id="100147_ieod_system_summary_raw", __name__="usage_value"`,
AggregateMethodList: []AggrMethod{
{
Name: "sum",
Expand Down Expand Up @@ -298,7 +298,7 @@ func TestCheckVmQuery(t *testing.T) {
Field: "usage",
IsSingleMetric: true,
VmRt: "100147_ieod_system_detail_raw",
VmCondition: `result_table_id="100147_ieod_system_detail_raw", __name__="cpu_detail_usage"`,
VmCondition: `result_table_id="100147_ieod_system_detail_raw", __name__="usage_value"`,
AggregateMethodList: []AggrMethod{
{
Name: "sum",
Expand All @@ -314,7 +314,7 @@ func TestCheckVmQuery(t *testing.T) {
Field: "usage",
IsSingleMetric: true,
VmRt: "100147_ieod_system_summary_raw",
VmCondition: `result_table_id="100147_ieod_system_summary_raw", __name__="cpu_summary_usage"`,
VmCondition: `result_table_id="100147_ieod_system_summary_raw", __name__="usage_value"`,
AggregateMethodList: []AggrMethod{
{
Name: "sum",
Expand All @@ -331,7 +331,7 @@ func TestCheckVmQuery(t *testing.T) {
expected: checkExpected{
ok: true,
vmConditions: map[string]string{
refNameA: `result_table_id="100147_ieod_system_detail_cmdb", __name__="cpu_detail_usage" or result_table_id="100147_ieod_system_summary_cmdb", __name__="cpu_summary_usage"`,
refNameA: `result_table_id="100147_ieod_system_detail_cmdb", __name__="usage_value" or result_table_id="100147_ieod_system_summary_cmdb", __name__="usage_value"`,
},
vmRtList: []string{
"100147_ieod_system_detail_cmdb",
Expand All @@ -351,7 +351,7 @@ func TestCheckVmQuery(t *testing.T) {
Field: "usage",
IsSingleMetric: false,
VmRt: "100147_ieod_system_detail_raw",
VmCondition: `result_table_id="100147_ieod_system_detail_raw", __name__="cpu_detail_usage"`,
VmCondition: `result_table_id="100147_ieod_system_detail_raw", __name__="usage_value"`,
AggregateMethodList: []AggrMethod{
{
Name: "sum",
Expand All @@ -373,7 +373,7 @@ func TestCheckVmQuery(t *testing.T) {
Field: "usage",
IsSingleMetric: false,
VmRt: "100147_ieod_system_summary_raw",
VmCondition: `result_table_id="100147_ieod_system_summary_raw", __name__="cpu_summary_usage"`,
VmCondition: `result_table_id="100147_ieod_system_summary_raw", __name__="usage_value"`,
AggregateMethodList: []AggrMethod{
{
Name: "sum",
Expand Down Expand Up @@ -412,7 +412,7 @@ func TestCheckVmQuery(t *testing.T) {
Field: "usage",
IsSingleMetric: false,
VmRt: "100147_ieod_system_detail_raw",
VmCondition: `result_table_id="100147_ieod_system_detail_raw", __name__="cpu_detail_usage"`,
VmCondition: `result_table_id="100147_ieod_system_detail_raw", __name__="usage_value"`,
AggregateMethodList: []AggrMethod{
{
Name: "sum",
Expand Down Expand Up @@ -441,7 +441,7 @@ func TestCheckVmQuery(t *testing.T) {
Field: "usage",
IsSingleMetric: false,
VmRt: "100147_ieod_system_summary_raw",
VmCondition: `result_table_id="100147_ieod_system_summary_raw", __name__="cpu_summary_usage"`,
VmCondition: `result_table_id="100147_ieod_system_summary_raw", __name__="usage_value"`,
AggregateMethodList: []AggrMethod{
{
Name: "sum",
Expand Down Expand Up @@ -506,7 +506,7 @@ func TestCheckVmQuery(t *testing.T) {
Field: "usage",
IsSingleMetric: false,
VmRt: "100147_ieod_system_summary_raw",
VmCondition: `result_table_id="100147_ieod_system_summary_raw", __name__="cpu_summary_usage"`,
VmCondition: `result_table_id="100147_ieod_system_summary_raw", __name__="usage_value"`,
AggregateMethodList: []AggrMethod{
{
Name: "sum",
Expand Down Expand Up @@ -564,7 +564,7 @@ func TestCheckVmQuery(t *testing.T) {
Field: "usage",
IsSingleMetric: true,
VmRt: "100147_ieod_system_summary_raw",
VmCondition: `result_table_id="100147_ieod_system_summary_raw", __name__="cpu_summary_usage"`,
VmCondition: `result_table_id="100147_ieod_system_summary_raw", __name__="usage_value"`,
AggregateMethodList: []AggrMethod{
{
Name: "sum",
Expand All @@ -580,7 +580,7 @@ func TestCheckVmQuery(t *testing.T) {
ok: true,
vmConditions: map[string]string{
refNameA: `result_table_id="100147_ieod_system_detail_raw", __name__="usage_value"`,
refNameB: `result_table_id="100147_ieod_system_summary_raw", __name__="cpu_summary_usage"`,
refNameB: `result_table_id="100147_ieod_system_summary_raw", __name__="usage_value"`,
},
vmRtList: []string{
"100147_ieod_system_detail_raw",
Expand All @@ -592,7 +592,7 @@ func TestCheckVmQuery(t *testing.T) {

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
ctx = context.Background()
ctx = InitHashID(ctx)

SetUser(ctx, tc.source, tc.spaceUid)

Expand Down
19 changes: 0 additions & 19 deletions pkg/unify-query/metadata/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,19 +216,7 @@ func (qRef QueryReference) CheckMustVmQuery(ctx context.Context) bool {
continue
}

// 更改为单指标单表
replaceLabels := make(ReplaceLabels)

oldMetric := fmt.Sprintf("%s_%s", query.Measurement, query.Field)
newMetric := fmt.Sprintf("%s_%s", query.Field, StaticField)
query.IsSingleMetric = true

replaceLabels["__name__"] = ReplaceLabel{
Source: oldMetric,
Target: newMetric,
}

query.VmCondition = ReplaceVmCondition(query.VmCondition, replaceLabels)
}
}

Expand Down Expand Up @@ -292,14 +280,7 @@ func (qRef QueryReference) CheckDruidCheck(ctx context.Context) bool {
}

if !query.IsSingleMetric {
oldMetric := fmt.Sprintf("%s_%s", query.Measurement, query.Field)
newMetric := fmt.Sprintf("%s_%s", query.Field, StaticField)
query.IsSingleMetric = true

replaceLabels["__name__"] = ReplaceLabel{
Source: oldMetric,
Target: newMetric,
}
}

query.VmCondition = ReplaceVmCondition(query.VmCondition, replaceLabels)
Expand Down
4 changes: 2 additions & 2 deletions pkg/unify-query/query/structured/query_ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,8 @@ func (q *Query) BuildMetadataQuery(

// 用于 vm 的查询逻辑特殊处理
var vmMetric string
if measurement != "" {
vmMetric = fmt.Sprintf("%s_%s", measurement, field)
if metricName != "" {
vmMetric = fmt.Sprintf("%s_%s", metricName, promql.StaticField)
}

// 因为 vm 查询指标会转换格式,所以在查询的时候需要把用到指标的函数都进行替换,例如 label_replace
Expand Down
14 changes: 7 additions & 7 deletions pkg/unify-query/query/structured/query_ts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ func TestQueryTs_ToQueryReference(t *testing.T) {
IsSingleMetric: false,
Measurement: "cpu_detail",
Field: "usage",
VmCondition: `bk_biz_id="2", result_table_id="100147_ieod_system_cpu_detail_raw", __name__="cpu_detail_usage"`,
VmCondition: `bk_biz_id="2", result_table_id="100147_ieod_system_cpu_detail_raw", __name__="usage_value"`,
},
},
},
Expand All @@ -645,7 +645,7 @@ func TestQueryTs_ToQueryReference(t *testing.T) {
IsSingleMetric: false,
Measurement: "cpu_summary",
Field: "usage",
VmCondition: `bk_biz_id="2", result_table_id="100147_ieod_system_cpu_summary_raw", __name__="cpu_summary_usage"`,
VmCondition: `bk_biz_id="2", result_table_id="100147_ieod_system_cpu_summary_raw", __name__="usage_value"`,
},
},
},
Expand All @@ -671,7 +671,7 @@ func TestQueryTs_ToQueryReference(t *testing.T) {
IsSingleMetric: false,
Measurement: "cpu_summary",
Field: "usage",
VmCondition: `bk_biz_id="2", result_table_id="100147_ieod_system_cpu_summary_raw", __name__="cpu_summary_usage"`,
VmCondition: `bk_biz_id="2", result_table_id="100147_ieod_system_cpu_summary_raw", __name__="usage_value"`,
},
},
},
Expand All @@ -697,7 +697,7 @@ func TestQueryTs_ToQueryReference(t *testing.T) {
IsSingleMetric: false,
Measurement: "cpu_detail",
Field: "usage",
VmCondition: `bk_biz_id="2", result_table_id="100147_ieod_system_cpu_detail_raw", __name__="cpu_detail_usage"`,
VmCondition: `bk_biz_id="2", result_table_id="100147_ieod_system_cpu_detail_raw", __name__="usage_value"`,
},
},
},
Expand Down Expand Up @@ -728,7 +728,7 @@ func TestQueryTs_ToQueryReference(t *testing.T) {
IsSingleMetric: false,
Measurement: "cpu_detail",
Field: "usage",
VmCondition: `bk_biz_id="2", result_table_id="100147_ieod_system_cpu_detail_raw", __name__="cpu_detail_usage"`,
VmCondition: `bk_biz_id="2", result_table_id="100147_ieod_system_cpu_detail_raw", __name__="usage_value"`,
},
},
},
Expand All @@ -738,7 +738,7 @@ func TestQueryTs_ToQueryReference(t *testing.T) {
IsSingleMetric: false,
Measurement: "disk",
Field: "usage",
VmCondition: `bk_biz_id="2", result_table_id="100147_ieod_system_disk_raw", __name__="disk_usage"`,
VmCondition: `bk_biz_id="2", result_table_id="100147_ieod_system_disk_raw", __name__="usage_value"`,
},
},
},
Expand All @@ -751,7 +751,7 @@ func TestQueryTs_ToQueryReference(t *testing.T) {
vmExpand *md.VmExpand
ok bool
)
ctx = mock.Init(ctx)
ctx = md.InitHashID(ctx)

md.SetUser(ctx, tc.source, tc.ts.SpaceUid)
ref, err = tc.ts.ToQueryReference(ctx)
Expand Down

0 comments on commit 715ff98

Please sign in to comment.