-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathslos.go
More file actions
310 lines (251 loc) · 8.69 KB
/
Copy pathslos.go
File metadata and controls
310 lines (251 loc) · 8.69 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024-present Datadog, Inc.
package cmd
import (
"fmt"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
"github.com/DataDog/pup/pkg/formatter"
"github.com/spf13/cobra"
)
var slosCmd = &cobra.Command{
Use: "slos",
Short: "Manage Service Level Objectives",
Long: `Manage Datadog Service Level Objectives (SLOs) for tracking service reliability.
SLOs help you define and track service reliability targets based on Service Level
Indicators (SLIs). They support various calculation types and target windows.
CAPABILITIES:
• List all SLOs with status and error budget
• Get detailed SLO configuration and history
• Delete SLOs (requires confirmation unless --yes flag is used)
• View SLO status, error budget burn rate, and target compliance
SLO TYPES:
• Metric-based: Based on metric queries (e.g., success rate, latency)
• Monitor-based: Based on monitor uptime
• Time slice: Based on time slices meeting criteria
TARGET WINDOWS:
• 7 days (7d)
• 30 days (30d)
• 90 days (90d)
• Custom rolling windows
CALCULATION METHODS:
• by_count: Count of good events / total events
• by_uptime: Percentage of time in good state
EXAMPLES:
# List all SLOs
pup slos list
# Get detailed SLO information
pup slos get abc-123-def
# Get SLO history and status
pup slos get abc-123-def | jq '.data'
# Delete an SLO with confirmation
pup slos delete abc-123-def
# Delete an SLO without confirmation (automation)
pup slos delete abc-123-def --yes
ERROR BUDGET:
Error budget represents the allowed amount of unreliability before breaching
the SLO target. It's calculated as (1 - target) * time_window.
Example: 99.9% target over 30 days = 0.1% * 30 days = 43.2 minutes allowed downtime
AUTHENTICATION:
Requires either OAuth2 authentication (pup auth login) or API keys
(DD_API_KEY and DD_APP_KEY environment variables).`,
}
var slosListCmd = &cobra.Command{
Use: "list",
Short: "List all SLOs",
Long: `List all Service Level Objectives with current status.
This command retrieves all SLOs from your Datadog account including their
current status, error budget, and compliance percentage.
EXAMPLES:
# List all SLOs
pup slos list
# List SLOs with table output
pup slos list --output=table
# Save SLO list to file
pup slos list > slos.json
# Find SLOs by name with jq
pup slos list | jq '.data[] | select(.name | contains("API"))'
# Check error budget for all SLOs
pup slos list | jq '.data[] | {name: .name, error_budget: .error_budget_remaining}'
OUTPUT FIELDS:
• id: SLO ID
• name: SLO name
• description: SLO description
• type: SLO type (metric, monitor)
• type_id: Specific type identifier
• tags: SLO tags
• thresholds: Array of target thresholds
- target: Target percentage (e.g., 99.9)
- target_display: Display string (e.g., "99.9%")
- timeframe: Target window (7d, 30d, 90d)
- warning: Optional warning threshold
• status: Current SLO status
- state: "breaching", "ok", or "no_data"
- error_budget_remaining: Percentage of error budget remaining
- sli_value: Current SLI value
• created_at: Creation timestamp
• modified_at: Last modification timestamp
• creator: User who created the SLO
• monitor_ids: Associated monitor IDs (for monitor-based SLOs)
• monitor_tags: Monitor tags used in query (for monitor-based SLOs)
SLO STATES:
• ok: SLO is meeting target
• breaching: SLO has breached target (error budget exhausted)
• no_data: No data available to calculate SLO
FILTERING:
Use jq to filter results:
• Breaching SLOs: pup slos list | jq '.data[] | select(.status.state == "breaching")'
• High error budget: pup slos list | jq '.data[] | select(.status.error_budget_remaining > 50)'
• By tag: pup slos list | jq '.data[] | select(.tags[] | contains("team:backend"))'`,
RunE: runSlosList,
}
var slosGetCmd = &cobra.Command{
Use: "get [slo-id]",
Short: "Get SLO details",
Long: `Get detailed configuration and status for a specific SLO.
This command retrieves complete information about an SLO including its
configuration, current status, historical performance, and error budget.
ARGUMENTS:
slo-id The SLO ID (format: xxx-xxx-xxx)
EXAMPLES:
# Get SLO details
pup slos get abc-123-def
# Get SLO and save to file
pup slos get abc-123-def > slo-backup.json
# Check error budget remaining
pup slos get abc-123-def | jq '.data.error_budget_remaining'
# Get current SLI value
pup slos get abc-123-def | jq '.data.sli_value'
# View SLO target thresholds
pup slos get abc-123-def | jq '.data.thresholds'
OUTPUT STRUCTURE:
• id: SLO ID
• name: SLO name
• description: Detailed description
• type: SLO type
- "metric": Based on metric queries
- "monitor": Based on monitor uptime
- "time_slice": Based on time slices
• type_id: Type-specific identifier (0=metric, 1=monitor, 2=time_slice)
• query: SLO query definition
- numerator: Good events query (metric-based)
- denominator: Total events query (metric-based)
• monitor_ids: Array of monitor IDs (monitor-based)
• monitor_search: Monitor query (monitor-based)
• groups: Grouping dimensions
• tags: SLO tags
• thresholds: Target definitions
- target: Target percentage
- timeframe: Time window
- warning: Warning threshold
• created_at: Creation timestamp
• modified_at: Last modification timestamp
• creator: Creator information
• team_tags: Team ownership tags
CURRENT STATUS:
• state: Current state (ok, breaching, no_data)
• sli_value: Current SLI percentage
• error_budget_remaining: Remaining error budget percentage
• error_budget_burn_rate: Current burn rate
HISTORICAL DATA:
• history: Array of historical data points
• uptime: Historical uptime percentages
• corrections: Manual SLO corrections applied
USE CASES:
• Monitor SLO compliance and error budget
• Backup SLO configuration
• Analyze historical SLO performance
• Track error budget burn rate
• Report on service reliability`,
Args: cobra.ExactArgs(1),
RunE: runSlosGet,
}
var slosDeleteCmd = &cobra.Command{
Use: "delete [slo-id]",
Short: "Delete an SLO",
Args: cobra.ExactArgs(1),
RunE: runSlosDelete,
}
func init() {
slosCmd.AddCommand(slosListCmd)
slosCmd.AddCommand(slosGetCmd)
slosCmd.AddCommand(slosDeleteCmd)
}
func runSlosList(cmd *cobra.Command, args []string) error {
client, err := getClient()
if err != nil {
return err
}
api := datadogV1.NewServiceLevelObjectivesApi(client.V1())
resp, r, err := api.ListSLOs(client.Context())
if err != nil {
if r != nil {
return fmt.Errorf("failed to list SLOs: %w (status: %d)", err, r.StatusCode)
}
return fmt.Errorf("failed to list SLOs: %w", err)
}
output, err := formatter.FormatOutput(resp, formatter.OutputFormat(outputFormat))
if err != nil {
return err
}
printOutput("%s\n", output)
return nil
}
func runSlosGet(cmd *cobra.Command, args []string) error {
client, err := getClient()
if err != nil {
return err
}
sloID := args[0]
api := datadogV1.NewServiceLevelObjectivesApi(client.V1())
resp, r, err := api.GetSLO(client.Context(), sloID)
if err != nil {
if r != nil {
return fmt.Errorf("failed to get SLO: %w (status: %d)", err, r.StatusCode)
}
return fmt.Errorf("failed to get SLO: %w", err)
}
output, err := formatter.FormatOutput(resp, formatter.OutputFormat(outputFormat))
if err != nil {
return err
}
printOutput("%s\n", output)
return nil
}
func runSlosDelete(cmd *cobra.Command, args []string) error {
client, err := getClient()
if err != nil {
return err
}
sloID := args[0]
// Check if auto-approve is enabled
if !cfg.AutoApprove {
printOutput("⚠️ WARNING: This will permanently delete SLO %s\n", sloID)
printOutput("Are you sure you want to continue? (y/N): ")
response, err := readConfirmation()
if err != nil {
// User cancelled or error reading input
printOutput("\nOperation cancelled\n")
return nil
}
if response != "y" && response != "Y" {
printOutput("Operation cancelled\n")
return nil
}
}
api := datadogV1.NewServiceLevelObjectivesApi(client.V1())
resp, r, err := api.DeleteSLO(client.Context(), sloID)
if err != nil {
if r != nil {
return fmt.Errorf("failed to delete SLO: %w (status: %d)", err, r.StatusCode)
}
return fmt.Errorf("failed to delete SLO: %w", err)
}
output, err := formatter.FormatOutput(resp, formatter.OutputFormat(outputFormat))
if err != nil {
return err
}
printOutput("%s\n", output)
return nil
}