Skip to content

Commit

Permalink
#47 fix alert condition symbols use in the monitors
Browse files Browse the repository at this point in the history
  • Loading branch information
agaurav committed Mar 4, 2022
1 parent 9cc3f59 commit 648a50a
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 49 deletions.
11 changes: 11 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ const FlagViewPrefixShort = "v"
const FlagViewDestroy = "viewDestroy"
const FlagDestroy = "destroy"
const FlagAsModule = "asModule"
const FlagUseViewHash = "useViewHash"

func init() {
cobra.OnInitialize(initConfig)
Expand Down Expand Up @@ -166,6 +167,11 @@ func init() {
"whether to generate the terraform config as a module",
)

rootCmd.Flags().Bool(FlagUseViewHash, false,
"whether to use descriptive or hashed name for the scheduled views, hashed names ensure data for "+
"old view is not used when the query for it changes",
)

rootCmd.Flags().SortFlags = false
}

Expand Down Expand Up @@ -233,6 +239,10 @@ func GetGenConf(cmd *cobra.Command) (*libs.GenConf, error) {
if err != nil {
return nil, err
}
useViewHash, err := cmd.Flags().GetBool(FlagUseViewHash)
if err != nil {
return nil, err
}

conf := &libs.GenConf{
IgnoreError: ie,
Expand All @@ -243,6 +253,7 @@ func GetGenConf(cmd *cobra.Command) (*libs.GenConf, error) {
DoPlan: doPlan,
DoApply: doApply,
AsModule: asModule,
UseViewHash: useViewHash,
}

return conf, nil
Expand Down
4 changes: 0 additions & 4 deletions credits/github.com/hashicorp/go-getter/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
Expand Down Expand Up @@ -102,7 +101,6 @@ golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4Iltr
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -116,9 +114,7 @@ golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0 h1:HyfiK1WMnHj5FXFXatD+Qs1A/xC2Run6RzeW1SyHxpc=
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2 h1:z99zHgr7hKfrUcX/KsoJk5FJfjTceCKIp96+biqP4To=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/hashicorp/terraform-exec v0.14.0
github.com/manifoldco/promptui v0.8.0
github.com/mattn/go-colorable v0.1.11
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.9.0
go.uber.org/zap v1.19.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eI
github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU=
github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp04twqAjfvlZyCfX3sDjEym8=
github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg=
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
Expand Down
2 changes: 0 additions & 2 deletions libs/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func GiveConnectionIDS(token string) ([]MonitorConnections, error) {
return nil, fmt.Errorf("Error: %s", resp.Status())
}


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

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


return respBody.Data, nil
}
17 changes: 8 additions & 9 deletions libs/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type LayoutItem struct {
}

func DashConfigFromSLO(sloConf SLO) (*SLODashboard, error) {
sloName := sloConf.Metadata.Name
sloName := sloConf.Name()
target := sloConf.Target()

configYamlBytes, err := yaml.Marshal(sloConf)
Expand Down Expand Up @@ -79,7 +79,7 @@ func DashConfigFromSLO(sloConf SLO) (*SLODashboard, error) {
StrYAMLConfig: string(configYamlBytes),
Labels: sloConf.Labels,
Fields: giveMapKeys(sloConf.Fields),
ViewName: sloConf.ViewName,
ViewName: sloConf.ViewName(),
}

return conf, nil
Expand Down Expand Up @@ -315,33 +315,32 @@ var vizSettingHourlyBurn string
//go:embed templates/visual-settings/breakdown-panel.gojson
var vizSettingBreakdownPanel string


//go:embed templates/visual-settings/forecasted-panel.json
var vizSettingBudgetForecastPanel string

func givePanelQuery(s SLO, key PanelKey) (string, error) {
//queryStr := givePanelQueryStr(key, s.ViewName)
//queryStr := givePanelQueryStr(key, s.ViewName())
queryTmplStr := ""
if s.Spec.BudgetingMethod == BudgetingMethodNameTimeSlices {
queryTmplStr = givePanelQueryTimesliceStr(key, s.ViewName)
queryTmplStr = givePanelQueryTimesliceStr(key, s.ViewName())
} else {
queryTmplStr = givePanelQueryStr(key, s.ViewName)
queryTmplStr = givePanelQueryStr(key, s.ViewName())
}

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

tmplParams := struct {
Target float64
TimesliceRatioTarget float64
GroupByStr string
GroupByStr string
}{
Target: s.Target(),
TimesliceRatioTarget: s.TimesliceTarget(),
GroupByStr : giveFieldsGroupByStr(s.Fields),
GroupByStr: giveFieldsGroupByStr(s.Fields),
}

queryPart, err := GiveStrFromTmpl(queryTmplStr, tmplParams)
return fmt.Sprintf("_view=%s %s %s", s.ViewName, wherePart, queryPart), err
return fmt.Sprintf("_view=%s %s %s", s.ViewName(), wherePart, queryPart), err
}

func givePanelQueryStr(Key PanelKey, view string) string {
Expand Down
12 changes: 5 additions & 7 deletions libs/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"embed"
"os"
"path/filepath"
"sort"
"text/template"
)

Expand Down Expand Up @@ -40,13 +41,13 @@ type GenConf struct {
IgnoreError bool
Clean bool
AsModule bool
UseViewHash bool
}

func init() {
var err error

tfTemplates, err = template.ParseFS(tmplFiles, "templates/terra/**")

if err != nil {
panic(err)
}
Expand All @@ -58,6 +59,8 @@ const ViewPrefix = "slogen_tf"
func GenTerraform(slos map[string]*SLO, c GenConf) (string, error) {

err := SetupOutDir(c)
useViewID = c.UseViewHash

if err != nil {
BadResult("error setting up path : %s", err)
return "", err
Expand All @@ -66,7 +69,6 @@ func GenTerraform(slos map[string]*SLO, c GenConf) (string, error) {
srvMap := map[string]bool{}

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

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

srvList := GiveKeys(srvMap)
sort.Strings(srvList)

err = GenFoldersTF(srvList, c.OutDir)
if err != nil {
Expand Down Expand Up @@ -124,11 +127,6 @@ func ExecSLOTmpl(tmplName string, slo SLO, outDir string) error {
return nil
}

func ExecServiceOverview(slos map[string]SLO, outDir string) error {

return nil
}

const (
VarNameMonRootFolder = "slo_mon_root_folder_id"
VarNameDashRootFolder = "slo_dash_root_folder_id"
Expand Down
4 changes: 2 additions & 2 deletions libs/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func TFExec(wdPath string, action TFAction) error {

tf, err := tfexec.NewTerraform(wdPath, execPath)
env := map[string]string{
EnvKeyHTTPProxy: os.Getenv(EnvKeyHTTPProxy),
EnvKeyHTTPSProxy: os.Getenv(EnvKeyHTTPSProxy),
EnvKeyHTTPProxy: os.Getenv(EnvKeyHTTPProxy),
EnvKeyHTTPSProxy: os.Getenv(EnvKeyHTTPSProxy),
EnvKeySumoAccessID: os.Getenv(EnvKeySumoAccessID),
EnvKeySumoAccessKey: os.Getenv(EnvKeySumoAccessKey),
EnvKeySumoEnvironment: os.Getenv(EnvKeySumoEnvironment),
Expand Down
20 changes: 12 additions & 8 deletions libs/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ import (
)

const (
BudgetingMethodNameTimeSlices = "Timeslices"
BudgetingMethodNameTimeSlices = "Timeslices"
BudgetingMethodNameOccurrences = "Occurrences"
)

type SLO struct {
*v1alpha.SLO `yaml:",inline"`
Labels map[string]string `yaml:"labels,omitempty"`
Fields map[string]string `yaml:"fields,omitempty"`
Alerts Alerts `yaml:"alerts,omitempty"`
ViewName string `yaml:"viewName"`
BurnRateAlerts []BurnRate `yaml:"burnRateAlerts,omitempty"` // deprecated
}

func (s SLO) Name() string {
return s.Metadata.Name
}

type Alerts struct {
BurnRate []BurnRate `yaml:"burnRate,omitempty"`
}
Expand All @@ -38,19 +42,19 @@ func Parse(filename string) (*SLO, error) {
func (s SLO) Target() float64 {
target := s.Spec.Objectives[0].BudgetTarget

if target == nil {
if target == nil {
return 0.99
}

return *target
return *target
}

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

if target == nil {
return s.Target()
}
if target == nil {
return s.Target()
}

return *target
return *target
}
90 changes: 90 additions & 0 deletions libs/spec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package libs

import (
"gopkg.in/yaml.v3"
"testing"
)

func TestSLO_ViewID(t *testing.T) {

var slo SLO
err := yaml.Unmarshal([]byte(testYaml1), &slo)

if err != nil {
t.Errorf("Error: %v", err)
}

tests := []struct {
name string
s SLO
want string
}{
// TODO: Add test cases.
{
name: "test",
s: slo,
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

if got := tt.s.ViewID(); got != tt.want {
t.Errorf("ViewID() = %v, want %v", got, tt.want)
}
})
}
}

var testYaml1 = `
apiVersion: openslo/v1alpha
kind: SLO
metadata:
displayName: CloudCollector Ingest Lag
name: cc-ingest-lag-v2
spec:
service: cloudcollector
description: Track number of seconds a message is delayed in the ingest pipeline
budgetingMethod: Timeslices
objectives:
- displayName: SLI to track ingest job is completed within 5 seconds for cloudcollector
target: 0.95
timeSliceTarget: 0.9 # ratio of good to total msgs, so as to consider that time window healthy, only applicable for Timeslices budgeting
ratioMetrics:
total:
source: sumologic
queryType: Logs
query: |
_sourcecategory=cloudcollector DefaultPerCustomerLagTracker !CustomerLagQueryDisablingStrategy "current lag"
| parse "current lag: Some(*) ms," as lag
| where lag != "*"
| parse "customer: *," as customer_id
| where customer_id matches "*"
| lag / 1000 as lag_seconds
good:
source: sumologic
queryType: Logs
query: lag_seconds <= 20
incremental: true
createView: true
fields:
customerID: "customer_id"
deployment: 'if(isNull(deployment),"dev",deployment)' # using an expression
cluster: 'if(isNull(cluster),"-",cluster)'
labels:
team: collection
tier: 0
alerts:
burnRate:
- shortWindow: '10m'
shortLimit: 14
longWindow: '1h'
longLimit: 14
notifications:
- connectionType: 'Email'
recipients:
- 'agaurav@sumologic.com'
triggerFor:
- Warning
- ResolvedWarning
`
8 changes: 4 additions & 4 deletions libs/templates/terra/monitor.tf.gotf
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ QUERY
time_range = "{{$Obj.TimeRange}}"
alert {
threshold = "{{$Obj.ValueCritical}}"
threshold_type = "GreaterThan"
threshold_type = "GreaterThanOrEqual"
}
resolution {
threshold = "{{$Obj.ValueCritical}}"
threshold_type = "LessThanOrEqual"
threshold_type = "LessThan"
}
}
warning {
time_range = "{{$Obj.TimeRange}}"
alert {
threshold = "{{$Obj.ValueWarning}}"
threshold_type = "GreaterThan"
threshold_type = "GreaterThanOrEqual"
}
resolution {
threshold = "{{$Obj.ValueWarning}}"
threshold_type = "LessThanOrEqual"
threshold_type = "LessThan"
}
}
}
Expand Down
Loading

0 comments on commit 648a50a

Please sign in to comment.