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 Async Profiler Feature #203

Merged
merged 27 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 24 additions & 0 deletions assets/graphqls/profiling/asyncprofiler/CreateTask.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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.

mutation ($condition: AsyncProfilerTaskCreationRequest!) {
result: createAsyncProfilerTask(asyncProfilerTaskCreationRequest: $condition) {
errorReason
code
id
}
}
31 changes: 31 additions & 0 deletions assets/graphqls/profiling/asyncprofiler/GetAnalysis.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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 ($condition: AsyncProfilerAnalyzationRequest!) {
result: queryAsyncProfilerAnalyze(request: $condition) {
tree {
type
elements {
id
parentId
codeSignature
total
self
}
}
}
}
31 changes: 31 additions & 0 deletions assets/graphqls/profiling/asyncprofiler/GetTaskList.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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 ($condition: AsyncProfilerTaskListRequest) {
zhengziyi0117 marked this conversation as resolved.
Show resolved Hide resolved
result: queryAsyncProfilerTaskList(request: $condition) {
errorReason
tasks {
serviceId
serviceInstanceIds
createTime
events
duration
execArgs
id
}
}
}
30 changes: 30 additions & 0 deletions assets/graphqls/profiling/asyncprofiler/GetTaskProgress.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 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 ($taskId: String!){
result: queryAsyncProfilerTaskProgress(taskId: $taskId) {
errorInstanceIds
successInstanceIds
logs {
id
instanceId
instanceName
operationType
operationTime
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
gopkg.in/yaml.v2 v2.4.0
k8s.io/apimachinery v0.22.1
sigs.k8s.io/controller-runtime v0.10.0
skywalking.apache.org/repo/goapi v0.0.0-20240227092755-edee3273b361
skywalking.apache.org/repo/goapi v0.0.0-20241023080050-2514649a8007
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -879,5 +879,5 @@ sigs.k8s.io/structured-merge-diff/v4 v4.1.2 h1:Hr/htKFmJEbtMgS/UD0N+gtgctAqz81t3
sigs.k8s.io/structured-merge-diff/v4 v4.1.2/go.mod h1:j/nl6xW8vLS49O8YvXW1ocPhZawJtm+Yrr7PPRQ0Vg4=
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-20240227092755-edee3273b361 h1:FCGGU4Tut3LI/zMRXSgJgUL/kmSQ4b7QktFgRBhqaDs=
skywalking.apache.org/repo/goapi v0.0.0-20240227092755-edee3273b361/go.mod h1:+n8BMuS8eRdzdnGh15ElRGBXPi0eYZSs2TKySBDmRTE=
skywalking.apache.org/repo/goapi v0.0.0-20241023080050-2514649a8007 h1:MQU9yOZxgbs7fU145wd400/E3ES/HWoxTtTCudaCBoA=
skywalking.apache.org/repo/goapi v0.0.0-20241023080050-2514649a8007/go.mod h1:+n8BMuS8eRdzdnGh15ElRGBXPi0eYZSs2TKySBDmRTE=
17 changes: 17 additions & 0 deletions internal/commands/profiling/asyncprofiler/asyncprofiler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package asyncprofiler

import (
"github.com/urfave/cli/v2"
)

var Command = &cli.Command{
Name: "asyncprofiler",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this command is under the profiling command, should we remove the profiler suffix?
For now, the command should be: swctl profiling asyncprofiler, I think it could be: swctl profiling async is much clear, what do you think?

Usage: "async profiler related sub-command",
UsageText: `todo`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the usage text.

Subcommands: []*cli.Command{
createCommand,
getTaskListCommand,
getTaskProgressCommand,
analysisCommand,
},
}
76 changes: 76 additions & 0 deletions internal/commands/profiling/asyncprofiler/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package asyncprofiler

import (

Check failure on line 3 in internal/commands/profiling/asyncprofiler/create.go

View workflow job for this annotation

GitHub Actions / Golang Lint

File is not `goimports`-ed with -local github.com/apache/skywalking-cli (goimports)
"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/profiling"
"github.com/urfave/cli/v2"
"skywalking.apache.org/repo/goapi/query"
"strings"
)

var createCommand = &cli.Command{
Name: "create",
Aliases: []string{"c"},
Usage: "Create a new async profiler task",
UsageText: `Create a new async profiler task

Examples:
1. Create async-profiler task
$ swctl profiling asyncprofiler create --service-name=someservicename --duration=60 --events=cpu --service-instance-ids=someinstance`,
Flags: flags.Flags(
flags.ServiceFlags,
[]cli.Flag{
&cli.StringSliceFlag{
Name: "service-instance-ids",
Usage: "which instances to execute task.",
Required: true,
},
&cli.IntFlag{
Name: "duration",
Usage: "task continuous time(second).",
Required: true,
},
&cli.StringSliceFlag{
Name: "events",
Usage: "which event types this task needs to collect.",
Required: true,
},
&cli.StringFlag{
Name: "exec-args",
Usage: "other async-profiler execution options, e.g. alloc=2k,lock=2s.",
},
},
),
Before: interceptor.BeforeChain(
interceptor.ParseService(false),
),
Action: func(ctx *cli.Context) error {
serviceID := ctx.String("service-id")

events := ctx.StringSlice("events")
eventTypes := make([]query.AsyncProfilerEventType, 0)
for _, event := range events {
upperCaseEvent := strings.ToUpper(event)
eventTypes = append(eventTypes, query.AsyncProfilerEventType(upperCaseEvent))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is best to create an enum model to obtain the event types, and the CLI should verify whether we have an enum value. You can refer to https://github.com/apache/skywalking-cli/blob/master/internal/model/type.go. However, the type you choose should be a slice, which is slightly different from the existing reference.


execArgs := ctx.String("exec-args")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make the type of execArgs as *string, and assign the value when judgment the ctx.String is not an empty string? You will get an empty string even if the exec-args is not declared.


request := &query.AsyncProfilerTaskCreationRequest{
ServiceID: serviceID,
ServiceInstanceIds: ctx.StringSlice("service-instance-ids"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually not using the StringSlice to get slice value, I think we should just change back to using String and split the value by self(,)?

Duration: ctx.Int("duration"),
Events: eventTypes,
ExecArgs: &execArgs,
}
task, err := profiling.CreateAsyncProfilerTask(ctx, request)
if err != nil {
return err
}

return display.Display(ctx, &displayable.Displayable{Data: task, Condition: request})
},
}
59 changes: 59 additions & 0 deletions internal/commands/profiling/asyncprofiler/getAnalyze.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package asyncprofiler

import (

Check failure on line 3 in internal/commands/profiling/asyncprofiler/getAnalyze.go

View workflow job for this annotation

GitHub Actions / Golang Lint

File is not `goimports`-ed with -local github.com/apache/skywalking-cli (goimports)
"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/profiling"
"github.com/urfave/cli/v2"
query "skywalking.apache.org/repo/goapi/query"
"strings"
)

var analysisCommand = &cli.Command{
Name: "analysis",
Aliases: []string{"a"},
Usage: "Query async-profiler analysis",
UsageText: `Query async-profiler analysis

Examples:
1. Query the flame graph produced by async-profiler
$ /swctl-dev-37550c6-darwin-arm64 profiling asyncprofiler analysis --task-id=task-id --service-instance-ids=instanceIds --event=execution_sample`,
zhengziyi0117 marked this conversation as resolved.
Show resolved Hide resolved
Flags: flags.Flags(
[]cli.Flag{
&cli.StringFlag{
Name: "task-id",
Usage: "async-profiler task id",
Required: true,
},
&cli.StringSliceFlag{
Name: "service-instance-ids",
Usage: "select which service instances' jfr files to analyze",
Required: true,
},
&cli.StringFlag{
Name: "event",
Usage: "which event types this task needs to collect.",
Required: true,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The enum should be a new model. Instant of a string value without any type check.

},
),
Action: func(ctx *cli.Context) error {
event := ctx.String("event")
eventType := query.JFREventType(strings.ToUpper(event))

request := &query.AsyncProfilerAnalyzationRequest{
TaskID: ctx.String("task-id"),
InstanceIds: ctx.StringSlice("service-instance-ids"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use String and split by self.

EventType: eventType,
}

analyze, err := profiling.GetAsyncProfilerAnalyze(ctx, request)

if err != nil {
return err
}

return display.Display(ctx, &displayable.Displayable{Data: analyze, Condition: request})
},
}
62 changes: 62 additions & 0 deletions internal/commands/profiling/asyncprofiler/getTaskList.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package asyncprofiler

import (
"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/profiling"
"github.com/urfave/cli/v2"
"skywalking.apache.org/repo/goapi/query"
)

var getTaskListCommand = &cli.Command{
Name: "list",
Aliases: []string{"l"},
Usage: "Query async-profiler task list",
UsageText: `Query async-profiler task list

Examples:
1. Query all async-profiler tasks
$ swctl profiling asyncprofiler list --service-name=TEST_AGENT`,
Flags: flags.Flags(
flags.ServiceFlags,
[]cli.Flag{
&cli.Int64Flag{
Name: "start-time",
Usage: "The start time (in milliseconds) of the event, measured between the current time and midnight, January 1, 1970 UTC.",
},
&cli.Int64Flag{
Name: "end-time",
Usage: "The end time (in milliseconds) of the event, measured between the current time and midnight, January 1, 1970 UTC.",
},
&cli.IntFlag{
Name: "limit",
Usage: "Limit defines the number of the tasks to be returned.",
},
},
),
Before: interceptor.BeforeChain(
interceptor.ParseService(true),
),
Action: func(ctx *cli.Context) error {
serviceID := ctx.String("service-id")
startTime := ctx.Int64("start-time")
endTime := ctx.Int64("end-time")
limit := ctx.Int("limit")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let us declare the startTime, endTime, and limit as *int64 or *int first, otherwise the value will be 0.

request := &query.AsyncProfilerTaskListRequest{
ServiceID: serviceID,
StartTime: &startTime,
EndTime: &endTime,
Limit: &limit,
}

tasks, err := profiling.GetAsyncProfilerTaskList(ctx, request)

if err != nil {
return err
}

return display.Display(ctx, &displayable.Displayable{Data: tasks, Condition: request})
},
}
36 changes: 36 additions & 0 deletions internal/commands/profiling/asyncprofiler/getTaskProgress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package asyncprofiler

import (
"github.com/apache/skywalking-cli/pkg/display"
"github.com/apache/skywalking-cli/pkg/display/displayable"
"github.com/apache/skywalking-cli/pkg/graphql/profiling"
"github.com/urfave/cli/v2"
)

var getTaskProgressCommand = &cli.Command{
Name: "progress",
Aliases: []string{"logs", "p"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be used as swctl profiling asyncprofiler logs? I think it's a difference in the meaning of progress.

Flags: []cli.Flag{
&cli.StringFlag{
Name: "task-id",
Usage: "async profiler task id.",
},
},
Usage: "Query async-profiler task progress",
UsageText: `Query async-profiler task progress

Examples:
1. Query task progress, including task logs and successInstances and errorInstances
$ swctl-dev-37550c6-darwin-arm64 profiling asyncprofiler progress --task-id=task-id`,
zhengziyi0117 marked this conversation as resolved.
Show resolved Hide resolved
Action: func(ctx *cli.Context) error {
taskID := ctx.String("task-id")

data, err := profiling.GetAsyncProfilerTaskProgress(ctx, taskID)

if err != nil {
return err
}

return display.Display(ctx, &displayable.Displayable{Data: data, Condition: taskID})
},
}
Loading
Loading