lib/aspecto/opentelemetry/config/remote_config.rb in aspecto-opentelemetry-0.1.5 vs lib/aspecto/opentelemetry/config/remote_config.rb in aspecto-opentelemetry-0.1.6

- old
+ new

@@ -1,10 +1,10 @@ # frozen_string_literal: true -require "faraday" -require "faraday_middleware" require "rufus/scheduler" +require "net/http" +require "json" require_relative "../sampler/rules_sampler" require_relative "../sampler/message_process_sampler" module Aspecto @@ -15,17 +15,14 @@ class RemoteConfig def initialize(aspecto_auth, service_name, env, fallback_sampler) @service_name = service_name @env = env @fallback_sampler = fallback_sampler + aspecto_config_host = ENV.fetch("ASPECTO_CONFIG_HOST", "https://config.aspecto.io") + @aspecto_config_url = URI("#{aspecto_config_host}/config/#{aspecto_auth}") + init_http_client - aspecto_config_url = ENV.fetch("ASPECTO_CONFIG_HOST", "https://config.aspecto.io") - @http_client = Faraday.new "#{aspecto_config_url}/config/#{aspecto_auth}" do |f| - f.response :json # decode response bodies as JSON - end - @http_client.options.timeout = 10 - @scheduler = Rufus::Scheduler.new @remote_config_poll_frequency = ENV.fetch("ASPECTO_REMOTE_CONFIG_POLL_FREQUENCY", "30s") @scheduler.interval @remote_config_poll_frequency, first: :now do update_config end @@ -37,22 +34,39 @@ @scheduler.shutdown end private + def init_http_client + write_timeout_supported = Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.6") + + @http_client = Net::HTTP.new(@aspecto_config_url.host, @aspecto_config_url.port) + @http_client.read_timeout = 10 + @http_client.open_timeout = 10 + @http_client.write_timeout = 10 if write_timeout_supported + @http_client.max_retries = 0 + + # use uri.scheme == 'https' instead + @http_client.use_ssl = true + @http_client.verify_mode = OpenSSL::SSL::VERIFY_PEER + end + def update_config # rubocop:disable Metrics/AbcSize ::OpenTelemetry::Common::Utilities.untraced do - response = @http_client.get do |req| - req.headers["If-None-Match"] = @latest_config_etag unless @latest_config_etag.nil? - end - @latest_config_etag = response.headers["etag"] - return if response.status == 304 + request = Net::HTTP::Get.new(@aspecto_config_url.path) + request["If-None-Match"] = @latest_config_etag unless @latest_config_etag.nil? + response = @http_client.request(request) + response_code = response.code.to_i - if response.status >= 400 + return if response_code == 304 + + if response_code >= 400 ::OpenTelemetry.logger.error("[Aspecto] error when trying to get remote config. will try again in #{@remote_config_poll_frequency}") return end - handle_new_config response.body if response.status < 300 + + @latest_config_etag = response["etag"] + handle_new_config JSON.parse(response.body) if response_code < 300 end rescue StandardError => e ::OpenTelemetry.logger.error "[Aspecto] updating remote config failed. using previous remote config" ::OpenTelemetry.logger.debug e end