Skip to content

Commit

Permalink
Make load time more precise
Browse files Browse the repository at this point in the history
move loading time to middleware and add time zone (#6)
  • Loading branch information
KapustaB authored Sep 30, 2024
1 parent 1b21a5a commit b16c65b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
11 changes: 4 additions & 7 deletions lib/treblle/generate_payload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ class GeneratePayload
SDK_LANG = 'ruby'
TIME_FORMAT = '%Y-%m-%d %H:%M:%S'

def initialize(request:, response:, started_at:, configuration: Treblle.configuration)
def initialize(request:, response:, started_at:, load_time:, configuration: Treblle.configuration)
@request = request
@response = response
@started_at = started_at
@load_time = load_time
@configuration = configuration
end

Expand All @@ -21,18 +22,14 @@ def call

private

attr_reader :request, :response, :started_at, :configuration
attr_reader :request, :response, :started_at, :load_time, :configuration

def sanitize(body)
Utils::HashSanitizer.sanitize(body, configuration.sensitive_attrs)
end

def timestamp
started_at.strftime(TIME_FORMAT)
end

def load_time
Time.now - started_at
started_at.utc.strftime(TIME_FORMAT)
end

def payload
Expand Down
15 changes: 11 additions & 4 deletions lib/treblle/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,25 @@ def call(env)
attr_reader :configuration

def call_with_treblle_monitoring(env)
started_at = Time.now
started_at = Time.now.utc

response = @app.call(env)
handle_monitoring(env, response, started_at)

load_time = Time.now.utc - started_at
handle_monitoring(env, response, started_at, load_time)

response
end

def handle_monitoring(env, rack_response, started_at)
def handle_monitoring(env, rack_response, started_at, load_time)
request = RequestBuilder.new(env).build
response = ResponseBuilder.new(rack_response).build
payload = GeneratePayload.new(request: request, response: response, started_at: started_at).call
payload = GeneratePayload.new(
request: request,
response: response,
started_at: started_at,
load_time: load_time
).call

Dispatcher.new(payload: payload).call
rescue StandardError => e
Expand Down

0 comments on commit b16c65b

Please sign in to comment.