Merge "Remove the hostname from AFD when related to cluster"
diff --git a/heka/_service.sls b/heka/_service.sls
index 70d80ce..76754ab 100644
--- a/heka/_service.sls
+++ b/heka/_service.sls
@@ -270,6 +270,9 @@
       alarm_name: {{ alarm_name }}
       alarm: {{ alarm|yaml }}
       trigger: {{ service_metadata.get('trigger', {})|yaml }}
+      {%- if service_name != 'remote_collector' %}
+      hostname: {{ grains.host }}
+      {%- endif %}
 
 /usr/share/lma_collector/common/lma_{{ alarm_name|replace('-', '_') }}.lua:
   file.managed:
diff --git a/heka/files/lua/common/afd.lua b/heka/files/lua/common/afd.lua
index 0c4dbcd..2f0bea0 100644
--- a/heka/files/lua/common/afd.lua
+++ b/heka/files/lua/common/afd.lua
@@ -156,9 +156,12 @@
             alerting_enabled = alerting_enabled,
             notification_enabled = notification_enabled,
             notification_handler = notification_handler,
-            tag_fields = {'hostname', 'member'}
+            tag_fields = {'member'}
         }
     }
+    if hostname then
+        table.insert(msg.Fields.tag_fields, hostname)
+    end
 
     for name, value in pairs(dimensions) do
         table.insert(msg.Fields.tag_fields, name)
diff --git a/heka/files/lua/filters/afd.lua b/heka/files/lua/filters/afd.lua
index 4b8de07..0e18e33 100644
--- a/heka/files/lua/filters/afd.lua
+++ b/heka/files/lua/filters/afd.lua
@@ -21,7 +21,7 @@
 
 local afd_file = read_config('afd_file') or error('afd_file must be specified')
 local afd_name = read_config('afd_name') or error('afd_name must be specified')
-local hostname = read_config('hostname') or error('hostname must be specified')
+local hostname = read_config('hostname')
 local dimensions_json = read_config('dimensions') or ''
 local activate_alerting = read_config('activate_alerting') or true
 local enable_notification = read_config('enable_notification') or false
diff --git a/heka/files/lua/filters/gse_cluster_filter.lua b/heka/files/lua/filters/gse_cluster_filter.lua
index 8b46fcf..a800cb1 100644
--- a/heka/files/lua/filters/gse_cluster_filter.lua
+++ b/heka/files/lua/filters/gse_cluster_filter.lua
@@ -66,8 +66,8 @@
         return -1, "Cannot find alarms in the AFD/GSE message"
     end
 
-    -- "hostname" is nil here when the input message is a GSE message, so nil
-    -- is not an error condition here
+    -- "hostname" is nil here when the input message is a GSE message or
+    -- an AFD message related to a cluster.
     local hostname = afd.get_entity_name('hostname')
 
     local cluster_ids = gse.find_cluster_memberships(member_id)
diff --git a/heka/files/toml/filter/afd_alarm.toml b/heka/files/toml/filter/afd_alarm.toml
index 7244022..5d4590c 100644
--- a/heka/files/toml/filter/afd_alarm.toml
+++ b/heka/files/toml/filter/afd_alarm.toml
@@ -9,7 +9,9 @@
 [afd_{{ alarm_name }}_filter.config]
 afd_file = "lma_{{ alarm_name|replace('-', '_') }}"
 afd_name = "{{ alarm_name }}"
-hostname = "{{ grains.host }}"
+{%- if hostname is defined %}
+hostname = "{{ hostname }}"
+{%- endif %}
 dimensions = '{{ salt['heka_alarming.dimensions'](alarm)|json }}'
 {%- set alerting = alarm.get('alerting', 'enabled') %}
 activate_alerting = {{ salt['heka_alarming.alarm_activate_alerting'](alerting) }}