From b6b8b8c7336cc1de0ea33368c8687c8402c03fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20G=C3=A9czi?= Date: Mon, 29 Apr 2024 12:00:00 +0000 Subject: [PATCH] fix: Prevent usage of Oj once requiring it already raised an exception MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes the following exception, which happens, when the require statement has raised an exception, but only after the 'Oj' name is already defined. ```` E, [2024-04-29T13:33:24.549280 #108] ERROR -- : undefined method `dump' for Oj:Module defined?(Oj) ? Oj.dump(data, mode: :strict) : JSON.dump(data) ^^^^^ Did you mean? dup ``` Signed-off-by: Ferenc Géczi --- lib/instana/backend/request_client.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/instana/backend/request_client.rb b/lib/instana/backend/request_client.rb index de6ac5ce..6955b437 100644 --- a/lib/instana/backend/request_client.rb +++ b/lib/instana/backend/request_client.rb @@ -8,8 +8,10 @@ # :nocov: begin require 'oj' + INSTANA_USE_OJ = true rescue LoadError => _e Instana.logger.warn("Unable to load Oj.") + INSTANA_USE_OJ = false end # :nocov: @@ -66,7 +68,7 @@ def send_request(method, path, data = nil, headers = {}) def encode_body(data) # :nocov: - defined?(Oj) ? Oj.dump(data, mode: :strict) : JSON.dump(data) + INSTANA_USE_OJ ? Oj.dump(data, mode: :strict) : JSON.dump(data) # :nocov: end end