diff --git a/fluent-plugin-logtail.gemspec b/fluent-plugin-logtail.gemspec index 380f44d..0706d15 100644 --- a/fluent-plugin-logtail.gemspec +++ b/fluent-plugin-logtail.gemspec @@ -3,7 +3,7 @@ require 'date' Gem::Specification.new do |s| s.name = 'fluent-plugin-logtail' - s.version = '0.1.1' + s.version = '0.2.1' s.date = Date.today.to_s s.summary = 'Logtail.com plugin for Fluentd' s.description = 'Streams Fluentd logs to the Logtail.com logging service.' diff --git a/lib/fluent/plugin/out_logtail.rb b/lib/fluent/plugin/out_logtail.rb index 19d2cea..98c2204 100644 --- a/lib/fluent/plugin/out_logtail.rb +++ b/lib/fluent/plugin/out_logtail.rb @@ -7,7 +7,6 @@ class LogtailOutput < Fluent::BufferedOutput VERSION = "0.1.1".freeze CONTENT_TYPE = "application/msgpack".freeze - HOST = "in.logtail.com".freeze PORT = 443 PATH = "/".freeze MAX_ATTEMPTS = 3.freeze @@ -16,9 +15,11 @@ class LogtailOutput < Fluent::BufferedOutput config_param :source_token, :string, secret: true config_param :ip, :string, default: nil + config_param :ingesting_host, :string, default: "in.logs.betterstack.com" def configure(conf) @source_token = conf["source_token"] + @ingesting_host = conf["ingesting_host"] super end @@ -90,7 +91,7 @@ def force_utf8_string_values(data) end def build_http_client - http = Net::HTTP.new(HOST, PORT) + http = Net::HTTP.new(@ingesting_host, PORT) http.use_ssl = true # Verification on Windows fails despite having a valid certificate. http.verify_mode = OpenSSL::SSL::VERIFY_NONE diff --git a/spec/fluent/plugin/out_logtail_spec.rb b/spec/fluent/plugin/out_logtail_spec.rb index be71afc..51c3404 100644 --- a/spec/fluent/plugin/out_logtail_spec.rb +++ b/spec/fluent/plugin/out_logtail_spec.rb @@ -8,6 +8,13 @@ } end + let(:cloud_config) do + %{ + source_token abcd1234 + ingesting_host s1234.eu-nbg-2.betterstackdata.com + } + end + let(:driver) do tag = "test" Fluent::Test::BufferedOutputTestDriver.new(Fluent::LogtailOutput, tag) { @@ -19,6 +26,19 @@ def format(tag, time, record) end }.configure(config) end + + let(:cloud_driver) do + tag = "test" + Fluent::Test::BufferedOutputTestDriver.new(Fluent::LogtailOutput, tag) { + # v0.12's test driver assume format definition. This simulates ObjectBufferedOutput format + if !defined?(Fluent::Plugin::Output) + def format(tag, time, record) + [time, record].to_msgpack + end + end + }.configure(cloud_config) + end + let(:record) do {'age' => 26, 'request_id' => '42', 'parent_id' => 'parent', 'routing_id' => 'routing'} end @@ -28,8 +48,8 @@ def format(tag, time, record) end describe "#write" do - it "should send a chunked request to the Logtail API" do - stub = stub_request(:post, "https://in.logtail.com/"). + it "should send a chunked request to the Logtail API using default host" do + stub = stub_request(:post, "https://in.logs.betterstack.com/"). with( :body => start_with("\xDD\x00\x00\x00\x01\x85\xA3age\x1A\xAArequest_id\xA242\xA9parent_id\xA6parent\xAArouting_id\xA7routing\xA2dt\xB4".force_encoding("ASCII-8BIT")), :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer abcd1234', 'Content-Type'=>'application/msgpack', 'User-Agent'=>'Logtail Fluentd/0.1.1'} @@ -43,7 +63,7 @@ def format(tag, time, record) end it "handles 500s" do - stub = stub_request(:post, "https://in.logtail.com/").to_return(:status => 500, :body => "", :headers => {}) + stub = stub_request(:post, "https://in.logs.betterstack.com/").to_return(:status => 500, :body => "", :headers => {}) driver.emit(record) driver.run @@ -52,7 +72,7 @@ def format(tag, time, record) end it "handle auth failures" do - stub = stub_request(:post, "https://in.logtail.com/").to_return(:status => 403, :body => "", :headers => {}) + stub = stub_request(:post, "https://in.logs.betterstack.com/").to_return(:status => 403, :body => "", :headers => {}) driver.emit(record) driver.run @@ -60,4 +80,20 @@ def format(tag, time, record) expect(stub).to have_been_requested.times(1) end end + + describe "#write to cloud" do + it "should send a chunked request to the Logtail API" do + stub = stub_request(:post, "https://s1234.eu-nbg-2.betterstackdata.com/"). + with( + :body => start_with("\xDD\x00\x00\x00\x01\x85\xA3age\x1A\xAArequest_id\xA242\xA9parent_id\xA6parent\xAArouting_id\xA7routing\xA2dt\xB4".force_encoding("ASCII-8BIT")), + :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer abcd1234', 'Content-Type'=>'application/msgpack', 'User-Agent'=>'Logtail Fluentd/0.1.1'} + ). + to_return(:status => 202, :body => "", :headers => {}) + + cloud_driver.emit(record) + cloud_driver.run + + expect(stub).to have_been_requested.times(1) + end + end end \ No newline at end of file