You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2022/04/12 06:34:40 UTC

[skywalking-cli] branch master updated: Add the sub-command for estimate the process scale (#142)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-cli.git


The following commit(s) were added to refs/heads/master by this push:
     new f8c5808  Add the sub-command for estimate the process scale (#142)
f8c5808 is described below

commit f8c58082cf5a6c035efce29a297fa7dfa78ed7f7
Author: mrproliu <74...@qq.com>
AuthorDate: Tue Apr 12 14:34:36 2022 +0800

    Add the sub-command for estimate the process scale (#142)
---
 CHANGES.md                                         |  1 +
 .../metadata/v2/EstimateProcessScale.graphql       | 20 ++++++
 .../process/{process.go => estimate/estimate.go}   | 18 ++---
 internal/commands/process/estimate/scale.go        | 80 ++++++++++++++++++++++
 internal/commands/process/process.go               |  3 +
 pkg/graphql/metadata/metadata.go                   | 13 ++++
 6 files changed, 124 insertions(+), 11 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index e2a8665..c098d1e 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -10,6 +10,7 @@ Release Notes.
 - Update the `profiling ebpf create` task relate to the service level.(#141)
 - Update the sub-command `process list/get` to add the `labels` field.(#141)
 - Add the sub-command `profiling ebpf create prepare` to query data for prepare creating task.(#141)
+- Add the sub-command `process estimate scale` to estimate the process scale.(#142)
 
 0.10.0
 ------------------
diff --git a/assets/graphqls/metadata/v2/EstimateProcessScale.graphql b/assets/graphqls/metadata/v2/EstimateProcessScale.graphql
new file mode 100644
index 0000000..4ac9491
--- /dev/null
+++ b/assets/graphqls/metadata/v2/EstimateProcessScale.graphql
@@ -0,0 +1,20 @@
+# 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!, $labels: [String!]!, $duration: Duration!) {
+    result: estimateProcessScale(serviceId: $serviceId, labels: $labels, duration: $duration)
+}
\ No newline at end of file
diff --git a/internal/commands/process/process.go b/internal/commands/process/estimate/estimate.go
similarity index 78%
copy from internal/commands/process/process.go
copy to internal/commands/process/estimate/estimate.go
index dda8571..833e4cd 100644
--- a/internal/commands/process/process.go
+++ b/internal/commands/process/estimate/estimate.go
@@ -15,18 +15,14 @@
 // specific language governing permissions and limitations
 // under the License.
 
-package process
+package estimate
 
-import (
-	"github.com/urfave/cli/v2"
-)
+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,
+var EstimateCommand = &cli.Command{
+	Name:  "estimate",
+	Usage: "Process estimate related sub-command",
+	Subcommands: []*cli.Command{
+		ScaleCommand,
 	},
 }
diff --git a/internal/commands/process/estimate/scale.go b/internal/commands/process/estimate/scale.go
new file mode 100644
index 0000000..82a3b3e
--- /dev/null
+++ b/internal/commands/process/estimate/scale.go
@@ -0,0 +1,80 @@
+// 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 estimate
+
+import (
+	"strings"
+
+	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/model"
+	"github.com/apache/skywalking-cli/pkg/display"
+	"github.com/apache/skywalking-cli/pkg/display/displayable"
+	"github.com/apache/skywalking-cli/pkg/graphql/metadata"
+)
+
+var ScaleCommand = &cli.Command{
+	Name:  "scale",
+	Usage: `estimate monitored process scale of the given id or name in service and labels`,
+	UsageText: `This command estimate monitored process scale, via id or name in service and labels.
+
+Examples:
+1. Estimate process scale by service name "abc" with labels "t1,t2":
+$ swctl process estimate scale --service-name abc --labels t1,t2`,
+	Flags: flags.Flags(
+		flags.ServiceFlags,
+		flags.DurationFlags,
+		[]cli.Flag{
+			&cli.StringFlag{
+				Name:  "labels",
+				Usage: "the `labels` by which labels of the process, multiple labels split by ',': l1,l2",
+			},
+		},
+	),
+	Before: interceptor.BeforeChain(
+		interceptor.ParseService(true),
+		interceptor.DurationInterceptor,
+	),
+	Action: func(ctx *cli.Context) error {
+		end := ctx.String("end")
+		start := ctx.String("start")
+		step := ctx.Generic("step")
+		serviceID := ctx.String("service-id")
+		labelString := ctx.String("labels")
+		labels := make([]string, 0)
+		if labelString != "" {
+			labels = strings.Split(labelString, ",")
+		}
+
+		scale, err := metadata.EstimateProcessScale(ctx, serviceID, labels, api.Duration{
+			Start: start,
+			End:   end,
+			Step:  step.(*model.StepEnumValue).Selected,
+		})
+
+		if err != nil {
+			return err
+		}
+
+		return display.Display(ctx, &displayable.Displayable{Data: scale})
+	},
+}
diff --git a/internal/commands/process/process.go b/internal/commands/process/process.go
index dda8571..b7ceb66 100644
--- a/internal/commands/process/process.go
+++ b/internal/commands/process/process.go
@@ -18,6 +18,8 @@
 package process
 
 import (
+	"github.com/apache/skywalking-cli/internal/commands/process/estimate"
+
 	"github.com/urfave/cli/v2"
 )
 
@@ -28,5 +30,6 @@ var Command = &cli.Command{
 	Subcommands: cli.Commands{
 		ListCommand,
 		GetCommand,
+		estimate.EstimateCommand,
 	},
 }
diff --git a/pkg/graphql/metadata/metadata.go b/pkg/graphql/metadata/metadata.go
index 6a48112..1926cda 100644
--- a/pkg/graphql/metadata/metadata.go
+++ b/pkg/graphql/metadata/metadata.go
@@ -197,6 +197,19 @@ func GetProcess(cliCtx *cli.Context, processID string) (api.Process, error) {
 	return response["result"], err
 }
 
+func EstimateProcessScale(cliCtx *cli.Context, serviceID string, labels []string, duration api.Duration) (int64, error) {
+	var response map[string]int64
+
+	request := graphql.NewRequest(assets.Read("graphqls/metadata/v2/EstimateProcessScale.graphql"))
+	request.Var("serviceId", serviceID)
+	request.Var("labels", labels)
+	request.Var("duration", duration)
+
+	err := client.ExecuteQuery(cliCtx, request, &response)
+
+	return response["result"], err
+}
+
 func ServerTimeInfo(cliCtx *cli.Context) (api.TimeInfo, error) {
 	var response map[string]api.TimeInfo