Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gunterze committed Feb 29, 2016
0 parents commit ae92368
Show file tree
Hide file tree
Showing 10 changed files with 302 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.iml
*.gem
Gemfile.lock
.bundle
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 2.0.0
- Plugins were updated to follow the new shutdown semantic, this mainly allows Logstash to instruct input plugins to terminate gracefully,
instead of using Thread.raise on the plugins' threads. Ref: https://github.com/elastic/logstash/pull/3895
- Dependency on logstash-core update to 2.0

2 changes: 2 additions & 0 deletions DEVELOPER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# logstash-codec-example
Example codec plugin. This should help bootstrap your effort to write your own codec plugin!
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source 'https://rubygems.org'
gemspec
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2012–2015 Elasticsearch <http://www.elastic.co>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
5 changes: 5 additions & 0 deletions NOTICE.TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Elasticsearch
Copyright 2012-2015 Elasticsearch

This product includes software developed by The Apache Software
Foundation (http://www.apache.org/).
98 changes: 98 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Logstash Plugin

[![Build
Status](http://build-eu-00.elastic.co/view/LS%20Plugins/view/LS%20Codecs/job/logstash-plugin-codec-example-unit/badge/icon)](http://build-eu-00.elastic.co/view/LS%20Plugins/view/LS%20Codecs/job/logstash-plugin-codec-example-unit/)

This is a plugin for [Logstash](https://github.com/elastic/logstash).

It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.

## Documentation

Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.co/guide/en/logstash/current/).

- For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
- For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide

## Need Help?

Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.

## Developing

### 1. Plugin Developement and Testing

#### Code
- To get started, you'll need JRuby with the Bundler gem installed.

- Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization.

- Install dependencies
```sh
bundle install
```

#### Test

```sh
bundle exec rspec
```

The Logstash code required to run the tests/specs is specified in the `Gemfile` by the line similar to:
```ruby
gem "logstash", :github => "elasticsearch/logstash", :branch => "1.5"
```
To test against another version or a local Logstash, edit the `Gemfile` to specify an alternative location, for example:
```ruby
gem "logstash", :github => "elasticsearch/logstash", :ref => "master"
```
```ruby
gem "logstash", :path => "/your/local/logstash"
```

Then update your dependencies and run your tests:

```sh
bundle install
bundle exec rspec
```

### 2. Running your unpublished Plugin in Logstash

#### 2.1 Run in a local Logstash clone

- Edit Logstash `tools/Gemfile` and add the local plugin path, for example:
```ruby
gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
```
- Update Logstash dependencies
```sh
rake vendor:gems
```
- Run Logstash with your plugin
```sh
bin/logstash -e 'filter {awesome {}}'
```
At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.

#### 2.2 Run in an installed Logstash

- Build your plugin gem
```sh
gem build logstash-filter-awesome.gemspec
```
- Install the plugin from the Logstash home
```sh
bin/plugin install /your/local/plugin/logstash-filter-awesome.gem
```
- Start Logstash and proceed to test the plugin

## Contributing

All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.

Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.

It is more important to me that you are able to contribute.

For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
68 changes: 68 additions & 0 deletions lib/logstash/codecs/frame.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# encoding: utf-8
require "logstash/codecs/base"
require "logstash/util/charset"

# Frame-oriented text data.
#
# Decoding behavior: Only whole frame events will be emitted.
#
# Encoding behavior: Each event will be emitted with a prefixed <length><delimiter>.
class LogStash::Codecs::Frame < LogStash::Codecs::Base
config_name "frame"

# Set the desired text format for encoding.
config :format, :validate => :string

# This only affects "plain" format logs since json is `UTF-8` already.
config :charset, :validate => ::Encoding.name_list, :default => "UTF-8"

# Change the delimiter that separates frame length from the data
config :delimiter, :validate => :string, :default => " "

public
def register
@buffer = ""
@offset
@frame_length
@converter = LogStash::Util::Charset.new(@charset)
@converter.logger = @logger
end

public
def decode(data)
@buffer += data
while (nextMessage)
yield LogStash::Event.new("message" => @converter.convert(@buffer[@offset, @frameLength]))
@buffer.slice!(0, @offset + @frameLength)
@offset = nil
end
end # def decode

public
def encode(event)
if event.is_a? LogStash::Event and @format
@on_event.call(event, encodeFrameLength(event.sprintf(@format)))
else
@on_event.call(event, encodeFrameLength(event.to_s))
end
end # def encode

private
def nextMessage()
if (@offset.nil?)
@offset = @buffer.index(@delimiter);
if (@offset.nil?)
return false
end
@frameLength = @buffer[0, @offset].to_i
@offset += @delimiter.length
end
return @buffer.length >= @offset + @frameLength
end # def nextMessage

private
def encodeFrameLength(message)
return message.length.to_s + @delimiter + message
end # def encodeFrameLength

end # class LogStash::Codecs::Frame
26 changes: 26 additions & 0 deletions logstash-codec-frame.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Gem::Specification.new do |s|

s.name = 'logstash-codec-frame'
s.version = '2.0.0'
s.licenses = ['Apache License (2.0)']
s.summary = "Frame-oriented text data."
s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
s.authors = ["Elastic"]
s.email = '[email protected]'
s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html"
s.require_paths = ["lib"]

# Files
s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']

# Tests
s.test_files = s.files.grep(%r{^(test|spec|features)/})

# Special flag to let us know this is actually a logstash plugin
s.metadata = { "logstash_plugin" => "true", "logstash_group" => "codec" }

# Gem dependencies
s.add_runtime_dependency "logstash-core", ">= 2.0.0.beta2", "< 3.0.0"

s.add_development_dependency 'logstash-devutils'
end
79 changes: 79 additions & 0 deletions spec/codecs/frame_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# encoding: utf-8

require "logstash/devutils/rspec/spec_helper"
require "logstash/codecs/frame"
require "logstash/event"

describe LogStash::Codecs::Frame do
subject do
next LogStash::Codecs::Frame.new
end

context "#encode" do
let (:event) {LogStash::Event.new({"message" => "hello world", "host" => "test"})}

it "should prefix message with length" do
subject.format = "%{message}"
expect(subject).to receive(:on_event).once.and_call_original
subject.on_event do |e, d|
insist {d} == "11 hello world"
end
subject.encode(event)
end

end

context "#decode" do
it "should return an event from an ascii string" do
decoded = false
subject.decode("11 hello world") do |e|
decoded = true
insist { e.is_a?(LogStash::Event) }
insist { e["message"] } == "hello world"
end
insist { decoded } == true
end

it "should return an event from a valid utf-8 string" do
subject.decode("7 München") do |e|
insist { e.is_a?(LogStash::Event) }
insist { e["message"] } == "München"
end
end

it "should return 1 event from 2 strings" do
messages = []
subject.decode("11 hello") do |e|
messages << e["message"]
end
subject.decode(" world") do |e|
messages << e["message"]
end
insist { messages } == ["hello world" ]
end

it "should return 2 events from 3 strings" do
messages = []
subject.decode("11 hello") do |e|
messages << e["message"]
end
subject.decode(" world7") do |e|
messages << e["message"]
end
subject.decode(" München") do |e|
messages << e["message"]
end
insist { messages } == ["hello world", "München"]
end

it "should return 2 events from one string" do
messages = []
subject.decode("11 hello world7 München") do |e|
messages << e["message"]
end
insist { messages } == ["hello world", "München"]
end

end

end

0 comments on commit ae92368

Please sign in to comment.