Skip to content

Commit

Permalink
Merge pull request #4 from logzio/dev
Browse files Browse the repository at this point in the history
v1.0.2
  • Loading branch information
yotamloe authored Nov 26, 2023
2 parents f436985 + 000f9ef commit e3ffc47
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
IMAGE_NAME := prometheus-alerts-migrator
IMAGE_TAG ?= v1.0.1
IMAGE_TAG ?= v1.0.2
DOCKER_REPO := logzio/$(IMAGE_NAME):$(IMAGE_TAG)


Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ data:
- Deploy the configmap to your cluster `kubectl apply -f <configmap-file>.yml`

## Changelog
- v1.0.2
- Add `reduce` query to alerts (grafana alerts can evaluate alerts only from reduced data)
- v1.0.1
- Update `logzio_terraform_client`: `1.18.0` -> `1.19.0`
- Use data source uid instead of name
Expand Down
75 changes: 62 additions & 13 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,40 @@ const (
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
randomStringLength = 5
refId = "A"
refIdA = "A"
refIdB = "B"
expressionString = "__expr__"
queryType = "query"
)

// PrometheusQuery represents a Prometheus query.
type PrometheusQuery struct {
// ReduceQueryModel represents a reduce query for time series data
type ReduceQueryModel struct {
DataSource map[string]string `json:"datasource"`
Expression string `json:"expression"`
Hide bool `json:"hide"`
RefId string `json:"refId"`
Reducer string `json:"reducer"`
Type string `json:"type"`
}

// ToJSON marshals the Query model into a JSON byte slice
func (r ReduceQueryModel) ToJSON() (json.RawMessage, error) {
marshaled, err := json.Marshal(r)
if err != nil {
return nil, err
}
return marshaled, nil
}

// PrometheusQueryModel represents a Prometheus query.
type PrometheusQueryModel struct {
Expr string `json:"expr"`
Hide bool `json:"hide"`
RefId string `json:"refId"`
}

// ToJSON marshals the Query into a JSON byte slice
func (p PrometheusQuery) ToJSON() (json.RawMessage, error) {
func (p PrometheusQueryModel) ToJSON() (json.RawMessage, error) {
marshaled, err := json.Marshal(p)
if err != nil {
return nil, err
Expand Down Expand Up @@ -381,35 +402,63 @@ func (c *Controller) writeRules(rulesToWrite []rulefmt.RuleNode, folderUid strin

// generateGrafanaAlert generates a GrafanaAlertRule from a Prometheus rule
func (c *Controller) generateGrafanaAlert(rule rulefmt.RuleNode, folderUid string) (grafana_alerts.GrafanaAlertRule, error) {
// Create an instance of the Prometheus query.
query := PrometheusQuery{
// Create promql query to return time series data for the expression.
promqlQuery := PrometheusQueryModel{
Expr: rule.Expr.Value,
Hide: false,
RefId: refId,
RefId: refIdA,
}
// Use the ToJSON method to marshal the Query struct.
model, err := query.ToJSON()
promqlModel, err := promqlQuery.ToJSON()
if err != nil {
return grafana_alerts.GrafanaAlertRule{}, err
}
data := grafana_alerts.GrafanaAlertQuery{
queryA := grafana_alerts.GrafanaAlertQuery{
DatasourceUid: c.rulesDataSource,
Model: model,
RefId: refId,
Model: promqlModel,
RefId: refIdA,
QueryType: queryType,
RelativeTimeRange: grafana_alerts.RelativeTimeRangeObj{
From: 300,
To: 0,
},
}
// Create reduce query to return the reduced last value of the time series data.
reduceQuery := ReduceQueryModel{
DataSource: map[string]string{
"type": expressionString,
"uid": expressionString,
},
Expression: refIdA,
Hide: false,
RefId: refIdB,
Reducer: "last",
Type: "reduce",
}
reduceModel, err := reduceQuery.ToJSON()
if err != nil {
return grafana_alerts.GrafanaAlertRule{}, err
}
queryB := grafana_alerts.GrafanaAlertQuery{
DatasourceUid: expressionString,
Model: reduceModel,
RefId: refIdB,
QueryType: "",
RelativeTimeRange: grafana_alerts.RelativeTimeRangeObj{
From: 300,
To: 0,
},
}
duration, err := parseDuration(rule.For.String())
if err != nil {
return grafana_alerts.GrafanaAlertRule{}, err
}

// Create the GrafanaAlertRule, we are alerting on the reduced last value of the time series data (query b).
grafanaAlert := grafana_alerts.GrafanaAlertRule{
Annotations: rule.Annotations,
Condition: refId,
Data: []*grafana_alerts.GrafanaAlertQuery{&data},
Condition: refIdB,
Data: []*grafana_alerts.GrafanaAlertQuery{&queryA, &queryB},
FolderUID: folderUid,
NoDataState: grafana_alerts.NoDataOk,
ExecErrState: grafana_alerts.ErrOK,
Expand Down

0 comments on commit e3ffc47

Please sign in to comment.