Skip to content

Commit 19a11f7

Browse files
authored
Use SHUB_VIRTUAL_SPIDER as spider name when available (#462)
* Use SHUB_VIRTUAL_SPIDER as spider name when available * Black fixes * Fix tests * Refactor spider name extracto into a new utility function * Add tests
1 parent baafaa8 commit 19a11f7

File tree

12 files changed

+58
-11
lines changed

12 files changed

+58
-11
lines changed

spidermon/contrib/actions/discord/templates/discord/spider/notifier/macros.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616

1717
{% macro render_job_url() %}{% if data.job %} / [view job in Scrapy Cloud](https://app.scrapinghub.com/p/{{ data.job.key }}){% endif %}{% endmacro %}
1818
{% macro render_url() %}{{ render_job_url() }}{% endmacro %}
19-
{% macro render_spider_name() %}{% if data.spider %}{{ data.spider.name }}{% elif data.job %}{{ data.job.metadata.get('spider') }}{% else %}??{% endif %}{% endmacro %}
19+
{% macro render_spider_name() %}{% if data.spider %}{{ data.sc_spider_name }}{% elif data.job %}{{ data.job.metadata.get('spider') }}{% else %}??{% endif %}{% endmacro %}

spidermon/contrib/actions/reports/templates/reports/email/monitors/result.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
{% set log_errors_count = data.stats.get('log_count/ERROR', 0) %}
169169
{% set running_time = data.stats.get('finish_time') - data.stats.get('start_time') %}
170170

171-
{{ render_header_data('Spider', data.spider.name, 'label label-blue') }}
171+
{{ render_header_data('Spider', data.sc_spider_name, 'label label-blue') }}
172172
{{ render_header_data('Items', items_count, "badge badge-green") }}
173173
{{ render_header_data('Requests', requests_count, "badge") }}
174174
{{ render_header_data('Errors', log_errors_count, "badge badge-red") }}

spidermon/contrib/actions/sentry/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def get_title(self):
6666
"{project_name} | {environment} | Spider {spider_name} notification".format(
6767
project_name=self.project_name,
6868
environment=self.environment,
69-
spider_name=self.data.spider.name,
69+
spider_name=self.data.sc_spider_name,
7070
)
7171
)
7272

@@ -83,7 +83,7 @@ def get_message(self):
8383
)
8484

8585
if self.data.spider:
86-
message["spider_name"] = self.data.spider.name
86+
message["spider_name"] = self.data.sc_spider_name
8787

8888
if self.data.stats:
8989
message["items_count"] = self.data.stats.get("item_scraped_count", 0)

spidermon/contrib/actions/slack/templates/slack/spider/notifier/macros.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
{% macro render_job_url() %}{% if data.job %} / <https://app.zyte.com/p/{{ data.job.key }}|view job in Scrapy Cloud>{% endif %}{% endmacro %}
2222
{% macro render_report_url() %}{% if include_report_link and data.meta.reports_links %} / <{{ data.meta.reports_links[report_index] }}|report>{% endif %}{% endmacro %}
2323
{% macro render_url() %}{{ render_report_url() }}{{ render_job_url() }}{% endmacro %}
24-
{% macro render_spider_name() %}{% if data.spider %}{{ data.spider.name }}{% elif data.job %}{{ data.job.metadata.get('spider') }}{% else %}??{% endif %}{% endmacro %}
24+
{% macro render_spider_name() %}{% if data.spider %}{{ data.sc_spider_name }}{% elif data.job %}{{ data.job.metadata.get('spider') }}{% else %}??{% endif %}{% endmacro %}

