Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add additional notification policy logic #12

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .spacelift/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
version: 1
module_version: 0.2.4
module_version: 0.2.5

tests:
- name: Default test
Expand Down
24 changes: 15 additions & 9 deletions assets/policy.rego
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ webhook[{"endpoint_id": endpoint_id, "payload": payload}] {
# Only send the webhook if the run reached a terminal state.
run_state == terminal[_]

payload := {"series": array.concat(
series_data := array.concat(
[
run_count(endpoint.labels),
resources("added", endpoint.labels),
Expand All @@ -34,7 +34,11 @@ webhook[{"endpoint_id": endpoint_id, "payload": payload}] {
policies(endpoint.labels),
state_timings(endpoint.labels),
),
)}
)

# Only send the webhook if the the payload is non-empty
count(series_data) > 0
payload := {"series": series_data}
}

# Metric definition for spacelift.integration.run.count.
Expand All @@ -61,9 +65,9 @@ resources(type, extra_tags) = {
"points": [{
"timestamp": timestamp,
"value": count([change |
change := input.run_updated.run.changes[_]
change.phase == "plan"
contains(change.action, type)
change := input.run_updated.run.changes[_]
change.phase == "plan"
contains(change.action, type)
]),
}],
"tags": array.concat(tags(extra_tags), [sprintf("change_type:%s", [type])]),
Expand Down Expand Up @@ -95,7 +99,7 @@ policies(extra_tags) = [metric |
#
# It is the duration of each phase of the run, broken down by the standard tags
# and the phase name.
#
#
# State timings reported by this webhook will be assigned to the time when the
# run is last updated (i.e. when it reaches the terminal state), not when each
# of the respective phases actually took place. In most cases this should be
Expand All @@ -112,7 +116,7 @@ state_timings(extra_tags) = [metric |
"tags": array.concat(tags(extra_tags), [sprintf("state:%s", [lower(state_timing.state)])]),
"unit": "nanosecond",
}
]
]

tags(extra_tags) = array.concat([tag | tag := extra_tags[_]; contains(tag, ":")], [
sprintf("account:%s", [input.account.name]),
Expand All @@ -125,7 +129,7 @@ tags(extra_tags) = array.concat([tag | tag := extra_tags[_]; contains(tag, ":")]
sprintf("space:%s", [lower(input.run_updated.stack.space.id)]),
sprintf("stack:%s", [lower(input.run_updated.stack.id)]),
sprintf("triggered_by:%s", [input.run_updated.run.triggered_by]),
sprintf("worker_pool:%s", [worker_pool]),
sprintf("worker_pool:%s", [worker_pool]),
])

default worker_pool = "public"
Expand All @@ -136,4 +140,6 @@ worker_pool = name {

# Only sample the webhook if the run reached a terminal state, and some metrics
# have been collected.
sample { run_state == terminal[_] }
sample {
run_state == terminal[_]
}