Class: ManageEngine::APMMetricsFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/agent/metrics/am_metricsformatter.rb

Instance Method Summary collapse

Instance Method Details

#apxarray(apx_stat, rt) ⇒ Object

Updates apdex score and increases statisfied, tolerating, frustrated count accordingly



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/agent/metrics/am_metricsformatter.rb', line 386

def apxarray apx_stat,rt 

	#	Apmagent::ApmLogger.instance.info "apxarray : start #{apx_stat}"
	apx_stat = updatert apx_stat,rt 
	if rt <= @apdex_threshold
		apx_stat[5] = apx_stat[5] + 1 
	elsif rt > (4 * @apdex_threshold)
		apx_stat[7] = apx_stat[7] + 1 
	else
		apx_stat[6] = apx_stat[6] + 1 
	end		

	if (apx_stat[3] > 0)
	  apx_stat[4] = (apx_stat[5].to_f + (apx_stat[6].to_f/2).to_f)/apx_stat[3].to_f
	end
	#	Apmagent::ApmLogger.instance.info "apxarray : end #{apx_stat}"
	apx_stat
end

#format(d) ⇒ Object

trans Vs #[0-rspTime,1-min rt,2-max rt,3-cnt,4-apdx,5-stat,6-toler,7-frustating,8-error_count] DBtrans Vs #[rspTime,min rt,max rt,cnt,error_count] trace



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/agent/metrics/am_metricsformatter.rb', line 13

def format d
	result = Array.new
	@obj = ManageEngine::APMObjectHolder.instance
	begin
		@apdex_threshold = @obj.config.apdex_t * 1000
		#@obj.log.info "[FORMAT]"
		@transaction = Hash.new
		@bgTransaction = Hash.new
		@db = Hash.new
		@instance = Hash.new
		@bginstance = Hash.new
		@dbinstance = Hash.new
		@dboperations = Hash.new
		@keystoremove = Array.new
		d.each do |key,value|
			@keystoremove.push(key)
			updatetransaction value
		end
		updateinstance
		updatebginstance
		updatedbinstance

		@transaction.each do |key,value|
			res = Hash.new
			res[@obj.constants.mf_namespace] = key
			res[@obj.constants.mf_name] =	@obj.constants.mf_apdex
			valArr= Array.new
			valArr[0] =res
			valArr[1] =value
			result.push(valArr)
		end

		@bgTransaction.each do |key,value|
            res = Hash.new
            res[@obj.constants.mf_namespace] = key
            res[@obj.constants.mf_name] = @obj.constants.mf_bckgrnd
            valArr= Array.new
            valArr[0] =res
            valArr[1] =value
            result.push(valArr)
          end
          
		@db.each do |key,value|
			#puts "#{key} == #{value}"
			res = Hash.new
			res[@obj.constants.mf_namespace] = value["tpath"]
			res[@obj.constants.mf_name] = value["path"]
			valArr= Array.new
			valArr[0] =res
			valArr[1] =value["metrics"]
			result.push(valArr)
		end
		@instance.each do |key,value|
			res = Hash.new
			res[@obj.constants.mf_namespace] = ""
			res[@obj.constants.mf_name] = 	@obj.constants.mf_apdex
			valArr= Array.new
			valArr[0] =res
			valArr[1] =value
			result.push(valArr)
		end
		@bginstance.each do |key,value|
        res = Hash.new
        res[@obj.constants.mf_namespace] = ""
        res[@obj.constants.mf_name] =   @obj.constants.mf_bckgrnd
        valArr= Array.new
        valArr[0] =res
        valArr[1] =value
        result.push(valArr)
      end
		@dbinstance.each do |key,value|
			res = Hash.new
			res[@obj.constants.mf_namespace] = ""
			res[@obj.constants.mf_name] =	@obj.constants.mf_db + @obj.constants.mf_separator + @obj.constants.mf_all + @obj.constants.mf_separator + @obj.constants.mf_all + @obj.constants.mf_separator + @obj.config.app_db # "db/all/all/dummydb"
			valArr= Array.new
			valArr[0] =res
			valArr[1] =value
			result.push(valArr)
		end
		@dboperations.each do |key,value|
			ind = @obj.config.db_operations.index(key)
			if (ind!=nil)	
				res = Hash.new
				res[@obj.constants.mf_namespace] = ""
				res[@obj.constants.mf_name] = @obj.constants.mf_db + @obj.constants.mf_separator + "#{key}" + @obj.constants.mf_separator + @obj.constants.mf_all + @obj.constants.mf_separator + @obj.config.app_db #  "db/"+key+"/all/dummydb"
				valArr= Array.new
				valArr[0] =res
				valArr[1] =value
				result.push(valArr)
			end
		end

		#@obj.log.info "[FORMAT] COMPLETED"
	rescue Exception=>e
		@obj.log.logException "[FORMAT]#{e.message}",e
	end
	@transaction.clear
	@db.clear
	@instance.clear
	result
