Skip to content

Commit

Permalink
Adapt alarm and MQE query API (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrproliu authored Dec 31, 2024
1 parent 97319af commit 67cbc89
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Release Notes.

* Add the sub-command `profiling async` for async-profiler query API by @zhengziyi0117 in https://github.com/apache/skywalking-cli/pull/203
* Support the owner in MQE response by using [10.2 MQE query protocol](https://github.com/apache/skywalking-query-protocol/pull/141) by @zhengziyi0117 in https://github.com/apache/skywalking-cli/pull/203
* 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

### Bug Fixes

Expand Down
22 changes: 22 additions & 0 deletions assets/graphqls/alarm/alarms.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ query ($duration: Duration!, $scope: Scope, $keyword: String, $paging: Paginatio
startTime
scope
id
name
message
tags {
key, value
Expand All @@ -39,6 +40,27 @@ query ($duration: Duration!, $scope: Scope, $keyword: String, $paging: Paginatio
uuid
layer
}
snapshot {
expression
metrics {
name
results {
metric {
labels {
key value
}
}
values {
id
value
traceID
owner {
scope serviceID serviceName normal serviceInstanceID serviceInstanceName endpointID endpointName
}
}
}
}
}
}
}
}
20 changes: 20 additions & 0 deletions assets/graphqls/alarm/tagAutocompleteKeys.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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 ($duration: Duration!) {
result: queryAlarmTagAutocompleteKeys(duration: $duration)
}
20 changes: 20 additions & 0 deletions assets/graphqls/alarm/tagAutocompleteValues.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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 ($tagKey: String!, $duration: Duration!) {
result: queryAlarmTagAutocompleteValues(tagKey: $tagKey, duration: $duration)
}
3 changes: 3 additions & 0 deletions assets/graphqls/metrics/ExecuteExpression.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ query ($expression: String!, $entity: Entity!, $duration: Duration!) {
id
value
traceID
owner {
scope serviceID serviceName normal serviceInstanceID serviceInstanceName endpointID endpointName
}
}
}
error
Expand Down
2 changes: 1 addition & 1 deletion dist/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ The text of each license is also included at licenses/license-[project].txt.
k8s.io/utils v0.0.0-20210802155522-efc7438f0176 Apache-2.0
sigs.k8s.io/controller-runtime v0.10.0 Apache-2.0
sigs.k8s.io/structured-merge-diff/v4 v4.1.2 Apache-2.0
skywalking.apache.org/repo/goapi v0.0.0-20241129131257-944118bb91b8 Apache-2.0
skywalking.apache.org/repo/goapi v0.0.0-20241231024338-a68ffc997f46 Apache-2.0

========================================================================
BSD-2-Clause licenses
Expand Down
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-20241129131257-944118bb91b8
skywalking.apache.org/repo/goapi v0.0.0-20241231024338-a68ffc997f46
)

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-20241129131257-944118bb91b8 h1:JkgizChUyT1mobFo2I3lI8+qsQU22i3FdxR0BSFC0uw=
skywalking.apache.org/repo/goapi v0.0.0-20241129131257-944118bb91b8/go.mod h1:+n8BMuS8eRdzdnGh15ElRGBXPi0eYZSs2TKySBDmRTE=
skywalking.apache.org/repo/goapi v0.0.0-20241231024338-a68ffc997f46 h1:Lb9cumKH17PWZSs6BKT91dbwJAl1jCvYO7EQMM9yFTw=
skywalking.apache.org/repo/goapi v0.0.0-20241231024338-a68ffc997f46/go.mod h1:+n8BMuS8eRdzdnGh15ElRGBXPi0eYZSs2TKySBDmRTE=
2 changes: 2 additions & 0 deletions internal/commands/alarm/alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ var Command = &cli.Command{
Usage: "Alarm related sub-command",
Subcommands: []*cli.Command{
listCommand,
autocompleteKeysCommand,
autocompleteValuesCommand,
},
}
67 changes: 67 additions & 0 deletions internal/commands/alarm/autopleteKeys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 alarm

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

"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/alarm"

api "skywalking.apache.org/repo/goapi/query"
)

var autocompleteKeysCommand = &cli.Command{
Name: "autocomplete-keys",
Aliases: []string{"ks"},
Usage: "Query autocomplete Keys",
UsageText: `Query autocomplete keys
Examples:
1. Query autocomplete keys:
$ swctl alarm autocomplete-keys
`,
Flags: flags.Flags(
flags.DurationFlags,
),
Before: interceptor.BeforeChain(
interceptor.DurationInterceptor,
),
Action: func(ctx *cli.Context) error {
start := ctx.String("start")
end := ctx.String("end")
step := ctx.Generic("step")

duration := api.Duration{
Start: start,
End: end,
Step: step.(*model.StepEnumValue).Selected,
}

autocompleteKeys, err := alarm.TagAutocompleteKeys(ctx, duration)
if err != nil {
return err
}

return display.Display(ctx, &displayable.Displayable{Data: autocompleteKeys, Condition: duration})
},
}
76 changes: 76 additions & 0 deletions internal/commands/alarm/autopleteValues.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 alarm

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

"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/alarm"

api "skywalking.apache.org/repo/goapi/query"
)

var autocompleteValuesCommand = &cli.Command{
Name: "autocomplete-values",
Aliases: []string{"vs"},
Usage: "Query autocomplete Values",
UsageText: `Query autocomplete values
Examples:
1. Query autocomplete values:
$ swctl alarm autocomplete-values --key=tagKey
`,
Flags: flags.Flags(
flags.DurationFlags,
[]cli.Flag{
&cli.StringFlag{
Name: "key",
Usage: "autocomplete tag key",
Required: true,
},
},
),
Before: interceptor.BeforeChain(
interceptor.DurationInterceptor,
),
Action: func(ctx *cli.Context) error {
start := ctx.String("start")
end := ctx.String("end")
step := ctx.Generic("step")

tagKey := ctx.String("key")

duration := api.Duration{
Start: start,
End: end,
Step: step.(*model.StepEnumValue).Selected,
}

autocompleteValues, err := alarm.TagAutocompleteValues(ctx, duration, tagKey)
if err != nil {
return err
}

return display.Display(ctx, &displayable.Displayable{Data: autocompleteValues, Condition: tagKey})
},
}
23 changes: 23 additions & 0 deletions pkg/graphql/alarm/alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,26 @@ func Alarms(ctx *cli.Context, condition *ListAlarmCondition) (api.Alarms, error)

return response["result"], err
}

func TagAutocompleteKeys(ctx *cli.Context, duration api.Duration) ([]string, error) {
var response map[string][]string

request := graphql.NewRequest(assets.Read("graphqls/alarm/tagAutocompleteKeys.graphql"))
request.Var("duration", duration)

err := client.ExecuteQuery(ctx, request, &response)

return response["result"], err
}

func TagAutocompleteValues(ctx *cli.Context, duration api.Duration, key string) ([]string, error) {
var response map[string][]string

request := graphql.NewRequest(assets.Read("graphqls/alarm/tagAutocompleteValues.graphql"))
request.Var("duration", duration)
request.Var("tagKey", key)

err := client.ExecuteQuery(ctx, request, &response)

return response["result"], err
}

0 comments on commit 67cbc89

Please sign in to comment.