Skip to content

Commit

Permalink
Telemetry: dump app.extensions and app.config.html_theme (#117)
Browse files Browse the repository at this point in the history
* Telemetry: dump `app.extensions` and `app.config.html_theme`

Dump data from Sphinx into a JSON file that will be read and processed by our
Telemetry collector. This will help us to get some insights about extensions and
theme usage.

* Telemetry: handle unexpected exceptions

* Telemetry: comment about config-inited

* Style

* Telemetry: save only extensions defined by the user

Instead of saving `app.extensions` (all extensions used by Sphinx, including
default ones), we only save `app.config.extensions` which are the ones defined
by the user itself.
  • Loading branch information
humitos committed Oct 17, 2022
1 parent 9ca7b2e commit 7dc7a28
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions readthedocs_ext/readthedocs.py
Expand Up @@ -338,6 +338,29 @@ def dump_sphinx_data(app, exception):
)


def dump_telemetry(app, config):
# We need to get the output directory where the docs are built
# _build/json.
build_json = os.path.abspath(
os.path.join(app.outdir, '..', 'json')
)
outjson = os.path.join(build_json, 'telemetry.json')
outdir = os.path.dirname(outjson)

try:
if not os.path.exists(outdir):
os.makedirs(outdir)

with open(outjson, 'w+') as json_file:
data = {
'extensions': app.config.extensions,
'html_theme': app.config.html_theme,
}
json.dump(data, json_file, indent=4)
except Exception:
log.exception("Something went wrong when dumping Telemetry data.")


class ReadtheDocsBuilder(StandaloneHTMLBuilder):

"""
Expand Down Expand Up @@ -396,6 +419,10 @@ def setup(app):
app.connect('build-finished', remove_search_init)
app.connect('build-finished', dump_sphinx_data)

if sphinx.version_info >= (1, 8, 0):
# `config-inited` event was introduced in Sphinx 1.8
app.connect('config-inited', dump_telemetry)

# Embed
app.add_directive('readthedocs-embed', EmbedDirective)
app.add_config_value('readthedocs_embed_project', '', 'html')
Expand Down

0 comments on commit 7dc7a28

Please sign in to comment.