Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ $ curl https://api.uc.gateway.dev/home
#### Infrastructure
* vpc connector
* redis
* alerts
* api gateway
* cloudtaskqueue
* pubsub topics
Expand All @@ -103,6 +102,11 @@ $ curl https://api.uc.gateway.dev/home
* cloudtask target
* uptime checks

#### Alerts
* Backend Alerts
* Uptime Alerts
* PubSub DLQ Alerts

## Data Typing Frameworks Supported

* pydantic
Expand Down
116 changes: 116 additions & 0 deletions docs/source/alerts.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
.. _alerts:

======
Alerts
======

You can deploy alerts related to your application in several ways, which creates GCP monitoring alerts behind the scenes.

You can create allerts for the backend by using the `@alert` decorator and by passing in a `BackendAlert`. Several resources also
support alerts and these are pass in directly to the resource when creating it.

Each alert takes a name and a list of conditions. Notification channels
can be added to the `alerts.notification_channel` key in `config.json` or explicity in the alert. The base `AlertCondition` class allows you to
fully customize your alert based on the fields privided by the `GCP Alert Resource <https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.alertPolicies#conditionhttps://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.alertPolicies#condition>`_

If you do not need a fully customized alert you can use the built in classes for `MetricCondition`, `LogMatchCondition`, and `CustomMetricCondition`. These come with
defaults in terms of duration and aggregations, but can be overriden as needed. The `CustomMetricCondition` creates a custom metric based on the filter provided and then
creates an alert using that metric.

For `LogMatchCondition` you can completely replace the filter if necessary by setting the `replace_filter` flag to True.

.. code:: python

from goblet.alerts.alert_conditions import MetricCondition,LogMatchCondition,CustomMetricCondition
from goblet.alerts.alerts import BackendAlert
app = Goblet()

# Example Metric Alert for the cloudfunction metric execution_count with a threshold of 10
metric_alert = BackendAlert(
"metric",
conditions=[
MetricCondition(
"test",
metric="cloudfunctions.googleapis.com/function/execution_count",
value=10
)
],
)
app.alert(metric_alert)

# Example Metric Alert for the cloudfunction metric execution_times with a custom aggregation
metric_alert_2 = BackendAlert(
"metric",
conditions=[
MetricCondition(
"test",
metric="cloudfunctions.googleapis.com/function/execution_times",
value=1000,
aggregations=[
{
"alignmentPeriod": "300s",
"crossSeriesReducer": "REDUCE_NONE",
"perSeriesAligner": "ALIGN_PERCENTILE_50",
}
],
)
],
)
app.alert(metric_alert_2)

# Example Log Match metric that will trigger an incendent off of any Error logs
log_alert = BackendAlert(
"error",
conditions=[LogMatchCondition("error", "severity>=ERROR")],
)
app.alert(log_alert)

# Example Metric Alert that creates a custom metric for severe errors with http code in the 500's and creates an alert with a threshold of 10
custom_alert = BackendAlert(
"custom",
conditions=[
CustomMetricCondition(
"custom",
metric_filter="severity=(ERROR OR CRITICAL OR ALERT OR EMERGENCY) httpRequest.status=(500 OR 501 OR 502 OR 503 OR 504)",
value=10,
)
],
)
app.alert(custom_alert)

`PubSubDLQCondition` is a special case of `MetricCondition` that will create an alert for `pubsub.googleapis.com/subscription/dead_letter_message_count` on a subscription.

.. code:: python

from goblet.alerts import PubSubDLQAlert, PubSubDLQCondition

# Pubsub Subscription with DLQ and alert
# Triggered by pubsub topic. Simulates failure to trigger DLQ
@app.pubsub_subscription(
"goblet-created-test-topic",
dlq=True,
dlq_alerts=[
PubSubDLQAlert(
"pubsubdlq",
conditions=[
PubSubDLQCondition(
"pubsublq-condition"
)
],
)
]
)
def failed_subscription(data):
raise Exception("Simulating failure")

You can also create an alert for an Uptime check

.. code:: python

from goblet.alerts import UptimeAlert, UptimeCondition

# Example uptime check with alert
@app.uptime(timeout="30s",alerts=[UptimeAlert("uptime", conditions=[UptimeCondition("uptime")])])
def uptime_check_with_alert():
app.log.info("success")
return "success"
8 changes: 8 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ Infrastructures

