Class: GCareRubyMethodProfiler

Inherits:
Object
  • Object
show all
Defined in:
lib/gcarerubymethodprofiler.rb

Overview

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clearObject



257
258
259
# File 'lib/gcarerubymethodprofiler.rb', line 257

def self.clear
  Thread.current[:_method_profiler] = nil
end

.decorateForOneArgument(klass, methods, name) ⇒ Object



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
212
213
# File 'lib/gcarerubymethodprofiler.rb', line 184

def self.decorateForOneArgument(klass, methods, name)
  patches = methods.map do |method_name|
    <<~RUBY
    unless defined?(#{method_name}__mp_unpatched)
      alias_method :#{method_name}__mp_unpatched, :#{method_name}
      def #{method_name}(*args, &blk)
        unless prof = Thread.current[:_method_profiler]
	puts 'test1'
          return #{method_name}__mp_unpatched(*args, &blk)
        end
        begin
          start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
	puts 'test2'
	a = __method__.to_s
	argsdata =  args.to_s
	puts a
	puts argsdata
          #{method_name}__mp_unpatched(*args, &blk)
        ensure
          data = (prof[:#{name}] ||= {duration: 0.0, calls: 0})
          data[:duration] += Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
          data[:calls] += 1
        end
      end
    end
    RUBY
  end.join("\n")

  klass.class_eval patches
end

.decorateHttp(methods, name, iKey, gcareurl, no_recurse: false) ⇒ Object



10
11
12
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
114
115
116
117
118
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
173
174
# File 'lib/gcarerubymethodprofiler.rb', line 10

def self.decorateHttp(methods, name, iKey, gcareurl, no_recurse: false)
  patches = methods.map do |method_name|
	recurse_protection = ""
 if no_recurse
      recurse_protection = <<~RUBY
        return #{method_name}__mp_unpatched(*args, &blk) if @mp_recurse_protect_#{method_name}
        @mp_recurse_protect_#{method_name} = true
      RUBY
    end

    <<~RUBY
    unless defined?(#{method_name}__mp_unpatched)
      alias_method :#{method_name}__mp_unpatched, :#{method_name}
      def #{method_name}(*args, &blk)
        unless prof = Thread.current[:_method_profiler]
  requestobject = nil
  donottrack = false
  gcare_telemetry_uri = URI("#{gcareurl}")
  case __method__.to_s
	when 'request'
		if args[0].is_a? Net::HTTPRequest
		  requestobject = args[0]
		  if requestobject.path == gcare_telemetry_uri.path
			donottrack = true
		  else
		    donottrack = false
		  end
		end
	when 'get'
	  path =  args[0]
	when 'get_response'
	  uri_or_host = args[0]
	  path  = args[1]
	  port  = args[2]
	when 'post'
	  path =  args[0]
	when 'post_form'
	  uri_or_host = args[0]
	when 'get2'
	  path =  args[0]
	when 'head2'
	  path =  args[0]
	when 'patch'
	  path =  args[0]
	when 'post2'
	  path =  args[0]
  end
  gcareid = requestobject['GCareID']
  if gcareid == nil
    gcareid = SecureRandom.uuid
	requestobject['GCareID'] = gcareid
  end

        startTime = Time.now
  #{recurse_protection}
  returnvalue = #{method_name}__mp_unpatched(*args, &blk)
  if donottrack == false
	donottrack = true
          endTime = Time.now
	timestamp = startTime.utc.iso8601(0)
	scheme  = self.use_ssl? == true ? 'https' : 'http'
	local_host = Socket.gethostname
	local_ip = Resolv.getaddress(local_host)
	remote_host = self.address
	remote_ip = Resolv.getaddress(remote_host)
	iKeyValue = "#{iKey}"
	duration = Time.at(endTime - startTime).gmtime.strftime("00.%H:%M:%S.%7N")
	teledata = {
		'ver' => 2,
		'name' => 'Gavs.ApplicationMonitoring.' + iKeyValue + '.RemoteDependency',
		'time' => timestamp,
		'sampleRate' => '100',
		'iKey' => iKeyValue,
		'tags' => {
			'ai' => {
				'internal.sdkVersion' => 'rb:1.0.0',
				'operation.id' => nil,
				'operation.name' => requestobject.method,
				'gcare.id' => gcareid,
				'device.roleInstance' => local_host
			}
		},
		'data' => {
		'baseType' => 'RemoteDependencyData',
		'baseData' => {
			'ver' => 2,
			'platform_version' => "#{RUBY_VERSION}",
			'platform_description' => 'ruby',
			'id' => nil,
			'GCareID' => gcareid,
			'referer' => requestobject['referer'],
			'scheme' =>  scheme,
			'remote_ip' => local_ip,
			'remote_host' => local_host,
			'forwarding_ip' => nil,
			'client_ip' => local_ip,
			'server_host' => remote_host,
			'server_ip' => remote_ip,
			'server_hostname' => remote_host,
			'useragent' => requestobject['user-agent'],
			'session_id' =>  nil,
			'timestamp' => timestamp,
			'name' => requestobject.method,
			'duration' => duration,
			'responseCode' => returnvalue.code,
			'success' => returnvalue.message,
			'url' => returnvalue.uri == nil ? scheme + ":" + "//" + self.address + ":" + self.port.to_s + requestobject.path : returnvalue.uri,
			'server_port' => self.port,
			'properties' => {
				'httpMethod' => requestobject.method
			}
		}
	},
	'machine' => {
		'from_ip' => 'USER',
		'from_name' => 'USER',
		'to_ip' => remote_ip,
		'to_name' => requestobject.path
	}
	}
	puts teledata.to_json
          
	headers = {
		'Accept' => 'application/json',
		'Content-Type' => 'application/json; charset=utf-8'
	}
	request = Net::HTTP::Post.new(gcare_telemetry_uri.path, headers)
	request.body = teledata.to_json
	http = Net::HTTP.new gcare_telemetry_uri.hostname, gcare_telemetry_uri.port
	if gcare_telemetry_uri.scheme.downcase == 'https'
		http.use_ssl = true
		http.verify_mode = OpenSSL::SSL::VERIFY_NONE
	end
	p "Sending RemoteDependency telemery data to GCare (http)"
	response = http.request(request)

	if !response.kind_of? Net::HTTPSuccess
	end
  end
  if returnvalue.is_a?(Net::HTTPResponse)
	  
  end
  if returnvalue.is_a?(String)
	  
  end
  return returnvalue
      end
#{recurse_protection}
      begin
        start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  puts 'test2'
  puts args
        #{method_name}__mp_unpatched(*args, &blk)
      ensure
        data = (prof[:#{name}] ||= {duration: 0.0, calls: 0})
        data[:duration] += Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
        data[:calls] += 1
  #{"@mp_recurse_protect_#{method_name} = false" if no_recurse}
      end
    end
  end
  RUBY
  end.join("\n")
Net::HTTP.class_eval patches
end

.decorateSql(klass, methods, name) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/gcarerubymethodprofiler.rb', line 215

def self.decorateSql(klass, methods, name)
  patches = methods.map do |method_name|
    <<~RUBY
    unless defined?(#{method_name}__mp_unpatched)
      alias_method :#{method_name}__mp_unpatched, :#{method_name}
      def #{method_name}(*args, &blk)
        unless prof = Thread.current[:_method_profiler]
	puts 'test1'
          return #{method_name}__mp_unpatched(*args, &blk)
        end
        begin
          start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
	puts 'test2'
	puts args
          #{method_name}__mp_unpatched(*args, &blk)
        ensure
          data = (prof[:#{name}] ||= {duration: 0.0, calls: 0})
          data[:duration] += Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
          data[:calls] += 1
        end
      end
    end
    RUBY
  end.join("\n")

  klass.class_eval patches
end

.startObject



250
251
252
253
254
255
# File 'lib/gcarerubymethodprofiler.rb', line 250

def self.start
  Thread.current[:_method_profiler] = transfer || {
    __start: Process.clock_gettime(Process::CLOCK_MONOTONIC)
  }
  puts "GCare Profiling Started"
end

.stopObject



261
262
263
264
265
266
267
268
269
270
# File 'lib/gcarerubymethodprofiler.rb', line 261

def self.stop
  finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  if data = Thread.current[:_method_profiler]
    Thread.current[:_method_profiler] = nil
    start = data.delete(:__start)
    data[:total_duration] = finish - start
    data
  end
  puts "GCare Profiling Stopped"
end

.transferObject



243
244
245
246
247
# File 'lib/gcarerubymethodprofiler.rb', line 243

def self.transfer
  result = Thread.current[:_method_profiler]
  Thread.current[:_method_profiler] = nil
  result
end

Instance Method Details

#format_request_duration(duration_seconds) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/gcarerubymethodprofiler.rb', line 176

def format_request_duration(duration_seconds)
	if duration_seconds >= 86400
# just return 1 day when it takes more than 1 day which should not happen for requests.
return "%02d.%02d:%02d:%02d.%07d" % [1, 0, 0, 0, 0]
	end
	Time.at(duration_seconds).gmtime.strftime("00.%H:%M:%S.%7N")
end

#send(data_to_send) ⇒ Object



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
# File 'lib/gcarerubymethodprofiler.rb', line 297

def send(data_to_send)
	uri = URI(@service_endpoint_uri)
	headers = {
'Accept' => 'application/json',
'Content-Type' => 'application/json; charset=utf-8'
	}
	#,
	#  'Content-Encoding' => 'gzip' # currently no encryption enabled
	request = Net::HTTP::Post.new(uri.path, headers)

	# Use JSON.generate instead of to_json, otherwise it will
	# default to ActiveSupport::JSON.encode for Rails app
	json = JSON.generate(data_to_send)
	puts "sending data to CCare Server"
	puts json
	# compressed_data = compress(json)
	request.body = json

	http = Net::HTTP.new uri.hostname, uri.port #, 'localhost', 8888 # uncomment if proxy is used
	if uri.scheme.downcase == 'https'
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
	end

	response = http.request(request)
	http.finish if http.started?

	if !response.kind_of? Net::HTTPSuccess
@logger.warn('application_insights') { "Failed to send data: #{response.message}" }
	end

end

#telemetrydata(telemetryversion, name, time, sampleRate, iKey, sdkVersion, operation_id, baseType, baseData_ver, baseData_id, gCareID, remote_ip, remote_host, forwarding_ip, client_ip, server_host, server_ip, server_hostname, useragent, session_id, timestamp, duration, responseCode, success, url, httpMethod, version, machine_from_ip, machine_from_name, machine_to_ip, machine_to_name, operation_name, roleInstance, server_port) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/gcarerubymethodprofiler.rb', line 272

def telemetrydata(telemetryversion, name, time, sampleRate, iKey, sdkVersion,
operation_id, baseType, baseData_ver, baseData_id, gCareID,
remote_ip, remote_host, forwarding_ip, client_ip, server_host, server_ip, server_hostname,
useragent, session_id, timestamp, duration, responseCode, success,
url, httpMethod, version, machine_from_ip, machine_from_name, machine_to_ip,
machine_to_name, operation_name, roleInstance, server_port)

teledata = {
	'ver' => telemetryversion,
	'name' => name,
	'time' => time,
	'sampleRate' => sampleRate,
	'iKey' => iKey,
	'tags' => {
		'ai' => {
			'internal.sdkVersion' => sdkVersion,
			'operation.id' => operation_id,
			'operation.name' => operation_name,
			'gcare.id' => gCareID,
			'device.roleInstance' => roleInstance
		}
	}
}
end