Skip to content

Commit

Permalink
fix(api): bigquery (#2885)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahargl authored Dec 23, 2024
1 parent baaf259 commit ec8db0e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 62 deletions.
62 changes: 4 additions & 58 deletions examples/workflows/bigquery.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
alert:
workflow:
id: bq-sql-query
triggers:
- type: manual
description: Monitor that time difference is no more than 1 hour
steps:
- name: get-max-datetime
Expand All @@ -12,63 +14,7 @@ alert:
- name: runbook-step1-bigquery-sql
provider:
type: bigquery
config: "{{ providers.bigquery-prod }}"
config: "{{ providers.bigquery }}"
with:
# Get max(datetime) from the random table
query: "SELECT * FROM `bigquery-public-data.austin_bikeshare.bikeshare_stations` LIMIT 10"
actions:
- name: opsgenie-alert
condition:
- name: threshold-condition
type: threshold
# datetime_compare(t1, t2) compares t1-t2 and returns the diff in hours
# utcnow() returns the local machine datetime in UTC
# to_utc() converts a datetime to UTC
value: keep.datetime_compare(keep.utcnow(), keep.to_utc("{{ steps.get-max-datetime.results[0][date] }}"))
compare_to: 1 # hours
compare_type: gt # greater than
# Give it an alias so we can use it in the slack action
alias: A
provider:
type: opsgenie
config: " {{ providers.opsgenie-prod }} "
with:
message: "DB datetime value ({{ actions.opsgenie-alert.conditions.threshold-condition.0.compare_value }}) is greater than 1! 🚨"
- name: trigger-slack
if: "{{ A }}"
provider:
type: slack
config: " {{ providers.slack-prod }} "
with:
message: "DB datetime value ({{ actions.opsgenie-alert.conditions.threshold-condition.0.compare_value }}) is greater than 1! 🚨"
- name: trigger-slack-2
if: "{{ A }}"
provider:
type: slack
config: " {{ providers.slack-prod }} "
with:
blocks:
- type: header
text:
type: plain_text
text: "Adding some context to the alert:"
emoji: true
- type: section
text:
type: mrkdwn
text: |-
{{#steps.runbook-step1-bigquery-sql.results}}
- Station id: {{station_id}} | Status: {{status}}
{{/steps.runbook-step1-bigquery-sql.results}}
providers:
bigquery-prod:
description: BigQuery Prod
authentication:
opsgenie-prod:
authentication:
api_key: "{{ env.OPSGENIE_API_KEY }}"
slack-prod:
authentication:
webhook_url: "{{ env.SLACKDEMO_WEBHOOK }}"
18 changes: 15 additions & 3 deletions keep/providers/bigquery_provider/bigquery_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import dataclasses
import json
import os
from typing import Optional

Expand Down Expand Up @@ -83,9 +84,20 @@ def validate_config(self):

def init_client(self):
if self.authentication_config.service_account_json:
self.client = bigquery.Client.from_service_account_json(
self.authentication_config.service_account_json
)
# this is the content of the service account json
if isinstance(self.authentication_config.service_account_json, dict):
self.client = bigquery.Client.from_service_account_info(
self.authentication_config.service_account_json
)
elif isinstance(self.authentication_config.service_account_json, str):
self.client = bigquery.Client.from_service_account_info(
json.loads(self.authentication_config.service_account_json)
)
# file? should never happen?
else:
self.client = bigquery.Client.from_service_account_json(
self.authentication_config.service_account_json
)
else:
self.client = bigquery.Client()
# check if the project id was set in the environment and use it if exists
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "keep"
version = "0.32.5"
version = "0.32.6"
description = "Alerting. for developers, by developers."
authors = ["Keep Alerting LTD"]
packages = [{include = "keep"}]
Expand Down

0 comments on commit ec8db0e

Please sign in to comment.