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

[skywalking-cli] branch master updated: Add layer to event related commands (#145)

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

hoshea 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 acede48  Add layer to event related commands (#145)
acede48 is described below

commit acede480caf3a9a8cbd62be6a368369dd2e9fb35
Author: Hoshea Jiang <fg...@gmail.com>
AuthorDate: Thu Apr 14 14:22:26 2022 +0800

    Add layer to event related commands (#145)
---
 .github/scripts/post-entrypoint.sh |  1 +
 README.md                          |  1 +
 action.yaml                        |  4 +++
 cmd/swctl/main.go                  |  1 +
 go.mod                             |  3 +-
 go.sum                             |  7 +++--
 internal/commands/event/list.go    |  9 ++++++
 internal/commands/event/report.go  | 54 ++++++++++++++++++++++++++++-------
 pkg/commands/event/report.go       | 58 --------------------------------------
 pkg/grpc/grpc.go                   |  2 +-
 10 files changed, 67 insertions(+), 73 deletions(-)

diff --git a/.github/scripts/post-entrypoint.sh b/.github/scripts/post-entrypoint.sh
index 3802e4d..cb0ad28 100755
--- a/.github/scripts/post-entrypoint.sh
+++ b/.github/scripts/post-entrypoint.sh
@@ -31,4 +31,5 @@ echo "Reporting event..."
     --message="$MESSAGE" \
     --startTime="$STATE_START_TIME" \
     --endTime=$(date +%s)000 \
+    --layer="$LAYER" \
     $PARAMETERS
diff --git a/README.md b/README.md
index e8e81f2..49a9433 100644
--- a/README.md
+++ b/README.md
@@ -147,6 +147,7 @@ jobs:
           endpoint: ""                                          # Optional. Endpoint of the service, if any
           message: "Upgrade from {fromVersion} to {toVersion}"  # Optional. The message of the event
           parameters: ""                                        # Optional. The parameters in the message, if any
+          layer: "GENERAL"                                      # Required. Name of the layer to which the event belongs (case-insensitive)
 
       # your package / deployment steps... 
 ```
diff --git a/action.yaml b/action.yaml
index b511b17..aaa28f2 100644
--- a/action.yaml
+++ b/action.yaml
@@ -45,6 +45,9 @@ inputs:
     description: The parameters in the message
     required: false
     default: ""
+  layer:
+    description: Name of the layer to which the event belongs (case-insensitive)
+    required: true
 
 runs:
   using: docker
@@ -58,5 +61,6 @@ runs:
     ENDPOINT: ${{ inputs.endpoint }}
     MESSAGE: ${{ inputs.message }}
     PARAMETERS: ${{ inputs.parameters }}
+    LAYER: ${{ inputs.layer }}
   entrypoint: /entrypoint.sh
   post-entrypoint: /post-entrypoint.sh
diff --git a/cmd/swctl/main.go b/cmd/swctl/main.go
index 58aa2d7..8ea20eb 100644
--- a/cmd/swctl/main.go
+++ b/cmd/swctl/main.go
@@ -63,6 +63,7 @@ func init() {
 
 func main() {
 	app := cli.NewApp()
+	app.Name = "swctl"
 	app.Usage = "The CLI (Command Line Interface) for Apache SkyWalking."
 	app.UsageText = `Commands in SkyWalking CLI are organized into two levels,
 in the form of "swctl --option <level1> --option <level2> --option",
diff --git a/go.mod b/go.mod
index a6f8f79..900fee6 100644
--- a/go.mod
+++ b/go.mod
@@ -7,6 +7,7 @@ replace golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 => golang.org/x/c
 require (
 	github.com/apache/skywalking-swck v0.2.0
 	github.com/gizak/termui/v3 v3.1.0
+	github.com/google/uuid v1.3.0
 	github.com/machinebox/graphql v0.2.2
 	github.com/mattn/go-runewidth v0.0.9
 	github.com/mitchellh/mapstructure v1.4.0 // indirect
@@ -20,5 +21,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-20220407130404-69485e13dbb3
+	skywalking.apache.org/repo/goapi v0.0.0-20220412071816-33e4ea2a99b4
 )
diff --git a/go.sum b/go.sum
index 824a627..73ead90 100644
--- a/go.sum
+++ b/go.sum
@@ -267,8 +267,9 @@ github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hf
 github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
 github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
-github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
 github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
+github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
 github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
 github.com/googleapis/gnostic v0.4.1/go.mod h1:LRhVm6pbyptWbWbuZ38d1eyptfvIytN3ir6b65WBswg=
@@ -941,5 +942,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-20220407130404-69485e13dbb3 h1:DHRqCV17xjLT/HIyNWzLmhrizkne8JgaQDYclHg1tLg=
-skywalking.apache.org/repo/goapi v0.0.0-20220407130404-69485e13dbb3/go.mod h1:uWwwvhcwe2MD/nJCg0c1EE/eL6KzaBosLHDfMFoEJ30=
+skywalking.apache.org/repo/goapi v0.0.0-20220412071816-33e4ea2a99b4 h1:2JJA2JLUkdj5vimJH4+JYdeqqlr+F+g2eQYS6EqxUrA=
+skywalking.apache.org/repo/goapi v0.0.0-20220412071816-33e4ea2a99b4/go.mod h1:uWwwvhcwe2MD/nJCg0c1EE/eL6KzaBosLHDfMFoEJ30=
diff --git a/internal/commands/event/list.go b/internal/commands/event/list.go
index 1ec38bd..4d4eb86 100644
--- a/internal/commands/event/list.go
+++ b/internal/commands/event/list.go
@@ -18,6 +18,8 @@
 package event
 
 import (
+	"strings"
+
 	event "skywalking.apache.org/repo/goapi/collect/event/v3"
 	api "skywalking.apache.org/repo/goapi/query"
 
@@ -53,6 +55,11 @@ $ swctl event list
 				Usage:    "event name",
 				Required: false,
 			},
+			&cli.StringFlag{
+				Name:     "layer",
+				Usage:    "Name of the layer to which the event belongs (case-insensitive), which can be queried via 'swctl layer list'",
+				Required: false,
+			},
 			&cli.GenericFlag{
 				Name:  "type",
 				Usage: "the type of the event",
@@ -85,6 +92,7 @@ $ swctl event list
 		endpointName := ctx.String("endpoint-name")
 		name := ctx.String("name")
 		eventType := api.EventType(ctx.Generic("type").(*model.EventTypeEnumValue).String())
+		layer := strings.ToUpper(ctx.String("layer"))
 		pageNum := 1
 		needTotal := true
 
@@ -102,6 +110,7 @@ $ swctl event list
 			Name:   &name,
 			Type:   &eventType,
 			Time:   &duration,
+			Layer:  &layer,
 			Order:  nil,
 			Paging: &paging,
 		}
diff --git a/internal/commands/event/report.go b/internal/commands/event/report.go
index 6d009d7..4c7b193 100644
--- a/internal/commands/event/report.go
+++ b/internal/commands/event/report.go
@@ -18,22 +18,28 @@
 package event
 
 import (
+	"strings"
+
+	"github.com/google/uuid"
+	"github.com/urfave/cli/v2"
 	event "skywalking.apache.org/repo/goapi/collect/event/v3"
 
+	"github.com/apache/skywalking-cli/internal/commands/interceptor"
 	"github.com/apache/skywalking-cli/internal/flags"
 	"github.com/apache/skywalking-cli/internal/logger"
 	"github.com/apache/skywalking-cli/internal/model"
-	pkgevent "github.com/apache/skywalking-cli/pkg/commands/event"
 	"github.com/apache/skywalking-cli/pkg/display"
 	"github.com/apache/skywalking-cli/pkg/display/displayable"
-
-	"github.com/urfave/cli/v2"
+	"github.com/apache/skywalking-cli/pkg/grpc"
 )
 
 var reportCommand = &cli.Command{
-	Name:      "report",
-	Aliases:   []string{"r"},
-	Usage:     "Report an event to OAP server via gRPC",
+	Name:    "report",
+	Aliases: []string{"r"},
+	Usage:   "Report an event to OAP server via gRPC",
+	Description: `When reporting an event, you typically execute the command twice, 
+one for starting of the event (--start-time is required) and the other one for ending of the event 
+(--uuid and --end-time are required).\n You can also report an event once with start time and end time.`,
 	ArgsUsage: "[parameters...]",
 	Flags: flags.Flags(
 		flags.InstanceFlags,
@@ -41,8 +47,10 @@ var reportCommand = &cli.Command{
 
 		[]cli.Flag{
 			&cli.StringFlag{
-				Name:  "uuid",
-				Usage: "Unique `ID` of the event.",
+				Name:        "uuid",
+				Usage:       "Unique `ID` of the event, if not specified, a random one will be generated.",
+				Value:       uuid.New().String(),
+				DefaultText: "",
 			},
 			&cli.StringFlag{
 				Name:  "name",
@@ -69,15 +77,41 @@ var reportCommand = &cli.Command{
 				Name:  "end-time",
 				Usage: "The end time (in milliseconds) of the event, measured between the current time and midnight, January 1, 1970 UTC.",
 			},
+			&cli.StringFlag{
+				Name:     "layer",
+				Usage:    "Name of the layer to which the event belongs (case-insensitive), which can be queried via 'swctl layer list'",
+				Required: true,
+			},
 		},
 	),
 	Action: func(ctx *cli.Context) error {
-		reply, err := pkgevent.Report(ctx)
+		parameters, err := interceptor.ParseParameters(ctx.Args())
+		if err != nil {
+			return err
+		}
+
+		e := event.Event{
+			Uuid: ctx.String("uuid"),
+			Source: &event.Source{
+				Service:         ctx.String("service-name"),
+				ServiceInstance: ctx.String("instance-name"),
+				Endpoint:        ctx.String("endpoint-name"),
+			},
+			Name:       ctx.String("name"),
+			Type:       ctx.Generic("type").(*model.EventTypeEnumValue).Selected,
+			Message:    ctx.String("message"),
+			Parameters: parameters,
+			StartTime:  ctx.Int64("start-time"),
+			EndTime:    ctx.Int64("end-time"),
+			Layer:      strings.ToUpper(ctx.String("layer")),
+		}
+
+		reply, err := grpc.ReportEvent(ctx.String("grpc-addr"), &e)
 		if err != nil {
 			return err
 		}
 
-		logger.Log.Println("Report the event successfully, whose uuid is ", ctx.String("uuid"))
+		logger.Log.Printf("Report the event successfully, whose uuid is %s\n", ctx.String("uuid"))
 		return display.Display(ctx, &displayable.Displayable{Data: reply})
 	},
 }
diff --git a/pkg/commands/event/report.go b/pkg/commands/event/report.go
deleted file mode 100644
index 19ec6c6..0000000
--- a/pkg/commands/event/report.go
+++ /dev/null
@@ -1,58 +0,0 @@
-// 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 event
-
-import (
-	common "skywalking.apache.org/repo/goapi/collect/common/v3"
-	event "skywalking.apache.org/repo/goapi/collect/event/v3"
-
-	"github.com/apache/skywalking-cli/internal/commands/interceptor"
-	"github.com/apache/skywalking-cli/internal/model"
-	"github.com/apache/skywalking-cli/pkg/grpc"
-
-	"github.com/urfave/cli/v2"
-)
-
-func Report(ctx *cli.Context) (*common.Commands, error) {
-	parameters, err := interceptor.ParseParameters(ctx.Args())
-	if err != nil {
-		return nil, err
-	}
-
-	e := event.Event{
-		Uuid: ctx.String("uuid"),
-		Source: &event.Source{
-			Service:         ctx.String("service-name"),
-			ServiceInstance: ctx.String("instance-name"),
-			Endpoint:        ctx.String("endpoint-name"),
-		},
-		Name:       ctx.String("name"),
-		Type:       ctx.Generic("type").(*model.EventTypeEnumValue).Selected,
-		Message:    ctx.String("message"),
-		Parameters: parameters,
-		StartTime:  ctx.Int64("start-time"),
-		EndTime:    ctx.Int64("end-time"),
-	}
-
-	reply, err := grpc.ReportEvent(ctx.String("grpc-addr"), &e)
-	if err != nil {
-		return nil, err
-	}
-
-	return reply, nil
-}
diff --git a/pkg/grpc/grpc.go b/pkg/grpc/grpc.go
index b97ae6f..47c0568 100644
--- a/pkg/grpc/grpc.go
+++ b/pkg/grpc/grpc.go
@@ -27,7 +27,7 @@ import (
 	"google.golang.org/grpc"
 )
 
-// ReportEvent creates a grpc client and report an event.
+// ReportEvent creates a grpc client and reports an event to OAP.
 func ReportEvent(addr string, e *event.Event) (*common.Commands, error) {
 	conn, err := grpc.Dial(addr, grpc.WithInsecure())
 	if err != nil {