Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 6d834e6

Browse files
committed
Add parameter to optionally send IP address
1 parent 5db6b4a commit 6d834e6

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ YARD::Rake::YardocTask.new do |task|
1212
task.files = ['lib/**/*.rb', '-', 'LICENSE.txt', 'README.md']
1313
end
1414

15-
task :default => [ :test, :build, :yard ]
15+
task :default => [ :test, :build, :yard ]

application_insights.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ Gem::Specification.new do |spec|
2626
spec.add_development_dependency 'rack', '>= 1.0.0'
2727
spec.add_development_dependency 'test-unit', '~> 3.0.8'
2828
spec.add_development_dependency 'mocha', '~> 1.5.0'
29+
spec.add_development_dependency 'pry'
2930

3031
end

lib/application_insights/channel/contracts/reopenings.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,17 @@ def http_method=(http_method)
2323
@properties["httpMethod"] = http_method
2424
end
2525
end
26+
27+
def client_ip
28+
@properties["clientIp"] if @properties
29+
end
30+
31+
def client_ip=(client_ip)
32+
if client_ip
33+
@properties ||= {}
34+
@properties["clientIp"] = client_ip
35+
end
36+
end
37+
2638
end
27-
end
39+
end

lib/application_insights/rack/track_request.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ class TrackRequest
1515
# send to Application Insights when buffer is full.
1616
# @param [Fixnum] send_interval the frequency (in seconds) to check buffer
1717
# and send buffered requests to Application Insights if any.
18-
def initialize(app, instrumentation_key, buffer_size = 500, send_interval = 60)
18+
def initialize(app, instrumentation_key, buffer_size = 500, send_interval = 60, store_ip = false)
1919
@app = app
2020
@instrumentation_key = instrumentation_key
2121
@buffer_size = buffer_size
2222
@send_interval = send_interval
23+
@store_ip = store_ip
2324

2425
@sender = Channel::AsynchronousSender.new
2526
@sender.send_interval = @send_interval
@@ -119,11 +120,13 @@ def operation_id(id)
119120
end
120121

121122
def options_hash(request)
122-
{
123+
options = {
123124
name: "#{request.request_method} #{request.path}",
124125
http_method: request.request_method,
125126
url: request.url
126127
}
128+
options[:client_ip] = request.ip if @store_ip
129+
options
127130
end
128131

129132
def request_data(request_id, start_time, duration, status, success, options)
@@ -137,7 +140,8 @@ def request_data(request_id, start_time, duration, status, success, options)
137140
:properties => options[:properties] || {},
138141
:measurements => options[:measurements] || {},
139142
# Must initialize http_method after properties because it's actually stored in properties
140-
:http_method => options[:http_method]
143+
:http_method => options[:http_method],
144+
:client_ip => options[:client_ip]
141145
)
142146
end
143147

test/application_insights/rack/test_track_request.rb

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,32 @@ def test_call_works_as_expected
4343
assert Time.parse(payload[0].time) - start_time < 0.01
4444
end
4545

46+
def test_call_with_ip
47+
response_code = rand(200..204)
48+
app = Proc.new {|env| sleep(2.5); [response_code, {"Content-Type" => "text/html"}, ["Hello Rack!"]]}
49+
url = "http://localhost:8080/foo?a=b"
50+
ip = "1.1.1.1"
51+
http_method = 'PUT'
52+
env = Rack::MockRequest.env_for(url, :method => http_method, "REMOTE_ADDR" => ip)
53+
instrumentation_key = 'key'
54+
sender = MockAsynchronousSender.new
55+
track_request = TrackRequest.new app, instrumentation_key, 500, 1, true
56+
track_request.send(:sender=, sender)
57+
start_time = Time.now
58+
59+
SecureRandom.expects(:base64).with(10).returns('y0NM2eOY/fnQPw==')
60+
result = track_request.call(env)
61+
62+
app_result = app.call(env)
63+
assert_equal app_result, result
64+
sleep(sender.send_interval)
65+
66+
payload = sender.buffer[0]
67+
request_data = payload[0].data.base_data
68+
69+
assert_equal request_data.properties['clientIp'], ip
70+
end
71+
4672
def test_call_with_failed_request
4773
response_code = rand(400..600)
4874
app = Proc.new {|env| [response_code, {"Content-Type" => "text/html"}, ["Hello Rack!"]]}
@@ -68,13 +94,13 @@ def test_call_with_unhandled_exception
6894
sender = MockAsynchronousSender.new
6995
track_request = TrackRequest.new app, instrumentation_key, 500, 1
7096
track_request.send(:sender=, sender)
71-
97+
7298
begin
7399
track_request.call(env)
74100
rescue => ex
75101
assert_equal 'Boom!', ex.message
76102
end
77-
103+
78104
sleep(sender.send_interval)
79105
payload = sender.buffer[0]
80106
assert_equal 2, payload.count
@@ -109,7 +135,7 @@ def test_format_request_duration_less_than_a_day
109135
track_request = TrackRequest.new app, 'one'
110136
duration_seconds = rand(86400) + rand
111137
time_span = track_request.send(:format_request_duration, duration_seconds)
112-
138+
113139
match = TIME_SPAN_FORMAT.match time_span
114140
assert_not_nil match
115141
days = duration_seconds.to_i/86400
@@ -129,7 +155,7 @@ def test_format_request_duration_more_than_a_day
129155
track_request = TrackRequest.new app, 'one'
130156
duration_seconds = rand(86400..240000) + rand
131157
time_span = track_request.send(:format_request_duration, duration_seconds)
132-
158+
133159
match = TIME_SPAN_FORMAT.match time_span
134160
assert_not_nil match
135161
assert_equal 1, match['day'].to_i

0 commit comments

Comments
 (0)