Skip to content

Commit

Permalink
#186438276 : Start worker on forked new process (#54)
Browse files Browse the repository at this point in the history
* consolidate moesif_helpers.log

* when add to queue check if worker run last

* delete unused code

* update useragent
  • Loading branch information
xinghengwang authored Nov 15, 2023
1 parent 866063d commit d6eefc8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 38 deletions.
36 changes: 17 additions & 19 deletions lib/moesif_rack/moesif_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(app, options = {})
@app = app
raise 'application_id required for Moesif Middleware' unless options['application_id']

@api_client = MoesifApi::MoesifAPIClient.new(options['application_id'], 'moesif-rack/2.2.1')
@api_client = MoesifApi::MoesifAPIClient.new(options['application_id'], 'moesif-rack/2.2.2')
@api_controller = @api_client.api

@api_version = options['api_version']
Expand Down Expand Up @@ -59,7 +59,7 @@ def initialize(app, options = {})

@moesif_helpers.log_debug 'Start Capturing outgoing requests'
require_relative '../../moesif_capture_outgoing/httplog'
MoesifCaptureOutgoing.start_capture_outgoing(options, @app_config, @events_queue, @moesif_helpers)
MoesifCaptureOutgoing.start_capture_outgoing(options, @app_config, method(:add_to_queue), @moesif_helpers)
end

def update_user(user_profile)
Expand Down Expand Up @@ -94,12 +94,6 @@ def base64_encode_body(body)
[Base64.encode64(body), 'base64']
end

def @moesif_helpers.log_debug(message)
return unless @debug

puts("#{Time.now} [Moesif Middleware] PID #{Process.pid} TID #{Thread.current.object_id} #{message}")
end

def parse_body(body, headers)
begin
if body.instance_of?(Hash) || body.instance_of?(Array)
Expand All @@ -124,6 +118,7 @@ def parse_body(body, headers)
end

def start_worker
@moesif_helpers.log_debug('start worker');
Thread.new do
loop do
# Update the last worker run, in case the events_queue is empty
Expand Down Expand Up @@ -163,6 +158,18 @@ def start_worker
end
end

def add_to_queue(_event_model)
# Add Event to the queue
if @events_queue.size >= @event_queue_size
@moesif_helpers.log_debug("Skipped Event due to events_queue size [#{@events_queue.size}] is over max #{@event_queue_size} ")
else
@events_queue << _event_model
@moesif_helpers.log_debug('Event added to the queue ')
end

start_worker if Time.now.utc > (@last_worker_run + 60)
end

def call(env)
start_time = Time.now.utc.iso8601(3)

Expand Down Expand Up @@ -284,10 +291,9 @@ def call(env)
end

process_send = lambda do |_event_model|
@moesif_helpers.log_debug 'sending data to moesif'
@moesif_helpers.log_debug 'incoming event to add to queue for sending to moesif'
@moesif_helpers.log_debug _event_model.to_json

# Perform the API call through the SDK function
begin
random_percentage = Random.rand(0.00..100.00)

Expand All @@ -303,15 +309,7 @@ def call(env)

if sampling_percentage > random_percentage
_event_model.weight = @app_config.calculate_weight(sampling_percentage)
# Add Event to the queue
if @events_queue.size >= @event_queue_size
@moesif_helpers.log_debug("Skipped Event due to events_queue size [#{@events_queue.size}] is over max #{@event_queue_size} ")
else
@events_queue << _event_model
@moesif_helpers.log_debug('Event added to the queue ')
end

start_worker if Time.now.utc > (@last_worker_run + 60)
add_to_queue(_event_model)
else
@moesif_helpers.log_debug('Skipped Event due to sampling percentage: ' + sampling_percentage.to_s + ' and random percentage: ' + random_percentage.to_s)
end
Expand Down
32 changes: 14 additions & 18 deletions moesif_capture_outgoing/httplog/http_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

module MoesifCaptureOutgoing
class << self
def start_capture_outgoing(options, app_config_manager, events_queue, moesif_helpers)
def start_capture_outgoing(options, app_config_manager, add_to_queue, moesif_helpers)
@moesif_options = options
raise 'application_id required for Moesif Middleware' unless @moesif_options['application_id']

Expand All @@ -23,9 +23,10 @@ def start_capture_outgoing(options, app_config_manager, events_queue, moesif_hel
@log_body_outgoing = options.fetch('log_body_outgoing', true)

@app_config = app_config_manager
# @app_config and @events_queue should be shared instance from the middleware
# so that we can use the same queue and same loaded @app_config
@events_queue = events_queue
# @app_config is shared instance from the middleware
# so same loaded @app_config
# add_to_queue is method from the middleware so that we can add to the same queue
@add_to_queue = add_to_queue
@sampling_percentage = 100
@last_updated_time = Time.now.utc
@moesif_helpers = moesif_helpers
Expand All @@ -51,7 +52,7 @@ def transform_response_code(response_code_name)

def send_moesif_event(url, request, request_time, response, response_time, body_from_req_call, req_body_from_stream)
if url.downcase.include? 'moesif'
puts 'Skip sending as it is moesif Event' if @debug
@moesif_helpers.log_debug 'Skip adding to queue as it is moesif Event'
else
response.code = transform_response_code(response.code) if response.code.is_a?(Symbol)

Expand Down Expand Up @@ -173,19 +174,14 @@ def send_moesif_event(url, request, request_time, response, response_time, body_

if @sampling_percentage > @random_percentage
event_model.weight = @app_config.calculate_weight(@sampling_percentage)
if @debug
puts 'Sending Outgoing Request Data to Moesif'
puts event_model.to_json
end

# we put in the queue and format abot it.
unless @events_queue.nil?
@events_queue << event_model
puts('Outgoing Event successfully added to event queue') if @debug
return
end
elsif @debug
puts('Skipped outgoing Event due to sampling percentage: ' + @sampling_percentage.to_s + ' and random percentage: ' + @random_percentage.to_s)
@moesif_helpers.log_debug 'Adding Outgoing Request Data to Queue'
@moesif_helpers.log_debug event_model.to_json
# we put in the queue and forget it.
@add_to_queue.call(event_model)
@moesif_helpers.log_debug 'Finished adding outgoing request data to Queue'

else
@moesif_helpers.log_debug('Skipped outgoing Event due to sampling percentage: ' + @sampling_percentage.to_s + ' and random percentage: ' + @random_percentage.to_s)
end
rescue MoesifApi::APIException => e
if e.response_code.between?(401, 403)
Expand Down
2 changes: 1 addition & 1 deletion moesif_rack.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'moesif_rack'
s.version = '2.2.1'
s.version = '2.2.2'
s.summary = 'moesif_rack'
s.description = 'Rack/Rails middleware to log API calls to Moesif API analytics and monitoring'
s.authors = ['Moesif, Inc', 'Xing Wang']
Expand Down

0 comments on commit d6eefc8

Please sign in to comment.