Skip to content

Commit e684fae

Browse files
authored
Adapt the new record query API for sub-command metrics sampled-record (#167)
1 parent 710114f commit e684fae

16 files changed

Lines changed: 225 additions & 39 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Release Notes.
3131
* Bump up swck dependency for transitive dep upgrade by @kezhenxu94 in https://github.com/apache/skywalking-cli/pull/162
3232
* Add the sub-commands for query sorted metrics/records by @mrproliu in https://github.com/apache/skywalking-cli/pull/163
3333
* Add compatibility documentation by @mrproliu in https://github.com/apache/skywalking-cli/pull/164
34+
* Add the sub-command `records list` for adapt the new record query API by @mrproliu in https://github.com/apache/skywalking-cli/pull/167
3435

3536
0.10.0
3637
------------------

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ SkyWalking CLI and SkyWalking OAP communicate with different query version, here
161161
| SkyWalking CLI | OAP Server Version |
162162
|----------------|---------------|
163163
| \> = 0.11.0 | \> = 9.2.0 |
164+
| \> = 0.12.0 | \> = 9.3.0 |
164165

165166
# Contributing
166167

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 ($condition: RecordCondition!, $duration: Duration!) {
19+
result: readRecords(condition: $condition, duration: $duration) {
20+
name
21+
id
22+
value
23+
refId
24+
}
25+
}

assets/graphqls/metrics/SampledRecords.graphql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
query ($condition:TopNCondition!, $duration: Duration!) {
1919
result: readSampledRecords(condition: $condition, duration: $duration) {
2020
name
21+
id
2122
value
23+
refId
2224
}
2325
}

cmd/swctl/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/apache/skywalking-cli/internal/commands/metrics"
3939
"github.com/apache/skywalking-cli/internal/commands/process"
4040
"github.com/apache/skywalking-cli/internal/commands/profiling"
41+
"github.com/apache/skywalking-cli/internal/commands/records"
4142
"github.com/apache/skywalking-cli/internal/commands/service"
4243
"github.com/apache/skywalking-cli/internal/commands/trace"
4344
"github.com/apache/skywalking-cli/internal/logger"
@@ -104,6 +105,7 @@ services, service instances, etc.`
104105
layer.Command,
105106
process.Command,
106107
profiling.Command,
108+
records.Command,
107109
}
108110

109111
app.Before = interceptor.BeforeChain(

dist/LICENSE

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ The text of each license is also included at licenses/license-[project].txt.
294294
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.22 Apache-2.0
295295
sigs.k8s.io/controller-runtime v0.10.0 Apache-2.0
296296
sigs.k8s.io/structured-merge-diff/v4 v4.1.2 Apache-2.0
297-
skywalking.apache.org/repo/goapi v0.0.0-20220714130828-0d56d1f4c592 Apache-2.0
297+
skywalking.apache.org/repo/goapi v0.0.0-20221019074310-53ebda305187 Apache-2.0
298298

299299
========================================================================
300300
BSD-2-Clause licenses
@@ -360,14 +360,14 @@ The text of each license is also included at licenses/license-[project].txt.
360360
golang.org/x/net v0.0.0-20220909164309-bea034e7d591 BSD-3-Clause
361361
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d BSD-3-Clause
362362
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c BSD-3-Clause
363-
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 BSD-3-Clause
363+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab BSD-3-Clause
364364
golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 BSD-3-Clause
365365
golang.org/x/text v0.3.7 BSD-3-Clause
366366
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac BSD-3-Clause
367367
golang.org/x/tools v0.1.3 BSD-3-Clause
368368
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 BSD-3-Clause
369369
google.golang.org/api v0.20.0 BSD-3-Clause
370-
google.golang.org/protobuf v1.28.0 BSD-3-Clause
370+
google.golang.org/protobuf v1.28.1 BSD-3-Clause
371371
gopkg.in/errgo.v2 v2.1.0 BSD-3-Clause
372372
gopkg.in/fsnotify.v1 v1.4.7 BSD-3-Clause
373373
gopkg.in/inf.v0 v0.9.1 BSD-3-Clause

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ require (
2626
gopkg.in/yaml.v2 v2.4.0
2727
k8s.io/apimachinery v0.22.1
2828
sigs.k8s.io/controller-runtime v0.10.0
29-
skywalking.apache.org/repo/goapi v0.0.0-20220714130828-0d56d1f4c592
29+
skywalking.apache.org/repo/goapi v0.0.0-20221019074310-53ebda305187
3030
)

go.sum

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,8 @@ golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwY
572572
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
573573
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
574574
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
575-
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
576575
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
576+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
577577
golang.org/x/net v0.0.0-20220909164309-bea034e7d591 h1:D0B/7al0LLrVC8aWF4+oxpv/m8bc7ViFfVS8/gXGdqI=
578578
golang.org/x/net v0.0.0-20220909164309-bea034e7d591/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
579579
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
@@ -640,9 +640,10 @@ golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBc
640640
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
641641
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
642642
golang.org/x/sys v0.0.0-20210817190340-bfb29a6856f2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
643-
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
644-
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 h1:WIoqL4EROvwiPdUtaip4VcDdpZ4kha7wBWZrbVKCIZg=
643+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
645644
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
645+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
646+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
646647
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
647648
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
648649
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
@@ -783,8 +784,8 @@ google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGj
783784
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
784785
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
785786
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
786-
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
787-
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
787+
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
788+
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
788789
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
789790
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
790791
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
@@ -858,6 +859,6 @@ sigs.k8s.io/structured-merge-diff/v4 v4.1.2 h1:Hr/htKFmJEbtMgS/UD0N+gtgctAqz81t3
858859
sigs.k8s.io/structured-merge-diff/v4 v4.1.2/go.mod h1:j/nl6xW8vLS49O8YvXW1ocPhZawJtm+Yrr7PPRQ0Vg4=
859860
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
860861
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
861-
skywalking.apache.org/repo/goapi v0.0.0-20220714130828-0d56d1f4c592 h1:3UbXoMUpGBoYLvuUCaKPzlHCM9Q+enaaOcQ19QbTDr8=
862-
skywalking.apache.org/repo/goapi v0.0.0-20220714130828-0d56d1f4c592/go.mod h1:uWwwvhcwe2MD/nJCg0c1EE/eL6KzaBosLHDfMFoEJ30=
862+
skywalking.apache.org/repo/goapi v0.0.0-20221019074310-53ebda305187 h1:6JgAg9aohcHd72VplZUGycZgCNo6iQrz735nmtOTCnE=
863+
skywalking.apache.org/repo/goapi v0.0.0-20221019074310-53ebda305187/go.mod h1:lxmYWY1uAP5SLVKNymAyDzn7KG6dhPWN+pYHmyt+0vo=
863864
software.sslmate.com/src/go-pkcs12 v0.0.0-20210415151418-c5206de65a78/go.mod h1:B7Wf0Ya4DHF9Yw+qfZuJijQYkWicqDa+79Ytmmq3Kjg=

internal/commands/metrics/aggregation/sampled-record.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package aggregation
1919

2020
import (
21+
"fmt"
22+
2123
api "skywalking.apache.org/repo/goapi/query"
2224

2325
"github.com/urfave/cli/v2"
@@ -28,6 +30,7 @@ import (
2830
"github.com/apache/skywalking-cli/internal/model"
2931
"github.com/apache/skywalking-cli/pkg/display"
3032
"github.com/apache/skywalking-cli/pkg/display/displayable"
33+
"github.com/apache/skywalking-cli/pkg/graphql/metadata"
3134
"github.com/apache/skywalking-cli/pkg/graphql/metrics"
3235
)
3336

@@ -44,7 +47,9 @@ $ swctl metrics sampled-record --name top_n_database_statement 5
4447
Flags: flags.Flags(
4548
flags.DurationFlags,
4649
flags.MetricsFlags,
47-
flags.ServiceFlags,
50+
flags.InstanceRelationFlags,
51+
flags.EndpointRelationFlags,
52+
flags.ProcessRelationFlags,
4853
[]cli.Flag{
4954
&cli.GenericFlag{
5055
Name: "order",
@@ -59,9 +64,33 @@ $ swctl metrics sampled-record --name top_n_database_statement 5
5964
),
6065
Before: interceptor.BeforeChain(
6166
interceptor.DurationInterceptor,
62-
interceptor.ParseService(false),
67+
interceptor.ParseEndpointRelation(false),
68+
interceptor.ParseInstanceRelation(false),
69+
interceptor.ParseProcessRelation(false),
6370
),
6471
Action: func(ctx *cli.Context) error {
72+
// read OAP version
73+
major, minor, err := metadata.BackendVersion(ctx)
74+
if err != nil {
75+
return fmt.Errorf("read backend version failure: %v", err)
76+
}
77+
78+
// since 9.3.0, use new record query API
79+
if major >= 9 && minor >= 3 {
80+
condition, duration, err1 := buildReadRecordsCondition(ctx)
81+
if err1 != nil {
82+
return err1
83+
}
84+
logger.Log.Debugln(condition.Name, condition.TopN)
85+
86+
records, err1 := metrics.ReadRecords(ctx, *condition, *duration)
87+
if err1 != nil {
88+
return err1
89+
}
90+
91+
return display.Display(ctx, &displayable.Displayable{Data: records})
92+
}
93+
6594
condition, duration, err := buildSortedCondition(ctx, false)
6695
if err != nil {
6796
return err

internal/commands/metrics/aggregation/sorted-condition.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,36 @@ func buildSortedCondition(ctx *cli.Context, parseScope bool) (*api.TopNCondition
7171
Step: step,
7272
}, nil
7373
}
74+
75+
func buildReadRecordsCondition(ctx *cli.Context) (*api.RecordCondition, *api.Duration, error) {
76+
start := ctx.String("start")
77+
end := ctx.String("end")
78+
step := ctx.Generic("step").(*model.StepEnumValue).Selected
79+
80+
metricsName := ctx.String("name")
81+
order := ctx.Generic("order").(*model.OrderEnumValue).Selected
82+
topN := 5
83+
entity, err := interceptor.ParseEntity(ctx)
84+
if err != nil {
85+
return nil, nil, err
86+
}
87+
88+
if ctx.NArg() > 0 {
89+
nn, err2 := strconv.Atoi(ctx.Args().First())
90+
if err2 != nil {
91+
return nil, nil, fmt.Errorf("the 1st argument must be a number: %v", err2)
92+
}
93+
topN = nn
94+
}
95+
96+
return &api.RecordCondition{
97+
Name: metricsName,
98+
ParentEntity: entity,
99+
TopN: topN,
100+
Order: order,
101+
}, &api.Duration{
102+
Start: start,
103+
End: end,
104+
Step: step,
105+
}, nil
106+
}

0 commit comments

Comments
 (0)