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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,19 @@ req['Request-Id'] = "#{application_insights_request_id}1" if application_insight

Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }
```

### Configuring Telemetry client with Proxy endpoint ###
```ruby
require 'application_insights'
# if proxy server doesnt have authentication, provide arbitrary username and password
# <proxyhost> : Address or FQDN of the proxy server
# <port> : port number for the proxy server
proxy = { :addr => '<proxyhost>', :port => '<port>', :user: '<user>', :pass: '<password>' }
sender = ApplicationInsights::Channel::AsynchronousSender.new 'https://dc.services.visualstudio.com/v2/track', proxy
queue = ApplicationInsights::Channel::AsynchronousQueue.new sender
channel = ApplicationInsights::Channel::TelemetryChannel.new nil, queue
tc = ApplicationInsights::TelemetryClient.new '<YOUR INSTRUMENTATION KEY GOES HERE>', channel
# Note: the event will be sent on a separate thread; if the app finishes before
# the thread finishes, the data is lost
tc.track_event 'My event'
```
9 changes: 7 additions & 2 deletions lib/application_insights/channel/asynchronous_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ class AsynchronousSender < SenderBase
SERVICE_ENDPOINT_URI = 'https://dc.services.visualstudio.com/v2/track'
# Initializes a new instance of the class.
# @param [String] service_endpoint_uri the address of the service to send
# @param [Hash] proxy server configuration to send (optional)
# telemetry data to.
def initialize(service_endpoint_uri = SERVICE_ENDPOINT_URI)
def initialize(service_endpoint_uri = SERVICE_ENDPOINT_URI, proxy = {})
# callers which requires proxy dont require to maintain service endpoint uri which potentially can change
if service_endpoint_uri.nil? || service_endpoint_uri.empty?
service_endpoint_uri = SERVICE_ENDPOINT_URI
end
@send_interval = 1.0
@send_remaining_time = 0
@send_time = 3.0
@lock_work_thread = Mutex.new
@work_thread = nil
@start_notification_processed = true
super service_endpoint_uri
super service_endpoint_uri, proxy
end

# The time span in seconds at which the the worker thread will check the
Expand Down
13 changes: 11 additions & 2 deletions lib/application_insights/channel/sender_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ module Channel
class SenderBase
# Initializes a new instance of the class.
# @param [String] service_endpoint_uri the address of the service to send
# @param [Hash] proxy server configuration to send (optional)
# telemetry data to.
def initialize(service_endpoint_uri)
def initialize(service_endpoint_uri, proxy = {})
@service_endpoint_uri = service_endpoint_uri
@queue = nil
@send_buffer_size = 100
@logger = Logger.new(STDOUT)
@proxy = proxy
end

# The service endpoint URI where this sender will send data to.
Expand All @@ -41,6 +43,9 @@ def initialize(service_endpoint_uri)
# The logger for the sender.
attr_accessor :logger

# The proxy for the sender.
attr_accessor :proxy

# Immediately sends the data passed in to {#service_endpoint_uri}. If the
# service request fails, the passed in items are pushed back to the {#queue}.
# @param [Array<Contracts::Envelope>] data_to_send an array of
Expand All @@ -60,7 +65,11 @@ def send(data_to_send)
compressed_data = compress(json)
request.body = compressed_data

http = Net::HTTP.new uri.hostname, uri.port
if @proxy.nil? || @proxy.empty?
http = Net::HTTP.new uri.hostname, uri.port
else
http = Net::HTTP.new(uri.hostname, uri.port, @proxy[:addr], @proxy[:port], @proxy[:user], @proxy[:pass])
end
if uri.scheme.downcase == 'https'
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
Expand Down
11 changes: 8 additions & 3 deletions lib/application_insights/channel/synchronous_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ module Channel
# A synchronous sender that works in conjunction with the {SynchronousQueue}.
# The queue will call {#send} on the current instance with the data to send.
class SynchronousSender < SenderBase
SERVICE_ENDPOINT_URI = 'https://dc.services.visualstudio.com/v2/track'
SERVICE_ENDPOINT_URI = "https://dc.services.visualstudio.com/v2/track"
# Initializes a new instance of the class.
# @param [String] service_endpoint_uri the address of the service to send
# @param [Hash] proxy server configuration to send (optional)
# telemetry data to.
def initialize(service_endpoint_uri = SERVICE_ENDPOINT_URI)
super service_endpoint_uri
def initialize(service_endpoint_uri = SERVICE_ENDPOINT_URI, proxy = {})
# callers which requires proxy dont require to maintain service endpoint uri which potentially can change
if service_endpoint_uri.nil? || service_endpoint_uri.empty?
service_endpoint_uri = SERVICE_ENDPOINT_URI
end
super service_endpoint_uri, proxy
end
end
end
Expand Down