From 0f071586571e45c14b29caae999d64e8257c1764 Mon Sep 17 00:00:00 2001 From: Ilias Tsangaris Date: Wed, 5 Jan 2022 11:14:32 -0600 Subject: [PATCH] feat: support an ignore option #351 --- packages/ruby/lib/readme/metrics.rb | 2 +- packages/ruby/lib/readme/payload.rb | 5 +++++ packages/ruby/spec/readme/metrics_spec.rb | 19 ++++++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/ruby/lib/readme/metrics.rb b/packages/ruby/lib/readme/metrics.rb index 159b47bd84..f646dd5aae 100644 --- a/packages/ruby/lib/readme/metrics.rb +++ b/packages/ruby/lib/readme/metrics.rb @@ -86,7 +86,7 @@ def process_response(response:, env:, start_time:, end_time:) Readme::Metrics.logger.warn "Request or response body MIME type isn't supported for filtering. Omitting request from ReadMe API logging" else payload = Payload.new(har, user_info, development: @development) - @@request_queue.push(payload.to_json) + @@request_queue.push(payload.to_json) if !payload.ignore end end diff --git a/packages/ruby/lib/readme/payload.rb b/packages/ruby/lib/readme/payload.rb index 9187c6fe50..c18436caf1 100644 --- a/packages/ruby/lib/readme/payload.rb +++ b/packages/ruby/lib/readme/payload.rb @@ -2,12 +2,17 @@ module Readme class Payload + attr_reader :ignore + def initialize(har, info, development:) @har = har # swap api_key for id info[:id] = info.delete :api_key unless info[:api_key].nil? + # pull log_id and ignore fields out of info to construct a user_info hash that can be assigned to the group key @log_id = info[:log_id] + @ignore = info[:ignore] info.delete :log_id + info.delete :ignore @user_info = info @development = development @uuid = UUID.new diff --git a/packages/ruby/spec/readme/metrics_spec.rb b/packages/ruby/spec/readme/metrics_spec.rb index 0d0ea6aa0f..0e35511266 100644 --- a/packages/ruby/spec/readme/metrics_spec.rb +++ b/packages/ruby/spec/readme/metrics_spec.rb @@ -1,10 +1,15 @@ require "readme/metrics" require "rack/test" require "webmock/rspec" +require "uuid" RSpec.describe Readme::Metrics do include Rack::Test::Methods + before do + @uuid = UUID.new + end + before :each do WebMock.reset_executed_requests! end @@ -429,6 +434,16 @@ def app .with { |request| validate_json("readmeMetrics", request.body) } }.to raise_error end + + it "can ignore sending logs" do + def app + json_app_with_middleware({}, {ignore: true}) + end + + post "/api/foo" + + expect(WebMock).not_to have_requested(:post, Readme::Metrics::ENDPOINT) + end end def json_app_with_middleware(option_overrides = {}, group_overrides = {}) @@ -449,7 +464,9 @@ def app_with_middleware(app, option_overrides = {}, group_overrides = {}) group = { id: env["CURRENT_USER"].id, label: env["CURRENT_USER"].name, - email: env["CURRENT_USER"].email + email: env["CURRENT_USER"].email, + log_id: @uuid.generate, + ignore: false }.merge(group_overrides) group.delete :id unless group[:api_key].nil? group