Skip to content

Commit

Permalink
Merge pull request #83 from kfdm/silence-fix
Browse files Browse the repository at this point in the history
Fixes to silence date parsing
  • Loading branch information
kfdm authored Jun 14, 2018
2 parents 6bef82b + 04d9522 commit 0639bd2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
11 changes: 6 additions & 5 deletions promgen/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,19 +432,20 @@ def silence(labels, duration=None, **kwargs):
end = start + datetime.timedelta(days=int(duration[:-1]))
else:
raise Exception('Unknown time modifier')
kwargs['endsAt'] = end.strftime('%Y-%m-%dT%H:%M:%S.000Z')
kwargs['endsAt'] = end.isoformat()
else:
local_timezone = pytz.timezone(settings.PROMGEN.get('timezone', 'UTC'))
for key in ['startsAt', 'endsAt']:
kwargs[key] = parser.parse(kwargs[key])\
.replace(tzinfo=local_timezone)\
.astimezone(pytz.utc)\
.strftime('%Y-%m-%dT%H:%M:%S.000Z')
kwargs[key] = local_timezone.localize(
parser.parse(kwargs[key])
).isoformat()

kwargs['matchers'] = [{
'name': name,
'value': value,
'isRegex': True if value.endswith("*") else False
} for name, value in labels.items()]

logger.debug('Sending silence for %s', kwargs)
url = urljoin(settings.PROMGEN['alertmanager']['url'], '/api/v1/silences')
util.post(url, json=kwargs).raise_for_status()
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"name": "instance"
}],
"comment": "Silenced from Promgen",
"endsAt": "2017-12-14T00:01:00.000Z"
"endsAt": "2017-12-14T00:01:00+00:00"
}
11 changes: 11 additions & 0 deletions promgen/tests/examples/silence.range.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"createdBy": "Promgen",
"matchers": [{
"value": "example.com:[0-9]*",
"isRegex": true,
"name": "instance"
}],
"comment": "Silenced from Promgen",
"startsAt": "2017-12-14T00:01:00+09:00",
"endsAt": "2017-12-14T00:05:00+09:00"
}
27 changes: 24 additions & 3 deletions promgen/tests/test_silence.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
from promgen.tests import PromgenTest

TEST_SETTINGS = PromgenTest.data_yaml('examples', 'promgen.yml')
TEST_SILENCE = PromgenTest.data_json('examples', 'silence.json')
TEST_DURATION = PromgenTest.data_json('examples', 'silence.duration.json')
TEST_RANGE = PromgenTest.data_json('examples', 'silence.range.json')

# Explicitly set a timezone for our test to try to catch conversion errors
TEST_SETTINGS['timezone'] = 'Asia/Tokyo'


class SilenceTest(PromgenTest):
Expand All @@ -21,7 +25,7 @@ def setUp(self):

@override_settings(PROMGEN=TEST_SETTINGS)
@mock.patch('promgen.util.post')
def test_silence(self, mock_post):
def test_duration(self, mock_post):
with mock.patch('django.utils.timezone.now') as mock_now:
mock_now.return_value = datetime.datetime(2017, 12, 14, tzinfo=datetime.timezone.utc)
# I would prefer to be able to test with multiple labels, but since
Expand All @@ -35,5 +39,22 @@ def test_silence(self, mock_post):
)
mock_post.assert_called_with(
'http://alertmanager:9093/api/v1/silences',
json=TEST_SILENCE
json=TEST_DURATION
)

@override_settings(PROMGEN=TEST_SETTINGS)
@mock.patch('promgen.util.post')
def test_range(self, mock_post):
with mock.patch('django.utils.timezone.now') as mock_now:
mock_now.return_value = datetime.datetime(2017, 12, 14, tzinfo=datetime.timezone.utc)
self.client.post(reverse('silence'),
data={
'start': '2017-12-14 00:01',
'stop': '2017-12-14 00:05',
'label.instance': 'example.com:[0-9]*'
},
)
mock_post.assert_called_with(
'http://alertmanager:9093/api/v1/silences',
json=TEST_RANGE
)

0 comments on commit 0639bd2

Please sign in to comment.