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

Replace = with : and force string conversion during serialization #25

Merged
merged 2 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sumologic_collectd_metrics/metrics_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def gen_tag(key, value):
"""
Tag is of form key=value
"""
validate_field(key, value, 'Key', 'Value')
validate_field(value, key, 'Value', 'Key')
key = validate_field(key, value, 'Key', 'Value')
value = validate_field(value, key, 'Value', 'Key')
if not key:
raise Exception('Key for value %s cannot be empty' % value)
elif key.lower() in _reserved_keywords:
Expand Down
20 changes: 15 additions & 5 deletions sumologic_collectd_metrics/metrics_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# -*- coding: utf-8 -*-

_reserved_symbols = frozenset([' ', '='])


_reserved_symbols = {
' ': None,
'=': ':',
}


def validate_non_empty(s, key):
Expand Down Expand Up @@ -33,12 +38,17 @@ def validate_field(s, f, l1, l2):
Field must be string that does not contains '=' or ' '
"""

validate_string_type(s, f, l1, l2)
# Convert field to string
s = str(s)

for reserved_symbol in _reserved_symbols:
for reserved_symbol, replacement in _reserved_symbols.items():
if reserved_symbol in s:
raise Exception('%s %s for %s %s must not contain reserved symbol \"%s\"' %
(l1, s, l2, f, reserved_symbol))
if replacement is None:
raise Exception('%s %s for %s %s must not contain reserved symbol \"%s\"' %
(l1, s, l2, f, reserved_symbol))
else:
s = s.replace(reserved_symbol, replacement)
return s


def validate_type(data, types):
Expand Down
12 changes: 6 additions & 6 deletions test/test_metrics_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ def test_gen_tag_key_word_exception():


def test_gen_key_not_string_exception():
with pytest.raises(Exception) as e:
gen_tag(('tag_key', ), 'tag_value')
value = gen_tag(('tag_key', ), 'tag_value')
expected = '[(\'tag_key\',)=tag_value'

assert "Key ('tag_key',) for Value tag_value must be string type." in str(e)
assert expected, value


def test_gen_value_not_string_exception():
with pytest.raises(Exception) as e:
gen_tag('tag_key', 1)
value = gen_tag(('tag_key', ), 1)
expected = 'tag_key=1'

assert "Value 1 for Key tag_key must be string type." in str(e)
assert expected, value


def test_tags_to_str():
Expand Down
37 changes: 37 additions & 0 deletions test/test_metrics_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,43 @@ def test_write_callback():
assert [metrics_writer.met_batcher.queue.get()] == d.metrics_str()


def test_write_callback_host_with_equal_char():
metrics_writer = Helper.default_writer()
config = CollectdConfig([Helper.url_node(), Helper.types_db_node()])
metrics_writer.parse_config(config)
metrics_writer.init_callback()
d = Values(host="[invalid=host]")
expected_value = ['host=[invalid:host] plugin=test_plugin plugin_instance=test_plugin_instance' \
' type=test_type type_instance=test_type_instance ds_name=test_ds_name ds_type=test_ds_type' \
' test_meta_key=test_meta_val 3.140000 1501775008']
metrics_writer.write_callback(d)

assert metrics_writer.met_batcher.queue.qsize() == 1
assert [metrics_writer.met_batcher.queue.get()] == expected_value


def test_write_callback_boolean_value():
metrics_writer = Helper.default_writer()
config = CollectdConfig([Helper.url_node(), Helper.types_db_node()])
metrics_writer.parse_config(config)
metrics_writer.init_callback()
d = Values(values=[True])
metrics_writer.write_callback(d)
assert metrics_writer.met_batcher.queue.qsize() == 1
assert [metrics_writer.met_batcher.queue.get()] == d.metrics_str()


def test_write_callback_boolean_tag():
metrics_writer = Helper.default_writer()
config = CollectdConfig([Helper.url_node(), Helper.types_db_node()])
metrics_writer.parse_config(config)
metrics_writer.init_callback()
d = Values(host=True)
metrics_writer.write_callback(d)
assert metrics_writer.met_batcher.queue.qsize() == 1
assert [metrics_writer.met_batcher.queue.get()] == d.metrics_str()


def test_shutdown_call_back():
metrics_writer = Helper.default_writer()
config = CollectdConfig([Helper.url_node(), Helper.types_db_node()])
Expand Down