From dcda8103cbc3a2b7e0891c64215df3b80c3e05a8 Mon Sep 17 00:00:00 2001 From: Raj Parekh Date: Tue, 22 Oct 2024 15:09:53 +0200 Subject: [PATCH] Add API implementation for Infra Alert Config --- instana/restapi/Instana-api.go | 5 +++++ instana/restapi/aggregation.go | 7 +++++- instana/restapi/aggregation_test.go | 3 ++- instana/restapi/infra-alert-configs.go | 29 +++++++++++++++++++++++++ instana/restapi/infra-alert-rule.go | 10 +++++++++ instana/restapi/infra-time-threshold.go | 6 +++++ instana/restapi/rule-with-threshold.go | 18 +++++++++++++++ instana/restapi/threshold.go | 8 +++++++ 8 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 instana/restapi/infra-alert-configs.go create mode 100644 instana/restapi/infra-alert-rule.go create mode 100644 instana/restapi/infra-time-threshold.go create mode 100644 instana/restapi/rule-with-threshold.go diff --git a/instana/restapi/Instana-api.go b/instana/restapi/Instana-api.go index 5242723..04db3ff 100644 --- a/instana/restapi/Instana-api.go +++ b/instana/restapi/Instana-api.go @@ -36,6 +36,7 @@ type InstanaAPI interface { SliConfigs() RestResource[*SliConfig] WebsiteMonitoringConfig() RestResource[*WebsiteMonitoringConfig] WebsiteAlertConfig() RestResource[*WebsiteAlertConfig] + InfraAlertConfig() RestResource[*InfraAlertConfig] Groups() RestResource[*Group] CustomDashboards() RestResource[*CustomDashboard] SyntheticTest() RestResource[*SyntheticTest] @@ -104,6 +105,10 @@ func (api *baseInstanaAPI) WebsiteAlertConfig() RestResource[*WebsiteAlertConfig return NewCreatePOSTUpdatePOSTRestResource(WebsiteAlertConfigResourcePath, NewCustomPayloadFieldsUnmarshallerAdapter(NewDefaultJSONUnmarshaller(&WebsiteAlertConfig{})), api.client) } +func (api *baseInstanaAPI) InfraAlertConfig() RestResource[*InfraAlertConfig] { + return NewCreatePOSTUpdatePOSTRestResource(InfraAlertConfigResourcePath, NewCustomPayloadFieldsUnmarshallerAdapter(NewDefaultJSONUnmarshaller(&InfraAlertConfig{})), api.client) +} + func (api *baseInstanaAPI) Groups() RestResource[*Group] { return NewCreatePOSTUpdatePUTRestResource(GroupsResourcePath, NewDefaultJSONUnmarshaller(&Group{}), api.client) } diff --git a/instana/restapi/aggregation.go b/instana/restapi/aggregation.go index 1304873..28e96c8 100644 --- a/instana/restapi/aggregation.go +++ b/instana/restapi/aggregation.go @@ -48,7 +48,12 @@ const ( DistinctCountAggregation = Aggregation("DISTINCT_COUNT") //SumPositiveAggregation constant value for the sum positive aggregation type SumPositiveAggregation = Aggregation("SUM_POSITIVE") + PerSecondAggregation = Aggregation("PER_SECOND") + IncreaseAggregation = Aggregation("INCREASE") ) // SupportedAggregations list of all supported Aggregation -var SupportedAggregations = Aggregations{SumAggregation, MeanAggregation, MaxAggregation, MinAggregation, Percentile25Aggregation, Percentile50Aggregation, Percentile75Aggregation, Percentile90Aggregation, Percentile95Aggregation, Percentile98Aggregation, Percentile99Aggregation, Percentile99_9Aggregation, Percentile99_99Aggregation, DistributionAggregation, DistinctCountAggregation, SumPositiveAggregation} +var SupportedAggregations = Aggregations{SumAggregation, MeanAggregation, MaxAggregation, MinAggregation, Percentile25Aggregation, + Percentile50Aggregation, Percentile75Aggregation, Percentile90Aggregation, Percentile95Aggregation, Percentile98Aggregation, + Percentile99Aggregation, Percentile99_9Aggregation, Percentile99_99Aggregation, DistributionAggregation, + DistinctCountAggregation, SumPositiveAggregation, PerSecondAggregation, IncreaseAggregation} diff --git a/instana/restapi/aggregation_test.go b/instana/restapi/aggregation_test.go index 7808d4d..aa6a590 100644 --- a/instana/restapi/aggregation_test.go +++ b/instana/restapi/aggregation_test.go @@ -8,6 +8,7 @@ import ( ) func TestShouldReturnSupportedAggregationsAsStringSlice(t *testing.T) { - expected := []string{"SUM", "MEAN", "MAX", "MIN", "P25", "P50", "P75", "P90", "P95", "P98", "P99", "P99_9", "P99_99", "DISTRIBUTION", "DISTINCT_COUNT", "SUM_POSITIVE"} + expected := []string{"SUM", "MEAN", "MAX", "MIN", "P25", "P50", "P75", "P90", "P95", "P98", "P99", "P99_9", "P99_99", + "DISTRIBUTION", "DISTINCT_COUNT", "SUM_POSITIVE", "PER_SECOND", "INCREASE"} require.Equal(t, expected, SupportedAggregations.ToStringSlice()) } diff --git a/instana/restapi/infra-alert-configs.go b/instana/restapi/infra-alert-configs.go new file mode 100644 index 0000000..9fbb5aa --- /dev/null +++ b/instana/restapi/infra-alert-configs.go @@ -0,0 +1,29 @@ +package restapi + +const InfraAlertConfigResourcePath = EventSettingsBasePath + "/infra-alert-configs" + +type InfraAlertConfig struct { + ID string `json:"id"` + Name string `json:"name"` + Description string `json:"description"` + TagFilterExpression *TagFilter `json:"tagFilterExpression"` + GroupBy []string `json:"groupBy"` + AlertChannelIDs []string `json:"alertChannelIds"` + Granularity Granularity `json:"granularity"` + TimeThreshold InfraTimeThreshold `json:"timeThreshold"` + CustomerPayloadFields []CustomPayloadField[any] `json:"customPayloadFields"` + Rules []RuleWithThreshold[InfraAlertRule] `json:"rules"` +} + +func (config *InfraAlertConfig) GetIDForResourcePath() string { + //config.Granularity == + return config.ID +} + +func (config *InfraAlertConfig) GetCustomerPayloadFields() []CustomPayloadField[any] { + return config.CustomerPayloadFields +} + +func (config *InfraAlertConfig) SetCustomerPayloadFields(fields []CustomPayloadField[any]) { + config.CustomerPayloadFields = fields +} diff --git a/instana/restapi/infra-alert-rule.go b/instana/restapi/infra-alert-rule.go new file mode 100644 index 0000000..1dea728 --- /dev/null +++ b/instana/restapi/infra-alert-rule.go @@ -0,0 +1,10 @@ +package restapi + +type InfraAlertRule struct { + AlertType string `json:"alertType"` + MetricName string `json:"metricName"` + EntityType string `json:"entityType"` + Aggregation *Aggregation `json:"aggregation"` + CrossSeriesAggregation *Aggregation `json:"crossSeriesAggregation"` + Regex bool `json:"regex"` +} diff --git a/instana/restapi/infra-time-threshold.go b/instana/restapi/infra-time-threshold.go new file mode 100644 index 0000000..f5bcea5 --- /dev/null +++ b/instana/restapi/infra-time-threshold.go @@ -0,0 +1,6 @@ +package restapi + +type InfraTimeThreshold struct { + Type string `json:"type"` + TimeWindow *int64 `json:"timeWindow"` +} diff --git a/instana/restapi/rule-with-threshold.go b/instana/restapi/rule-with-threshold.go new file mode 100644 index 0000000..74d3d70 --- /dev/null +++ b/instana/restapi/rule-with-threshold.go @@ -0,0 +1,18 @@ +package restapi + +type RuleWithThreshold[R InfraAlertRule | ApplicationAlertRule | WebsiteAlertRule] struct { + ThresholdOperator ThresholdOperator `json:"thresholdOperator"` + Rule R `json:"rule"` + Thresholds map[AlertSeverity]ThresholdRule `json:"thresholds"` +} + +type AlertSeverity string +type AlertSeverities []AlertSeverity + +const ( + WarningSeverity = AlertSeverity("WARNING") + CriticalSeverity = AlertSeverity("CRITICAL") +) + +// SupportedAlertSeverities : will be used as part of validation in a follow-up. +var SupportedAlertSeverities = AlertSeverities{WarningSeverity, CriticalSeverity} diff --git a/instana/restapi/threshold.go b/instana/restapi/threshold.go index eb46040..c20e245 100644 --- a/instana/restapi/threshold.go +++ b/instana/restapi/threshold.go @@ -72,3 +72,11 @@ type TimeThreshold struct { Requests *int32 `json:"requests"` Violations *int32 `json:"violations"` } + +type ThresholdRule struct { + Type string `json:"type"` + Value *float64 `json:"value"` + Seasonality *ThresholdSeasonality `json:"seasonality"` + Baseline *[][]float64 `json:"baseline"` + DeviationFactor *float32 `json:"deviationFactor"` +}