Skip to content

Commit ce0e426

Browse files
authored
feat: add support to query trace from cold-stage storage (#217)
1 parent 6e2413a commit ce0e426

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Licensed to Apache Software Foundation (ASF) under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Apache Software Foundation (ASF) licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
query ($traceId: ID!, $duration: Duration!) {
19+
result: queryTraceFromColdStage(traceId: $traceId, duration: $duration) {
20+
spans {
21+
traceId
22+
segmentId
23+
spanId
24+
parentSpanId
25+
refs {
26+
traceId
27+
parentSegmentId
28+
parentSpanId
29+
type
30+
}
31+
serviceCode
32+
serviceInstanceName
33+
startTime
34+
endTime
35+
endpointName
36+
type
37+
peer
38+
component
39+
isError
40+
layer
41+
tags {
42+
key value
43+
}
44+
logs {
45+
time data {
46+
key value
47+
}
48+
}
49+
attachedEvents {
50+
startTime {
51+
seconds nanos
52+
}
53+
event
54+
endTime {
55+
seconds nanos
56+
}
57+
tags {
58+
key value
59+
}
60+
summary {
61+
key value
62+
}
63+
}
64+
}
65+
}
66+
}

internal/commands/trace/list.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ $ swctl trace ls --service-name "business-zone::projectB" --endpoint-name "/proj
5353
5454
3. Query the monitored trace of id "321661b1-9a31-4e12-ad64-c8f6711f108d":
5555
$ swctl trace ls --trace-id "321661b1-9a31-4e12-ad64-c8f6711f108d"
56+
57+
4. Query the monitored trace of id "321661b1-9a31-4e12-ad64-c8f6711f108d" from cold-stage storage:
58+
$ swctl trace ls --trace-id "321661b1-9a31-4e12-ad64-c8f6711f108d" --cold
5659
`,
5760
Flags: flags.Flags(
5861
flags.DurationFlags,
@@ -74,6 +77,11 @@ $ swctl trace ls --trace-id "321661b1-9a31-4e12-ad64-c8f6711f108d"
7477
Usage: "`order` of the returned traces, can be `duration` or `startTime`",
7578
Value: "duration",
7679
},
80+
&cli.BoolFlag{
81+
Name: "cold",
82+
Usage: "query trace from cold-stage storage, must be used with trace-id",
83+
Value: false,
84+
},
7785
},
7886
),
7987
Before: interceptor.BeforeChain(
@@ -96,6 +104,20 @@ $ swctl trace ls --trace-id "321661b1-9a31-4e12-ad64-c8f6711f108d"
96104
serviceInstanceID := ctx.String("instance-id")
97105
traceID := ctx.String("trace-id")
98106
tagStr := ctx.String("tags")
107+
cold := ctx.Bool("cold")
108+
109+
if cold && traceID == "" {
110+
return fmt.Errorf("cold storage must be queried with trace-id")
111+
}
112+
113+
if cold {
114+
trace, err := trace.ColdTrace(ctx.Context, duration, traceID)
115+
if err != nil {
116+
return err
117+
}
118+
return display.Display(ctx.Context, &displayable.Displayable{Data: trace})
119+
}
120+
99121
var tags []*api.SpanTag = nil
100122
if tagStr != "" {
101123
tagArr := strings.SplitSeq(tagStr, ",")

pkg/graphql/trace/trace.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ func Trace(ctx context.Context, traceID string) (api.Trace, error) {
3939
return response["result"], err
4040
}
4141

42+
func ColdTrace(ctx context.Context, duration api.Duration, traceID string) (api.Trace, error) {
43+
var response map[string]api.Trace
44+
45+
request := graphql.NewRequest(assets.Read("graphqls/trace/ColdTrace.graphql"))
46+
request.Var("traceId", traceID)
47+
request.Var("duration", duration)
48+
err := client.ExecuteQuery(ctx, request, &response)
49+
50+
return response["result"], err
51+
}
52+
4253
func Traces(ctx context.Context, condition *api.TraceQueryCondition) (api.TraceBrief, error) {
4354
var response map[string]api.TraceBrief
4455

0 commit comments

Comments
 (0)