end

#intializeObject



5
6
7
8
# File 'lib/agent/metrics/am_metricsformatter.rb', line 5

def intialize
	@obj = ManageEngine::APMObjectHolder.instance
	@apdex_threshold = 0
end

#keysToRemoveObject



115
116
117
# File 'lib/agent/metrics/am_metricsformatter.rb', line 115

def keysToRemove
	@keystoremove
end

#updatebginstanceObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/agent/metrics/am_metricsformatter.rb', line 174

def updatebginstance
  if (@bgTransaction.size == 0)
    return
  end 
  
  stats = [0,-1,-1,0,0]
  logmetric = Hash.new
  error_rt = 0
  @bgTransaction.each do |key,value|
    txnValue = value[0]
    stats[0] += txnValue[0]
    if stats[1] == -1
      stats[1] = txnValue[1]
      stats[2] = txnValue[2]
    else
      if(txnValue[1]<stats[1])
        stats[1] = txnValue[1]
      end
      if (txnValue[2]>stats[2])
        stats[2] = txnValue[2]
      end
    end
    stats[3] += txnValue[3]
    stats[4] += txnValue[4]  
    
    ert =  value[1][@obj.constants.error_rt]
    if (ert != nil)
      error_rt += ert
    end
    exceptions = value[1][@obj.constants.mf_logmetric]
    if (exceptions != nil)
      exceptions.each do |name, count|
        logmetric[name] = logmetric[name].to_i + count
      end
    end
  end
  @bginstance[":bckgrnd"]=[stats, {@obj.constants.mf_logmetric=>logmetric, @obj.constants.error_rt=>error_rt}]
end

#updatedb(dpl, tpath) ⇒ Object

DBtrans Vs #[rspTime,min rt,max rt,cnt,error_count]



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/agent/metrics/am_metricsformatter.rb', line 421

def updatedb dpl,tpath
	#	Apmagent::ApmLogger.instance.info "updatedb : start"
	dpl.each do |pl|
		rt = pl["rt"].round(2)
		path = pl["sql-strip"]
		dpath = @obj.constants.mf_db + @obj.constants.mf_separator + path
		path = tpath + @obj.constants.mf_separator + dpath
		sql = pl["sql"]
		stat =  nil
		val = nil
		if(@db.has_key?(path))
			val = @db[path]
			stat = val["metrics"]
		else
			val=Hash.new
			val["tpath"] = tpath
        val["path"] = dpath
			stat = Array.new
			stat = [0,rt,rt,0,0]
		end
		if (pl.has_key?("error"))
		  stat[4] += 1
		else
		  stat = updatert stat,rt
		end
		val["metrics"] = stat
		@db[path] = val
		updatedboperations rt, pl["operation"], pl["error"]
	end
	#Apmagent::ApmLogger.instance.info "updatedb : end"
end

#updatedbinstanceObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/agent/metrics/am_metricsformatter.rb', line 213

def updatedbinstance
	cnt = 0;
	rt = 0;
	min = -1;
	max = 0;
	error_count = 0;
	if(@db.length>0)
		@db.each do |key,val|
			value = val["metrics"]
			rt = rt + value[0]
			if min == -1
				min = value[1]
				max = value[2]
			end
			if(value[1]<min)
				min = value[1]
			end
			if (value[2]>max)
				max = value[2]
			end
			cnt = cnt  + value[3]
			error_count += value[4]
		end
		@dbinstance[":apdex"]=[rt.round(2),min,max,cnt,error_count]
	end
end

#updatedboperations(rt, operation, isError) ⇒ Object



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/agent/metrics/am_metricsformatter.rb', line 453

