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

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

commit 2ada9e14fa07a0fb13f17909790d0c12a739c18b
Author: mrproliu <741550...@qq.com>
AuthorDate: Thu Sep 18 09:36:45 2025 +0800

    Adapt the new trace v2 query protocol
---
 CHANGES.md                                   |  3 ++
 assets/graphqls/trace/ColdTrace.graphql      | 66 --------------------------
 assets/graphqls/trace/Trace.graphql          |  4 +-
 assets/graphqls/tracev2/Traces.graphql       | 71 ++++++++++++++++++++++++++++
 cmd/swctl/main.go                            |  2 +
 internal/commands/trace/list.go              |  8 ----
 internal/commands/trace/trace.go             | 28 ++++++++++-
 internal/commands/{trace => tracev2}/list.go | 24 ++++------
 internal/commands/tracev2/tracev2.go         | 29 ++++++++++++
 pkg/display/graph/tree/list.go               |  8 ++--
 pkg/graphql/trace/trace.go                   | 14 +-----
 pkg/graphql/{trace => tracev2}/trace.go      | 26 +---------
 12 files changed, 150 insertions(+), 133 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 70bed05..4ae94b8 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -12,6 +12,9 @@ Release Notes.
 * Add the sub-command `alarm autocomplete-keys` and `alarm 
auto-complete-values` for alarm query API by @mrproliu in 
https://github.com/apache/skywalking-cli/pull/210
 * Adapt the alarm message query API by @mrproliu in 
https://github.com/apache/skywalking-cli/pull/210
 * Add the owner field in the `metrics exec` query API by @mrproliu in 
https://github.com/apache/skywalking-cli/pull/210
+* Add the `trace-v2 list` command for adapt the new trace query API by 
@mrproliu in https://github.com/apache/skywalking-cli/pull/225
+* Add the duration field in the `trace list` command by @mrproliu in 
https://github.com/apache/skywalking-cli/pull/225
+* Remove the oldest `queryTraceFromColdStage` query call in the `trace list` 
command by @mrproliu in https://github.com/apache/skywalking-cli/pull/225
 
 ### Bug Fixes
 
diff --git a/assets/graphqls/trace/ColdTrace.graphql 
b/assets/graphqls/trace/ColdTrace.graphql
deleted file mode 100644
index eb53ef7..0000000
--- a/assets/graphqls/trace/ColdTrace.graphql
+++ /dev/null
@@ -1,66 +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.
-
-query ($traceId: ID!, $duration: Duration!) {
-    result: queryTraceFromColdStage(traceId: $traceId, duration: $duration) {
-        spans {
-            traceId
-            segmentId
-            spanId
-            parentSpanId
-            refs {
-                traceId
-                parentSegmentId
-                parentSpanId
-                type
-            }
-            serviceCode
-            serviceInstanceName
-            startTime
-            endTime
-            endpointName
-            type
-            peer
-            component
-            isError
-            layer
-            tags {
-                key value
-            }
-            logs {
-                time data {
-                    key value
-                }
-            }
-            attachedEvents {
-                startTime {
-                    seconds nanos
-                }
-                event
-                endTime {
-                    seconds nanos
-                }
-                tags {
-                    key value
-                }
-                summary {
-                    key value
-                }
-            }
-        }
-    }
-}
diff --git a/assets/graphqls/trace/Trace.graphql 
b/assets/graphqls/trace/Trace.graphql
index 8f52c01..156352f 100644
--- a/assets/graphqls/trace/Trace.graphql
+++ b/assets/graphqls/trace/Trace.graphql
@@ -15,8 +15,8 @@
 # specific language governing permissions and limitations
 # under the License.
 
