From 102588edee9b19c0176c38616278291cd68db9e8 Mon Sep 17 00:00:00 2001 From: Mrproliu <741550557@qq.com> Date: Thu, 10 Mar 2022 18:36:30 +0800 Subject: [PATCH 1/2] Add the Process metadata query command --- CHANGES.md | 1 + .../graphqls/metadata/v2/GetProcess.graphql | 22 ++++++ assets/graphqls/metadata/v2/Processes.graphql | 22 ++++++ cmd/swctl/main.go | 2 + go.mod | 2 +- go.sum | 15 ++-- internal/commands/process/get.go | 50 +++++++++++++ internal/commands/process/list.go | 72 +++++++++++++++++++ internal/commands/process/process.go | 32 +++++++++ pkg/graphql/metadata/metadata.go | 23 ++++++ 10 files changed, 233 insertions(+), 8 deletions(-) create mode 100644 assets/graphqls/metadata/v2/GetProcess.graphql create mode 100644 assets/graphqls/metadata/v2/Processes.graphql create mode 100644 internal/commands/process/get.go create mode 100644 internal/commands/process/list.go create mode 100644 internal/commands/process/process.go diff --git a/CHANGES.md b/CHANGES.md index 054db3fd..174c9455 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,7 @@ Release Notes. - Add the sub-command `endpoint get` to query single endpoint info.(#134) - Change the GraphQL method to the v9 version according to the server version.(#134) - Add `normal` field to Service entity.(#136) +- Add the command `process` for query Process metadata.(#137) ### Bug Fixes diff --git a/assets/graphqls/metadata/v2/GetProcess.graphql b/assets/graphqls/metadata/v2/GetProcess.graphql new file mode 100644 index 00000000..f3e1b9ef --- /dev/null +++ b/assets/graphqls/metadata/v2/GetProcess.graphql @@ -0,0 +1,22 @@ +# 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 ($processId: ID!) { + result: getProcess(processId: $processId) { + id name serviceId serviceName instanceId instanceName layer agentId detectType attributes { name value } + } +} \ No newline at end of file diff --git a/assets/graphqls/metadata/v2/Processes.graphql b/assets/graphqls/metadata/v2/Processes.graphql new file mode 100644 index 00000000..2b5b7603 --- /dev/null +++ b/assets/graphqls/metadata/v2/Processes.graphql @@ -0,0 +1,22 @@ +# 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 ($serviceId: ID, $instanceId: ID) { + result: listProcesses(serviceId: $serviceId, instanceId: $instanceId) { + id name serviceId serviceName instanceId instanceName layer agentId detectType attributes { name value } + } +} \ No newline at end of file diff --git a/cmd/swctl/main.go b/cmd/swctl/main.go index 27f9b2e7..4944a273 100644 --- a/cmd/swctl/main.go +++ b/cmd/swctl/main.go @@ -35,6 +35,7 @@ import ( "github.com/apache/skywalking-cli/internal/commands/layer" "github.com/apache/skywalking-cli/internal/commands/logs" "github.com/apache/skywalking-cli/internal/commands/metrics" + "github.com/apache/skywalking-cli/internal/commands/process" "github.com/apache/skywalking-cli/internal/commands/profile" "github.com/apache/skywalking-cli/internal/commands/service" "github.com/apache/skywalking-cli/internal/commands/trace" @@ -100,6 +101,7 @@ services, service instances, etc.` dependency.Command, alarm.Command, layer.Command, + process.Command, } app.Before = interceptor.BeforeChain( diff --git a/go.mod b/go.mod index 1ba571c1..977b1d6b 100644 --- a/go.mod +++ b/go.mod @@ -20,5 +20,5 @@ require ( gopkg.in/yaml.v2 v2.4.0 k8s.io/apimachinery v0.21.1 sigs.k8s.io/controller-runtime v0.7.0 - skywalking.apache.org/repo/goapi v0.0.0-20220121092418-9c455d0dda3f + skywalking.apache.org/repo/goapi v0.0.0-20220310042848-795a2b3fcdfe ) diff --git a/go.sum b/go.sum index 626d8e7a..bf3baf94 100644 --- a/go.sum +++ b/go.sum @@ -637,8 +637,8 @@ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210224082022-3d97a244fca7/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420 h1:a8jGStKg0XqKDlKqjLrXn0ioF5MH36pT7Z0BRTqLhbk= -golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY= +golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -698,8 +698,8 @@ golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210917161153-d61c044b1678 h1:J27LZFQBFoihqXoegpscI10HpjZ7B5WQLLKL2FZXQKw= -golang.org/x/sys v0.0.0-20210917161153-d61c044b1678/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0= +golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -710,8 +710,9 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -940,5 +941,5 @@ sigs.k8s.io/structured-merge-diff/v4 v4.1.0/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q= sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc= -skywalking.apache.org/repo/goapi v0.0.0-20220121092418-9c455d0dda3f h1:i0cL/mhLt8xVO05SCKshkzWNUkXOkiW+zRIIuVWjPko= -skywalking.apache.org/repo/goapi v0.0.0-20220121092418-9c455d0dda3f/go.mod h1:4KrWd+Oi4lkB+PtxZgIlf+3T6EECPru4fOWNMEHjxRk= +skywalking.apache.org/repo/goapi v0.0.0-20220310042848-795a2b3fcdfe h1:Txa1/FZfiA++l6gEC843VMhM4nMGatag/mzk1j4dhWw= +skywalking.apache.org/repo/goapi v0.0.0-20220310042848-795a2b3fcdfe/go.mod h1:wzVLZ8F88Idy5tlmcGgJCH2NY8GUWxJ2RuNVXkao+SM= diff --git a/internal/commands/process/get.go b/internal/commands/process/get.go new file mode 100644 index 00000000..5a9d3354 --- /dev/null +++ b/internal/commands/process/get.go @@ -0,0 +1,50 @@ +// 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 process + +import ( + "fmt" + + "github.com/urfave/cli/v2" + + "github.com/apache/skywalking-cli/pkg/display" + "github.com/apache/skywalking-cli/pkg/display/displayable" + "github.com/apache/skywalking-cli/pkg/graphql/metadata" +) + +var GetCommand = &cli.Command{ + Name: "get", + Usage: `get monitored process of the given `, + UsageText: `This command get single process, via process-id. + +Examples: +1. get single process by process id "2b9e46c13c91803695a4364257415e523af7cbf17bf4058e025c16b944a6a85b": +$ swctl process get 2b9e46c13c91803695a4364257415e523af7cbf17bf4058e025c16b944a6a85b`, + Action: func(ctx *cli.Context) error { + if ctx.Args().Len() == 0 { + return fmt.Errorf("process-id must be provide") + } + + instance, err := metadata.GetProcess(ctx, ctx.Args().First()) + if err != nil { + return err + } + + return display.Display(ctx, &displayable.Displayable{Data: instance}) + }, +} diff --git a/internal/commands/process/list.go b/internal/commands/process/list.go new file mode 100644 index 00000000..6331c7a3 --- /dev/null +++ b/internal/commands/process/list.go @@ -0,0 +1,72 @@ +// 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 process + +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/pkg/display" + "github.com/apache/skywalking-cli/pkg/display/displayable" + "github.com/apache/skywalking-cli/pkg/graphql/metadata" +) + +var ListCommand = &cli.Command{ + Name: "list", + Aliases: []string{"ls"}, + Usage: `list all monitored processes of the given id or name in service or instance`, + UsageText: `This command lists all processes of the service-instance, via id or name in service or instance. + +Examples: +1. List all processes by service name "provider": +$ swctl process ls --service-name provider + +2. List all processes by service id "YnVzaW5lc3Mtem9uZTo6cHJvamVjdEM=.1": +$ swctl process ls --service-id YnVzaW5lc3Mtem9uZTo6cHJvamVjdEM=.1 + +3. List all processes by instance name "provider-01" and service name "provider": +$ swctl process ls --instance-name provider-01 --service-name provider + +4. List all processes by instance id "cHJvdmlkZXI=.1_cHJvdmlkZXIx": +$ swctl process ls --service-id cHJvdmlkZXI=.1_cHJvdmlkZXIx`, + Flags: flags.Flags( + flags.ServiceFlags, + flags.InstanceFlags, + ), + Before: interceptor.BeforeChain( + interceptor.ParseService(false), + interceptor.ParseInstance(false), + ), + Action: func(ctx *cli.Context) error { + serviceID := ctx.String("service-id") + instanceID := ctx.String("instance-id") + if serviceID == "" && instanceID == "" { + return fmt.Errorf("service or instance must provide one") + } + + processes, err := metadata.Processes(ctx, serviceID, instanceID) + if err != nil { + return err + } + + return display.Display(ctx, &displayable.Displayable{Data: processes}) + }, +} diff --git a/internal/commands/process/process.go b/internal/commands/process/process.go new file mode 100644 index 00000000..dda85714 --- /dev/null +++ b/internal/commands/process/process.go @@ -0,0 +1,32 @@ +// 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 process + +import ( + "github.com/urfave/cli/v2" +) + +var Command = &cli.Command{ + Name: "process", + Aliases: []string{"p"}, + Usage: "Process related sub-command", + Subcommands: cli.Commands{ + ListCommand, + GetCommand, + }, +} diff --git a/pkg/graphql/metadata/metadata.go b/pkg/graphql/metadata/metadata.go index 2a997d97..6a481123 100644 --- a/pkg/graphql/metadata/metadata.go +++ b/pkg/graphql/metadata/metadata.go @@ -174,6 +174,29 @@ func GetEndpointInfo(cliCtx *cli.Context, endpointID string) (api.EndpointInfo, return response["result"], err } +func Processes(cliCtx *cli.Context, serviceID, instanceID string) ([]api.Process, error) { + var response map[string][]api.Process + + request := graphql.NewRequest(assets.Read("graphqls/metadata/v2/Processes.graphql")) + request.Var("serviceId", serviceID) + request.Var("instanceID", instanceID) + + err := client.ExecuteQuery(cliCtx, request, &response) + + return response["result"], err +} + +func GetProcess(cliCtx *cli.Context, processID string) (api.Process, error) { + var response map[string]api.Process + + request := graphql.NewRequest(assets.Read("graphqls/metadata/v2/GetProcess.graphql")) + request.Var("processId", processID) + + err := client.ExecuteQuery(cliCtx, request, &response) + + return response["result"], err +} + func ServerTimeInfo(cliCtx *cli.Context) (api.TimeInfo, error) { var response map[string]api.TimeInfo From 380720501caa014200aa407faaf82fba5235751e Mon Sep 17 00:00:00 2001 From: Mrproliu <741550557@qq.com> Date: Thu, 10 Mar 2022 18:56:33 +0800 Subject: [PATCH 2/2] fix issue --- internal/commands/process/get.go | 2 +- internal/commands/process/list.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/commands/process/get.go b/internal/commands/process/get.go index 5a9d3354..e7a035a5 100644 --- a/internal/commands/process/get.go +++ b/internal/commands/process/get.go @@ -37,7 +37,7 @@ Examples: $ swctl process get 2b9e46c13c91803695a4364257415e523af7cbf17bf4058e025c16b944a6a85b`, Action: func(ctx *cli.Context) error { if ctx.Args().Len() == 0 { - return fmt.Errorf("process-id must be provide") + return fmt.Errorf("process-id must be provided") } instance, err := metadata.GetProcess(ctx, ctx.Args().First()) diff --git a/internal/commands/process/list.go b/internal/commands/process/list.go index 6331c7a3..2faca351 100644 --- a/internal/commands/process/list.go +++ b/internal/commands/process/list.go @@ -46,7 +46,7 @@ $ swctl process ls --service-id YnVzaW5lc3Mtem9uZTo6cHJvamVjdEM=.1 $ swctl process ls --instance-name provider-01 --service-name provider 4. List all processes by instance id "cHJvdmlkZXI=.1_cHJvdmlkZXIx": -$ swctl process ls --service-id cHJvdmlkZXI=.1_cHJvdmlkZXIx`, +$ swctl process ls --instance-id cHJvdmlkZXI=.1_cHJvdmlkZXIx`, Flags: flags.Flags( flags.ServiceFlags, flags.InstanceFlags,