Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support global dependency query command #198

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ Changes by Version
==================
Release Notes.

0.14.0
------------------

### Features

* Add the sub-command `dependency global` for adapt the global dependency query API by @mrproliu in https://github.com/apache/skywalking-cli/pull/198

### Bug Fixes

0.13.0
------------------

Expand Down
35 changes: 35 additions & 0 deletions assets/graphqls/dependency/GlobalTopology.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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 ($layer: String, $duration: Duration!) {
result: getGlobalTopology(duration: $duration, layer: $layer) {
nodes {
id
name
type
isReal
}
calls {
id
source
detectPoints
target
sourceComponents
targetComponents
}
}
}
35 changes: 35 additions & 0 deletions assets/graphqls/dependency/GlobalTopologyWithoutLayer.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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 ($duration: Duration!) {
result: getGlobalTopology(duration: $duration) {
nodes {
id
name
type
isReal
}
calls {
id
source
detectPoints
target
sourceComponents
targetComponents
}
}
}
1 change: 1 addition & 0 deletions internal/commands/dependency/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ var Command = &cli.Command{
ServiceCommand,
InstanceCommand,
ProcessCommand,
GlobalCommand,
},
}
86 changes: 86 additions & 0 deletions internal/commands/dependency/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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 dependency

import (
"fmt"

"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/model"
"github.com/apache/skywalking-cli/pkg/display"
"github.com/apache/skywalking-cli/pkg/display/displayable"
"github.com/apache/skywalking-cli/pkg/graphql/dependency"
"github.com/apache/skywalking-cli/pkg/graphql/metadata"

api "skywalking.apache.org/repo/goapi/query"
)

var GlobalCommand = &cli.Command{
Name: "global",
Aliases: []string{"glb"},
Usage: "Query the global dependencies",
Flags: flags.Flags(
flags.DurationFlags,
[]cli.Flag{
&cli.StringFlag{
Name: "layer",
Usage: "Name of the layer to query the dependency of this layer",
Required: false,
},
},
),
Before: interceptor.BeforeChain(
interceptor.DurationInterceptor,
),
Action: func(ctx *cli.Context) error {
layer := ctx.String("layer")
end := ctx.String("end")
start := ctx.String("start")
step := ctx.Generic("step")

duration := api.Duration{
Start: start,
End: end,
Step: step.(*model.StepEnumValue).Selected,
}

major, _, err := metadata.BackendVersion(ctx)
if err != nil {
return err
}

var topology api.Topology
if major >= 10 {
topology, err = dependency.GlobalTopology(ctx, layer, duration)
} else {
if layer != "" {
return fmt.Errorf("the layer parameter only available when OAP version >= 10.0.0")
}
topology, err = dependency.GlobalTopologyWithoutLayer(ctx, duration)
}

if err != nil {
return err
}

return display.Display(ctx, &displayable.Displayable{Data: topology})
},
}
23 changes: 23 additions & 0 deletions pkg/graphql/dependency/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,29 @@ func EndpointDependency(ctx *cli.Context, endpointID string, duration api.Durati
return response["result"], err
}

func GlobalTopology(ctx *cli.Context, layer string, duration api.Duration) (api.Topology, error) {
var response map[string]api.Topology

request := graphql.NewRequest(assets.Read("graphqls/dependency/GlobalTopology.graphql"))
request.Var("layer", layer)
request.Var("duration", duration)

err := client.ExecuteQuery(ctx, request, &response)

return response["result"], err
}

func GlobalTopologyWithoutLayer(ctx *cli.Context, duration api.Duration) (api.Topology, error) {
var response map[string]api.Topology

request := graphql.NewRequest(assets.Read("graphqls/dependency/GlobalTopologyWithoutLayer.graphql"))
request.Var("duration", duration)

err := client.ExecuteQuery(ctx, request, &response)

return response["result"], err
}

func ServiceTopology(ctx *cli.Context, serviceID string, duration api.Duration) (api.Topology, error) {
var response map[string]api.Topology

Expand Down
Loading