-query ($traceId: ID!) {
-    result: queryTrace(traceId: $traceId) {
+query ($traceId: ID!, $duration: Duration) {
+    result: queryTrace(traceId: $traceId, duration: $duration) {
         spans {
             traceId
             segmentId
diff --git a/assets/graphqls/tracev2/Traces.graphql 
b/assets/graphqls/tracev2/Traces.graphql
new file mode 100644
index 0000000..058b795
--- /dev/null
+++ b/assets/graphqls/tracev2/Traces.graphql
@@ -0,0 +1,71 @@
+# 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: TraceQueryCondition){
+    result: queryTraces(condition: $condition){
+        traces {
+            spans {
+                traceId
+                segmentId
+                spanId
+                parentSpanId
+                refs {
+                    traceId
+                    parentSegmentId
+                    parentSpanId
+                    type
+                }
+                serviceCode
+                serviceInstanceName
+                startTime
+                endTime
+                endpointName
+                type
+                peer
+                component
+                isError
+                layer
+                tags {
+                    key value
+                }
+                logs {
+                    time data {
+                        key value
+                    }
+                }
+                attachedEvents {
+                    startTime {
+                        seconds nanos
+                    }
+                    event
+                    endTime {
+                        seconds nanos
+                    }
+                    tags {
+                        key value
+                    }
+                    summary {
+                        key value
+                    }
+                }
+            }
+        }
+        retrievedTimeRange {
+            startTime endTime
+        }
+    }
+}
\ No newline at end of file
diff --git a/cmd/swctl/main.go b/cmd/swctl/main.go
index 81914fa..6a29883 100644
--- a/cmd/swctl/main.go
+++ b/cmd/swctl/main.go
@@ -43,6 +43,7 @@ import (
        "github.com/apache/skywalking-cli/internal/commands/records"
        "github.com/apache/skywalking-cli/internal/commands/service"
        "github.com/apache/skywalking-cli/internal/commands/trace"
+       "github.com/apache/skywalking-cli/internal/commands/tracev2"
        intutil "github.com/apache/skywalking-cli/internal/util"
        "github.com/apache/skywalking-cli/pkg/logger"
        "github.com/apache/skywalking-cli/pkg/util"
@@ -99,6 +100,7 @@ services, service instances, etc.`
                service.Command,
                metrics.Command,
                trace.Command,
+               tracev2.Command,
                healthcheck.Command,
                dashboard.Command,
                install.Command,
diff --git a/internal/commands/trace/list.go b/internal/commands/trace/list.go
index fe738cc..ac20ed5 100644
--- a/internal/commands/trace/list.go
+++ b/internal/commands/trace/list.go
@@ -102,14 +102,6 @@ $ swctl trace ls --trace-id 
"321661b1-9a31-4e12-ad64-c8f6711f108d" --cold
                traceID := ctx.String("trace-id")
                tagStr := ctx.String("tags")
 
-               if coldStage && traceID != "" {
-                       trace, err := trace.ColdTrace(ctx.Context, duration, 
traceID)
-                       if err != nil {
-                               return err
-                       }
-                       return display.Display(ctx.Context, 
&displayable.Displayable{Data: trace})
-               }
-
                var tags []*api.SpanTag = nil
                if tagStr != "" {
                        tagArr := strings.SplitSeq(tagStr, ",")
diff --git a/internal/commands/trace/trace.go b/internal/commands/trace/trace.go
index f7f31a0..25b7df5 100644
--- a/internal/commands/trace/trace.go
+++ b/internal/commands/trace/trace.go
@@ -22,6 +22,11 @@ import (
 
        "github.com/urfave/cli/v2"
 
+       api "skywalking.apache.org/repo/goapi/query"
+
+       "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/trace"
@@ -39,12 +44,33 @@ with specified options, like service name, endpoint name, 
etc.
 Examples:
 1. Query the trace details (spans) of id 
"321661b1-9a31-4e12-ad64-c8f6711f108d":
 $ swctl trace "321661b1-9a31-4e12-ad64-c8f6711f108d"`,
+       Flags: flags.Flags(flags.DurationFlags),
        Action: func(ctx *cli.Context) error {
                if ctx.NArg() == 0 {
                        return fmt.Errorf("command trace without sub command 
requires 1 trace id as argument")
                }
 
-               trace, err := trace.Trace(ctx.Context, ctx.Args().First())
+               // if the user has set start or end, then we use the duration
+               var duration *api.Duration
+               start := ctx.String("start")
+               end := ctx.String("end")
+               if start != "" || end != "" {
+                       if err := interceptor.DurationInterceptor(ctx); err != 
nil {
+                               return err
+                       }
+                       step := ctx.Generic("step")
+                       coldStage := ctx.Bool("cold")
+                       start = ctx.String("start")
+                       end = ctx.String("end")
+                       duration = &api.Duration{
+                               Start:     start,
+                               End:       end,
+                               Step:      step.(*model.StepEnumValue).Selected,
+                               ColdStage: &coldStage,
+                       }
+               }
+
+               trace, err := trace.Trace(ctx.Context, duration, 
ctx.Args().First())
                if err != nil {
                        return err
                }
diff --git a/internal/commands/trace/list.go b/internal/commands/tracev2/list.go
similarity index 86%
copy from internal/commands/trace/list.go
copy to internal/commands/tracev2/list.go
index fe738cc..0c1faf0 100644
--- a/internal/commands/trace/list.go
+++ b/internal/commands/tracev2/list.go
@@ -15,7 +15,7 @@
 // specific language governing permissions and limitations
 // under the License.
 
-package trace
+package tracev2
 
 import (
        "fmt"
@@ -30,7 +30,7 @@ import (
        "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/trace"
+       "github.com/apache/skywalking-cli/pkg/graphql/tracev2"
 )
 
 const DefaultPageSize = 15
@@ -43,19 +43,19 @@ var ListCommand = &cli.Command{
 
 Examples:
 1. Query all monitored traces:
-$ swctl trace ls
+$ swctl trace-v2 ls
 
 2. Query all monitored traces of service "business-zone::projectB":
-$ swctl trace ls --service-name "business-zone::projectB"
+$ swctl trace-v2 ls --service-name "business-zone::projectB"
 
 3. Query all monitored traces of endpoint "/projectB/{value}" of service 
"business-zone::projectB":
-$ swctl trace ls --service-name "business-zone::projectB" --endpoint-name 
"/projectB/{value}"
+$ swctl trace-v2 ls --service-name "business-zone::projectB" --endpoint-name 
"/projectB/{value}"
 
 3. Query the monitored trace of id "321661b1-9a31-4e12-ad64-c8f6711f108d":
-$ swctl trace ls --trace-id "321661b1-9a31-4e12-ad64-c8f6711f108d"
+$ swctl trace-v2 ls --trace-id "321661b1-9a31-4e12-ad64-c8f6711f108d"
 
 4. Query the monitored trace of id "321661b1-9a31-4e12-ad64-c8f6711f108d" from 
cold-stage storage:
-$ swctl trace ls --trace-id "321661b1-9a31-4e12-ad64-c8f6711f108d" --cold
+$ swctl trace-v2 ls --trace-id "321661b1-9a31-4e12-ad64-c8f6711f108d" --cold
 `,
        Flags: flags.Flags(
                flags.DurationFlags,
@@ -102,14 +102,6 @@ $ swctl trace ls --trace-id 
"321661b1-9a31-4e12-ad64-c8f6711f108d" --cold
                traceID := ctx.String("trace-id")
                tagStr := ctx.String("tags")
 
-               if coldStage && traceID != "" {
-                       trace, err := trace.ColdTrace(ctx.Context, duration, 
traceID)
-                       if err != nil {
-                               return err
-                       }
-                       return display.Display(ctx.Context, 
&displayable.Displayable{Data: trace})
-               }
-
                var tags []*api.SpanTag = nil
                if tagStr != "" {
                        tagArr := strings.SplitSeq(tagStr, ",")
@@ -148,7 +140,7 @@ $ swctl trace ls --trace-id 
"321661b1-9a31-4e12-ad64-c8f6711f108d" --cold
                        Tags:              tags,
                        Paging:            &paging,
                }
-               traces, err := trace.Traces(ctx.Context, condition)
+               traces, err := tracev2.Traces(ctx.Context, condition)
                if err != nil {
                        return err
                }
diff --git a/internal/commands/tracev2/tracev2.go 
b/internal/commands/tracev2/tracev2.go
new file mode 100644
index 0000000..f826966
--- /dev/null
+++ b/internal/commands/tracev2/tracev2.go
@@ -0,0 +1,29 @@
+// 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 tracev2
+
+import "github.com/urfave/cli/v2"
+
+var Command = &cli.Command{
+       Name:    "trace-v2",
+       Aliases: []string{"tv2"},
+       Usage:   "Trace v2 related sub-command",
+       Subcommands: cli.Commands{
+               ListCommand,
+       },
+}
diff --git a/pkg/display/graph/tree/list.go b/pkg/display/graph/tree/list.go
index daf9dc1..7ff1208 100644
--- a/pkg/display/graph/tree/list.go
+++ b/pkg/display/graph/tree/list.go
@@ -93,7 +93,7 @@ func DisplayList(ctx context.Context, displayable 
*d.Displayable) error {
 }
 
 func draw(ctx context.Context, list *widgets.List, tree *widgets.Tree, detail, 
help *widgets.Paragraph, data api.TraceBrief,
-       _ *api.TraceQueryCondition,
+       condition *api.TraceQueryCondition,
 ) {
        x, y := ui.TerminalDimensions()
 
@@ -101,7 +101,7 @@ func draw(ctx context.Context, list *widgets.List, tree 
*widgets.Tree, detail, h
                showIndex := list.SelectedRow
                traceID := data.Traces[showIndex].TraceIds[0]
                list.Title = fmt.Sprintf("[%s]", traceID)
-               nodes, serviceNames := getNodeData(ctx, traceID)
+               nodes, serviceNames := getNodeData(ctx, traceID, 
condition.QueryDuration)
                tree.Title = fmt.Sprintf("[%s]", strings.Join(serviceNames, 
"->"))
                tree.SetNodes(nodes)
                list.Rows = rows(data, x/4)
@@ -202,8 +202,8 @@ func listActions(key string, list *widgets.List, tree 
*widgets.Tree, listActive
        return f
 }
 
-func getNodeData(ctx context.Context, traceID string) (nodes 
[]*widgets.TreeNode, serviceNames []string) {
-       data, err := trace.Trace(ctx, traceID)
+func getNodeData(ctx context.Context, traceID string, duration *api.Duration) 
(nodes []*widgets.TreeNode, serviceNames []string) {
+       data, err := trace.Trace(ctx, duration, traceID)
        if err != nil {
                logger.Log.Fatalln(err)
        }
diff --git a/pkg/graphql/trace/trace.go b/pkg/graphql/trace/trace.go
index fa6669e..c6f9e0c 100644
--- a/pkg/graphql/trace/trace.go
+++ b/pkg/graphql/trace/trace.go
@@ -28,23 +28,13 @@ import (
        "github.com/apache/skywalking-cli/pkg/graphql/client"
 )
 
-func Trace(ctx context.Context, traceID string) (api.Trace, error) {
+func Trace(ctx context.Context, duration *api.Duration, traceID string) 
(api.Trace, error) {
        var response map[string]api.Trace
 
        request := 
graphql.NewRequest(assets.Read("graphqls/trace/Trace.graphql"))
        request.Var("traceId", traceID)
-
-       err := client.ExecuteQuery(ctx, request, &response)
-
-       return response["result"], err
-}
-
-func ColdTrace(ctx context.Context, duration api.Duration, traceID string) 
(api.Trace, error) {
-       var response map[string]api.Trace
-
-       request := 
graphql.NewRequest(assets.Read("graphqls/trace/ColdTrace.graphql"))
-       request.Var("traceId", traceID)
        request.Var("duration", duration)
+
        err := client.ExecuteQuery(ctx, request, &response)
 
        return response["result"], err
diff --git a/pkg/graphql/trace/trace.go b/pkg/graphql/tracev2/trace.go
similarity index 62%
copy from pkg/graphql/trace/trace.go
copy to pkg/graphql/tracev2/trace.go
index fa6669e..fbcef0d 100644
--- a/pkg/graphql/trace/trace.go
+++ b/pkg/graphql/tracev2/trace.go
@@ -15,7 +15,7 @@
 // specific language governing permissions and limitations
 // under the License.
 
-package trace
+package tracev2
 
 import (
        "context"
@@ -28,32 +28,10 @@ import (
        "github.com/apache/skywalking-cli/pkg/graphql/client"
 )
 
-func Trace(ctx context.Context, traceID string) (api.Trace, error) {
-       var response map[string]api.Trace
-
-       request := 
graphql.NewRequest(assets.Read("graphqls/trace/Trace.graphql"))
-       request.Var("traceId", traceID)
-
-       err := client.ExecuteQuery(ctx, request, &response)
-
-       return response["result"], err
-}
-
-func ColdTrace(ctx context.Context, duration api.Duration, traceID string) 
(api.Trace, error) {
-       var response map[string]api.Trace
-
-       request := 
graphql.NewRequest(assets.Read("graphqls/trace/ColdTrace.graphql"))
-       request.Var("traceId", traceID)
-       request.Var("duration", duration)
-       err := client.ExecuteQuery(ctx, request, &response)
-
-       return response["result"], err
-}
-
 func Traces(ctx context.Context, condition *api.TraceQueryCondition) 
(api.TraceBrief, error) {
        var response map[string]api.TraceBrief
 
-       request := 
graphql.NewRequest(assets.Read("graphqls/trace/Traces.graphql"))
+       request := 
graphql.NewRequest(assets.Read("graphqls/tracev2/Traces.graphql"))
        request.Var("condition", condition)
 
        err := client.ExecuteQuery(ctx, request, &response)

Reply via email to