Skip to content

Commit 964d93b

Browse files
Moving alert to a separate package
1 parent e1fd661 commit 964d93b

File tree

7 files changed

+176
-55
lines changed

7 files changed

+176
-55
lines changed

manifest/v1alpha/alert.go

-50
This file was deleted.

manifest/v1alpha/alert/alert.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package alert
2+
3+
import (
4+
"github.com/nobl9/nobl9-go/manifest"
5+
"github.com/nobl9/nobl9-go/manifest/v1alpha"
6+
)
7+
8+
//go:generate go run ../../../scripts/generate-object-impl.go Alert
9+
10+
// New creates a new Alert based on provided Metadata nad Spec.
11+
func New(metadata Metadata, spec Spec) Alert {
12+
return Alert{
13+
APIVersion: manifest.VersionV1alpha.String(),
14+
Kind: manifest.KindAlert,
15+
Metadata: metadata,
16+
Spec: spec,
17+
}
18+
}
19+
20+
// Alert represents triggered alert
21+
type Alert struct {
22+
APIVersion string `json:"apiVersion"`
23+
Kind manifest.Kind `json:"kind"`
24+
Metadata Metadata `json:"metadata"`
25+
Spec Spec `json:"spec"`
26+
27+
Organization string `json:"organization,omitempty"`
28+
ManifestSource string `json:"manifestSrc,omitempty"`
29+
}
30+
31+
type Metadata struct {
32+
Name string `json:"name" validate:"required,objectName"`
33+
Project string `json:"project,omitempty" validate:"objectName"`
34+
}
35+
36+
// Spec represents content of Alert's Spec
37+
type Spec struct {
38+
AlertPolicy ObjectMetadata `json:"alertPolicy"`
39+
SLO ObjectMetadata `json:"slo"`
40+
Service ObjectMetadata `json:"service"`
41+
Objective Objective `json:"objective"`
42+
Severity string `json:"severity"`
43+
Status string `json:"status"`
44+
TriggeredMetricTime string `json:"triggeredMetricTime"`
45+
TriggeredClockTime string `json:"triggeredClockTime"`
46+
ResolvedClockTime *string `json:"resolvedClockTime,omitempty"`
47+
ResolvedMetricTime *string `json:"resolvedMetricTime,omitempty"`
48+
CoolDown string `json:"coolDown"`
49+
Conditions []Condition `json:"conditions"`
50+
}
51+
52+
type Objective struct {
53+
Value float64 `json:"value"`
54+
Name string `json:"name"`
55+
DisplayName string `json:"displayName"`
56+
}
57+
58+
type ObjectMetadata struct {
59+
Name string `json:"name"`
60+
DisplayName string `json:"displayName,omitempty"`
61+
Project string `json:"project,omitempty"`
62+
Labels v1alpha.Labels `json:"labels,omitempty"`
63+
}
64+
65+
type Condition struct {
66+
Measurement string `json:"measurement"`
67+
Value interface{} `json:"value"`
68+
AlertingWindow string `json:"alertingWindow,omitempty"`
69+
LastsForDuration string `json:"lastsFor,omitempty"`
70+
Operator string `json:"op,omitempty"`
71+
}

manifest/v1alpha/alert_object.go manifest/v1alpha/alert/alert_object.go

+7-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manifest/v1alpha/alert/doc.go

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package alert defines Alert object definitions.
2+
package alert

manifest/v1alpha/alert/example.yaml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: n9/v1alpha
2+
kind: Alert
3+
metadata:
4+
name: 6fbc76bc-ff8a-40a2-8ac6-65d7d7a2686e
5+
project: alerting-test
6+
spec:
7+
alertPolicy:
8+
name: burn-rate-is-4x-immediately
9+
project: alerting-test
10+
service:
11+
name: triggering-alerts-service
12+
project: alerting-test
13+
objective:
14+
value: 99.0
15+
name: ok
16+
displayName: ok
17+
severity: Medium
18+
slo:
19+
name: prometheus-rolling-timeslices-threshold
20+
project: alerting-test
21+
status: Triggered
22+
triggeredClockTime: "2022-01-16T00:28:05Z
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package alert_test
2+
3+
import (
4+
"context"
5+
"log"
6+
7+
"github.com/nobl9/nobl9-go/internal/examples"
8+
"github.com/nobl9/nobl9-go/manifest"
9+
"github.com/nobl9/nobl9-go/manifest/v1alpha/alert"
10+
)
11+
12+
func ExampleAlert() {
13+
alertInstance := alert.New(
14+
alert.Metadata{
15+
Name: "my-alert",
16+
Project: "default",
17+
},
18+
alert.Spec{
19+
AlertPolicy: alert.ObjectMetadata{
20+
Name: "burn-rate-is-4x-immediately",
21+
Project: "alerting-test",
22+
},
23+
Service: alert.ObjectMetadata{
24+
Name: "triggering-alerts-service",
25+
Project: "alerting-test",
26+
},
27+
SLO: alert.ObjectMetadata{
28+
Name: "prometheus-rolling-timeslices-threshold",
29+
Project: "alerting-test",
30+
},
31+
Objective: alert.Objective{
32+
Name: "ok",
33+
DisplayName: "ok",
34+
Value: 99,
35+
},
36+
Severity: "Medium",
37+
Status: "Triggered",
38+
TriggeredClockTime: "2022-01-16T00:28:05Z",
39+
},
40+
)
41+
// Apply the object:
42+
client := examples.GetOfflineEchoClient()
43+
if err := client.ApplyObjects(context.Background(), []manifest.Object{alertInstance}); err != nil {
44+
log.Fatal("failed to apply alert err: %w", err)
45+
}
46+
// Output:
47+
// apiVersion: n9/v1alpha
48+
// kind: Alert
49+
// metadata:
50+
// name: my-alert
51+
// project: default
52+
// spec:
53+
// alertPolicy:
54+
// name: burn-rate-is-4x-immediately
55+
// project: alerting-test
56+
// slo:
57+
// name: prometheus-rolling-timeslices-threshold
58+
// project: alerting-test
59+
// service:
60+
// name: triggering-alerts-service
61+
// project: alerting-test
62+
// objective:
63+
// value: 99.0
64+
// name: ok
65+
// displayName: ok
66+
// severity: Medium
67+
// status: Triggered
68+
// triggeredMetricTime: ""
69+
// triggeredClockTime: 2022-01-16T00:28:05Z
70+
// coolDown: ""
71+
// conditions: []
72+
}

manifest/v1alpha/parser/parser.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/nobl9/nobl9-go/manifest"
1212
"github.com/nobl9/nobl9-go/manifest/v1alpha"
13+
"github.com/nobl9/nobl9-go/manifest/v1alpha/alert"
1314
"github.com/nobl9/nobl9-go/manifest/v1alpha/annotation"
1415
"github.com/nobl9/nobl9-go/manifest/v1alpha/dataexport"
1516
"github.com/nobl9/nobl9-go/manifest/v1alpha/project"
@@ -61,7 +62,7 @@ func parseObject(kind manifest.Kind, unmarshal unmarshalFunc) (manifest.Object,
6162
case manifest.KindDirect:
6263
return genericParseObject[v1alpha.Direct](unmarshal)
6364
case manifest.KindAlert:
64-
return genericParseObject[v1alpha.Alert](unmarshal)
65+
return genericParseObject[alert.Alert](unmarshal)
6566
case manifest.KindAlertMethod:
6667
return genericParseObject[v1alpha.AlertMethod](unmarshal)
6768
case manifest.KindAlertPolicy:

0 commit comments

Comments
 (0)