def updatedboperations rt, operation, isError
	if(@dboperations.has_key?(operation))
		opstats = @dboperations[operation]
	else
	  opstats = Array.new;
		opstats = [0,rt,rt,0,0]
	end
	
	if (isError)
	  opstats[4] += 1
	else
			opstats[0] = opstats[0] + rt
			if(rt<opstats[1])
				opstats[1] = rt
			end
			if (rt>opstats[2])
				opstats[2] = rt
			end
			opstats[3] = opstats[3] +1
		end
	@dboperations[operation]=opstats
end

#updateinstanceObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/agent/metrics/am_metricsformatter.rb', line 119

def updateinstance
  if (@transaction.size == 0)
    return
  end 
  
	ins_apdx = [0,-1,-1,0,0,0,0,0,0]
	logmetric = Hash.new
	error_rt = 0
	httpstatus = Hash.new
	
	@transaction.each do |key,value|
	  apdexValue = value[0]
	  ins_apdx[0] += apdexValue[0]
		if ins_apdx[1] == -1
		  ins_apdx[1] = apdexValue[1]
		  ins_apdx[2] = apdexValue[2]
		else
				if(apdexValue[1]<ins_apdx[1])
				  ins_apdx[1] = apdexValue[1]
        end
        if (apdexValue[2]>ins_apdx[2])
          ins_apdx[2] = apdexValue[2]
        end
		end
		ins_apdx[3] += apdexValue[3]

		ins_apdx[5] += apdexValue[5]	
		ins_apdx[6] += apdexValue[6]	
		ins_apdx[7] += apdexValue[7]	
		ins_apdx[8] += apdexValue[8]
		ert =  value[1][@obj.constants.error_rt]
		if (ert != nil)
		  error_rt += ert
		end
		exceptions = value[1][@obj.constants.mf_logmetric]
		if (exceptions != nil)
		  logmetric = logmetric.merge(exceptions) { |key, oldval, newval| oldval + newval }
#  				exceptions.each do |name, count|
#  				  logmetric[name] = logmetric[name].to_i + count
#  				end
		end
		
      status = value[1][@obj.constants.httpstatus]
      if (status != nil)
        httpstatus = httpstatus.merge(status) { |key, oldval, newval| oldval + newval }
      end
	end
	if (ins_apdx[3] > 0)
	  ins_apdx[4] = (ins_apdx[5].to_f + (ins_apdx[6]/2).to_f).to_f/ins_apdx[3].to_f
	  ins_apdx[0] = ins_apdx[0].round(2)
	end
	
	@instance[":apdex"]=[ins_apdx, {@obj.constants.mf_logmetric=>logmetric, @obj.constants.error_rt=>error_rt, @obj.constants.httpstatus=>httpstatus}]
end

#updatert(apx_stat, rt) ⇒ Object

Updates resp time, min rt and max rt in apdex metric



406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/agent/metrics/am_metricsformatter.rb', line 406

def updatert apx_stat,rt 
	#	Apmagent::ApmLogger.instance.info "updatert : start"
	apx_stat[3] =  apx_stat[3] + 1
	apx_stat[0] = apx_stat[0] + rt
	if(apx_stat[1] == 0 || rt < apx_stat[1])
		apx_stat[1] = rt
	end
	if(rt > apx_stat[2])
		apx_stat[2] = rt
	end
	#Apmagent::ApmLogger.instance.info "updatert : end"
	apx_stat
end

#updatetransaction(d) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/agent/metrics/am_metricsformatter.rb', line 240

def updatetransaction d
	begin
		pl = d["td"]
		dbl = d["db"]
		exc = d["exception"]
#				comp = d["comp"]

		rt = pl["rt"].round(2)
