-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathscript.liquid
82 lines (65 loc) · 2.61 KB
/
script.liquid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{% comment %}
Only send error emails for the same errors at these thresholds.
{% endcomment %}
{% assign error_thresholds = array %}
{% assign error_thresholds[0] = 10 %}
{% assign error_thresholds[1] = 25 %}
{% assign error_thresholds[2] = 50 %}
{% assign error_thresholds[3] = 100 %}
{% assign error_thresholds[4] = 200 %}
{% comment %}
Fingerprinting the error, so that we can track if this is something we have received recently.
See: https://learn.mechanic.dev/techniques/debouncing-events#fingerprinting
{% endcomment %}
{% assign fingerprint_parts = hash %}
{% assign fingerprint_parts["topic"] = event.topic %}
{% assign fingerprint_parts["message"] = error.message %}
{% assign fingerprint_parts["task_id"] = error.task.id %}
{% assign fingerprint_parts["action_type"] = error.action.type %}
{% assign fingerprint = fingerprint_parts | json | sha256 %}
{% assign error_count_cache_key = "errors/" | append: fingerprint %}
{% comment %} How many times have we seen this error recently? {% endcomment %}
{% assign recent_error_count = cache[error_count_cache_key] | default: 0 %}
{% assign event_url = "https://admin.shopify.com/store/" | append: shop.myshopify_domain | replace: ".myshopify.com", "/apps/mechanic/events/" | append: error.event.id %}
{% capture email_subject -%}
{{ event.topic }} on {{ shop.name }}
{%- endcapture %}
{% capture email_body %}
<p><b>This is an automated error notification from your Mechanic error reporting task.</b></p>
{% if error_thresholds contains recent_error_count -%}
<p><b>Note:</b> There have been more than {{ recent_error_count }} errors of this type in the last 10 minutes.</p>
{%- endif %}
{% if event.preview %}
<p>View error details</p>
{% else %}
<p><a href="{{ event_url }}">View error details</a></p>
{% endif %}
<p>Error topic: {{ event.topic }}</p>
<p>Error message: <pre>{{ error.message }}</pre></p>
<p>Thanks,<br><br>{{ shop.name }} via Mechanic</p>
{% endcapture %}
{% comment %}
We only send emails on the first error and again on specific thresholds
{% endcomment %}
{% if recent_error_count == 0 or error_thresholds contains recent_error_count %}
{% action "email" %}
{
"to": {{ options.email_recipients__email_array_required | json }},
"subject": {{ email_subject | json }},
"body": {{ email_body | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% endif %}
{% comment %}
Increment recent error count for this error
{% endcomment %}
{% action "cache" %}
{
"incr": {
"key": {{ fingerprint | json }},
"ttl": 600
}
}
{% endaction %}