From 11a0924042c4bf8fe7d79050df7a554cdc721696 Mon Sep 17 00:00:00 2001 From: Ry Walker Date: Sat, 17 Aug 2019 20:13:12 -0400 Subject: [PATCH] [AIRFLOW-1498] Add analytics script --- airflow/config_templates/default_airflow.cfg | 8 +++-- airflow/www/app.py | 11 +++++- .../templates/analytics/google_analytics.html | 26 ++++++++++++++ .../www/templates/analytics/metarouter.html | 23 ++++++++++++ airflow/www/templates/analytics/segment.html | 23 ++++++++++++ .../www/templates/appbuilder/baselayout.html | 27 ++++++++------ docs/howto/index.rst | 1 + docs/howto/tracking-user-activity.rst | 36 +++++++++++++++++++ 8 files changed, 141 insertions(+), 14 deletions(-) create mode 100644 airflow/www/templates/analytics/google_analytics.html create mode 100644 airflow/www/templates/analytics/metarouter.html create mode 100644 airflow/www/templates/analytics/segment.html create mode 100644 docs/howto/tracking-user-activity.rst diff --git a/airflow/config_templates/default_airflow.cfg b/airflow/config_templates/default_airflow.cfg index 2d4e9df481001..660ed57359321 100644 --- a/airflow/config_templates/default_airflow.cfg +++ b/airflow/config_templates/default_airflow.cfg @@ -337,6 +337,10 @@ cookie_samesite = # Default setting for wrap toggle on DAG code and TI log views. default_wrap = False +# Send anonymous user activity to your analytics tool +# analytics_tool = # choose from google_analytics, segment, or metarouter +# analytics_id = XXXXXXXXXXX + [email] email_backend = airflow.utils.email.send_email_smtp @@ -553,8 +557,8 @@ basedn = dc=example,dc=com cacert = /etc/ca/ldap_ca.crt search_scope = LEVEL -# This setting allows the use of LDAP servers that either return a -# broken schema, or do not return a schema. +# This setting allows the use of LDAP servers that either return a +# broken schema, or do not return a schema. ignore_malformed_schema = False [kerberos] diff --git a/airflow/www/app.py b/airflow/www/app.py index eb8b57cbea131..e0b4b001536bb 100644 --- a/airflow/www/app.py +++ b/airflow/www/app.py @@ -204,11 +204,20 @@ def init_plugin_blueprints(app): @app.context_processor def jinja_globals(): # pylint: disable=unused-variable - return { + + globals = { 'hostname': socket.getfqdn(), 'navbar_color': conf.get('webserver', 'NAVBAR_COLOR'), } + if 'analytics_tool' in conf.getsection('webserver'): + globals.update({ + 'analytics_tool': conf.get('webserver', 'ANALYTICS_TOOL'), + 'analytics_id': conf.get('webserver', 'ANALYTICS_ID') + }) + + return globals + @app.teardown_appcontext def shutdown_session(exception=None): # pylint: disable=unused-variable settings.Session.remove() diff --git a/airflow/www/templates/analytics/google_analytics.html b/airflow/www/templates/analytics/google_analytics.html new file mode 100644 index 0000000000000..5f06c5014d741 --- /dev/null +++ b/airflow/www/templates/analytics/google_analytics.html @@ -0,0 +1,26 @@ +{# + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +#} + diff --git a/airflow/www/templates/analytics/metarouter.html b/airflow/www/templates/analytics/metarouter.html new file mode 100644 index 0000000000000..799428e30f289 --- /dev/null +++ b/airflow/www/templates/analytics/metarouter.html @@ -0,0 +1,23 @@ +{# + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +#} + diff --git a/airflow/www/templates/analytics/segment.html b/airflow/www/templates/analytics/segment.html new file mode 100644 index 0000000000000..86838eea179ea --- /dev/null +++ b/airflow/www/templates/analytics/segment.html @@ -0,0 +1,23 @@ +{# + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +#} + diff --git a/airflow/www/templates/appbuilder/baselayout.html b/airflow/www/templates/appbuilder/baselayout.html index 481e33e8f459a..a54fc38d5c497 100644 --- a/airflow/www/templates/appbuilder/baselayout.html +++ b/airflow/www/templates/appbuilder/baselayout.html @@ -75,15 +75,20 @@ {% block tail_js %} -{{ super() }} - - - + {{ super() }} + + + + + + {% if analytics_tool is defined and analytics_tool %} + {% include "analytics/" + analytics_tool + ".html" %} + {% endif %} {% endblock %} diff --git a/docs/howto/index.rst b/docs/howto/index.rst index 9360d59ee0f61..a6e5faf35f124 100644 --- a/docs/howto/index.rst +++ b/docs/howto/index.rst @@ -43,3 +43,4 @@ configuring an Airflow environment. check-health define_extra_link cli-completion + tracking-user-activity diff --git a/docs/howto/tracking-user-activity.rst b/docs/howto/tracking-user-activity.rst new file mode 100644 index 0000000000000..384ec13b0f9a2 --- /dev/null +++ b/docs/howto/tracking-user-activity.rst @@ -0,0 +1,36 @@ + .. Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + .. http://www.apache.org/licenses/LICENSE-2.0 + + .. Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + +Tracking User Activity +============================= + +You can configure Airflow to route anonymous data to +`Google Analytics `_, +`Segment `_, or `Metarouter `_. + +Edit ``airflow.cfg`` and set the ``analytics`` block to have a ``tool`` and ``id``: + +.. code-block:: python + + [webserver] + # Send anonymous user activity to Google Analytics, Segment, or Metarouter + analytics_tool = google_analytics # valid options: google_analytics, segment, metarouter + analytics_id = XXXXXXXXXXX + +.. note:: You can see view injected tracker html within Airflow's source code at + ``airflow/www/templates/appbuilder/baselayout.html``. The related global + variables are set in ``airflow/www/templates/app.py``.