Skip to content

Commit abc2ce9

Browse files
authored
Add metric-usages statev1 commands (#38)
1 parent 214b8b3 commit abc2ce9

File tree

49 files changed

+4789
-721
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+4789
-721
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* Adds new resource `v1/config/LogScaleAction`
66
* Adds new resource `v1/config/LogScaleAlert`
77
* Adds new field `pool.allocation.fixed_values` to `v1/config/ResourcePools`
8-
* New unstable state: metric-usages-by-metric-name
9-
* New unstable state: metric-usages-by-label-name
8+
* Adds new state command `metric-usages-by-metric-name`
9+
* Adds new state command `metric-usages-by-label-name`
1010

1111
## v1.9.0
1212

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ For details about these resource types, see their
4848

4949
## State
5050

51-
Chronoctl also provides a command to review the state of rule evaluations in
52-
Chronosphere:
51+
Chronoctl also provides commands to review state in Chronosphere:
5352

5453
* `rule-evaluations`
54+
* `metric-usages-by-metric-name`
55+
* `metric-usages-by-label-name`
5556

5657
## Additional commands
5758

src/cmd/pkg/metricusages/list.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import (
2626
"github.com/chronosphereio/chronoctl-core/src/cmd/pkg/client"
2727
"github.com/chronosphereio/chronoctl-core/src/cmd/pkg/output"
2828
"github.com/chronosphereio/chronoctl-core/src/cmd/pkg/pagination"
29-
state_unstable "github.com/chronosphereio/chronoctl-core/src/generated/swagger/stateunstable/client/operations"
30-
"github.com/chronosphereio/chronoctl-core/src/generated/swagger/stateunstable/models"
29+
statev1 "github.com/chronosphereio/chronoctl-core/src/generated/swagger/statev1/client/operations"
30+
"github.com/chronosphereio/chronoctl-core/src/generated/swagger/statev1/models"
3131
)
3232

3333
type listOptions[T any] struct {
@@ -38,12 +38,12 @@ type listOptions[T any] struct {
3838
nextToken string
3939
originalCommandString string
4040

41-
client state_unstable.ClientService
41+
client statev1.ClientService
4242

4343
listFn listFn[T]
4444
}
4545

46-
type listFn[T any] func(ctx context.Context, client state_unstable.ClientService, p pagination.Page) (items []T, token string, err error)
46+
type listFn[T any] func(ctx context.Context, client statev1.ClientService, p pagination.Page) (items []T, token string, err error)
4747

4848
func newListOptions[T any](listFn listFn[T]) *listOptions[T] {
4949
return &listOptions[T]{
@@ -75,7 +75,7 @@ func (o *listOptions[T]) buildCmd(short, example string) *cobra.Command {
7575
}
7676

7777
func (o *listOptions[T]) validate() error {
78-
client, err := o.clientFlags.StateUnstableClient()
78+
client, err := o.clientFlags.StateV1Client()
7979
if err != nil {
8080
return err
8181
}
@@ -88,7 +88,7 @@ func (o *listOptions[T]) addFlags(cmd *cobra.Command) {
8888
o.clientFlags.AddFlags(cmd)
8989
o.outputFlags.AddFlags(cmd)
9090

91-
cmd.Flags().Int64Var(&o.maxItems, "max-items", 0, "Maximum number of rule evaluations to return. If omitted, all rule evaluations will be returned.")
91+
cmd.Flags().Int64Var(&o.maxItems, "max-items", 0, "Maximum number of metric usages to return. If omitted, default is used.")
9292
cmd.Flags().StringVar(&o.nextToken, "next-token", "", "Pagination token to use")
9393
}
9494

@@ -102,8 +102,8 @@ func (o *listOptions[T]) run(w io.Writer) error {
102102
if err != nil {
103103
return err
104104
}
105-
for _, ruleEvaluation := range byMetric {
106-
if err := o.outputFlags.WriteObject(ruleEvaluation, w); err != nil {
105+
for _, metricUsage := range byMetric {
106+
if err := o.outputFlags.WriteObject(metricUsage, w); err != nil {
107107
return err
108108
}
109109
}
@@ -128,10 +128,10 @@ func (o *listOptions[T]) query(ctx context.Context) (usages []T, token string, _
128128

129129
func listMetricUsageByMetricName(
130130
ctx context.Context,
131-
client state_unstable.ClientService,
131+
client statev1.ClientService,
132132
p pagination.Page,
133-
) (items []*models.StateunstableMetricUsageByMetricName, token string, err error) {
134-
resp, err := client.ListMetricUsagesByMetricName(&state_unstable.ListMetricUsagesByMetricNameParams{
133+
) (items []*models.Statev1MetricUsageByMetricName, token string, err error) {
134+
resp, err := client.ListMetricUsagesByMetricName(&statev1.ListMetricUsagesByMetricNameParams{
135135
Context: ctx,
136136
PageMaxSize: &p.Size,
137137
PageToken: &p.Token,
@@ -148,10 +148,10 @@ func listMetricUsageByMetricName(
148148

149149
func listMetricUsageByLabelName(
150150
ctx context.Context,
151-
client state_unstable.ClientService,
151+
client statev1.ClientService,
152152
p pagination.Page,
153-
) (items []*models.StateunstableMetricUsageByLabelName, token string, err error) {
154-
resp, err := client.ListMetricUsagesByLabelName(&state_unstable.ListMetricUsagesByLabelNameParams{
153+
) (items []*models.Statev1MetricUsageByLabelName, token string, err error) {
154+
resp, err := client.ListMetricUsagesByLabelName(&statev1.ListMetricUsagesByLabelNameParams{
155155
Context: ctx,
156156
PageMaxSize: &p.Size,
157157
PageToken: &p.Token,

src/cmd/pkg/metricusages/list_test.go

+32-36
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ import (
1919
"strings"
2020
"testing"
2121

22-
state_unstable "github.com/chronosphereio/chronoctl-core/src/generated/swagger/stateunstable/client/operations"
23-
"github.com/chronosphereio/chronoctl-core/src/generated/swagger/stateunstable/mocks"
24-
"github.com/chronosphereio/chronoctl-core/src/generated/swagger/stateunstable/models"
22+
statev1 "github.com/chronosphereio/chronoctl-core/src/generated/swagger/statev1/client/operations"
23+
"github.com/chronosphereio/chronoctl-core/src/generated/swagger/statev1/mocks"
24+
"github.com/chronosphereio/chronoctl-core/src/generated/swagger/statev1/models"
2525
"github.com/golang/mock/gomock"
2626
"github.com/stretchr/testify/assert"
2727
"github.com/stretchr/testify/require"
2828
)
2929

3030
func TestListMetricUsagesByMetricName(t *testing.T) {
31-
newFn := func(name string) *models.StateunstableMetricUsageByMetricName {
32-
return &models.StateunstableMetricUsageByMetricName{
31+
newFn := func(name string) *models.Statev1MetricUsageByMetricName {
32+
return &models.Statev1MetricUsageByMetricName{
3333
MetricName: name,
34-
Usage: &models.StateunstableMetricUsage{
34+
Usage: &models.Statev1MetricUsage{
3535
TotalReferences: 1,
3636
TotalQueryExecutions: 2,
3737
TotalUniqueUsers: 3,
@@ -49,8 +49,7 @@ func TestListMetricUsagesByMetricName(t *testing.T) {
4949
External: 3,
5050
},
5151
},
52-
Cardinality: 1,
53-
Dpps: 2,
52+
Dpps: 2,
5453
}
5554
}
5655

@@ -64,8 +63,8 @@ func TestListMetricUsagesByMetricName(t *testing.T) {
6463
var buf bytes.Buffer
6564

6665
// Nil result.
67-
cli.EXPECT().ListMetricUsagesByMetricName(gomock.Any()).Return(&state_unstable.ListMetricUsagesByMetricNameOK{
68-
Payload: &models.StateunstableListMetricUsagesByMetricNameResponse{
66+
cli.EXPECT().ListMetricUsagesByMetricName(gomock.Any()).Return(&statev1.ListMetricUsagesByMetricNameOK{
67+
Payload: &models.Statev1ListMetricUsagesByMetricNameResponse{
6968
Usages: nil,
7069
},
7170
}, nil)
@@ -74,9 +73,9 @@ func TestListMetricUsagesByMetricName(t *testing.T) {
7473
buf.Reset()
7574

7675
// All results.
77-
cli.EXPECT().ListMetricUsagesByMetricName(gomock.Any()).Return(&state_unstable.ListMetricUsagesByMetricNameOK{
78-
Payload: &models.StateunstableListMetricUsagesByMetricNameResponse{
79-
Usages: []*models.StateunstableMetricUsageByMetricName{
76+
cli.EXPECT().ListMetricUsagesByMetricName(gomock.Any()).Return(&statev1.ListMetricUsagesByMetricNameOK{
77+
Payload: &models.Statev1ListMetricUsagesByMetricNameResponse{
78+
Usages: []*models.Statev1MetricUsageByMetricName{
8079
newFn("metric-a"),
8180
newFn("metric-b"),
8281
newFn("metric-c"),
@@ -100,7 +99,6 @@ usage:
10099
explorer: 1
101100
dashboard: 2
102101
external: 3
103-
cardinality: 1
104102
dpps: 2
105103
---
106104
metric_name: metric-b
@@ -119,7 +117,6 @@ usage:
119117
explorer: 1
120118
dashboard: 2
121119
external: 3
122-
cardinality: 1
123120
dpps: 2
124121
---
125122
metric_name: metric-c
@@ -138,16 +135,15 @@ usage:
138135
explorer: 1
139136
dashboard: 2
140137
external: 3
141-
cardinality: 1
142138
dpps: 2
143139
`
144140
assert.Equal(t, expected, buf.String())
145141
buf.Reset()
146142

147143
// All results paginated.
148-
first := cli.EXPECT().ListMetricUsagesByMetricName(gomock.Any()).Return(&state_unstable.ListMetricUsagesByMetricNameOK{
149-
Payload: &models.StateunstableListMetricUsagesByMetricNameResponse{
150-
Usages: []*models.StateunstableMetricUsageByMetricName{
144+
first := cli.EXPECT().ListMetricUsagesByMetricName(gomock.Any()).Return(&statev1.ListMetricUsagesByMetricNameOK{
145+
Payload: &models.Statev1ListMetricUsagesByMetricNameResponse{
146+
Usages: []*models.Statev1MetricUsageByMetricName{
151147
newFn("metric-a"),
152148
newFn("metric-b"),
153149
},
@@ -156,9 +152,9 @@ dpps: 2
156152
},
157153
},
158154
}, nil)
159-
second := cli.EXPECT().ListMetricUsagesByMetricName(gomock.Any()).Return(&state_unstable.ListMetricUsagesByMetricNameOK{
160-
Payload: &models.StateunstableListMetricUsagesByMetricNameResponse{
161-
Usages: []*models.StateunstableMetricUsageByMetricName{
155+
second := cli.EXPECT().ListMetricUsagesByMetricName(gomock.Any()).Return(&statev1.ListMetricUsagesByMetricNameOK{
156+
Payload: &models.Statev1ListMetricUsagesByMetricNameResponse{
157+
Usages: []*models.Statev1MetricUsageByMetricName{
162158
newFn("metric-c"),
163159
},
164160
},
@@ -170,10 +166,10 @@ dpps: 2
170166
}
171167

172168
func TestListMetricUsagesByLabelName(t *testing.T) {
173-
newFn := func(name string) *models.StateunstableMetricUsageByLabelName {
174-
return &models.StateunstableMetricUsageByLabelName{
169+
newFn := func(name string) *models.Statev1MetricUsageByLabelName {
170+
return &models.Statev1MetricUsageByLabelName{
175171
LabelName: name,
176-
Usage: &models.StateunstableMetricUsage{
172+
Usage: &models.Statev1MetricUsage{
177173
TotalReferences: 1,
178174
TotalQueryExecutions: 2,
179175
TotalUniqueUsers: 3,
@@ -207,8 +203,8 @@ func TestListMetricUsagesByLabelName(t *testing.T) {
207203
var buf bytes.Buffer
208204

209205
// Nil result.
210-
cli.EXPECT().ListMetricUsagesByLabelName(gomock.Any()).Return(&state_unstable.ListMetricUsagesByLabelNameOK{
211-
Payload: &models.StateunstableListMetricUsagesByLabelNameResponse{
206+
cli.EXPECT().ListMetricUsagesByLabelName(gomock.Any()).Return(&statev1.ListMetricUsagesByLabelNameOK{
207+
Payload: &models.Statev1ListMetricUsagesByLabelNameResponse{
212208
Usages: nil,
213209
},
214210
}, nil)
@@ -217,9 +213,9 @@ func TestListMetricUsagesByLabelName(t *testing.T) {
217213
buf.Reset()
218214

219215
// All results.
220-
cli.EXPECT().ListMetricUsagesByLabelName(gomock.Any()).Return(&state_unstable.ListMetricUsagesByLabelNameOK{
221-
Payload: &models.StateunstableListMetricUsagesByLabelNameResponse{
222-
Usages: []*models.StateunstableMetricUsageByLabelName{
216+
cli.EXPECT().ListMetricUsagesByLabelName(gomock.Any()).Return(&statev1.ListMetricUsagesByLabelNameOK{
217+
Payload: &models.Statev1ListMetricUsagesByLabelNameResponse{
218+
Usages: []*models.Statev1MetricUsageByLabelName{
223219
newFn("label-a"),
224220
newFn("label-b"),
225221
newFn("label-c"),
@@ -291,9 +287,9 @@ percent_of_series_with_label_name: 3
291287
buf.Reset()
292288

293289
// All results paginated.
294-
first := cli.EXPECT().ListMetricUsagesByLabelName(gomock.Any()).Return(&state_unstable.ListMetricUsagesByLabelNameOK{
295-
Payload: &models.StateunstableListMetricUsagesByLabelNameResponse{
296-
Usages: []*models.StateunstableMetricUsageByLabelName{
290+
first := cli.EXPECT().ListMetricUsagesByLabelName(gomock.Any()).Return(&statev1.ListMetricUsagesByLabelNameOK{
291+
Payload: &models.Statev1ListMetricUsagesByLabelNameResponse{
292+
Usages: []*models.Statev1MetricUsageByLabelName{
297293
newFn("label-a"),
298294
newFn("label-b"),
299295
},
@@ -302,9 +298,9 @@ percent_of_series_with_label_name: 3
302298
},
303299
},
304300
}, nil)
305-
second := cli.EXPECT().ListMetricUsagesByLabelName(gomock.Any()).Return(&state_unstable.ListMetricUsagesByLabelNameOK{
306-
Payload: &models.StateunstableListMetricUsagesByLabelNameResponse{
307-
Usages: []*models.StateunstableMetricUsageByLabelName{
301+
second := cli.EXPECT().ListMetricUsagesByLabelName(gomock.Any()).Return(&statev1.ListMetricUsagesByLabelNameOK{
302+
Payload: &models.Statev1ListMetricUsagesByLabelNameResponse{
303+
Usages: []*models.Statev1MetricUsageByLabelName{
308304
newFn("label-c"),
309305
},
310306
},

src/cmd/pkg/metricusages/root.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
package metricusages
1717

1818
import (
19-
"github.com/chronosphereio/chronoctl-core/src/cmd/pkg/groups"
2019
"github.com/spf13/cobra"
20+
21+
"github.com/chronosphereio/chronoctl-core/src/cmd/pkg/groups"
2122
)
2223

2324
// NewCommands returns a new cobra command for metric usages.
@@ -52,8 +53,8 @@ func newCommandByLabelName() *cobra.Command {
5253
chronoctl metric-usages-by-label-name list`,
5354
}
5455
cmd.AddCommand(newListOptions(listMetricUsageByLabelName).buildCmd(
55-
"List all metric usages by metric name.",
56-
`# List all metric usages by metric name.
57-
chronoctl metric-usages-by-metric-name list`))
56+
"List all metric usages by label name.",
57+
`# List all metric usages by label name.
58+
chronoctl metric-usages-by-label-name list`))
5859
return cmd
5960
}

src/cmd/root.go

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/chronosphereio/chronoctl-core/src/cmd/pkg/auth"
2121
"github.com/chronosphereio/chronoctl-core/src/cmd/pkg/groups"
22+
"github.com/chronosphereio/chronoctl-core/src/cmd/pkg/metricusages"
2223
"github.com/chronosphereio/chronoctl-core/src/cmd/pkg/ruleevaluations"
2324
"github.com/chronosphereio/chronoctl-core/src/cmd/pkg/unstable"
2425
"github.com/chronosphereio/chronoctl-core/src/generated/cli/configv1"
@@ -49,6 +50,7 @@ func New(options Options) (*cobra.Command, error) {
4950
cmd.AddCommand(NewApplyCommand(options.ApplyOptions))
5051
cmd.AddCommand(unstable.NewCommand())
5152
cmd.AddCommand(ruleevaluations.NewCommand())
53+
cmd.AddCommand(metricusages.NewCommands()...)
5254
cmd.AddCommand(auth.NewCommand())
5355
configv1.AddCommandsTo(cmd)
5456

0 commit comments

Comments
 (0)