lib/logtail/log_devices/http.rb in logtail-ruby-0.1.2 vs lib/logtail/log_devices/http.rb in logtail-ruby-0.1.3

- old
+ new

@@ -15,11 +15,11 @@ # in a thread as not to block application execution and efficiently deliver logs for # multi-threaded environments. # # See {#initialize} for options and more details. class HTTP - LOGTAIL_STAGING_HOST = "in-staging.logtail.com".freeze + LOGTAIL_STAGING_HOST = "in.logtail.dev".freeze LOGTAIL_PRODUCTION_HOST = "in.logtail.com".freeze LOGTAIL_HOST = ENV['LOGTAIL_STAGING'] ? LOGTAIL_STAGING_HOST : LOGTAIL_PRODUCTION_HOST LOGTAIL_PORT = 443 LOGTAIL_SCHEME = "https".freeze CONTENT_TYPE = "application/msgpack".freeze @@ -34,11 +34,11 @@ # By default, the buffer will apply back pressure when the rate of log messages exceeds # the maximum delivery rate. If you don't want to sacrifice app performance in this case # you can drop the log messages instead by passing a {DroppingSizedQueue} via the # `:request_queue` option. # - # @param api_key [String] The API key provided to you after you add your application to + # @param source_token [String] The API key provided to you after you add your application to # [Logtail](https://logtail.com). # @param [Hash] options the options to create a HTTP log device with. # @option attributes [Symbol] :batch_size (1000) Determines the maximum of log lines in # each HTTP payload. If the queue exceeds this limit an HTTP request will be issued. Bigger # payloads mean higher throughput, but also use more memory. Logtail will not accept @@ -62,17 +62,17 @@ # an example. # @option attributes [Symbol] :logtail_host The Logtail host to delivery the log lines to. # The default is set via {LOGTAIL_HOST}. # # @example Basic usage - # Logtail::Logger.new(Logtail::LogDevices::HTTP.new("my_logtail_api_key")) + # Logtail::Logger.new(Logtail::LogDevices::HTTP.new("my_logtail_source_token")) # # @example Apply back pressure instead of dropping messages - # http_log_device = Logtail::LogDevices::HTTP.new("my_logtail_api_key", request_queue: SizedQueue.new(25)) + # http_log_device = Logtail::LogDevices::HTTP.new("my_logtail_source_token", request_queue: SizedQueue.new(25)) # Logtail::Logger.new(http_log_device) - def initialize(api_key, options = {}) - @api_key = api_key || raise(ArgumentError.new("The api_key parameter cannot be blank")) + def initialize(source_token, options = {}) + @source_token = source_token || raise(ArgumentError.new("The source_token parameter cannot be blank")) @logtail_host = options[:logtail_host] || ENV['LOGTAIL_HOST'] || LOGTAIL_HOST @logtail_port = options[:logtail_port] || ENV['LOGTAIL_PORT'] || LOGTAIL_PORT @logtail_scheme = options[:logtail_scheme] || ENV['LOGTAIL_SCHEME'] || LOGTAIL_SCHEME @batch_size = options[:batch_size] || 1_000 @flush_continuously = options[:flush_continuously] != false @@ -166,11 +166,11 @@ MESSAGE end end raise <<-MESSAGE - + Log delivery failed! No request was made. You can enable internal debug logging with the following: Logtail::Config.instance.debug_logger = ::Logger.new(STDOUT) @@ -199,14 +199,24 @@ path = '/' req = Net::HTTP::Post.new(path) req['Authorization'] = authorization_payload req['Content-Type'] = CONTENT_TYPE req['User-Agent'] = USER_AGENT - req.body = msgs.to_msgpack + req.body = msgs.map { |msg| force_utf8_encoding(msg.to_hash) }.to_msgpack req end + def force_utf8_encoding(data) + if data.respond_to?(:force_encoding) + data.dup.force_encoding('UTF-8') + elsif data.respond_to?(:transform_values) + data.transform_values { |val| force_utf8_encoding(val) } + else + data + end + end + # Flushes the message buffer asynchronously. The reason we provide this # method is because the message buffer limit is constricted by the # Logtail API. The application limit is multiples of the buffer limit, # hence the `@request_queue`, allowing us to buffer beyond the Logtail API # imposed limit. @@ -359,10 +369,10 @@ true end # Builds the `Authorization` header value for HTTP delivery to the Logtail API. def authorization_payload - "Bearer #{@api_key}" + "Bearer #{@source_token}" end end end end