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

Commit

Permalink
[#48] Expose request id within parent app when using middleware
Browse files Browse the repository at this point in the history
* Update TravisCI testing matrix
* Update CHANGELOG
* Update README
* Update `ApplicationInsights::Rack::TrackRequest#call` to implement the above
* Add associated tests
  • Loading branch information
mattrayner committed Apr 26, 2018
1 parent 780d33e commit 8d2a358
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ rvm:
- 2.2.0
- 2.3.3
- 2.4.0
- 2.5.0
- ruby-head

cache: bundler
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This file needs to be updated with every significant pull request. It is used to write down release notes.

## Version 0.5.6

* Expose request id to parent Rack application when using `ApplicationInsights::Rack::TrackRequest` middleware through `env['ApplicationInsights.request.id']`

## Version 0.5.5
* Add some basic logging when failed to send telemetry to the server
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,17 @@ use ApplicationInsights::Rack::TrackRequest, '<YOUR INSTRUMENTATION KEY GOES HER
# For rails, suggest to set up this middleware in application.rb so that unhandled exceptions from controllers are also collected
config.middleware.use 'ApplicationInsights::Rack::TrackRequest', '<YOUR INSTRUMENTATION KEY GOES HERE>', <buffer size>
```

#### Retrieving a request's AppInsight ID ####
```ruby
# from time to time you may need to access a request's AppInsight id from within your app
application_insights_request_id = env['ApplicationInsights.request.id']

# this can be used for a number of different purposes, including telemetry correlation
uri = URI('http://api.example.com/search/?q=test')

req = Net::HTTP::Get.new(uri)
req['Request-Id'] = "|#{application_insights_request_id}." if application_insights_request_id

Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }
```
4 changes: 3 additions & 1 deletion lib/application_insights/rack/track_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def initialize(app, instrumentation_key, buffer_size = 500, send_interval = 60)
# Track requests and send data to Application Insights asynchronously.
# @param [Hash] env the rack environment.
def call(env)
id = rand(16**32).to_s(16)
env['ApplicationInsights.request.id'] = id

start = Time.now
begin
status, headers, response = @app.call(env)
Expand All @@ -44,7 +47,6 @@ def call(env)
end

request = ::Rack::Request.new env
id = rand(16**32).to_s(16)
start_time = start.iso8601(7)
duration = format_request_duration(stop - start)
success = status.to_i < 400
Expand Down
2 changes: 1 addition & 1 deletion lib/application_insights/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ApplicationInsights
VERSION = '0.5.6'
VERSION = '0.5.6'.freeze
end
6 changes: 6 additions & 0 deletions test/application_insights/rack/test_track_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def test_call_works_as_expected
assert_equal url, request_data.url
assert_equal true, request_data.duration.start_with?("00.00:00:02")
assert Time.parse(request_data.start_time) - start_time < 0.01

assert env['ApplicationInsights.request.id'] =~ (/^\h{1,32}$/)
end

def test_call_with_failed_request
Expand All @@ -54,6 +56,8 @@ def test_call_with_failed_request
payload = sender.buffer[0]
request_data = payload[0].data.base_data
assert_equal false, request_data.success

assert env['ApplicationInsights.request.id'] =~ (/^\h{1,32}$/)
end

def test_call_with_unhandled_exception
Expand All @@ -80,6 +84,8 @@ def test_call_with_unhandled_exception
exception_data = payload[1].data.base_data
assert_equal instrumentation_key, payload[1].i_key
assert_equal 'Unhandled', exception_data.handled_at

assert env['ApplicationInsights.request.id'] =~ (/^\h{1,32}$/)
end

def test_internal_client
Expand Down

0 comments on commit 8d2a358

Please sign in to comment.