Skip to content

Commit

Permalink
monitor: add definition field (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
vikramraman authored Nov 10, 2023
1 parent bd14376 commit afb538e
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 19 deletions.
1 change: 1 addition & 0 deletions client/internal/meta/operation/monitor.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fragment Monitor on Monitor {
freshnessGoal
useDefaultFreshness
source
definition
managedById
query {
outputStage
Expand Down
46 changes: 39 additions & 7 deletions client/internal/meta/schema/monitors.graphql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extend type Query {
monitorAction(id: ObjectId!): MonitorAction
getChannelAction(id: ObjectId!): ChannelAction @deprecated(reason:"use monitorAction instead")
searchMonitorActions(workspaceId: ObjectId, name: String, channelIds: [ObjectId!], ids:[ObjectId!]): [MonitorAction!]!
searchChannelActions(workspaceId: ObjectId, name: String, channelIds: [ObjectId!], ids:[ObjectId!]): [ChannelAction!]! @deprecated(reason:"use searchMonitorActions instead")
searchMonitorActions(workspaceId: ObjectId, name: String, channelIds: [ObjectId!], ids:[ObjectId!], isPrivate: Boolean): [MonitorAction!]!
searchChannelActions(workspaceId: ObjectId, name: String, channelIds: [ObjectId!], ids:[ObjectId!], isPrivate: Boolean): [ChannelAction!]! @deprecated(reason:"use searchMonitorActions instead")
getChannel(id: ObjectId!): Channel
searchChannels(workspaceId: ObjectId, name: String, boundToMonitors: [ObjectId!]): [Channel!]!

Expand All @@ -22,9 +22,20 @@ extend type Query {
Version is a timestamp in nanoseconds.
"""
notificationQuery(monitorId: ObjectId!, version: String!): MultiStageQuery
"""
See OBS-23646. This is a helper to retreive the query information needed to get notifications for a monitor that
may or may not be historical against previous definitions of the monitor. This is added to handle the
acquisition of not just the preview query, but the start/end times and any other query params the UI
needs to make a preview query.
"""
notificationQueryPreview(monitorId: ObjectId!, version: String!, start: Time!, groupings: JsonObject!): NotificationPreviewQuery
"""
Build the template payload data for an existing monitor by retrieving the latest notification.
"""
notificationSampleDataQuery(monitorId: ObjectId!): MonitorSampleData

"""
Get sample notification data for a monitor.
Get sample notification data for a monitor with the provided monitor input.
"""
monitorPreviewSampleDataQuery(workspaceId: ObjectId, monitorId: ObjectId, monitorInput: MonitorInput!, queryParams: QueryParams!): MonitorSampleData

Expand Down Expand Up @@ -57,7 +68,7 @@ extend type Mutation {
deleteChannel(id: ObjectId!): ResultStatus!

createMonitor(workspaceId: ObjectId!, monitor: MonitorInput!): MonitorUpdateResult!
createMonitorWithActions(workspaceId: ObjectId!, monitor: MonitorInput!, newActions:[ActionInput!]!, addActionsToMonitorIds:[ObjectId!]!): MonitorUpdateResult!
createMonitorWithActions(workspaceId: ObjectId!, monitor: MonitorInput!, newActions:[ActionInput!], addActionsToMonitorIds:[ObjectId!]): MonitorUpdateResult!
updateMonitor(id: ObjectId!, monitor: MonitorInput!): MonitorUpdateResult!
"""
Update the monitor.
Expand Down Expand Up @@ -125,6 +136,7 @@ type Monitor implements WorkspaceObject & AuditedObject @goModel(model: "observe
or it may include actual transform work
"""
query: MultiStageQuery!
definition: JsonObject
rule: MonitorRule!
notificationSpec: NotificationSpecification!
channels: [Channel!] @deprecated(reason:"field will be removed in favor of actionIds")
Expand Down Expand Up @@ -153,6 +165,11 @@ type Monitor implements WorkspaceObject & AuditedObject @goModel(model: "observe
statusInfo: MonitorStatusInfo! @goField(forceResolver:true) @deprecated(reason:"use activeMonitorInfo.statusInfo instead")
}

type NotificationInfo @goModel(model: "observe/meta/metatypes.NotificationInfo") {
lookbackTime: Duration!
count: Int64!
}

type ActiveMonitorInfo implements AccelerableObject @goModel(model: "observe/meta/metatypes.ActiveMonitorInfo") {
# AccelerableObject
accelerationInfo: AccelerationInfo! @goField(forceResolver:true)
Expand All @@ -168,6 +185,10 @@ type ActiveMonitorInfo implements AccelerableObject @goModel(model: "observe/met
Currently active global and per-monitor mute rules, sorted by the startDate.
"""
muteRules: [MonitorMuteRule!]! @goField(forceResolver:true)
"""
Gives back the default lookbackTime of 24 hours and the exact notification count for the current version of monitor,
"""
notificationInfo: NotificationInfo! @goField(forceResolver:true)
}

type MonitorStatusInfoError implements ErrorInstance @goModel(model: "observe/meta/metatypes.MonitorStatusInfoError") {
Expand Down Expand Up @@ -229,6 +250,7 @@ input MonitorInput @goModel(model: "observe/meta/metatypes.MonitorInput") {
"""
useDefaultFreshness: Boolean
query: MultiStageQueryInput
definition: JsonObject
rule: MonitorRuleInput
notificationSpec: NotificationSpecificationInput
channels: [ObjectId!]
Expand Down Expand Up @@ -362,18 +384,18 @@ type MonitorRuleLog implements MonitorRule @goModel(model: "observe/meta/metatyp
compareFunction: CompareFunction!
compareValues: [Number!]!
lookbackTime: Duration!
thresholdAggFunction: ThresholdAggFunction! @deprecated(reason:"log monitors no longer have an aggregation function")
expressionSummary: String!
logStageId: String!
sourceLogDatasetId: ObjectId
}

input MonitorRuleLogInput @goModel(model: "observe/meta/metatypes.MonitorRuleLogInput") {
compareFunction: CompareFunction
compareValues: [Number!]
lookbackTime: Duration
thresholdAggFunction: ThresholdAggFunction @deprecated(reason:"log monitors no longer have an aggregation function")
expressionSummary: String
logStageId: String
sourceLogDatasetId: ObjectId
}

type MonitorRuleChange implements MonitorRule @goModel(model: "observe/meta/metatypes.MonitorRuleChange") {
Expand Down Expand Up @@ -953,5 +975,15 @@ type MustacheRender @goModel(model: "observe/meta/metatypes.MustacheRender") {
}

type MonitorSampleData @goModel(model: "observe/meta/metatypes.MonitorSampleData") {
payload: String!
payload: JsonObject!
}

type NotificationPreviewQueryParams @goModel(model: "observe/meta/metatypes.NotificationPreviewQueryParams") {
startTime: Time!
endTime: Time!
}

type NotificationPreviewQuery @goModel(model: "observe/meta/metatypes.NotificationPreviewQuery") {
params: NotificationPreviewQueryParams!
query: MultiStageQuery!
}
35 changes: 24 additions & 11 deletions client/meta/genqlient.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/data-sources/monitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ One of `name` or `id` must be set. If `name` is provided, `workspace` must be se
### Read-Only

- `comment` (String) A long-form comment describing the content of the monitor.
- `definition` (String) Monitor definition in JSON format.
- `description` (String) A brief description of the monitor.
- `disabled` (Boolean) Set to `true` to disable monitor.
- `icon_url` (String) Icon to be displayed for this object. Icons are sourced from the [fluency-filled](https://icons8.com/icons/fluency-systems-filled) icon set.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/monitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ its predecessor. (see [below for nested schema](#nestedblock--stage))
### Optional

- `comment` (String) A long-form comment describing the content of the monitor.
- `definition` (String) Monitor definition in JSON format.
- `description` (String) A brief description of the monitor.
- `disabled` (Boolean) Set to `true` to disable monitor.
- `freshness` (String) Target freshness for results. Reducing the freshness will increase the
Expand Down
5 changes: 5 additions & 0 deletions observe/data_source_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func dataSourceMonitor() *schema.Resource {
Computed: true,
Description: descriptions.Get("transform", "schema", "inputs"),
},
"definition": {
Type: schema.TypeString,
Computed: true,
Description: descriptions.Get("monitor", "schema", "definition"),
},
"stage": {
Type: schema.TypeList,
Computed: true,
Expand Down
2 changes: 2 additions & 0 deletions observe/data_source_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestAccObserveSourceMonitor(t *testing.T) {
description = "description"
comment = "comment"
is_template = true
definition = jsonencode({ "hello" = "world" })
inputs = {
"test" = observe_datastream.test.dataset
Expand Down Expand Up @@ -56,6 +57,7 @@ func TestAccObserveSourceMonitor(t *testing.T) {
resource.TestCheckResourceAttr("data.observe_monitor.lookup", "disabled", "true"),
resource.TestCheckResourceAttr("data.observe_monitor.lookup", "is_template", "true"),
resource.TestCheckResourceAttr("data.observe_monitor.lookup", "description", "description"),
resource.TestCheckResourceAttr("data.observe_monitor.lookup", "definition", `{"hello":"world"}`),
resource.TestCheckResourceAttr("data.observe_monitor.lookup", "comment", "comment"),
resource.TestCheckResourceAttr("data.observe_monitor.lookup", "stage.0.pipeline", ""),
),
Expand Down
4 changes: 3 additions & 1 deletion observe/descriptions/monitor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ schema:
This resource is template for creating new monitors
disabled: |
Set to `true` to disable monitor.
definition: |
Monitor definition in JSON format.
notification_spec:
reminder_frequency: |
How often to send reminders when a monitor notification is triggered.
To disable reminder notifications, omit this attribute.
notify_on_reminder:
Whether notification reminders are enabled for this monitor. To enable them, set `reminder_frequency`.
notify_on_close: |
Enables a final update when a monitor notification is closed (no longer triggered).
Enables a final update when a monitor notification is closed (no longer triggered).
18 changes: 18 additions & 0 deletions observe/resource_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ func resourceMonitor() *schema.Resource {
Optional: true,
Description: descriptions.Get("monitor", "schema", "disabled"),
},
"definition": {
Type: schema.TypeString,
Default: "{}",
Optional: true,
ValidateDiagFunc: validateStringIsJSON,
DiffSuppressFunc: diffSuppressJSON,
Description: descriptions.Get("monitor", "schema", "definition"),
},
"stage": {
Type: schema.TypeList,
MinItems: 1,
Expand Down Expand Up @@ -636,6 +644,10 @@ func newMonitorConfig(data *schema.ResourceData) (input *gql.MonitorInput, diags
input.Comment = stringPtr(v.(string))
}

if v, ok := data.GetOk("definition"); ok {
input.Definition = types.JsonObject(v.(string)).Ptr()
}

return
}

Expand Down Expand Up @@ -729,6 +741,12 @@ func resourceMonitorRead(ctx context.Context, data *schema.ResourceData, meta in
diags = append(diags, diag.FromErr(err)...)
}

if monitor.Definition != nil {
if err := data.Set("definition", monitor.Definition); err != nil {
diags = append(diags, diag.FromErr(err)...)
}
}

if err := data.Set("oid", monitor.Oid().String()); err != nil {
diags = append(diags, diag.FromErr(err)...)
}
Expand Down
3 changes: 3 additions & 0 deletions observe/resource_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func TestAccObserveMonitor(t *testing.T) {
"test" = observe_datastream.test.dataset
}
definition = jsonencode({ "hello" = "world" })
stage {}
rule {
Expand All @@ -45,6 +47,7 @@ func TestAccObserveMonitor(t *testing.T) {
resource.TestCheckResourceAttr("observe_monitor.first", "name", randomPrefix),
resource.TestCheckResourceAttr("observe_monitor.first", "freshness", "4m0s"),
resource.TestCheckResourceAttr("observe_monitor.first", "comment", "a descriptive comment"),
resource.TestCheckResourceAttr("observe_monitor.first", "definition", `{"hello":"world"}`),
resource.TestCheckResourceAttrSet("observe_monitor.first", "inputs.test"),
resource.TestCheckResourceAttr("observe_monitor.first", "stage.0.pipeline", ""),
resource.TestCheckResourceAttr("observe_monitor.first", "rule.0.count.0.compare_function", "less_or_equal"),
Expand Down

0 comments on commit afb538e

Please sign in to comment.