Skip to content

Commit 648a50a

Browse files
author
Ashish Gaurav
committed
#47 fix alert condition symbols use in the monitors
1 parent 9cc3f59 commit 648a50a

12 files changed

Lines changed: 187 additions & 49 deletions

File tree

cmd/root.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ const FlagViewPrefixShort = "v"
118118
const FlagViewDestroy = "viewDestroy"
119119
const FlagDestroy = "destroy"
120120
const FlagAsModule = "asModule"
121+
const FlagUseViewHash = "useViewHash"
121122

122123
func init() {
123124
cobra.OnInitialize(initConfig)
@@ -166,6 +167,11 @@ func init() {
166167
"whether to generate the terraform config as a module",
167168
)
168169

170+
rootCmd.Flags().Bool(FlagUseViewHash, false,
171+
"whether to use descriptive or hashed name for the scheduled views, hashed names ensure data for "+
172+
"old view is not used when the query for it changes",
173+
)
174+
169175
rootCmd.Flags().SortFlags = false
170176
}
171177

@@ -233,6 +239,10 @@ func GetGenConf(cmd *cobra.Command) (*libs.GenConf, error) {
233239
if err != nil {
234240
return nil, err
235241
}
242+
useViewHash, err := cmd.Flags().GetBool(FlagUseViewHash)
243+
if err != nil {
244+
return nil, err
245+
}
236246

237247
conf := &libs.GenConf{
238248
IgnoreError: ie,
@@ -243,6 +253,7 @@ func GetGenConf(cmd *cobra.Command) (*libs.GenConf, error) {
243253
DoPlan: doPlan,
244254
DoApply: doApply,
245255
AsModule: asModule,
256+
UseViewHash: useViewHash,
246257
}
247258

248259
return conf, nil

credits/github.com/hashicorp/go-getter/go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU
2525
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
2626
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
2727
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
28-
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
2928
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
3029
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
3130
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
@@ -102,7 +101,6 @@ golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4Iltr
102101
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
103102
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
104103
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
105-
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ=
106104
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
107105
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
108106
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -116,9 +114,7 @@ golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7w
116114
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
117115
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0 h1:HyfiK1WMnHj5FXFXatD+Qs1A/xC2Run6RzeW1SyHxpc=
118116
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
119-
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
120117
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
121-
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2 h1:z99zHgr7hKfrUcX/KsoJk5FJfjTceCKIp96+biqP4To=
122118
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
123119
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
124120
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/hashicorp/terraform-exec v0.14.0
1010
github.com/manifoldco/promptui v0.8.0
1111
github.com/mattn/go-colorable v0.1.11
12+
github.com/mitchellh/hashstructure/v2 v2.0.2
1213
github.com/spf13/cobra v1.2.1
1314
github.com/spf13/viper v1.9.0
1415
go.uber.org/zap v1.19.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eI
354354
github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU=
355355
github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp04twqAjfvlZyCfX3sDjEym8=
356356
github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg=
357+
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
358+
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
357359
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
358360
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
359361
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=

libs/connections.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ func GiveConnectionIDS(token string) ([]MonitorConnections, error) {
4141
return nil, fmt.Errorf("Error: %s", resp.Status())
4242
}
4343

44-
4544
respBody := struct {
4645
Data []MonitorConnections `json:"data,omitempty"`
4746
Token string `json:"token,omitempty"`
@@ -51,6 +50,5 @@ func GiveConnectionIDS(token string) ([]MonitorConnections, error) {
5150

5251
json.Unmarshal(resp.Body(), &respBody)
5352

54-
5553
return respBody.Data, nil
5654
}

libs/dashboard.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type LayoutItem struct {
5151
}
5252

5353
func DashConfigFromSLO(sloConf SLO) (*SLODashboard, error) {
54-
sloName := sloConf.Metadata.Name
54+
sloName := sloConf.Name()
5555
target := sloConf.Target()
5656

5757
configYamlBytes, err := yaml.Marshal(sloConf)
@@ -79,7 +79,7 @@ func DashConfigFromSLO(sloConf SLO) (*SLODashboard, error) {
7979
StrYAMLConfig: string(configYamlBytes),
8080
Labels: sloConf.Labels,
8181
Fields: giveMapKeys(sloConf.Fields),
82-
ViewName: sloConf.ViewName,
82+
ViewName: sloConf.ViewName(),
8383
}
8484

8585
return conf, nil
@@ -315,33 +315,32 @@ var vizSettingHourlyBurn string
315315
//go:embed templates/visual-settings/breakdown-panel.gojson
316316
var vizSettingBreakdownPanel string
317317

318-
319318
//go:embed templates/visual-settings/forecasted-panel.json
320319
var vizSettingBudgetForecastPanel string
321320

322321
func givePanelQuery(s SLO, key PanelKey) (string, error) {
323-
//queryStr := givePanelQueryStr(key, s.ViewName)
322+
//queryStr := givePanelQueryStr(key, s.ViewName())
324323
queryTmplStr := ""
325324
if s.Spec.BudgetingMethod == BudgetingMethodNameTimeSlices {
326-
queryTmplStr = givePanelQueryTimesliceStr(key, s.ViewName)
325+
queryTmplStr = givePanelQueryTimesliceStr(key, s.ViewName())
327326
} else {
328-
queryTmplStr = givePanelQueryStr(key, s.ViewName)
327+
queryTmplStr = givePanelQueryStr(key, s.ViewName())
329328
}
330329

331330
wherePart := giveWhereClause(giveMapKeys(s.Fields))
332331

333332
tmplParams := struct {
334333
Target float64
335334
TimesliceRatioTarget float64
336-
GroupByStr string
335+
GroupByStr string
337336
}{
338337
Target: s.Target(),
339338
TimesliceRatioTarget: s.TimesliceTarget(),
340-
GroupByStr : giveFieldsGroupByStr(s.Fields),
339+
GroupByStr: giveFieldsGroupByStr(s.Fields),
341340
}
342341

343342
queryPart, err := GiveStrFromTmpl(queryTmplStr, tmplParams)
344-
return fmt.Sprintf("_view=%s %s %s", s.ViewName, wherePart, queryPart), err
343+
return fmt.Sprintf("_view=%s %s %s", s.ViewName(), wherePart, queryPart), err
345344
}
346345

347346
func givePanelQueryStr(Key PanelKey, view string) string {

libs/gen.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"embed"
66
"os"
77
"path/filepath"
8+
"sort"
89
"text/template"
910
)
1011

@@ -40,13 +41,13 @@ type GenConf struct {
4041
IgnoreError bool
4142
Clean bool
4243
AsModule bool
44+
UseViewHash bool
4345
}
4446

4547
func init() {
4648
var err error
4749

4850
tfTemplates, err = template.ParseFS(tmplFiles, "templates/terra/**")
49-
5051
if err != nil {
5152
panic(err)
5253
}
@@ -58,6 +59,8 @@ const ViewPrefix = "slogen_tf"
5859
func GenTerraform(slos map[string]*SLO, c GenConf) (string, error) {
5960

6061
err := SetupOutDir(c)
62+
useViewID = c.UseViewHash
63+
6164
if err != nil {
6265
BadResult("error setting up path : %s", err)
6366
return "", err
@@ -66,7 +69,6 @@ func GenTerraform(slos map[string]*SLO, c GenConf) (string, error) {
6669
srvMap := map[string]bool{}
6770

6871
for path, s := range slos {
69-
s.ViewName = GiveScheduleViewName(*s)
7072
srvMap[s.Spec.Service] = true
7173

7274
tmplToDo := []string{NameViewTmpl, NameDashboardTmpl, NameMonitorTmpl}
@@ -83,6 +85,7 @@ func GenTerraform(slos map[string]*SLO, c GenConf) (string, error) {
8385
}
8486

8587
srvList := GiveKeys(srvMap)
88+
sort.Strings(srvList)
8689

8790
err = GenFoldersTF(srvList, c.OutDir)
8891
if err != nil {
@@ -124,11 +127,6 @@ func ExecSLOTmpl(tmplName string, slo SLO, outDir string) error {
124127
return nil
125128
}
126129

127-
func ExecServiceOverview(slos map[string]SLO, outDir string) error {
128-
129-
return nil
130-
}
131-
132130
const (
133131
VarNameMonRootFolder = "slo_mon_root_folder_id"
134132
VarNameDashRootFolder = "slo_dash_root_folder_id"

libs/plan.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ func TFExec(wdPath string, action TFAction) error {
4141

4242
tf, err := tfexec.NewTerraform(wdPath, execPath)
4343
env := map[string]string{
44-
EnvKeyHTTPProxy: os.Getenv(EnvKeyHTTPProxy),
45-
EnvKeyHTTPSProxy: os.Getenv(EnvKeyHTTPSProxy),
44+
EnvKeyHTTPProxy: os.Getenv(EnvKeyHTTPProxy),
45+
EnvKeyHTTPSProxy: os.Getenv(EnvKeyHTTPSProxy),
4646
EnvKeySumoAccessID: os.Getenv(EnvKeySumoAccessID),
4747
EnvKeySumoAccessKey: os.Getenv(EnvKeySumoAccessKey),
4848
EnvKeySumoEnvironment: os.Getenv(EnvKeySumoEnvironment),

libs/spec.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@ import (
77
)
88

99
const (
10-
BudgetingMethodNameTimeSlices = "Timeslices"
10+
BudgetingMethodNameTimeSlices = "Timeslices"
1111
BudgetingMethodNameOccurrences = "Occurrences"
1212
)
13+
1314
type SLO struct {
1415
*v1alpha.SLO `yaml:",inline"`
1516
Labels map[string]string `yaml:"labels,omitempty"`
1617
Fields map[string]string `yaml:"fields,omitempty"`
1718
Alerts Alerts `yaml:"alerts,omitempty"`
18-
ViewName string `yaml:"viewName"`
1919
BurnRateAlerts []BurnRate `yaml:"burnRateAlerts,omitempty"` // deprecated
2020
}
2121

22+
func (s SLO) Name() string {
23+
return s.Metadata.Name
24+
}
25+
2226
type Alerts struct {
2327
BurnRate []BurnRate `yaml:"burnRate,omitempty"`
2428
}
@@ -38,19 +42,19 @@ func Parse(filename string) (*SLO, error) {
3842
func (s SLO) Target() float64 {
3943
target := s.Spec.Objectives[0].BudgetTarget
4044

41-
if target == nil {
45+
if target == nil {
4246
return 0.99
4347
}
4448

45-
return *target
49+
return *target
4650
}
4751

4852
func (s SLO) TimesliceTarget() float64 {
4953
target := s.Spec.Objectives[0].TimeSliceTarget
5054

51-
if target == nil {
52-
return s.Target()
53-
}
55+
if target == nil {
56+
return s.Target()
57+
}
5458

55-
return *target
59+
return *target
5660
}

libs/spec_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package libs
2+
3+
import (
4+
"gopkg.in/yaml.v3"
5+
"testing"
6+
)
7+
8+
func TestSLO_ViewID(t *testing.T) {
9+
10+
var slo SLO
11+
err := yaml.Unmarshal([]byte(testYaml1), &slo)
12+
13+
if err != nil {
14+
t.Errorf("Error: %v", err)
15+
}
16+
17+
tests := []struct {
18+
name string
19+
s SLO
20+
want string
21+
}{
22+
// TODO: Add test cases.
23+
{
24+
name: "test",
25+
s: slo,
26+
want: "",
27+
},
28+
}
29+
for _, tt := range tests {
30+
t.Run(tt.name, func(t *testing.T) {
31+
32+
if got := tt.s.ViewID(); got != tt.want {
33+
t.Errorf("ViewID() = %v, want %v", got, tt.want)
34+
}
35+
})
36+
}
37+
}
38+
39+
var testYaml1 = `
40+
apiVersion: openslo/v1alpha
41+
kind: SLO
42+
metadata:
43+
displayName: CloudCollector Ingest Lag
44+
name: cc-ingest-lag-v2
45+
spec:
46+
service: cloudcollector
47+
description: Track number of seconds a message is delayed in the ingest pipeline
48+
budgetingMethod: Timeslices
49+
objectives:
50+
- displayName: SLI to track ingest job is completed within 5 seconds for cloudcollector
51+
target: 0.95
52+
timeSliceTarget: 0.9 # ratio of good to total msgs, so as to consider that time window healthy, only applicable for Timeslices budgeting
53+
ratioMetrics:
54+
total:
55+
source: sumologic
56+
queryType: Logs
57+
query: |
58+
_sourcecategory=cloudcollector DefaultPerCustomerLagTracker !CustomerLagQueryDisablingStrategy "current lag"
59+
| parse "current lag: Some(*) ms," as lag
60+
| where lag != "*"
61+
| parse "customer: *," as customer_id
62+
| where customer_id matches "*"
63+
| lag / 1000 as lag_seconds
64+
good:
65+
source: sumologic
66+
queryType: Logs
67+
query: lag_seconds <= 20
68+
incremental: true
69+
createView: true
70+
fields:
71+
customerID: "customer_id"
72+
deployment: 'if(isNull(deployment),"dev",deployment)' # using an expression
73+
cluster: 'if(isNull(cluster),"-",cluster)'
74+
labels:
75+
team: collection
76+
tier: 0
77+
alerts:
78+
burnRate:
79+
- shortWindow: '10m'
80+
shortLimit: 14
81+
longWindow: '1h'
82+
longLimit: 14
83+
notifications:
84+
- connectionType: 'Email'
85+
recipients:
86+
- 'agaurav@sumologic.com'
87+
triggerFor:
88+
- Warning
89+
- ResolvedWarning
90+
`

0 commit comments

Comments
 (0)