logstash-output-vespa 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 05d52921a31026d0ec3211b06fe3c79d8d0244253015bb74695ea3aaf80ec93f
4
+ data.tar.gz: b8b69aea8f790482f799574e73ac00496d1dfd95b2ea9a97ba56b50665ff5c7a
5
+ SHA512:
6
+ metadata.gz: b5644a86918bb0f72e9b7656ae4a6c864c12afb00979603366175d42728d1816d1285088a2a652543d6de703430e345d2dc6c658fc2203e19a97ab4aa13fe567
7
+ data.tar.gz: 2b4c7e6922a94efda2923fc8af562e1c9ae5f78a62e326922f3c02406a9f270ffd899b152fcc6d74a476766c738ba182bfeac9e0ef207e721dbc3f9eb2d5e3ea
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## 0.1.0
2
+ - Plugin created with the logstash plugin generator
data/CONTRIBUTORS ADDED
@@ -0,0 +1,10 @@
1
+ The following is a list of people who have contributed ideas, code, bug
2
+ reports, or in general have helped logstash along its way.
3
+
4
+ Contributors:
5
+ * Radu Gheorghe - radu@vespa.ai
6
+
7
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
8
+ Logstash, and you aren't on the list above and want to be, please let us know
9
+ and we'll make sure you're here. Contributions from folks like you are what make
10
+ open source awesome.
data/DEVELOPER.md ADDED
@@ -0,0 +1,2 @@
1
+ # logstash-output-vespa
2
+ Example output plugin. This should help bootstrap your effort to write your own output plugin!
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ logstash_path = ENV['LOGSTASH_PATH'] || '/opt/logstash/logstash-8.14.2'
5
+
6
+ if Dir.exist?(logstash_path)
7
+ gem 'logstash-core', :path => "#{logstash_path}/logstash-core"
8
+ gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api"
9
+ end
10
+
data/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Licensed under the Apache License, Version 2.0 (the "License");
2
+ you may not use this file except in compliance with the License.
3
+ You may obtain a copy of the License at
4
+
5
+ http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Logstash Plugin
2
+
3
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
4
+
5
+ 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.
6
+
7
+ ## Documentation
8
+
9
+ 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/).
10
+
11
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
12
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
13
+
14
+ ## Need Help?
15
+
16
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
17
+
18
+ ## Developing
19
+
20
+ ### 1. Plugin Developement and Testing
21
+
22
+ #### Code
23
+ - To get started, you'll need JRuby with the Bundler gem installed.
24
+
25
+ - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example).
26
+
27
+ - Install dependencies
28
+ ```sh
29
+ bundle install
30
+ ```
31
+
32
+ #### Test
33
+
34
+ - Update your dependencies
35
+
36
+ ```sh
37
+ bundle install
38
+ ```
39
+
40
+ - Run tests
41
+
42
+ ```sh
43
+ bundle exec rspec
44
+ ```
45
+
46
+ ### 2. Running your unpublished Plugin in Logstash
47
+
48
+ #### 2.1 Run in a local Logstash clone
49
+
50
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
51
+ ```ruby
52
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
53
+ ```
54
+ - Install plugin
55
+ ```sh
56
+ bin/logstash-plugin install --no-verify
57
+ ```
58
+ - Run Logstash with your plugin
59
+ ```sh
60
+ bin/logstash -e 'filter {awesome {}}'
61
+ ```
62
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
63
+
64
+ #### 2.2 Run in an installed Logstash
65
+
66
+ You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:
67
+
68
+ - Build your plugin gem
69
+ ```sh
70
+ gem build logstash-filter-awesome.gemspec
71
+ ```
72
+ - Install the plugin from the Logstash home
73
+ ```sh
74
+ bin/logstash-plugin install /your/local/plugin/logstash-filter-awesome.gem
75
+ ```
76
+ - Start Logstash and proceed to test the plugin
77
+
78
+ ## Contributing
79
+
80
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
81
+
82
+ 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.
83
+
84
+ It is more important to the community that you are able to contribute.
85
+
86
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/main/CONTRIBUTING.md) file.
@@ -0,0 +1,89 @@
1
+ # encoding: utf-8
2
+ require "logstash/outputs/base"
3
+ require "logstash/namespace"
4
+ require "net/http"
5
+ require "uri"
6
+ require "securerandom"
7
+
8
+ class LogStash::Outputs::Vespa < LogStash::Outputs::Base
9
+ config_name "vespa"
10
+
11
+ # URL to the Vespa instance
12
+ config :vespa_url, :validate => :string, :required => true, :default => "http://localhost:8080"
13
+
14
+ # Namespace. You should see it in the Vespa services.xml file as "id" in the <content> tag
15
+ config :namespace, :validate => :string, :required => true
16
+
17
+ # Document type. You should see it in the Vespa services.xml file as well as in the schema file
18
+ config :document_type, :validate => :string, :required => true
19
+
20
+ # Field to get the document id from. If not present, a UUID will be generated
21
+ config :id_field, :validate => :string, :default => "id"
22
+
23
+ # On failure, retry this many times
24
+ config :max_retries, :validate => :number, :default => 60
25
+
26
+ concurrency :shared
27
+
28
+ public
29
+ def register
30
+ # initialize a dead letter queue writer
31
+ @dlq_writer = execution_context.dlq_writer
32
+
33
+ @base_uri = URI.parse(@vespa_url)
34
+ end # def register
35
+
36
+ public
37
+ def receive(event)
38
+ retry_attempts = @max_retries
39
+
40
+ # if there's a document "id" field, we should use it as the document id
41
+ # otherwise, we generate a UUID
42
+ id = event.get(@id_field) || SecureRandom.uuid
43
+
44
+ # build the path from the cluster name, document type and document id
45
+ uri = @base_uri.dup
46
+ uri.path = "/document/v1/#{@namespace}/#{@document_type}/docid/#{id}"
47
+
48
+ http_client = Net::HTTP.new(uri.host, uri.port)
49
+ request = Net::HTTP::Post.new(uri.request_uri, {'Content-Type' => 'application/json'})
50
+
51
+ document = {
52
+ "fields" => event.to_hash()
53
+ }
54
+
55
+ request.body = document.to_json
56
+ response = http_client.request(request)
57
+
58
+ # Retry on 429, 500, 502, 503, 504
59
+ retry_count = 0
60
+ while [429, 500, 502, 503, 504].include?(response.code.to_i) && retry_count < @max_retries
61
+ @logger.warn("Received #{response.code} for path #{uri.path} and content #{document.to_json}. " \
62
+ "Retrying... (attempt #{retry_count + 1}/#{@max_retries})")
63
+ retry_count += 1
64
+ sleep 2 ** retry_count # Exponential backoff
65
+ response = http_client.request(request)
66
+ end
67
+
68
+ if response.code.to_i != 200
69
+ @logger.error("Error sending event to Vespa. Writing to dead letter queue (if it's configured in logstash.yml).",
70
+ :response_code => response.code, :response_body => response.body)
71
+ @dlq_writer.write(event, response.body)
72
+ end
73
+
74
+ rescue => e
75
+ # initialize retry count if it's not already set
76
+ retry_count ||= 0
77
+ if retry_count < @max_retries
78
+ @logger.error("Exception caught while sending event to Vespa. Retrying... (attempt #{retry_count + 1}/#{@max_retries})",
79
+ :exception => e, :event => event)
80
+ retry_count += 1
81
+ sleep 2 ** retry_count # Exponential backoff
82
+ retry
83
+ else
84
+ @logger.error("Giving up on retrying. Writing to dead letter queue (if it's configured in logstash.yml).",
85
+ :exception => e, :event => event)
86
+ @dlq_writer.write(event, e.message)
87
+ end
88
+ end # def receive
89
+ end # class LogStash::Outputs::Vespa
@@ -0,0 +1,93 @@
1
+ # encoding: utf-8
2
+ require "logstash/outputs/base"
3
+ require "logstash/namespace"
4
+ require "httpx"
5
+ require "json"
6
+ require "uri"
7
+ require "securerandom"
8
+
9
+ class LogStash::Outputs::Vespa < LogStash::Outputs::Base
10
+ config_name "vespa"
11
+
12
+ # URL to the Vespa instance
13
+ config :vespa_url, :validate => :string, :required => true, :default => "http://localhost:8080"
14
+
15
+ # Content cluster name. You should see it in the Vespa services.xml file as "id" in the <content> tag
16
+ config :content_cluster, :validate => :string, :required => true
17
+
18
+ # Document type name. You should see it in the Vespa services.xml file as well as in the schema file
19
+ config :document_type, :validate => :string, :required => true
20
+
21
+ # field name to use as the document id. If not present, a UUID will be generated
22
+ config :id_field, :validate => :string, :default => "id"
23
+
24
+ # On failure, retry this many times
25
+ config :max_retries, :validate => :number, :default => 60
26
+
27
+ concurrency :shared
28
+
29
+ public
30
+ def register
31
+ # initialize a dead letter queue writer
32
+ @dlq_writer = execution_context.dlq_writer
33
+
34
+ @base_uri = URI.parse(@vespa_url)
35
+ end # def register
36
+
37
+ public
38
+ def multi_receive(events)
39
+ return if events.empty?
40
+
41
+ # HTTPX client with HTTP/2 support and retries
42
+ http_client = HTTPX.with(:http2).plugin(:retries, max_retries: @max_retries).with(
43
+ persistent: true,
44
+ max_concurrent_requests: 100,
45
+ buffer_size: 64_000, # 64 KB r/w buffer
46
+ window_size: 64_000 # 64 KB flow control window
47
+ )
48
+
49
+ # initialize list of requests
50
+ requests = []
51
+
52
+ events.map do |event|
53
+ # If there's a document "id" field, we should use it as the document id
54
+ # Otherwise, we generate a UUID
55
+ id = event.get(@id_field) || SecureRandom.uuid
56
+
57
+ # Build the path from the cluster name, document type, and document id
58
+ uri = @base_uri.dup
59
+ uri.path = "/document/v1/#{@content_cluster}/#{@document_type}/docid/#{id}"
60
+
61
+ document = {
62
+ "fields" => event.to_hash
63
+ }
64
+
65
+ # Build the request. We don't run it yet
66
+ request = http_client.build_request(:post, uri.to_s, json: document)
67
+ # add it to the list of requests
68
+ requests << request
69
+ end # events.map
70
+
71
+ @logger.debug("Sending #{requests.size} requests to Vespa")
72
+ # Run all requests async
73
+ responses = http_client.request(*requests)
74
+
75
+ # Handle responses
76
+ responses.each do |response|
77
+ if response.status != 200
78
+ # Handle non-200 response by writing to the DLQ
79
+ # TODO Should we retry some of those?
80
+ @logger.error("Error sending event to Vespa. Writing to dead letter queue (if configured).",
81
+ :response_code => response.status, :response_body => response.body)
82
+ # TODO fix this, we need to write the corresponding event
83
+ @dlq_writer.write(event, response.body)
84
+ end
85
+ end
86
+
87
+ rescue => e
88
+ @logger.error("Exception caught while sending events to Vespa. Writing to dead letter queue (if configured).", :exception => e)
89
+ events.each do |event|
90
+ @dlq_writer.write(event, e.message)
91
+ end
92
+ end # def multi_receive
93
+ end # class LogStash::Outputs::Vespa
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-output-vespa'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'Logstash Output Plugin for Vespa'
6
+ s.description = 'Writes documents to Vespa'
7
+ s.homepage = 'http://vespa.ai'
8
+ s.authors = ['Radu Gheorghe']
9
+ s.email = 'radu@vespa.ai'
10
+ s.require_paths = ['lib']
11
+
12
+ # Files
13
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
14
+ # Tests
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+
17
+ # Special flag to let us know this is actually a logstash plugin
18
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "output" }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"
22
+ s.add_runtime_dependency "logstash-codec-plain"
23
+ s.add_runtime_dependency "httpx"
24
+ s.add_development_dependency "logstash-devutils"
25
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/outputs/vespa"
4
+ require "logstash/codecs/plain"
5
+
6
+
7
+ describe LogStash::Outputs::Vespa do
8
+ let(:sample_event) { LogStash::Event.new }
9
+ let(:output) { LogStash::Outputs::Vespa.new }
10
+
11
+ before do
12
+ output.register
13
+ end
14
+
15
+ describe "receive message" do
16
+ subject { output.receive(sample_event) }
17
+
18
+ it "returns a string" do
19
+ expect(subject).to eq("Event received")
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-output-vespa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Radu Gheorghe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-07-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logstash-core-plugin-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: logstash-codec-plain
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httpx
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: logstash-devutils
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Writes documents to Vespa
70
+ email: radu@vespa.ai
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - CHANGELOG.md
76
+ - CONTRIBUTORS
77
+ - DEVELOPER.md
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - lib/logstash/outputs/vespa.rb
82
+ - lib/logstash/outputs/vespa.rb.http2
83
+ - logstash-output-vespa.gemspec
84
+ - spec/outputs/vespa_spec.rb
85
+ homepage: http://vespa.ai
86
+ licenses:
87
+ - Apache-2.0
88
+ metadata:
89
+ logstash_plugin: 'true'
90
+ logstash_group: output
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubygems_version: 3.2.3
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Logstash Output Plugin for Vespa
110
+ test_files:
111
+ - spec/outputs/vespa_spec.rb