Skip to content

Commit 660a355

Browse files
authored
Add the sub-command service of command dependency (#113)
1 parent 2b0f4d0 commit 660a355

File tree

7 files changed

+194
-0
lines changed

7 files changed

+194
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,22 @@ You can imitate the content of [the default template file](examples/global.yml)
564564

565565
### `dependency`
566566

567+
#### `service`
568+
569+
<details>
570+
571+
<summary>dependency service <service-id> [--start=start-time] [--end=end-time]</summary>
572+
573+
`dependency service` shows all the dependencies of given `[service-id]` in the time range of `[start, end]`.
574+
575+
| argument | description | default |
576+
| :--- | :--- | :--- |
577+
| `service-id` | The service id whose dependencies are to displayed. | |
578+
| `--start` | See [Common options](#common-options) | See [Common options](#common-options) |
579+
| `--end` | See [Common options](#common-options) | See [Common options](#common-options) |
580+
581+
</details>
582+
567583
#### `endpoint`
568584

569585
<details>

assets/assets.gen.go

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Licensed to Apache Software Foundation (ASF) under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Apache Software Foundation (ASF) licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
query ($serviceId: ID!, $duration: Duration!) {
19+
result: getServiceTopology(duration: $duration, serviceId: $serviceId) {
20+
nodes {
21+
id
22+
name
23+
type
24+
isReal
25+
}
26+
calls {
27+
id
28+
source
29+
detectPoints
30+
target
31+
}
32+
}
33+
}

internal/commands/dependency/dependency.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ var Command = cli.Command{
2727
Usage: "Dependency related subcommand",
2828
Subcommands: cli.Commands{
2929
EndpointCommand,
30+
ServiceCommand,
3031
},
3132
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Licensed to Apache Software Foundation (ASF) under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Apache Software Foundation (ASF) licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package dependency
19+
20+
import (
21+
"fmt"
22+
23+
"github.com/urfave/cli"
24+
25+
"github.com/apache/skywalking-cli/internal/commands/interceptor"
26+
"github.com/apache/skywalking-cli/internal/flags"
27+
"github.com/apache/skywalking-cli/internal/logger"
28+
"github.com/apache/skywalking-cli/internal/model"
29+
30+
"github.com/apache/skywalking-cli/pkg/display"
31+
"github.com/apache/skywalking-cli/pkg/display/displayable"
32+
33+
"github.com/apache/skywalking-cli/pkg/graphql/dependency"
34+
35+
api "skywalking.apache.org/repo/goapi/query"
36+
)
37+
38+
var ServiceCommand = cli.Command{
39+
Name: "service",
40+
ShortName: "svc",
41+
Usage: "Query the dependencies of given service",
42+
ArgsUsage: "<serviceId>",
43+
Flags: flags.Flags(
44+
flags.DurationFlags,
45+
),
46+
Before: interceptor.BeforeChain([]cli.BeforeFunc{
47+
interceptor.TimezoneInterceptor,
48+
interceptor.DurationInterceptor,
49+
}),
50+
51+
Action: func(ctx *cli.Context) error {
52+
if ctx.NArg() == 0 {
53+
return fmt.Errorf("command service requires serviceId as argument")
54+
}
55+
56+
end := ctx.String("end")
57+
start := ctx.String("start")
58+
step := ctx.Generic("step")
59+
60+
duration := api.Duration{
61+
Start: start,
62+
End: end,
63+
Step: step.(*model.StepEnumValue).Selected,
64+
}
65+
66+
dependency, err := dependency.ServiceTopology(ctx, ctx.Args().First(), duration)
67+
68+
if err != nil {
69+
logger.Log.Fatalln(err)
70+
}
71+
72+
return display.Display(ctx, &displayable.Displayable{Data: dependency})
73+
},
74+
}

pkg/graphql/dependency/dependency.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,15 @@ func EndpointDependency(ctx *cli.Context, endpointID string, duration api.Durati
3838

3939
return response["result"], err
4040
}
41+
42+
func ServiceTopology(ctx *cli.Context, serviceID string, duration api.Duration) (api.Topology, error) {
43+
var response map[string]api.Topology
44+
45+
request := graphql.NewRequest(assets.Read("graphqls/dependency/ServiceTopology.graphql"))
46+
request.Var("serviceId", serviceID)
47+
request.Var("duration", duration)
48+
49+
err := client.ExecuteQuery(ctx, request, &response)
50+
51+
return response["result"], err
52+
}

scripts/test_commands.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@ ${swctl} --display=json trace ls >/dev/null 2>&1
6666

6767
# Test `dashboard global`
6868
${swctl} --display=json db g >/dev/null 2>&1
69+
70+
# Test `dependency`
71+
${swctl} --display=json dependency service "test" > /dev/null 2>&1
72+
73+
${swctl} --display=json dependency endpoint "test" > /dev/null 2>&1

0 commit comments

Comments
 (0)