Skip to content
This repository has been archived by the owner on May 19, 2021. It is now read-only.

Commit

Permalink
Prevent scrape failures for months w/o usage details (fixes #9) (#10)
Browse files Browse the repository at this point in the history
* add failing tests for an "empty" month
* special treatment for "empty" moths
* adapted changelog accordingly
* moved the API output for an empty month to the sample_data file
  • Loading branch information
ManuelBahr authored Jun 9, 2017
1 parent 2abd1cb commit a90bc02
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Change Log
All notable changes to this project are noted in this file. This project adheres to [Semantic
Versioning](http://semver.org/).

0.3.1
-----

- Fixed the exporter to cope with the non-standard response for months
without usage details.


0.3.0
-----
Expand Down
4 changes: 4 additions & 0 deletions azure_costs_exporter/prometheus_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def _get_azure_data(self, month=None):
rsp = requests.get(url, headers=headers, timeout=10)
rsp.raise_for_status()

if rsp.text.startswith('"Usage Data Extract"'):
# special treatement for no usage details. Azure API doesn't return a JSON document in that case...
return dict()

return rsp.json()

def _create_counter(self):
Expand Down
4 changes: 4 additions & 0 deletions tests/data.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
api_output_for_empty_months = """"Usage Data Extract",
"",
"AccountOwnerId","Account Name","ServiceAdministratorId","SubscriptionId","SubscriptionGuid","Subscription Name","Date","Month","Day","Year","Product","Meter ID","Meter Category","Meter Sub-Category","Meter Region","Meter Name","Consumed Quantity","ResourceRate","ExtendedCost","Resource Location","Consumed Service","Instance ID","ServiceInfo1","ServiceInfo2","AdditionalInfo","Tags","Store Service Identifier","Department Name","Cost Center","Unit Of Measure","Resource Group",'
"""
sample_data = [{u'AccountName': u'Platform',
u'AccountOwnerId': u'donald.duck',
u'AdditionalInfo': u'',
Expand Down
20 changes: 19 additions & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import datetime

from azure_costs_exporter.main import create_app
from .data import sample_data
from .data import sample_data, api_output_for_empty_months


@pytest.fixture
Expand Down Expand Up @@ -61,6 +61,24 @@ def test_metrics(app, now, enrollment):
assert rsp.data.count(b'azure_costs_eur') == 4


@responses.activate
def test_metrics_no_usage(app, now, enrollment):


responses.add(
method='GET',
url="https://ea.azure.com/rest/{0}/usage-report?month={1}&type=detail&fmt=Json".format(enrollment, now),
match_querystring=True,
body=api_output_for_empty_months
)

rsp = app.test_client().get('/metrics')
assert rsp.status_code == 200

# expect only metric definition and help but no content in the output
assert rsp.data.count(b'azure_costs_eur') == 2


@responses.activate
def test_failing_target(client, now):
responses.add(
Expand Down
33 changes: 29 additions & 4 deletions tests/test_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from azure_costs_exporter.prometheus_collector import convert_json_df, AzureEABillingCollector
from azure_costs_exporter.prometheus_collector import base_columns, cost_column

from .data import sample_data
from .data import sample_data, api_output_for_empty_months


def current_month():
Expand Down Expand Up @@ -59,15 +59,40 @@ def test_extract_metrics():
@responses.activate
def test_get_azure_data():

enrollment='12345'
enrollment = '123'
base_url = "https://ea.azure.com/rest/{}/usage-report".format(enrollment)
params = "?month={}&type=detail&fmt=Json".format(current_month())

c = AzureEABillingCollector('cloud_costs', enrollment, 'abc123xyz')

responses.add(
method='GET',
url="https://ea.azure.com/rest/{}/usage-report?month=2017-03&type=detail&fmt=Json".format(enrollment),
url=base_url+params,
match_querystring=True,
json=sample_data
)

data = c._get_azure_data('2017-03')
data = c._get_azure_data(current_month())
assert data == sample_data


@responses.activate
def test_empty_month():
"""
If no usage details have are available for a given month the API does not return a JSON document.
"""
enrollment = '123'
base_url = "https://ea.azure.com/rest/{}/usage-report".format(enrollment)
params = "?month={}&type=detail&fmt=Json".format(current_month())

c = AzureEABillingCollector('cloud_costs', enrollment, 'abc123xyz')

responses.add(
method='GET',
url=base_url+params,
match_querystring=True,
body=api_output_for_empty_months
)

data = c._get_azure_data(current_month())
assert data == dict()

0 comments on commit a90bc02

Please sign in to comment.