From 5e03b20ff89c276bcc7f0bcf47de71da95271c89 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Tue, 12 Nov 2019 11:11:26 +0100
Subject: [PATCH] misc: remove statsd support (#37587)

---
 help/fr/settings-monitoring.page |  76 ---------------------
 wcs/publisher.py                 |   3 -
 wcs/qommon/ident/password.py     |   5 --
 wcs/qommon/publisher.py          |  12 ----
 wcs/qommon/vendor/pystatsd.py    | 113 -------------------------------
 5 files changed, 209 deletions(-)
 delete mode 100644 help/fr/settings-monitoring.page
 delete mode 100644 wcs/qommon/vendor/pystatsd.py

diff --git a/help/fr/settings-monitoring.page b/help/fr/settings-monitoring.page
deleted file mode 100644
index fb19148a7..000000000
--- a/help/fr/settings-monitoring.page
+++ /dev/null
@@ -1,76 +0,0 @@
-<page xmlns="http://projectmallard.org/1.0/"
-      type="topic" id="settings-monitoring" xml:lang="fr">
-
-<info>
-  <link type="guide" xref="index#system-settings" />
-  <revision docversion="0.1" date="2013-07-06" status="draft"/>
-  <credit type="author">
-    <name>Frédéric Péters</name>
-    <email>fpeters@entrouvert.com</email>
-  </credit>
-  <desc>Intégration avec un serveur StatsD</desc>
-</info>
-
-<title>Surveillance et suivi applicatif</title>
-
-<section id="statsd">
-  <title>StatsD</title>
-
-  <p>
-    <app>w.c.s.</app> peut communiquer à un serveur <app>StatsD</app> une série
-    d'informations concernant le traitement des requêtes HTTP et
-    l'authentification des utilisateurs.
-  </p>
-
-  <p>
-    L'adresse du serveur <app>StatsD</app> est récupérée d'une variable
-    d'environnement nommée <var>QOMMON_STATSD_HOSTNAME</var> qui sera
-    communément positionnée au niveau du serveur Web; par défaut
-    <var>localhost</var> sera utilisé.
-  </p>
-
-  <listing>
-    <title>Exemple de configuration Apache</title>
-    <code>SetEnv QOMMON_STATSD_HOSTNAME statsd.example.net</code>
-  </listing>
-
-  <p>
-    Les clés positionnées sont de la forme
-    <code>wcs.<var>domaine</var>.<var>clé</var></code>, les points présents dans le
-    nom de domaine remplacés par des tirets; par exemple
-    <code>wcs.teleservices-example-net.login-error</code>.
-  </p>
-
-  <p>
-    Les informations suivantes sont communiquées :
-  </p>
-
-  <table shade="rows">
-    <tr>
-      <td><p><code>login-submit</code></p></td>
-      <td><p>Compteur incrémenté lors de la validation d'une page
-      d'authentification</p></td>
-    </tr>
-    <tr>
-      <td><p><code>login-error</code></p></td>
-      <td><p>Compteur incrémenté lors d'une tentative d'authentification
-      infructueuse.</p></td>
-    </tr>
-    <tr>
-      <td><p><code>login-success</code></p></td>
-      <td><p>Compteur incrémenté lors d'une tentative d'authentification
-      réussie</p></td>
-    </tr>
-    <tr>
-      <td><p><code>successful-request</code></p></td>
-      <td><p>Compteur incrémenté à la fin d'une requête HTTP réussie</p></td>
-    </tr>
-    <tr>
-      <td><p><code>failed-request</code></p></td>
-      <td><p>Compteur incrémenté à la fin d'une requête HTTP avortée par
-             une erreur interne (erreur HTTP 500)</p></td>
-    </tr>
-  </table>
-</section>
-
-</page>
diff --git a/wcs/publisher.py b/wcs/publisher.py
index c2369bf4f..f084772c3 100644
--- a/wcs/publisher.py
+++ b/wcs/publisher.py
@@ -351,9 +351,6 @@ class WcsPublisher(StubWcsPublisher):
         sql.reindex()
 
     def cleanup(self):
-        if self.statsd:
-            self.statsd.udp_sock.close()
-            self.statsd = None
         if self.is_using_postgresql():
             from . import sql
             sql.cleanup_connection()
diff --git a/wcs/qommon/ident/password.py b/wcs/qommon/ident/password.py
index cb286eff6..1821f96c0 100644
--- a/wcs/qommon/ident/password.py
+++ b/wcs/qommon/ident/password.py
@@ -252,17 +252,12 @@ class MethodDirectory(Directory):
         username = form.get_widget('username').parse()
         password = form.get_widget('password').parse()
 
-        get_publisher().statsd.increment('login-submit')
-
         try:
             user = PasswordAccount.get_with_credentials(username, password)
         except:
             form.set_error('username', _('Invalid credentials'))
-            get_publisher().statsd.increment('login-error')
             return
 
-        get_publisher().statsd.increment('login-success')
-
         account = PasswordAccount.get(username)
         return self.login_submit_account_user(account, user, form)
 
diff --git a/wcs/qommon/publisher.py b/wcs/qommon/publisher.py
index 0271f892d..d5ca220c7 100644
--- a/wcs/qommon/publisher.py
+++ b/wcs/qommon/publisher.py
@@ -92,7 +92,6 @@ class QommonPublisher(Publisher, object):
     auto_create_appdir = True
     missing_appdir_redirect = None
     use_sms_feature = True
-    statsd = None
 
     gettext = lambda self, message: message
     ngettext = lambda self, msgid1, msgid2, n: msgid1
@@ -249,7 +248,6 @@ class QommonPublisher(Publisher, object):
     def finish_successful_request(self):
         if not self.get_request().ignore_session:
             self.session_manager.finish_successful_request()
-        self.statsd.increment('successful-request')
 
     def finish_failed_request(self):
         # duplicate at lot from parent class, just to use our own HTTPResponse
@@ -257,9 +255,6 @@ class QommonPublisher(Publisher, object):
         original_response = request.response
         request.response = HTTPResponse()
 
-        if self.statsd: # maybe unset if very early failure
-            self.statsd.increment('failed-request')
-
         (exc_type, exc_value, tb) = sys.exc_info()
 
         if exc_type is NotImplementedError:
@@ -465,13 +460,6 @@ class QommonPublisher(Publisher, object):
     def init_publish(self, request):
         self.set_app_dir(request)
 
-        from vendor import pystatsd
-        self.statsd = pystatsd.Client(
-                        host=request.get_environ('QOMMON_STATSD_HOSTNAME', 'localhost'),
-                        prefix='%s.%s' % (
-                                self.APP_NAME,
-                                os.path.split(self.app_dir)[-1].replace('+', '-').replace('.', '-')))
-
         self.initialize_app_dir()
         self.set_config(request)
 
diff --git a/wcs/qommon/vendor/pystatsd.py b/wcs/qommon/vendor/pystatsd.py
deleted file mode 100644
index 96f2833f2..000000000
--- a/wcs/qommon/vendor/pystatsd.py
+++ /dev/null
@@ -1,113 +0,0 @@
-# w.c.s. - web application for online forms
-# Copyright (C) 2005-2012  Entr'ouvert
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, see <http://www.gnu.org/licenses/>.
-#
-#
-# statsd.py - Python implementation of the statsd client
-#
-# Initially released (as License :: OSI Approved :: BSD License, from setup.py)
-# by Steve Ivy <steveivy@gmail.com> - <https://github.com/sivy/py-statsd>
-
-import random
-import socket
-import time
-
-
-# Sends statistics to the stats daemon over UDP
-class Client(object):
-
-    def __init__(self, host='localhost', port=8125, prefix=None):
-        """
-        Create a new Statsd client.
-        * host: the host where statsd is listening, defaults to localhost
-        * port: the port where statsd is listening, defaults to 8125
-
-        >>> from pystatsd import statsd
-        >>> stats_client = statsd.Statsd(host, port)
-        """
-        self.addr = (socket.gethostbyname(host), port)
-        self.prefix = prefix
-        self.udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
-
-    def timing_since(self, stat, start, sample_rate=1):
-        """
-        Log timing information as the number of microseconds since the provided time float
-        >>> start = time.time()
-        >>> # do stuff
-        >>> statsd_client.timing_since('some.time', start)
-        """
-        self.timing(stat, int((time.time() - start) * 1000000), sample_rate)
-
-    def timing(self, stat, time, sample_rate=1):
-        """
-        Log timing information for a single stat
-        >>> statsd_client.timing('some.time',500)
-        """
-        stats = {stat: "%f|ms" % time}
-        self.send(stats, sample_rate)
-
-    def gauge(self, stat, value, sample_rate=1):
-        """
-        Log gauge information for a single stat
-        >>> statsd_client.gauge('some.gauge',42)
-        """
-        stats = {stat: "%f|g" % value}
-        self.send(stats, sample_rate)
-
-    def increment(self, stats, sample_rate=1):
-        """
-        Increments one or more stats counters
-        >>> statsd_client.increment('some.int')
-        >>> statsd_client.increment('some.int',0.5)
-        """
-        self.update_stats(stats, 1, sample_rate=sample_rate)
-
-    def decrement(self, stats, sample_rate=1):
-        """
-        Decrements one or more stats counters
-        >>> statsd_client.decrement('some.int')
-        """
-        self.update_stats(stats, -1, sample_rate=sample_rate)
-
-    def update_stats(self, stats, delta, sample_rate=1):
-        """
-        Updates one or more stats counters by arbitrary amounts
-        >>> statsd_client.update_stats('some.int',10)
-        """
-        if not isinstance(stats, list):
-            stats = [stats]
-
-        data = dict((stat, "%s|c" % delta) for stat in stats)
-        self.send(data, sample_rate)
-
-    def send(self, data, sample_rate=1):
-        """
-        Squirt the metrics over UDP
-        """
-
-        if self.prefix:
-            data = dict((".".join((self.prefix, stat)), value) for stat, value in data.iteritems())
-
-        if sample_rate < 1:
-            if random.random() > sample_rate:
-                return
-            sampled_data = dict((stat, "%s|@%s" % (value, sample_rate)) for stat, value in data.iteritems())
-        else:
-            sampled_data = data
-
-        try:
-            [self.udp_sock.sendto("%s:%s" % (stat, value), self.addr) for stat, value in sampled_data.iteritems()]
-        except:
-            pass
-- 
2.24.0

