-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtopology_state.go
More file actions
222 lines (186 loc) · 6.12 KB
/
topology_state.go
File metadata and controls
222 lines (186 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package topology
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/stackvista/stackstate-cli/generated/stackstate_api"
"github.com/stackvista/stackstate-cli/internal/common"
"github.com/stackvista/stackstate-cli/internal/di"
"github.com/stackvista/stackstate-cli/internal/printer"
)
type StateArgs struct {
ComponentType string
Tags []string
Identifiers []string
Limit int
}
func StateCommand(cli *di.Deps) *cobra.Command {
args := &StateArgs{}
cmd := &cobra.Command{
Use: "state",
Short: "Show the health state of topology components",
Long: "Show the health state of topology components by type, tags, and identifiers. Displays the health state for each matching component.",
Example: `# show state of components of a specific type
sts topology state --type "otel service instance"
# show state with tag filtering
sts topology state --type "otel service instance" --tag "service.namespace:opentelemetry-demo-demo-dev"
# show state with multiple tags (ANDed)
sts topology state --type "otel service instance" \
--tag "service.namespace:opentelemetry-demo-demo-dev" \
--tag "service.name:accountingservice"
# show state with identifier filtering
sts topology state --type "otel service instance" --identifier "urn:opentelemetry:..."
# show state with limit on number of results
sts topology state --type "otel service instance" --limit 10
# show state and display as JSON
sts topology state --type "otel service instance" -o json`,
RunE: cli.CmdRunEWithApi(func(cmd *cobra.Command, cli *di.Deps, api *stackstate_api.APIClient, serverInfo *stackstate_api.ServerInfo) common.CLIError {
return RunStateCommand(cmd, cli, api, serverInfo, args)
}),
}
cmd.Flags().StringVar(&args.ComponentType, "type", "", "Component type")
cmd.Flags().StringSliceVar(&args.Tags, "tag", []string{}, "Filter by tags in format 'tag-name:tag-value' (multiple allowed, ANDed together)")
cmd.Flags().StringSliceVar(&args.Identifiers, "identifier", []string{}, "Filter by component identifiers (multiple allowed, ANDed together)")
cmd.Flags().IntVar(&args.Limit, "limit", 0, "Maximum number of components to output (must be positive)")
return cmd
}
func RunStateCommand(
_ *cobra.Command,
cli *di.Deps,
api *stackstate_api.APIClient,
_ *stackstate_api.ServerInfo,
args *StateArgs,
) common.CLIError {
if args.Limit < 0 {
return common.NewExecutionError(fmt.Errorf("limit must be a positive number, got: %d", args.Limit))
}
query := buildSTQLQuery(args.ComponentType, args.Tags, args.Identifiers)
metadata := stackstate_api.NewQueryMetadata(
false,
false,
0,
false,
false,
false,
false,
false,
false,
true,
)
request := stackstate_api.NewViewSnapshotRequest(
"SnapshotRequest",
query,
"0.0.1",
*metadata,
)
result, resp, err := api.SnapshotApi.QuerySnapshot(cli.Context).
ViewSnapshotRequest(*request).
Execute()
if err != nil {
return common.NewResponseError(err, resp)
}
componentStates, parseErr := parseStateResponse(result)
if parseErr != nil {
if typedErr := handleSnapshotError(result.ViewSnapshotResponse, resp); typedErr != nil {
return typedErr
}
return common.NewExecutionError(parseErr)
}
// Apply limit if specified
if args.Limit > 0 && len(componentStates) > args.Limit {
componentStates = componentStates[:args.Limit]
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"components": componentStates,
})
return nil
} else {
printStateTableOutput(cli, componentStates)
}
return nil
}
// ComponentState holds the component information along with its health state.
type ComponentState struct {
ID int64 `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Identifiers []string `json:"identifiers"`
HealthState string `json:"healthState"`
}
func parseStateResponse(result *stackstate_api.QuerySnapshotResult) ([]ComponentState, error) {
respMap := result.ViewSnapshotResponse
if respMap == nil {
return nil, fmt.Errorf("response data is nil")
}
respType, ok := respMap["_type"].(string)
if !ok {
return nil, fmt.Errorf("response has no _type discriminator")
}
if respType != "ViewSnapshot" {
return nil, fmt.Errorf("response is an error type: %s", respType)
}
metadata := parseMetadata(respMap)
var componentStates []ComponentState
if componentsSlice, ok := respMap["components"].([]interface{}); ok {
for _, comp := range componentsSlice {
if compMap, ok := comp.(map[string]interface{}); ok {
componentStates = append(componentStates, parseComponentStateFromMap(compMap, metadata))
}
}
}
return componentStates, nil
}
func parseComponentStateFromMap(compMap map[string]interface{}, metadata ComponentMetadata) ComponentState {
cs := ComponentState{
Identifiers: []string{},
HealthState: "UNKNOWN",
}
// Parse basic fields
if id, ok := compMap["id"].(float64); ok {
cs.ID = int64(id)
}
if name, ok := compMap["name"].(string); ok {
cs.Name = name
}
// Parse type
if typeID, ok := compMap["type"].(float64); ok {
if typeName, found := metadata.ComponentTypes[int64(typeID)]; found {
cs.Type = typeName
} else {
cs.Type = fmt.Sprintf("Unknown (%d)", int64(typeID))
}
}
// Parse identifiers
if identifiersRaw, ok := compMap["identifiers"].([]interface{}); ok {
for _, idRaw := range identifiersRaw {
if id, ok := idRaw.(string); ok {
cs.Identifiers = append(cs.Identifiers, id)
}
}
}
// Parse health state from state.healthState
if stateMap, ok := compMap["state"].(map[string]interface{}); ok {
if healthState, ok := stateMap["healthState"].(string); ok {
cs.HealthState = healthState
}
}
return cs
}
func printStateTableOutput(cli *di.Deps, componentStates []ComponentState) {
var tableData [][]interface{}
for _, cs := range componentStates {
identifiersStr := strings.Join(cs.Identifiers, ", ")
tableData = append(tableData, []interface{}{
cs.Name,
cs.Type,
cs.HealthState,
identifiersStr,
})
}
cli.Printer.Table(printer.TableData{
Header: []string{"Name", "Type", "Health State", "Identifiers"},
Data: tableData,
MissingTableDataMsg: printer.NotFoundMsg{Types: "components"},
})
}