From bd6ce142191d61819cd5abf7367a7274e1fd2e96 Mon Sep 17 00:00:00 2001 From: mrproliu <741550557@qq.com> Date: Fri, 1 Dec 2023 11:12:54 +0800 Subject: [PATCH 1/2] Support global dependency query command --- CHANGES.md | 9 +++ .../dependency/GlobalTopology.graphql | 35 ++++++++++ internal/commands/dependency/dependency.go | 1 + internal/commands/dependency/global.go | 70 +++++++++++++++++++ pkg/graphql/dependency/dependency.go | 12 ++++ 5 files changed, 127 insertions(+) create mode 100644 assets/graphqls/dependency/GlobalTopology.graphql create mode 100644 internal/commands/dependency/global.go diff --git a/CHANGES.md b/CHANGES.md index 8520a3ec..5285fb00 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 ------------------ diff --git a/assets/graphqls/dependency/GlobalTopology.graphql b/assets/graphqls/dependency/GlobalTopology.graphql new file mode 100644 index 00000000..f09602ef --- /dev/null +++ b/assets/graphqls/dependency/GlobalTopology.graphql @@ -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 + } + } +} diff --git a/internal/commands/dependency/dependency.go b/internal/commands/dependency/dependency.go index 4b373e1b..c3b62f95 100644 --- a/internal/commands/dependency/dependency.go +++ b/internal/commands/dependency/dependency.go @@ -30,5 +30,6 @@ var Command = &cli.Command{ ServiceCommand, InstanceCommand, ProcessCommand, + GlobalCommand, }, } diff --git a/internal/commands/dependency/global.go b/internal/commands/dependency/global.go new file mode 100644 index 00000000..d74f4130 --- /dev/null +++ b/internal/commands/dependency/global.go @@ -0,0 +1,70 @@ +// 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 ( + "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" + + 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, + } + + dependency, err := dependency.GlobalTopology(ctx, layer, duration) + + if err != nil { + return err + } + + return display.Display(ctx, &displayable.Displayable{Data: dependency}) + }, +} diff --git a/pkg/graphql/dependency/dependency.go b/pkg/graphql/dependency/dependency.go index ea5fe1ab..074c871d 100644 --- a/pkg/graphql/dependency/dependency.go +++ b/pkg/graphql/dependency/dependency.go @@ -39,6 +39,18 @@ 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 ServiceTopology(ctx *cli.Context, serviceID string, duration api.Duration) (api.Topology, error) { var response map[string]api.Topology From e81a5218a73400e83083e0e4e9baa1936c8928fb Mon Sep 17 00:00:00 2001 From: mrproliu <741550557@qq.com> Date: Fri, 1 Dec 2023 12:02:58 +0800 Subject: [PATCH 2/2] Adding backend version check --- .../GlobalTopologyWithoutLayer.graphql | 35 +++++++++++++++++++ internal/commands/dependency/global.go | 20 +++++++++-- pkg/graphql/dependency/dependency.go | 11 ++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 assets/graphqls/dependency/GlobalTopologyWithoutLayer.graphql diff --git a/assets/graphqls/dependency/GlobalTopologyWithoutLayer.graphql b/assets/graphqls/dependency/GlobalTopologyWithoutLayer.graphql new file mode 100644 index 00000000..0a25ed6a --- /dev/null +++ b/assets/graphqls/dependency/GlobalTopologyWithoutLayer.graphql @@ -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 + } + } +} diff --git a/internal/commands/dependency/global.go b/internal/commands/dependency/global.go index d74f4130..c0ad7982 100644 --- a/internal/commands/dependency/global.go +++ b/internal/commands/dependency/global.go @@ -18,6 +18,8 @@ package dependency import ( + "fmt" + "github.com/urfave/cli/v2" "github.com/apache/skywalking-cli/internal/commands/interceptor" @@ -26,6 +28,7 @@ import ( "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" ) @@ -59,12 +62,25 @@ var GlobalCommand = &cli.Command{ Step: step.(*model.StepEnumValue).Selected, } - dependency, err := dependency.GlobalTopology(ctx, layer, duration) + 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: dependency}) + return display.Display(ctx, &displayable.Displayable{Data: topology}) }, } diff --git a/pkg/graphql/dependency/dependency.go b/pkg/graphql/dependency/dependency.go index 074c871d..958835ee 100644 --- a/pkg/graphql/dependency/dependency.go +++ b/pkg/graphql/dependency/dependency.go @@ -51,6 +51,17 @@ func GlobalTopology(ctx *cli.Context, layer string, duration api.Duration) (api. 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