Repository: MatterMiners/tardis

Issue:
Add information about hostname to logging config
The logging configuration should include information about the *hostname*. Currently it works implicitly via the global configuration for telegraf but this can be disabled by the user.

To ensure the availability of information about the host and reduce external dependency we should explicitly include host information into the tardis config:

* [x] include `hostname` information in tardis config
* [x] add documentation about `hostname` field in config

Discussion:
codecov-io: # [Codecov](https://codecov.io/gh/MatterMiners/tardis/pull/77?src=pr&el=h1) Report
> Merging [#77](https://codecov.io/gh/MatterMiners/tardis/pull/77?src=pr&el=desc) into [master](https://codecov.io/gh/MatterMiners/tardis/commit/06d67fbbd6e3a4cf6cee51f871f86f3fdc25405a?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/MatterMiners/tardis/pull/77/graphs/tree.svg?width=650&token=BKeuDMxoaj&height=150&src=pr)](https://codecov.io/gh/MatterMiners/tardis/pull/77?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master      #77      +/-   ##
==========================================
+ Coverage   94.61%   94.62%   +<.01%     
==========================================
  Files          39       39              
  Lines        1355     1357       +2     
==========================================
+ Hits         1282     1284       +2     
  Misses         73       73
```


| [Impacted Files](https://codecov.io/gh/MatterMiners/tardis/pull/77?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [tardis/plugins/telegrafmonitoring.py](https://codecov.io/gh/MatterMiners/tardis/pull/77/diff?src=pr&el=tree#diff-dGFyZGlzL3BsdWdpbnMvdGVsZWdyYWZtb25pdG9yaW5nLnB5) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/MatterMiners/tardis/pull/77?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/MatterMiners/tardis/pull/77?src=pr&el=footer). Last update [06d67fb...b5b6b45](https://codecov.io/gh/MatterMiners/tardis/pull/77?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/docs/source/plugins/plugins.rst b/docs/source/plugins/plugins.rst
index ab4d881..bf26cec 100644
--- a/docs/source/plugins/plugins.rst
+++ b/docs/source/plugins/plugins.rst
@@ -2,7 +2,7 @@
 Plugins
 =======
 
-All plugins are configured within the `plugins` section of the TARDIS configuration. Using multiple plugins is```
+All plugins are configured within the `plugins` section of the TARDIS configuration. Using multiple plugins is
 supported by using a separate MappingNode per plugin.
 
 .. code-block:: yaml
@@ -42,6 +42,10 @@ Telegraf Monitoring
 The :py:class:`~tardis.plugins.telegrafmonitoring.TelegrafMonitoring` implements an interface to monitor state changes
 of the Drones in a telegraf service running a UDP input module.
 
+.. Note::
+    By default the machine name of the host running tardis is added as default tag. It can be overwritten by adding
+    `tardis_machine_name: 'something_else'` as `default_tag` in the configuration.
+
 Available configuration options
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
diff --git a/tardis/plugins/telegrafmonitoring.py b/tardis/plugins/telegrafmonitoring.py
index bae7b19..a94f975 100644
--- a/tardis/plugins/telegrafmonitoring.py
+++ b/tardis/plugins/telegrafmonitoring.py
@@ -6,6 +6,7 @@ from ..utilities.attributedict import AttributeDict
 import aiotelegraf
 from datetime import datetime
 import logging
+import platform
 
 
 class TelegrafMonitoring(Plugin):
@@ -20,7 +21,8 @@ class TelegrafMonitoring(Plugin):
 
         host = config.host
         port = config.port
-        default_tags = getattr(config, 'default_tags', None)
+        default_tags = dict(tardis_machine_name=platform.node())
+        default_tags.update(getattr(config, 'default_tags', {}))
         self.metric = getattr(config, 'metric', 'tardis_data')
 
         self.client = aiotelegraf.Client(host=host, port=port, tags=default_tags)


Test patch:
diff --git a/tests/plugins_t/test_telegrafmonitoring.py b/tests/plugins_t/test_telegrafmonitoring.py
index 955dd24..c2da69f 100644
--- a/tests/plugins_t/test_telegrafmonitoring.py
+++ b/tests/plugins_t/test_telegrafmonitoring.py
@@ -10,6 +10,8 @@ from unittest.mock import patch
 from ..utilities.utilities import async_return
 from ..utilities.utilities import run_async
 
+import platform
+
 
 class TestTelegrafMonitoring(TestCase):
     @classmethod
@@ -26,11 +28,11 @@ class TestTelegrafMonitoring(TestCase):
 
     @patch('tardis.plugins.telegrafmonitoring.logging', Mock())
     def setUp(self):
-        config = self.mock_config.return_value
-        config.Plugins.TelegrafMonitoring.host = "telegraf.test"
-        config.Plugins.TelegrafMonitoring.port = 1234
-        config.Plugins.TelegrafMonitoring.default_tags = {'test_tag': 'test'}
-        config.Plugins.TelegrafMonitoring.metric = "tardis_data"
+        self.config = self.mock_config.return_value
+        self.config.Plugins.TelegrafMonitoring.host = "telegraf.test"
+        self.config.Plugins.TelegrafMonitoring.port = 1234
+        self.config.Plugins.TelegrafMonitoring.default_tags = {'test_tag': 'test'}
+        self.config.Plugins.TelegrafMonitoring.metric = "tardis_data"
 
         telegraf_client = self.mock_aiotelegraf.Client.return_value
         telegraf_client.connect.return_value = async_return()
@@ -50,7 +52,24 @@ class TestTelegrafMonitoring(TestCase):
                            updated=datetime.timestamp(test_param.updated))
         test_tags = dict(site_name=test_param.site_name, machine_type=test_param.machine_type)
         run_async(self.plugin.notify, test_state, test_param)
-        self.mock_aiotelegraf.Client.assert_called_with(host='telegraf.test', port=1234, tags={'test_tag': 'test'})
+        self.mock_aiotelegraf.Client.assert_called_with(host='telegraf.test', port=1234,
+                                                        tags={'tardis_machine_name': platform.node(),
+                                                              'test_tag': 'test'})
+        self.mock_aiotelegraf.Client.return_value.connect.assert_called_with()
+        self.mock_aiotelegraf.Client.return_value.metric.assert_called_with('tardis_data', test_result, tags=test_tags)
+        self.mock_aiotelegraf.Client.return_value.close.assert_called_with()
+
+        self.mock_aiotelegraf.reset()
+
+        # Test to overwrite tardis_node_name in plugin configuration
+        self.config.Plugins.TelegrafMonitoring.default_tags = {'test_tag': 'test',
+                                                               'tardis_machine_name': 'my_test_node'}
+        self.plugin = TelegrafMonitoring()
+        run_async(self.plugin.notify, test_state, test_param)
+
+        self.mock_aiotelegraf.Client.assert_called_with(host='telegraf.test', port=1234,
+                                                        tags={'tardis_machine_name': 'my_test_node',
+                                                              'test_tag': 'test'})
         self.mock_aiotelegraf.Client.return_value.connect.assert_called_with()
         self.mock_aiotelegraf.Client.return_value.metric.assert_called_with('tardis_data', test_result, tags=test_tags)
         self.mock_aiotelegraf.Client.return_value.close.assert_called_with()