spidermon/contrib/actions/sns/notifiers.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ def run_action(self):
66
subject = "Spider Started"
77
attributes = {
88
"EventType": {"DataType": "String", "StringValue": "SpiderStarted"},
9-
"SpiderName": {"DataType": "String", "StringValue": self.data.spider.name},
9+
"SpiderName": {
10+
"DataType": "String",
11+
"StringValue": self.data.sc_spider_name,
12+
},
1013
"StartTime": {
1114
"DataType": "String",
1215
"StringValue": str(self.data.stats.start_time),
@@ -20,7 +23,10 @@ def run_action(self):
2023
subject = "Spider Finished"
2124
attributes = {
2225
"EventType": {"DataType": "String", "StringValue": "SpiderFinished"},
23-
"SpiderName": {"DataType": "String", "StringValue": self.data.spider.name},
26+
"SpiderName": {
27+
"DataType": "String",
28+
"StringValue": self.data.sc_spider_name,
29+
},
2430
"ItemsScraped": {
2531
"DataType": "Number",
2632
"StringValue": str(self.data.stats.item_scraped_count),

spidermon/contrib/actions/telegram/templates/telegram/spider/notifier/macros.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616

1717
{% macro render_job_url() %}{% if data.job %} / [view job in Scrapy Cloud](https://app.zyte.com/p/{{ data.job.key }}){% endif %}{% endmacro %}
1818
{% macro render_url() %}{{ render_job_url() }}{% endmacro %}
19-
{% macro render_spider_name() %}{% if data.spider %}{{ data.spider.name }}{% elif data.job %}{{ data.job.metadata.get('spider') }}{% else %}??{% endif %}{% endmacro %}
19+
{% macro render_spider_name() %}{% if data.spider %}{{ data.sc_spider_name }}{% elif data.job %}{{ data.job.metadata.get('spider') }}{% else %}??{% endif %}{% endmacro %}

spidermon/contrib/scrapy/extensions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import os
2+
13
from scrapy import signals
24
from scrapy.exceptions import NotConfigured
35
from scrapy.utils.misc import load_object
46
from twisted.internet.task import LoopingCall
57
from itemadapter import ItemAdapter
68

79
from spidermon import MonitorSuite
10+
from spidermon.contrib.utils.spider import get_spider_name
811
from spidermon.contrib.scrapy.runners import SpiderMonitorRunner
912
from spidermon.python import factory
1013
from spidermon.python.monitors import ExpressionsMonitor
@@ -238,5 +241,6 @@ def _generate_data_for_spider(self, spider):
238241
else [],
239242
"crawler": self.crawler,
240243
"spider": spider,
244+
"sc_spider_name": get_spider_name(spider),
241245
"job": self.client.job if self.client.available else None,
242246
}

spidermon/contrib/stats/statscollectors/local_storage.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
from scrapy.statscollectors import StatsCollector
66
from scrapy.utils.project import data_path
77

8+
from spidermon.contrib.utils.spider import get_spider_name
9+
810

911
class LocalStorageStatsHistoryCollector(StatsCollector):
1012
def _stats_location(self, spider):
1113
statsdir = data_path("stats", createdir=True)
12-
return os.path.join(statsdir, f"{spider.name}_stats_history")
14+
spider_name = get_spider_name(spider)
15+
return os.path.join(statsdir, f"{spider_name}_stats_history")
1316

1417
def open_spider(self, spider):
1518
stats_location = self._stats_location(spider)

spidermon/contrib/stats/statscollectors/sc_collections.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import scrapinghub
66
from sh_scrapy.stats import HubStorageStatsCollector
77

8+
from spidermon.contrib.utils.spider import get_spider_name
9+
810
logger = logging.getLogger(__name__)
911

1012

@@ -18,7 +20,8 @@ def _open_collection(self, spider):
1820

1921
project = sh_client.get_project(proj_id)
2022
collections = project.collections
21-
stats_location = f"{spider.name}_stats_history"
23+
spider_name = get_spider_name(spider)
24+
stats_location = f"{spider_name}_stats_history"
2225
store = collections.get_store(stats_location)
2326
return store
2427

spidermon/contrib/utils/spider.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import os
2+
3+
4+
def get_spider_name(spider):
5+
return os.getenv("SHUB_VIRTUAL_SPIDER") or spider.name

0 commit comments

Comments
 (0)