Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,11 @@ raise Exception, 'Boom!'
```ruby
# set up the TrackRequest middleware in the rackup (config.ru) file
require 'application_insights'
use ApplicationInsights::Rack::TrackRequest, '<YOUR INSTRUMENTATION KEY GOES HERE>', <buffer size>
use ApplicationInsights::Rack::TrackRequest, '<YOUR INSTRUMENTATION KEY GOES HERE>', <buffer size>, <send_interval>, <roles>
# 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>
config.middleware.use 'ApplicationInsights::Rack::TrackRequest', '<YOUR INSTRUMENTATION KEY GOES HERE>', <buffer size>, <send_interval>, <roles>

Example: config.middleware.use 'ApplicationInsights::Rack::TrackRequest', 'd770211a-fdeb-4e91-b7e0-a1fffa282ec4', 500, 60, { role: 'test-role', role_instance: 'teste-role-instance' }
```

#### Rerieving the Request-Id value from ApplicationInsights ####
Expand Down
2 changes: 1 addition & 1 deletion lib/application_insights/channel/queue_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def push(item)
return unless item

@queue.push(item)

flush if @queue.length >= @max_queue_length
end

Expand Down
10 changes: 7 additions & 3 deletions lib/application_insights/rack/track_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ class TrackRequest
# send to Application Insights when buffer is full.
# @param [Fixnum] send_interval the frequency (in seconds) to check buffer
# and send buffered requests to Application Insights if any.
def initialize(app, instrumentation_key, buffer_size = 500, send_interval = 60)
# @param [Hash] roles send a role and role instance string to Application Insights.
def initialize(app, instrumentation_key, buffer_size = 500, send_interval = 60, roles = {})
@app = app
@instrumentation_key = instrumentation_key
@buffer_size = buffer_size
@send_interval = send_interval
@roles = roles

@sender = Channel::AsynchronousSender.new
@sender.send_interval = @send_interval
Expand Down Expand Up @@ -54,7 +56,7 @@ def call(env)
options = options_hash(request)

data = request_data(request_id, start_time, duration, status, success, options)
context = telemetry_context(request_id, env['HTTP_REQUEST_ID'])
context = telemetry_context(request_id, env['HTTP_REQUEST_ID'], @roles)

@client.channel.write data, context, start_time

Expand Down Expand Up @@ -141,11 +143,13 @@ def request_data(request_id, start_time, duration, status, success, options)
)
end

def telemetry_context(request_id, request_id_header)
def telemetry_context(request_id, request_id_header, roles)
context = Channel::TelemetryContext.new
context.instrumentation_key = @instrumentation_key
context.operation.id = operation_id(request_id)
context.operation.parent_id = request_id_header
context.cloud.role = roles.values[0].instance_of?(String) == true ? roles.values[0] : nil
context.cloud.role_instance = roles.values[1].instance_of?(String) == true ? roles.values[1] : nil

context
end
Expand Down