@@ -143,11 +143,33 @@ class YarnCheck(AgentCheck):
143143 '''
144144 Extract statistics from YARN's ResourceManger REST API
145145 '''
146+ _ALLOWED_APPLICATION_TAGS = [
147+ 'applicationTags' ,
148+ 'applicationType' ,
149+ 'name' ,
150+ 'queue' ,
151+ 'user'
152+ ]
146153
147154 def check (self , instance ):
148155
149156 # Get properties from conf file
150157 rm_address = instance .get ('resourcemanager_uri' , DEFAULT_RM_URI )
158+ app_tags = instance .get ('application_tags' , {})
159+
160+ if type (app_tags ) is not dict :
161+ self .log .error ('application_tags is incorrect: %s is not a dictionary' , app_tags )
162+ app_tags = {}
163+
164+ filtered_app_tags = {}
165+ for dd_prefix , yarn_key in app_tags .iteritems ():
166+ if yarn_key in self ._ALLOWED_APPLICATION_TAGS :
167+ filtered_app_tags [dd_prefix ] = yarn_key
168+ app_tags = filtered_app_tags
169+
170+ # Collected by default
171+ app_tags ['app_name' ] = 'name'
172+
151173
152174 # Get additional tags from the conf file
153175 tags = instance .get ('tags' , [])
@@ -166,7 +188,7 @@ def check(self, instance):
166188
167189 # Get metrics from the Resource Manager
168190 self ._yarn_cluster_metrics (rm_address , tags )
169- self ._yarn_app_metrics (rm_address , tags )
191+ self ._yarn_app_metrics (rm_address , app_tags , tags )
170192 self ._yarn_node_metrics (rm_address , tags )
171193
172194 def _yarn_cluster_metrics (self , rm_address , addl_tags ):
@@ -182,44 +204,50 @@ def _yarn_cluster_metrics(self, rm_address, addl_tags):
182204 if yarn_metrics is not None :
183205 self ._set_yarn_metrics_from_json (addl_tags , yarn_metrics , YARN_CLUSTER_METRICS )
184206
185- def _yarn_app_metrics (self , rm_address , addl_tags ):
207+ def _yarn_app_metrics (self , rm_address , app_tags , addl_tags ):
186208 '''
187209 Get metrics for running applications
188210 '''
189- metrics_json = self ._rest_request_to_json (rm_address ,
211+ metrics_json = self ._rest_request_to_json (
212+ rm_address ,
190213 YARN_APPS_PATH ,
191- states = YARN_APPLICATION_STATES )
214+ states = YARN_APPLICATION_STATES
215+ )
192216
193- if metrics_json :
194- if metrics_json ['apps' ] is not None :
195- if metrics_json ['apps' ]['app' ] is not None :
217+ if (metrics_json and metrics_json ['apps' ] is not None and
218+ metrics_json ['apps' ]['app' ] is not None ):
196219
197- for app_json in metrics_json ['apps' ]['app' ]:
220+ for app_json in metrics_json ['apps' ]['app' ]:
198221
199- app_name = app_json ['name' ]
222+ tags = []
223+ for dd_tag , yarn_key in app_tags .iteritems ():
224+ try :
225+ tags .append ("{tag}:{value}" .format (
226+ tag = dd_tag , value = app_json [yarn_key ]
227+ ))
228+ except KeyError :
229+ self .log .error ("Invalid value %s for application_tag" , yarn_key )
200230
201- tags = ['app_name:%s' % str (app_name )]
202- tags .extend (addl_tags )
231+ tags .extend (addl_tags )
203232
204- self ._set_yarn_metrics_from_json (tags , app_json , YARN_APP_METRICS )
233+ self ._set_yarn_metrics_from_json (tags , app_json , YARN_APP_METRICS )
205234
206235 def _yarn_node_metrics (self , rm_address , addl_tags ):
207236 '''
208237 Get metrics related to YARN nodes
209238 '''
210239 metrics_json = self ._rest_request_to_json (rm_address , YARN_NODES_PATH )
211240
212- if metrics_json :
213- if metrics_json ['nodes' ] is not None :
214- if metrics_json ['nodes' ]['node' ] is not None :
241+ if (metrics_json and metrics_json ['nodes' ] is not None and
242+ metrics_json ['nodes' ]['node' ] is not None ):
215243
216- for node_json in metrics_json ['nodes' ]['node' ]:
217- node_id = node_json ['id' ]
244+ for node_json in metrics_json ['nodes' ]['node' ]:
245+ node_id = node_json ['id' ]
218246
219- tags = ['node_id:%s' % str (node_id )]
220- tags .extend (addl_tags )
247+ tags = ['node_id:%s' % str (node_id )]
248+ tags .extend (addl_tags )
221249
222- self ._set_yarn_metrics_from_json (tags , node_json , YARN_NODE_METRICS )
250+ self ._set_yarn_metrics_from_json (tags , node_json , YARN_NODE_METRICS )
223251
224252 def _set_yarn_metrics_from_json (self , tags , metrics_json , yarn_metrics ):
225253 '''
@@ -243,7 +271,7 @@ def _set_metric(self, metric_name, metric_type, value, tags=None, device_name=No
243271 elif metric_type == INCREMENT :
244272 self .increment (metric_name , value , tags = tags , device_name = device_name )
245273 else :
246- self .log .error ('Metric type "%s" unknown' % ( metric_type ) )
274+ self .log .error ('Metric type "%s" unknown' , metric_type )
247275
248276 def _rest_request_to_json (self , address , object_path , * args , ** kwargs ):
249277 '''
0 commit comments