Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Added optional clean insights measurement collection #360 #365

Merged
merged 11 commits into from
Feb 24, 2024

Conversation

pandafy
Copy link
Member

@pandafy pandafy commented Nov 29, 2023

Closes #360

Changes

#Please explain changes made in your PR#

Screenshots

#For UI changes, please share a screenshot#

Checklist

  • I have read the contributing guidelines.
  • I have manually tested the proposed changes.
  • I have written new test cases to avoid regressions. (if necessary)
  • I have updated the documentation. (e.g. README.rst)
  • I have added [change!] to commit title to indicate a backward incompatible change. (if required)
  • I have checked the links added / modified in the documentation.

#Closes #(issue-number)#

Base automatically changed from issues/273-openwisp-version to master November 29, 2023 21:26
@pandafy pandafy changed the title [skip ci][feature] Added optional clean insights measurement collecti… [skip ci][feature] Added optional clean insights measurement collection #360 Dec 1, 2023
@pandafy pandafy force-pushed the issues/360-clean-insights branch from 5724918 to 4d7d04a Compare December 1, 2023 13:42
@pandafy pandafy changed the title [skip ci][feature] Added optional clean insights measurement collection #360 [feature] Added optional clean insights measurement collection #360 Dec 1, 2023
@pandafy pandafy marked this pull request as ready for review December 1, 2023 13:42
@pandafy pandafy force-pushed the issues/360-clean-insights branch from 4d7d04a to d564b07 Compare December 1, 2023 14:29
time.sleep(5)
logger.error(
f'Maximum tries reach to upload Clean Insights measurements. Error: {message}'
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the retry mechanism offered by the requests library that you know of ;)

README.rst Outdated Show resolved Hide resolved
@pandafy pandafy force-pushed the issues/360-clean-insights branch from f4c7288 to a6dbf26 Compare February 2, 2024 17:50
Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not mention clean insights in the code and comments except in the README.
Let's not assume people reading the code know what clean insights is, let's use a generic term "Usage Metric Collection".

See my comments below.

README.rst Outdated Show resolved Hide resolved
README.rst Outdated Show resolved Hide resolved
README.rst Outdated Show resolved Hide resolved

We collect data on OpenWISP usage to gauge user engagement, satisfaction,
and upgrade patterns. This informs our development decisions, ensuring
continuous improvement aligned with user needs.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a paragraph introducing and linking to cleaninsights.

openwisp_utils/measurements/tasks.py Outdated Show resolved Hide resolved
openwisp_utils/measurements/tasks.py Outdated Show resolved Hide resolved
},
)
assert response.status_code == 204
except Exception as error:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you absolutely sure this code works also when the server does not reply at all?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested the following scenarios:

  1. Trying to establish connection to the port where no application is listening - Check the command output on Hastebin
  2. When the server time outs (hangs) after establishing the connection - Check the command output on Hastebin

I have added a test case for this https://github.com/openwisp/openwisp-utils/pull/365/files#diff-118b67d11be9c05a77bccfedd814f41974ca3fb2bfd21eb010350faf22d59d3bR180-R196

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used this code for the mock server

from http.server import SimpleHTTPRequestHandler
from socketserver import TCPServer


class MyHandler(SimpleHTTPRequestHandler):
    def do_GET(self):
        import time
        time.sleep(40000000)


def run_server():
    server_address = ('', 8000)
    httpd = TCPServer(server_address, MyHandler)

    print(f"Server running on http://localhost:{server_address[1]}")

    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    finally:
        httpd.server_close()


if __name__ == "__main__":
    run_server()

Comment on lines +6 to +13
"""
This function takes a category and data representing usage metrics,
and returns a list of events in a format accepted by the
Clean Insights Matomo Proxy (CIMP) API.

Read the "Event Measurement Schema" in the CIMP documentation:
https://cutt.ly/SwBkC40A
"""
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally left this reference to Clean Insights here to remind us in the future why we are using this specific structure.

@pandafy pandafy force-pushed the issues/360-clean-insights branch from 7e1564e to 59f9490 Compare February 19, 2024 15:51
@coveralls
Copy link

coveralls commented Feb 19, 2024

Coverage Status

coverage: 98.395% (+0.2%) from 98.243%
when pulling 35d0cda on issues/360-clean-insights
into 66cce46 on master.

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you look into why the code coverage is decreasing?

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the metric collection logic is good to merge, but I think the retryable_request needs a bit more love so we can reuse it in other OpenWISP python modules too.

Comment on lines 70 to 75
def retryable_request(method, timeout=(4, 8), max_retries=3, **kwargs):
request_session = requests.Session()
retries = Retry(
total=max_retries,
backoff_factor=1,
status_forcelist=[429, 500, 502, 503, 504],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new utility function will be useful, please allow to custoimize the arguments passed to Retry(), eg:

  • backoff_factor
  • backoff_jitter
  • status_forcelist
  • allowed_methods
  • add another argument called retry_kwargs=None

Then I recommend doing something like:

if retry_kwargs is None:
    retry_kwargs = {}
retry_kwargs.update(dict(
    total=max_retries,
    backoff_factor=backoff_factor,
    backoff_jitter=backoff_jitter,
    status_forcelist=status_forcelist,
    allowed_methods=allowed_methods
))
retries = Retry(**retry_kwargs)

This will allow us to reuse this function in other modules because as you know it's a common need.
Please add this function to the README and make sure there's at least one unit test dedicated to this function only.

@pandafy pandafy force-pushed the issues/360-clean-insights branch from 31db451 to 35d0cda Compare February 23, 2024 13:50
backoff_factor=1,
backoff_jitter=0.0,
status_forcelist=(429, 500, 502, 503, 504),
allowed_methods=('HEAD', 'GET', 'PUT', 'DELETE', 'OPTIONS', 'TRACE', 'POST'),
Copy link
Member

@nemesifier nemesifier Feb 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one more time, let's not use mutable default arguments for the reasons described here:
https://dev.to/sharmapacific/mutable-default-arguments-in-python-22ce

We have already been hit a few times by obscure bugs which originated from using mutable default arguments, let's not repeat the same mistakes.

This applies to timeout, status_forcelist, allowed_methods.

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After giving a second look to the mutable argument subject, I think it's ok since we don't return the mutable objects nor manipulate these inside the function, so we are not risking to be hit by weird bugs and this usage is fine.

@nemesifier nemesifier merged commit 8270d92 into master Feb 24, 2024
20 checks passed
@nemesifier nemesifier deleted the issues/360-clean-insights branch February 24, 2024 17:59
@pandafy
Copy link
Member Author

pandafy commented Feb 26, 2024

@nemesifier Tuples in Python are immutable. That's why I used tuples and not lists. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature] Add optional clean insights data collection
3 participants