Skip to content

Commit 07f43e7

Browse files
committed
✨ Initial commit
0 parents  commit 07f43e7

File tree

8 files changed

+253
-0
lines changed

8 files changed

+253
-0
lines changed

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.DS_Store
2+
.rvmrc
3+
.ruby-version
4+
coverage
5+
*.swp
6+
*.gem
7+
Gemfile.lock
8+
9+
gemfiles/*.lock
10+
11+
/.bundle
12+
/.yardoc
13+
/doc
14+
/log
15+
/tmp
16+
/pkg

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
source 'https://rubygems.org'
2+
gemspec

LICENSE.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# License
2+
3+
Copyright (c) 2021, Logtail
4+
5+
Permission to use, copy, modify, and/or distribute this software for any purpose
6+
with or without fee is hereby granted, provided that the above copyright notice
7+
and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
11+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
13+
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
14+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
15+
THIS SOFTWARE.

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 🪵 Fluent::Plugin::Logtail, a plugin for [Fluentd](http://fluentd.org)
2+
3+
A Fluentd plugin that delivers events to the [Logtail.com logging service](https://logtail.com). It uses batching, msgpack, and retry logic for highly efficient and reliable delivery of log data.
4+
5+
## Installation
6+
7+
```
8+
gem install fluent-plugin-logtail
9+
```
10+
11+
## Usage
12+
13+
In your Fluentd configuration, use `@type logtail`:
14+
15+
```
16+
<match your_match>
17+
@type logtail
18+
source_token YOUR_SOURCE_TOKEN
19+
# ip 127.0.0.1
20+
buffer_chunk_limit 1m # Must be < 5m
21+
flush_at_shutdown true # Only needed with file buffer
22+
</match>
23+
```
24+
25+
## Configuration
26+
27+
* `source_token` - This is your [Logtail source token](https://logtail.com).
28+
29+
For advanced configuration options, please see to the [buffered output parameters documentation.](http://docs.fluentd.org/articles/output-plugin-overview#buffered-output-parameters).

fluent-plugin-logtail.gemspec

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- encoding: utf-8 -*-
2+
require 'date'
3+
4+
Gem::Specification.new do |s|
5+
s.name = 'fluent-plugin-logtail'
6+
s.version = '0.1.0'
7+
s.date = Date.today.to_s
8+
s.summary = 'Logtail.com plugin for Fluentd'
9+
s.description = 'Streams Fluentd logs to the Logtail.com logging service.'
10+
s.authors = ['Logtail.com']
11+
s.email = '[email protected]'
12+
s.homepage = 'https://github.com/logtail/fluent-plugin-logtail'
13+
s.license = 'ISC'
14+
15+
s.files = `git ls-files`.split("\n")
16+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18+
s.require_paths = ["lib"]
19+
20+
s.required_ruby_version = Gem::Requirement.new(">= 2.4.0".freeze)
21+
22+
s.add_runtime_dependency('fluentd', '> 1', '< 2')
23+
s.add_runtime_dependency('http', '~> 2.0', '>= 2.0.3')
24+
25+
s.add_development_dependency('rspec', '~> 3.4')
26+
s.add_development_dependency('test-unit', '~> 3.3.9')
27+
s.add_development_dependency('webmock', '~> 2.3')
28+
end

lib/fluent/plugin/out_logtail.rb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
require 'fluent/output'
2+
3+
module Fluent
4+
class LogtailOutput < Fluent::BufferedOutput
5+
Fluent::Plugin.register_output('logtail', self)
6+
7+
VERSION = "0.1.0".freeze
8+
CONTENT_TYPE = "application/msgpack".freeze
9+
HOST = "https://in.logtail.com".freeze
10+
PATH = "/".freeze
11+
MAX_ATTEMPTS = 3.freeze
12+
RETRYABLE_CODES = [429, 500, 502, 503, 504].freeze
13+
USER_AGENT = "Logtail Logstash/#{VERSION}".freeze
14+
15+
config_param :source_token, :string, secret: true
16+
config_param :ip, :string, default: nil
17+
18+
def configure(conf)
19+
source_token = conf["source_token"]
20+
@headers = {
21+
"Authorization" => "Bearer #{source_token}",
22+
"Content-Type" => CONTENT_TYPE,
23+
"User-Agent" => USER_AGENT
24+
}
25+
super
26+
end
27+
28+
def start
29+
super
30+
require 'http'
31+
HTTP.default_options = {:keep_alive_timeout => 29}
32+
@http_client = HTTP.persistent(HOST)
33+
end
34+
35+
def shutdown
36+
@http_client.close if @http_client
37+
super
38+
end
39+
40+
def format(tag, time, record)
41+
record.merge("dt" => Time.at(time).utc.iso8601).to_msgpack
42+
end
43+
44+
def write(chunk)
45+
deliver(chunk, 1)
46+
end
47+
48+
private
49+
def deliver(chunk, attempt)
50+
if attempt > MAX_ATTEMPTS
51+
log.error("msg=\"Max attempts exceeded dropping chunk\" attempt=#{attempt}")
52+
return false
53+
end
54+
55+
body = chunk.read
56+
response = @http_client.headers(@headers).post(PATH, body: body)
57+
response.flush
58+
code = response.code
59+
60+
if code >= 200 && code <= 299
61+
true
62+
elsif RETRYABLE_CODES.include?(code)
63+
sleep_time = sleep_for_attempt(attempt)
64+
log.warn("msg=\"Retryable response from the Logtail API\" " +
65+
"code=#{code} attempt=#{attempt} sleep=#{sleep_time}")
66+
sleep(sleep_time)
67+
deliver(chunk, attempt + 1)
68+
else
69+
log.error("msg=\"Fatal response from the Logtail API\" code=#{code} attempt=#{attempt}")
70+
false
71+
end
72+
end
73+
74+
def sleep_for_attempt(attempt)
75+
sleep_for = attempt ** 2
76+
sleep_for = sleep_for <= 60 ? sleep_for : 60
77+
(sleep_for / 2) + (rand(0..sleep_for) / 2)
78+
end
79+
end
80+
end
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require "spec_helper"
2+
require "fluent/plugin/out_logtail"
3+
4+
describe Fluent::LogtailOutput do
5+
let(:config) do
6+
%{
7+
source_token abcd1234
8+
}
9+
end
10+
11+
let(:driver) do
12+
tag = "test"
13+
Fluent::Test::BufferedOutputTestDriver.new(Fluent::LogtailOutput, tag) {
14+
# v0.12's test driver assume format definition. This simulates ObjectBufferedOutput format
15+
if !defined?(Fluent::Plugin::Output)
16+
def format(tag, time, record)
17+
[time, record].to_msgpack
18+
end
19+
end
20+
}.configure(config)
21+
end
22+
let(:record) do
23+
{'age' => 26, 'request_id' => '42', 'parent_id' => 'parent', 'routing_id' => 'routing'}
24+
end
25+
26+
before(:each) do
27+
Fluent::Test.setup
28+
end
29+
30+
describe "#write" do
31+
it "should send a chunked request to the Logtail API" do
32+
stub = stub_request(:post, "https://in.logtail.com/").
33+
with(
34+
:body => start_with("\x85\xA3age\x1A\xAArequest_id\xA242\xA9parent_id\xA6parent\xAArouting_id\xA7routing\xA2dt\xB4".force_encoding("ASCII-8BIT")),
35+
:headers => {'Authorization'=>'Bearer abcd1234', 'Connection'=>'Keep-Alive', 'Content-Type'=>'application/msgpack', 'User-Agent'=>'Logtail Logstash/0.1.0'}
36+
).
37+
to_return(:status => 200, :body => "", :headers => {})
38+
39+
driver.emit(record)
40+
driver.run
41+
42+
expect(stub).to have_been_requested.times(1)
43+
end
44+
45+
it "handles 500s" do
46+
stub = stub_request(:post, "https://in.logtail.com/").to_return(:status => 500, :body => "", :headers => {})
47+
48+
driver.emit(record)
49+
driver.run
50+
51+
expect(stub).to have_been_requested.times(3)
52+
end
53+
54+
it "handle auth failures" do
55+
stub = stub_request(:post, "https://in.logtail.com/").to_return(:status => 403, :body => "", :headers => {})
56+
57+
driver.emit(record)
58+
driver.run
59+
60+
expect(stub).to have_been_requested.times(1)
61+
end
62+
end
63+
end

spec/spec_helper.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Base
2+
require 'rubygems'
3+
require 'bundler/setup'
4+
5+
# Testing
6+
require 'rspec'
7+
8+
# Webmock
9+
require 'webmock/rspec'
10+
WebMock.disable_net_connect!
11+
12+
# Fluent
13+
require "fluent/test"
14+
15+
# Rspec
16+
RSpec.configure do |config|
17+
config.color = true
18+
config.order = :random
19+
config.warnings = false
20+
end

0 commit comments

Comments
 (0)