#				path = @obj.constants.mf_transaction + @obj.constants.mf_separator + pl["path"]
		  path = pl["path"]
		
		if (pl["type"] == @obj.constants.mf_http)
				apx_stat =  nil
				additionalInfo = nil
				if(@transaction.has_key?(path))
					apx_stat = @transaction[path][0]
					additionalInfo = @transaction[path][1]
				else
					if @transaction.length == @obj.config.metric_overflow_t
					  @obj.log.debug "Metricstore overflow. Current Size: #{@obj.config.metric_overflow_t} #{path}"
					  return
					end
					
					apx_stat = Array.new
					apx_stat = [0,0,0,0,0,0,0,0,0]
					additionalInfo = Hash.new
				end
				
				if (pl.has_key?("error"))
				  apx_stat[8] += 1
				  if (additionalInfo[@obj.constants.error_rt] == nil)
            additionalInfo[@obj.constants.error_rt] = rt
          else
            additionalInfo[@obj.constants.error_rt] += rt
          end
				else
				  apx_stat = apxarray apx_stat,rt
				end
		
				if (pl.has_key?("status"))
				  statusHash = additionalInfo[@obj.constants.httpstatus]
				   if (statusHash == nil)
				     statusHash = Hash.new
             additionalInfo[@obj.constants.httpstatus] = statusHash
				   end
				   
				   if (statusHash.has_key?(pl["status"]))
				      statusHash[pl["status"]] += 1
				   else
				      statusHash[pl["status"]] = 1
				   end
				end
				
				if (exc != nil)
				  logmetric = additionalInfo[@obj.constants.mf_logmetric]
				  if (logmetric == nil)
            additionalInfo[@obj.constants.mf_logmetric] = exc
				  else
            exc.each do |name, count|
              logmetric[name] = logmetric[name].to_i + count
            end
          end
				end
				
#  				if (comp != nil)
#  				  begin
#    				  compData = additionalInfo[@obj.constants.components]
#    				    if (compData == nil) 
#    				      compData = Array.new
#                  additionalInfo[@obj.constants.components] = compData
#    				    end
#    				    comp.each do |name, info|
#    				       if (compData.has)
#    				    end
#				    rescue Exception=>e
#  				  end
#  				end
				
				@transaction[path] = [apx_stat, additionalInfo]		
      else
        stat =  nil
        additionalInfo = nil
        if(@bgTransaction.has_key?(path))
          stat = @bgTransaction[path][0]
          additionalInfo = @bgTransaction[path][1]
        else
          if @bgTransaction.length == @obj.config.metric_overflow_t
            @obj.log.debug "Metricstore overflow. Current Size: #{@obj.config.metric_overflow_t} #{path}"
            return
          end
          
          stat = Array.new
          stat = [0,0,0,0,0]
          additionalInfo = Hash.new
        end
        
        if (pl.has_key?("error"))
          stat[4] += 1
          if (additionalInfo[@obj.constants.error_rt] == nil)
            additionalInfo[@obj.constants.error_rt] = rt
          else
            additionalInfo[@obj.constants.error_rt] += rt
          end
        else
          stat = updatert stat,rt
        end
      
        if (exc != nil)
          logmetric = additionalInfo[@obj.constants.mf_logmetric]
          if (logmetric == nil)
            additionalInfo[@obj.constants.mf_logmetric] = exc
          else
            exc.each do |name, count|
              logmetric[name] = logmetric[name].to_i + count
            end
          end
        end
        
        @bgTransaction[path] = [stat, additionalInfo]   
      end
		 
		if(dbl!=nil)
			if @db.length < @obj.config.dbmetric_overflow_t
				updatedb dbl,path
			elsif @db.length == @obj.config.dbmetric_overflow_t
				@obj.log.debug "DB metric overflow. Current Size: #{@obj.config.dbmetric_overflow_t} #{path}"
				#@obj.log.info "data = #{@db}"
				of = Hash.new
				stats = Array.new
				stats = [0,0,0,0,0]
				of["tpath"] = @obj.constants.mf_overflow
				#of["tpath"] = @obj.constants.mf_transaction + @obj.constants.mf_separator + @obj.constants.mf_overflow #using this for testing purpose
				of["path"] = @obj.constants.mf_db + @obj.constants.mf_separator + @obj.constants.mf_overflow + @obj.constants.mf_separator + "-" + @obj.constants.mf_separator
				of["metrics"] = stats
				@db[@obj.constants.mf_overflow]=of
				#@obj.log.info "data updated = #{@db}"
			end
		end
	rescue Exception=>e
		@obj.log.info "#{e.message}"
		@obj.log.logException "[Format] [ updatetransaction ] #{e.message}",e
	end
	#	Apmagent::ApmLogger.instance.info "update transaction end"
end