Skip to content

Commit 3af8f52

Browse files
committed
[IMP] sentry: adds an option to use sentry's structured log feature
1 parent e1ffc79 commit 3af8f52

File tree

4 files changed

+57
-21
lines changed

4 files changed

+57
-21
lines changed

sentry/hooks.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ def initialize_sentry(config):
128128
# Remove logging_level, since in sentry_sdk is include in 'integrations'
129129
del options["logging_level"]
130130

131+
if config.get("sentry_enable_logs", False):
132+
options["enable_logs"] = True
133+
131134
client = sentry_sdk.init(**options)
132135

133136
sentry_sdk.set_tag("include_context", config.get("sentry_include_context", True))

sentry/readme/CONFIGURE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ options:
1919
sentry_enabled = true
2020
sentry_logging_level = warn
2121
sentry_exclude_loggers = werkzeug
22+
sentry_enable_logs = true
2223
sentry_ignore_exceptions = odoo.exceptions.AccessDenied,
2324
odoo.exceptions.AccessError,odoo.exceptions.MissingError,
2425
odoo.exceptions.RedirectWarning,odoo.exceptions.UserError,

sentry/static/description/index.html

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
6-
<title>README.rst</title>
6+
<title>Sentry</title>
77
<style type="text/css">
88

99
/*
@@ -360,21 +360,16 @@
360360
</style>
361361
</head>
362362
<body>
363-
<div class="document">
363+
<div class="document" id="sentry">
364+
<h1 class="title">Sentry</h1>
364365

365-
366-
<a class="reference external image-reference" href="https://odoo-community.org/get-involved?utm_source=readme">
367-
<img alt="Odoo Community Association" src="https://odoo-community.org/readme-banner-image" />
368-
</a>
369-
<div class="section" id="sentry">
370-
<h1>Sentry</h1>
371366
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
372367
!! This file is generated by oca-gen-addon-readme !!
373368
!! changes will be overwritten. !!
374369
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
375370
!! source digest: sha256:e2f1d0bc83bf031b61df768de9c2f6d4f1feb303facf171a8d1889fc6a2635ca
376371
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
377-
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/license-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/server-tools/tree/18.0/sentry"><img alt="OCA/server-tools" src="https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/server-tools-18-0/server-tools-18-0-sentry"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/server-tools&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
372+
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/server-tools/tree/18.0/sentry"><img alt="OCA/server-tools" src="https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/server-tools-18-0/server-tools-18-0-sentry"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/server-tools&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
378373
<p>This module allows painless <a class="reference external" href="https://sentry.io/">Sentry</a> integration
379374
with Odoo.</p>
380375
<p><strong>Table of contents</strong></p>
@@ -398,7 +393,7 @@ <h1>Sentry</h1>
398393
</ul>
399394
</div>
400395
<div class="section" id="installation">
401-
<h2><a class="toc-backref" href="#toc-entry-1">Installation</a></h2>
396+
<h1><a class="toc-backref" href="#toc-entry-1">Installation</a></h1>
402397
<p>The module can be installed just like any other Odoo module, by adding
403398
the module’s directory to Odoo <em>addons_path</em>. In order for the module to
404399
correctly wrap the Odoo WSGI application, it also needs to be loaded as
@@ -412,7 +407,7 @@ <h2><a class="toc-backref" href="#toc-entry-1">Installation</a></h2>
412407
</pre>
413408
</div>
414409
<div class="section" id="configuration">
415-
<h2><a class="toc-backref" href="#toc-entry-2">Configuration</a></h2>
410+
<h1><a class="toc-backref" href="#toc-entry-2">Configuration</a></h1>
416411
<p>The following additional configuration options can be added to your Odoo
417412
configuration file:</p>
418413
<p>[TABLE]</p>
@@ -423,7 +418,7 @@ <h2><a class="toc-backref" href="#toc-entry-2">Configuration</a></h2>
423418
are:
424419
<tt class="docutils literal">with_locals, max_breadcrumbs, release, environment, server_name, shutdown_timeout, in_app_include, in_app_exclude, default_integrations, dist, sample_rate, send_default_pii, http_proxy, https_proxy, request_bodies, debug, attach_stacktrace, ca_certs, propagate_traces, traces_sample_rate, auto_enabling_integrations</tt>.</p>
425420
<div class="section" id="example-odoo-configuration">
426-
<h3><a class="toc-backref" href="#toc-entry-3">Example Odoo configuration</a></h3>
421+
<h2><a class="toc-backref" href="#toc-entry-3">Example Odoo configuration</a></h2>
427422
<p>Below is an example of Odoo configuration file with <em>Odoo Sentry</em>
428423
options:</p>
429424
<pre class="literal-block">
@@ -432,6 +427,7 @@ <h3><a class="toc-backref" href="#toc-entry-3">Example Odoo configuration</a></h
432427
sentry_enabled = true
433428
sentry_logging_level = warn
434429
sentry_exclude_loggers = werkzeug
430+
sentry_enable_logs = true
435431
sentry_ignore_exceptions = odoo.exceptions.AccessDenied,
436432
odoo.exceptions.AccessError,odoo.exceptions.MissingError,
437433
odoo.exceptions.RedirectWarning,odoo.exceptions.UserError,
@@ -445,14 +441,14 @@ <h3><a class="toc-backref" href="#toc-entry-3">Example Odoo configuration</a></h
445441
</div>
446442
</div>
447443
<div class="section" id="usage">
448-
<h2><a class="toc-backref" href="#toc-entry-4">Usage</a></h2>
444+
<h1><a class="toc-backref" href="#toc-entry-4">Usage</a></h1>
449445
<p>Once configured and installed, the module will report any logging event
450446
at and above the configured Sentry logging level, no additional actions
451447
are necessary.</p>
452448
<p><a class="reference external image-reference" href="https://runbot.odoo-community.org/runbot/149/14.0"><img alt="Try me on Runbot" src="https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas" /></a></p>
453449
</div>
454450
<div class="section" id="known-issues-roadmap">
455-
<h2><a class="toc-backref" href="#toc-entry-5">Known issues / Roadmap</a></h2>
451+
<h1><a class="toc-backref" href="#toc-entry-5">Known issues / Roadmap</a></h1>
456452
<ul class="simple">
457453
<li><strong>No database separation</strong> – This module functions by intercepting
458454
all Odoo logging records in a running Odoo process. This means that
@@ -468,17 +464,17 @@ <h2><a class="toc-backref" href="#toc-entry-5">Known issues / Roadmap</a></h2>
468464
</ul>
469465
</div>
470466
<div class="section" id="bug-tracker">
471-
<h2><a class="toc-backref" href="#toc-entry-6">Bug Tracker</a></h2>
467+
<h1><a class="toc-backref" href="#toc-entry-6">Bug Tracker</a></h1>
472468
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/server-tools/issues">GitHub Issues</a>.
473469
In case of trouble, please check there if your issue has already been reported.
474470
If you spotted it first, help us to smash it by providing a detailed and welcomed
475471
<a class="reference external" href="https://github.com/OCA/server-tools/issues/new?body=module:%20sentry%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
476472
<p>Do not contact contributors directly about support or help with technical issues.</p>
477473
</div>
478474
<div class="section" id="credits">
479-
<h2><a class="toc-backref" href="#toc-entry-7">Credits</a></h2>
475+
<h1><a class="toc-backref" href="#toc-entry-7">Credits</a></h1>
480476
<div class="section" id="authors">
481-
<h3><a class="toc-backref" href="#toc-entry-8">Authors</a></h3>
477+
<h2><a class="toc-backref" href="#toc-entry-8">Authors</a></h2>
482478
<ul class="simple">
483479
<li>Mohammed Barsi</li>
484480
<li>Versada</li>
@@ -487,7 +483,7 @@ <h3><a class="toc-backref" href="#toc-entry-8">Authors</a></h3>
487483
</ul>
488484
</div>
489485
<div class="section" id="contributors">
490-
<h3><a class="toc-backref" href="#toc-entry-9">Contributors</a></h3>
486+
<h2><a class="toc-backref" href="#toc-entry-9">Contributors</a></h2>
491487
<ul class="simple">
492488
<li>Mohammed Barsi &lt;<a class="reference external" href="mailto:barsintod&#64;gmail.com">barsintod&#64;gmail.com</a>&gt;</li>
493489
<li>Andrius Preimantas &lt;<a class="reference external" href="mailto:andrius&#64;versada.eu">andrius&#64;versada.eu</a>&gt;</li>
@@ -500,13 +496,13 @@ <h3><a class="toc-backref" href="#toc-entry-9">Contributors</a></h3>
500496
</ul>
501497
</div>
502498
<div class="section" id="other-credits">
503-
<h3><a class="toc-backref" href="#toc-entry-10">Other credits</a></h3>
499+
<h2><a class="toc-backref" href="#toc-entry-10">Other credits</a></h2>
504500
<ul class="simple">
505501
<li>Vauxoo</li>
506502
</ul>
507503
</div>
508504
<div class="section" id="maintainers">
509-
<h3><a class="toc-backref" href="#toc-entry-11">Maintainers</a></h3>
505+
<h2><a class="toc-backref" href="#toc-entry-11">Maintainers</a></h2>
510506
<p>This module is maintained by the OCA.</p>
511507
<a class="reference external image-reference" href="https://odoo-community.org">
512508
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
@@ -521,6 +517,5 @@ <h3><a class="toc-backref" href="#toc-entry-11">Maintainers</a></h3>
521517
</div>
522518
</div>
523519
</div>
524-
</div>
525520
</body>
526521
</html>

sentry/tests/test_client.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ def has_event(self, event_level, event_msg):
5353
return event
5454
return False
5555

56+
def has_log_item(self, log_level: str, log_msg: str) -> bool:
57+
for envelope in self.envelopes:
58+
log_items = filter(
59+
lambda envelope_item: envelope_item.data_category == "log_item",
60+
envelope.items,
61+
)
62+
payload_items = []
63+
for item in log_items:
64+
payload_items.extend(item.payload.json["items"])
65+
if any(
66+
(item["level"] == log_level and item["body"] == log_msg)
67+
for item in payload_items
68+
):
69+
return True
70+
71+
return False
72+
5673
def flush(self, *args, **kwargs):
5774
pass
5875

@@ -123,6 +140,13 @@ def assertEventNotCaptured(self, client, event_level, event_msg):
123140
msg=f"Event: {event_msg} was captured",
124141
)
125142

143+
def assertLogCaptured(self, client, log_level: str, log_msg: str):
144+
client.flush()
145+
self.assertTrue(
146+
client.transport.has_log_item(log_level, log_msg),
147+
msg=f"Log item: {log_msg} was not captured",
148+
)
149+
126150
def test_initialize_raven_sets_dsn(self):
127151
self.assertEqual(self.client.dsn, self.dsn)
128152

@@ -224,6 +248,19 @@ def test_exclude_logger(self):
224248
remove_handler_ignore(self.logger.name)
225249
self.assertEventNotCaptured(client, level, msg)
226250

251+
def test_enable_logs(self):
252+
self.patch_config(
253+
{
254+
"sentry_enable_logs": True,
255+
}
256+
)
257+
client = initialize_sentry(config)._client
258+
client.transport = InMemoryTransport({"dsn": self.dsn})
259+
level, msg = logging.WARNING, "Test log sending is enabled"
260+
self.log(level, msg)
261+
level = "warn"
262+
self.assertLogCaptured(client, level, msg)
263+
227264
def test_invalid_logging_level(self):
228265
self.patch_config(
229266
{

0 commit comments

Comments
 (0)