infrastructures

Alerts
------

.. toctree::
:maxdepth: 2

alerts

Integrations
------------

Expand Down
33 changes: 1 addition & 32 deletions docs/source/infrastructures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,8 @@
Infrastructure
================

You can now provision infrastructure within your Goblet code.
You can provision infrastructure within your Goblet code.

Alerts
^^^^^^

You can deploy alerts related to your application by using the alert method. Each alert takes a name and a list of conditions. Notification channels
can be added to the `alerts.notification_channel` key in `config.json` or explicity in the alert. The base `AlertCondition` class allows you to
fully customize your alert based on the fields privided by the `GCP Alert Resource <https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.alertPolicies#conditionhttps://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.alertPolicies#condition>`_

If you do not need a fully customized alert you can use the built in classes for `MetricCondition`, `LogMatchCondition`, and `CustomMetricCondition`. These come with
defaults in terms of duration and aggregations, but can be overriden as needed. The `CustomMetricCondition` creates a custom metric based on the filter provided and then
creates an alert using that metric.

For `LogMatchCondition` you can completely replace the filter if necessary by setting the `replace_filter` flag to True.

`PubSubDLQCondition` is a special case of `MetricCondition` that will create an alert for `pubsub.googleapis.com/subscription/dead_letter_message_count` on a subscription.

.. code:: python

from goblet.infrastructures.alerts import MetricCondition,LogMatchCondition,CustomMetricCondition
app = Goblet()

# Example Metric Alert for the cloudfunction metric execution_count with a threshold of 10
app.alert("metric",conditions=[MetricCondition("test", metric="cloudfunctions.googleapis.com/function/execution_count", value=10)])

# Example Log Match metric that will trigger an incendent off of any Error logs
app.alert("error",conditions=[LogMatchCondition("error", "severity>=ERROR")])

# Example Metric Alert that creates a custom metric for severe errors with http code in the 500's and creates an alert with a threshold of 10
app.alert("custom",conditions=[CustomMetricCondition("custom", metric_filter='severity=(ERROR OR CRITICAL OR ALERT OR EMERGENCY) httpRequest.status=(500 OR 501 OR 502 OR 503 OR 504)', value=10)])

# Example PubSub Alert that will trigger an incident if there are more than 10 dead letter messages in the subscription
app.alert("pubsub",conditions=[PubSubDLQCondition("pubsub", subscription_id="{subscription}", value=10)])
.. _redis:

Redis
Expand Down
3 changes: 2 additions & 1 deletion docs/source/local.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ Pubsub Emulator
Google has a pubsub emulator that you can use to test your pubsub functions locally. You can install and run the emulator with gcloud as shown in the docs `here <https://cloud.google.com/pubsub/docs/emulator>`_.
Or if you prefer you can also use docker. There's a Dockerfile and docker-compose.yml files on the examples to run the emulator:
.. code:: yaml

services:
pubsub-emulator:
build:
context: .
dockerfile: pubsub-emulator.Dockerfile
ports:
- "8085:8085"
- "8085:8085"

Run the emulator with ``docker-compose up``. Then set the environment variables ``PUBSUB_EMULATOR_HOST=localhost:8085`` and ``GOBLET_LOCAL_URL=http://host.docker.internal:8080`` to use the emulator with goblet. \
You can run `goblet local --extras` to deploy topics and subscriptions to the emulator and also start a local function that listens to the subscription messages.
Expand Down
13 changes: 8 additions & 5 deletions examples/example_pubsub_topics/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from goblet import Goblet, goblet_entrypoint
from goblet.infrastructures.pubsub import PubSubClient
from goblet_gcp_client import Client
from goblet.alerts.alerts import PubSubDLQAlert
from goblet.alerts.alert_conditions import PubSubDLQCondition

app = Goblet(function_name="create-pubsub-topic")

Expand All @@ -30,11 +32,12 @@ def publish(request):
@app.pubsub_subscription(
"goblet-created-test-topic",
dlq=True,
dlq_alert=True,
dlq_alert_config={
# Trigger alert if 10 messages fail within 1 minute
"trigger_value": 10,
},
dlq_alerts=[
PubSubDLQAlert(
"pubsubdlq",
conditions=[PubSubDLQCondition("pubsublq-condition")],
)
],
)
def subscription(data: str):
raise Exception("Simulating failure")
Expand Down
Loading