Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the sub-commands for query sorted metrics/records
- Loading branch information
Showing
9 changed files
with
277 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Licensed to Apache Software Foundation (ASF) under one or more contributor | ||
# license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright | ||
# ownership. Apache Software Foundation (ASF) licenses this file to you under | ||
# the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
query ($condition:TopNCondition!, $duration: Duration!) { | ||
result: readSampledRecords(condition: $condition, duration: $duration) { | ||
name | ||
value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Licensed to Apache Software Foundation (ASF) under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Apache Software Foundation (ASF) licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package aggregation | ||
|
||
import ( | ||
api "skywalking.apache.org/repo/goapi/query" | ||
|
||
"github.com/urfave/cli/v2" | ||
|
||
"github.com/apache/skywalking-cli/internal/commands/interceptor" | ||
"github.com/apache/skywalking-cli/internal/flags" | ||
"github.com/apache/skywalking-cli/internal/logger" | ||
"github.com/apache/skywalking-cli/internal/model" | ||
"github.com/apache/skywalking-cli/pkg/display" | ||
"github.com/apache/skywalking-cli/pkg/display/displayable" | ||
"github.com/apache/skywalking-cli/pkg/graphql/metrics" | ||
) | ||
|
||
var SortedMetrics = &cli.Command{ | ||
Name: "sorted", | ||
Usage: "query the top <n> entities sorted by the specified metrics", | ||
ArgsUsage: "<n>", | ||
UsageText: `Query the top <n> entities sorted by the specified metrics. | ||
Examples: | ||
1. Query the top 5 services whose sla are largest: | ||
$ swctl metrics sorted --name service_sla 5 | ||
2. Query the top 5 endpoints whose sla is largest: | ||
$ swctl metrics sorted --name endpoint_sla 5 | ||
3. Query the top 5 instances of service "boutique::adservice" whose sla are largest: | ||
$ swctl metrics sorted --name service_instance_sla --service-name boutique::adservice 5 | ||
`, | ||
Flags: flags.Flags( | ||
flags.DurationFlags, | ||
flags.MetricsFlags, | ||
flags.ServiceFlags, | ||
[]cli.Flag{ | ||
&cli.GenericFlag{ | ||
Name: "order", | ||
Usage: "the `order` by which the top entities are sorted", | ||
Value: &model.OrderEnumValue{ | ||
Enum: api.AllOrder, | ||
Default: api.OrderDes, | ||
Selected: api.OrderDes, | ||
}, | ||
}, | ||
}, | ||
), | ||
Before: interceptor.BeforeChain( | ||
interceptor.DurationInterceptor, | ||
interceptor.ParseService(false), | ||
), | ||
Action: func(ctx *cli.Context) error { | ||
condition, duration, err := buildSortedCondition(ctx, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logger.Log.Debugln(condition.Name, condition.Scope, condition.TopN) | ||
metricsValues, err := metrics.SortMetrics(ctx, *condition, *duration) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return display.Display(ctx, &displayable.Displayable{Data: metricsValues}) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Licensed to Apache Software Foundation (ASF) under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Apache Software Foundation (ASF) licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package aggregation | ||
|
||
import ( | ||
api "skywalking.apache.org/repo/goapi/query" | ||
|
||
"github.com/urfave/cli/v2" | ||
|
||
"github.com/apache/skywalking-cli/internal/commands/interceptor" | ||
"github.com/apache/skywalking-cli/internal/flags" | ||
"github.com/apache/skywalking-cli/internal/logger" | ||
"github.com/apache/skywalking-cli/internal/model" | ||
"github.com/apache/skywalking-cli/pkg/display" | ||
"github.com/apache/skywalking-cli/pkg/display/displayable" | ||
"github.com/apache/skywalking-cli/pkg/graphql/metrics" | ||
) | ||
|
||
var SampledRecords = &cli.Command{ | ||
Name: "sampled-record", | ||
Usage: "query the top <n> entities sorted by the specified records", | ||
ArgsUsage: "<n>", | ||
UsageText: `Query the top <n> entities sorted by the specified records. | ||
Examples: | ||
1. Query the top 5 database statements whose execute duration are largest: | ||
$ swctl metrics sampled-record --name top_n_database_statement 5 | ||
`, | ||
Flags: flags.Flags( | ||
flags.DurationFlags, | ||
flags.MetricsFlags, | ||
flags.ServiceFlags, | ||
[]cli.Flag{ | ||
&cli.GenericFlag{ | ||
Name: "order", | ||
Usage: "the `order` by which the top entities are sorted", | ||
Value: &model.OrderEnumValue{ | ||
Enum: api.AllOrder, | ||
Default: api.OrderDes, | ||
Selected: api.OrderDes, | ||
}, | ||
}, | ||
}, | ||
), | ||
Before: interceptor.BeforeChain( | ||
interceptor.DurationInterceptor, | ||
interceptor.ParseService(false), | ||
), | ||
Action: func(ctx *cli.Context) error { | ||
condition, duration, err := buildSortedCondition(ctx, false) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logger.Log.Debugln(condition.Name, condition.Scope, condition.TopN) | ||
sampledRecords, err := metrics.SampledRecords(ctx, *condition, *duration) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return display.Display(ctx, &displayable.Displayable{Data: sampledRecords}) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Licensed to Apache Software Foundation (ASF) under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Apache Software Foundation (ASF) licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package aggregation | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/apache/skywalking-cli/internal/commands/interceptor" | ||
"github.com/apache/skywalking-cli/internal/model" | ||
"github.com/apache/skywalking-cli/pkg/graphql/utils" | ||
|
||
"github.com/urfave/cli/v2" | ||
|
||
api "skywalking.apache.org/repo/goapi/query" | ||
) | ||
|
||
// buildSortedCondition from context of cli, the first argument must be the count of top N | ||
func buildSortedCondition(ctx *cli.Context, parseScope bool) (*api.TopNCondition, *api.Duration, error) { | ||
start := ctx.String("start") | ||
end := ctx.String("end") | ||
step := ctx.Generic("step").(*model.StepEnumValue).Selected | ||
|
||
metricsName := ctx.String("name") | ||
var scope *api.Scope | ||
if parseScope { | ||
tmp := utils.ParseScopeInTop(metricsName) | ||
scope = &tmp | ||
} | ||
order := ctx.Generic("order").(*model.OrderEnumValue).Selected | ||
topN := 5 | ||
parentServiceID := ctx.String("service-id") | ||
parentService, normal, err := interceptor.ParseServiceID(parentServiceID) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
if ctx.NArg() > 0 { | ||
nn, err2 := strconv.Atoi(ctx.Args().First()) | ||
if err2 != nil { | ||
return nil, nil, fmt.Errorf("the 1st argument must be a number: %v", err2) | ||
} | ||
topN = nn | ||
} | ||
|
||
return &api.TopNCondition{ | ||
Name: metricsName, | ||
ParentService: &parentService, | ||
Normal: &normal, | ||
Scope: scope, | ||
TopN: topN, | ||
Order: order, | ||
}, &api.Duration{ | ||
Start: start, | ||
End: end, | ||
Step: step, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters