Skip to content
This repository has been archived by the owner on Jan 7, 2019. It is now read-only.

158-simplify-cloudwatch-to-graphite-app-remove-leadbutt-dependency #14

Conversation

benvand
Copy link
Contributor

@benvand benvand commented Sep 20, 2018

Refactor/ simplification of the app:

  • Remove pipe-watchdog.py entirely

    • We're no longer using pipes to direct the output. An error should be output to the cloudwatch error logs on failure cf logs --recent digitalmarketplace-cloudwatch-to-graphite-preview
    • Remove this from the run command in manifest.yml.example
  • Add flake8 dependency and .flake8 file in line with GDS recommendations

  • Activate travis service on repo to check Flake8 rules and requirements files

    • Add .travis.yml
  • Add Makefile with shortcuts for running requirements freeze and flake8

  • Update README.md

  • Change requirements handling

    • Add requirements-app.txt
    • Ensure requirements.txt contains full pinned requirements
    • Add flake8 requirement for devs
  • Rewrite app.py

    • Add function descriptions to all functions
    • Remove dependency on leadbutt, signal and subprocess
    • Use boto3 instead of leadbutts boto (https://github.com/crccheck/cloudwatch-to-graphite/blob/master/leadbutt.py#L25)
    • Remove get_timestamp method. The same can be achieved with time.time
    • logger.warn is deprecated, use logger.warning instead
    • Bring string formatting in line with rest of strings in file
    • Rename initialize_metrics to create_hostedgraphite_base_metrics for better description
    • Remove call_leadbutt function and replace with code calling aws ourselves. Extrapolated from (https://github.com/crccheck/cloudwatch-to-graphite/blob/master/leadbutt.py)
    • Change loglevel to INFO this is all that is required now we're not piping output
    • Move retry decorator on to wrapper method in if __name__ block
  • Update config.yaml.j2 template

    • Dimensions default = {} to avoid type manipulations
    • Remove Auth section from config. This can be hard coded until we have any need to specify it as a config variable
    • Remove Options.Count again, this can be hard coded. It's only used in one place and we've never needed to change it
  • Replace retrying with tenacity

## TL:DR;

Extrapolate code out of leadbutt
This code drops leadbutts retry functionality. Boto3 has the capacity to retry failed attempts. We should prefer this.

@benvand benvand force-pushed the 158-simplify-cloudwatch-to-graphite-app-remove-leadbutt-dependency branch 8 times, most recently from 9fcec3a to e03ef5f Compare September 21, 2018 14:12
@benvand benvand changed the title 158-simplify-cloudwatch-to-graphite-app-remove-leadbutt-dependency [Tests are a WIP] 158-simplify-cloudwatch-to-graphite-app-remove-leadbutt-dependency Sep 21, 2018
@lfdebrux lfdebrux self-requested a review September 21, 2018 14:51
@benvand benvand force-pushed the 158-simplify-cloudwatch-to-graphite-app-remove-leadbutt-dependency branch 6 times, most recently from 5e340fd to 42d6911 Compare September 23, 2018 08:25
@benvand benvand changed the title [Tests are a WIP] 158-simplify-cloudwatch-to-graphite-app-remove-leadbutt-dependency 158-simplify-cloudwatch-to-graphite-app-remove-leadbutt-dependency Sep 23, 2018
Copy link
Contributor

@katstevens katstevens left a comment

Choose a reason for hiding this comment

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

Nice work on this - just a few minor tweaks suggested.

README.md Outdated
@@ -3,7 +3,8 @@ Digital Marketplace Cloudwatch to Graphite

## Purpose

Ships Cloudwatch metrics to Hosted Graphite using https://github.com/crccheck/cloudwatch-to-graphite.
Ships Cloudwatch metrics to Hosted Graphite.
Heavily draws on the work of https://github.com/crccheck/cloudwatch-to-graphite
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we be more specific and say which bits we have been 'inspired by'?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done 👍

@retry(wait_fixed=60000, retry_on_result=lambda res: res is None)
def call_leadbutt():
def create_hostedgraphite_base_metrics(config):
"""Take a metric entry from the config format it into a base metric used to initialise the metric on hostedgraphite.
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we simplify this to """Initialise each base metric in the config with a timestamp"""?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Almost, but we're also initialising it with a base number.

They come out like:
cloudwatch.request_time_buckets.preview.antivirus-api.request_time_bucket_0.sum 0.0 1537113600
{metric_name} {base_value} {timestamp}

Setting a starting point for the metric.

Copy link
Contributor

@katstevens katstevens Sep 24, 2018

Choose a reason for hiding this comment

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

OK - Initialise each base metric in the config with a timestamp and base number then? 😸 The current docstring is a bit hard to read.



def get_metric_from_cloudwatch(client, metric):
"""Call the client once for th supplied metric."""
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo th?

def setup_method(self):
# Mock out boto3 cloudwatch client
self.client = boto3.client('cloudwatch', region_name="eu-west-1")
stubber = Stubber(self.client)
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting! Is this a better option than moto?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so in so much as it prevents another dependency and should be supported for the lifetime of the boto3 lib. Pretty nifty, but it hasn't been around for that long. I expect the dependency in utils was added before this existed.


# Mock out send_to_hostedgraphite method
self.send_to_hostedgraphite_mock = mock.patch('app.send_to_hostedgraphite')
self.send_to_hostedgraphite_mock.start()
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we have an equivalent .stop() in a teardown_method()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good shout 👍 Done

@benvand benvand force-pushed the 158-simplify-cloudwatch-to-graphite-app-remove-leadbutt-dependency branch 3 times, most recently from c6f2d27 to dd64f14 Compare September 24, 2018 11:41
README.md Outdated
@@ -3,9 +3,10 @@ Digital Marketplace Cloudwatch to Graphite

## Purpose

Ships Cloudwatch metrics to Hosted Graphite using https://github.com/crccheck/cloudwatch-to-graphite.
Ships Cloudwatch metrics to Hosted Graphite.
Heavily on the work of https://github.com/crccheck/cloudwatch-to-graphite and the `leadbutt` script. Particularly config parsing to define metrics.
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 (Though I think it's now missing a word: Heavily based?)

Copy link
Contributor

@katstevens katstevens left a comment

Choose a reason for hiding this comment

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

👍

@benvand benvand force-pushed the 158-simplify-cloudwatch-to-graphite-app-remove-leadbutt-dependency branch from dd64f14 to 2c075f3 Compare September 25, 2018 11:56
Refactor/ simplification of the app:

* Remove `pipe-watchdog.py` entirely
  * We're no longer using pipes to direct the output. An error should be output to the cloudwatch error logs on failure `cf logs --recent digitalmarketplace-cloudwatch-to-graphite-preview`
  * Remove this from the run command in `manifest.yml.example`

* Add `flake8` dependency and `.flake8` file in line with GDS recommendations
* Activate travis service on repo to check Flake8 rules and requirements files
  * Add `.travis.yml`

* Add `Makefile` with shortcuts for running requirements freeze and flake8
* Update `README.md`
* Change requirements handling
  * Add `requirements-app.txt`
  * Ensure `requirements.txt` contains full pinned requirements
  * Add `flake8` requirement for devs

* Rewrite `app.py`
  * Add function descriptions to all functions
  * Remove dependency on `leadbutt`, `signal` and `subprocess`
  * Use `boto3` instead of `leadbutt`s `boto` (https://github.com/crccheck/cloudwatch-to-graphite/blob/master/leadbutt.py#L25)
  * Remove `get_timestamp` method. The same can be achieved with `time.time`
  * `logger.warn` is deprecated, use `logger.warning` instead
  * Bring string formatting in line with rest of strings in file
  * Rename `initialize_metrics ` to `create_hostedgraphite_base_metrics` for better description
  * Remove `call_leadbutt` function and replace with code calling aws ourselves. Extrapolated from (https://github.com/crccheck/cloudwatch-to-graphite/blob/master/leadbutt.py)
  *  Change `loglevel` to `INFO` this is all that is required now we're not piping output
  * Move retry decorator on to wrapper method in `if __name__` block

* Update `config.yaml.j2` template
  * `Dimensions` default = `{}` to avoid type manipulations
  * Remove `Auth` section from config. This can be hard coded until we have any need to specify it as a config variable
  * Remove `Options.Count` again, this can be hard coded. It's only used in one place and we've never needed to change it

Extrapolate code out of leadbutt
This code drops leadbutts retry functionality. Boto3 has the capacity to retry failed attempts. We should prefer this.
As per rholder/retrying#79 retrying is dead. Tenaciy is a community maintained fork.
This also fixes a problem with retry swallowing exceptions.
@benvand benvand force-pushed the 158-simplify-cloudwatch-to-graphite-app-remove-leadbutt-dependency branch from 2c075f3 to 20acba6 Compare September 25, 2018 17:19
@benvand benvand merged commit 08a9730 into master Sep 25, 2018
@benvand benvand deleted the 158-simplify-cloudwatch-to-graphite-app-remove-leadbutt-dependency branch September 25, 2018 17:22
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants