logstash-filter-csv 2.1.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 74d72358f3baf0fb616273e5b0c3d6dac3784aa2
4
- data.tar.gz: dc732a6835ae9b9c37a647d9e0541ffdc8f8d130
3
+ metadata.gz: 5f4aa9f9214e37010bf9e1248710142f2c7cb8af
4
+ data.tar.gz: 0c56b56dcd712437cbf0922a1f26a5cd028ee792
5
5
  SHA512:
6
- metadata.gz: caea7cbc014ed27d8e207e257558e086e170a20c120551e11cde96cdbe58424b9b2ce7ed7516a7f0292d8b962abad434b4592a9393e23a60640b987e6f22e0d6
7
- data.tar.gz: 9d04652e8989ab10a3d5ec11961fcf53a0ee666557065a6f4e6a017399ef9e8b30e34b1302dd06f55f14673cc5614687f050fb6583ee6f496822739a9884ac1a
6
+ metadata.gz: f6ac80e0d09aabff2022a9096866a6890f9d1a44ba24aa99386c43f4dc9173f4524281c5d5f5fc1ab9f2d4fd2d905a36a43b094239dad932baf014a4529a3ce6
7
+ data.tar.gz: 481be1574ce4fcef426cf259aa7ed09a5d3a66d8f9016a629b475a640a4e4e9beb91a0bc9621bfe7f33d4c6a91f34345dbb03bdcc9bd0a8866b86913872a0e75
@@ -1,3 +1,6 @@
1
+ ## 2.1.1
2
+ - Fixed field references, refactors converters, code cleanups
3
+
1
4
  ## 2.1.0
2
5
  - Added support for not parsing coluns without a defined header.
3
6
  - Added support for dropping columns that has no value
@@ -7,7 +10,7 @@
7
10
  helper methods.
8
11
 
9
12
  ## 2.0.0
10
- - Plugins were updated to follow the new shutdown semantic, this mainly allows Logstash to instruct input plugins to terminate gracefully,
13
+ - Plugins were updated to follow the new shutdown semantic, this mainly allows Logstash to instruct input plugins to terminate gracefully,
11
14
  instead of using Thread.raise on the plugins' threads. Ref: https://github.com/elastic/logstash/pull/3895
12
15
  - Dependency on logstash-core update to 2.0
13
16
 
@@ -56,98 +56,89 @@ class LogStash::Filters::CSV < LogStash::Filters::Base
56
56
  # }
57
57
  config :convert, :validate => :hash, :default => {}
58
58
 
59
-
60
- ##
61
- # List of valid conversion types used for the convert option
62
- ##
63
- VALID_CONVERT_TYPES = [ "integer", "float", "date", "date_time", "boolean" ].freeze
64
-
59
+ CONVERTERS = {
60
+ :integer => lambda do |value|
61
+ CSV::Converters[:integer].call(value)
62
+ end,
63
+
64
+ :float => lambda do |value|
65
+ CSV::Converters[:float].call(value)
66
+ end,
67
+
68
+ :date => lambda do |value|
69
+ CSV::Converters[:date].call(value)
70
+ end,
71
+
72
+ :date_time => lambda do |value|
73
+ CSV::Converters[:date_time].call(value)
74
+ end,
75
+
76
+ :boolean => lambda do |value|
77
+ value = value.strip.downcase
78
+ return false if value == "false"
79
+ return true if value == "true"
80
+ return value
81
+ end
82
+ }
83
+ CONVERTERS.default = lambda {|v| v}
84
+ CONVERTERS.freeze
65
85
 
66
86
  def register
67
87
  # validate conversion types to be the valid ones.
68
- @convert.each_pair do |column, type|
69
- if !VALID_CONVERT_TYPES.include?(type)
70
- raise LogStash::ConfigurationError, "#{type} is not a valid conversion type."
71
- end
72
- end
73
- end # def register
88
+ bad_types = @convert.values.select do |type|
89
+ !CONVERTERS.has_key?(type.to_sym)
90
+ end.uniq
91
+
92
+ raise(LogStash::ConfigurationError, "Invalid conversion types: #{bad_types.join(', ')}") unless bad_types.empty?
93
+
94
+ # @convert_symbols contains the symbolized types to avoid symbol conversion in the transform method
95
+ @convert_symbols = @convert.inject({}){|result, (k, v)| result[k] = v.to_sym; result}
96
+
97
+ # make sure @target is in the format [field name] if defined, i.e. surrounded by brakets
98
+ @target = "[#{@target}]" if @target && @target !~ /^\[[^\[\]]+\]$/
99
+ end
74
100
 
75
101
  def filter(event)
76
- @logger.debug("Running csv filter", :event => event)
102
+ @logger.debug? && @logger.debug("Running csv filter", :event => event)
77
103
 
78
- if event[@source]
79
- source = event[@source].clone
104
+ if (source = event[@source])
80
105
  begin
81
106
  values = CSV.parse_line(source, :col_sep => @separator, :quote_char => @quote_char)
82
- if @target.nil?
83
- # Default is to write to the root of the event.
84
- dest = event
85
- else
86
- dest = event[@target] ||= {}
87
- end
88
107
 
89
108
  values.each_index do |i|
90
- if !(@skip_empty_columns && (values[i].nil? || values[i].empty?))
91
- if !ignore_field?(i)
92
- field_name = @columns[i] ? @columns[i] : "column#{i+1}"
93
- dest[field_name] = if should_transform?(field_name)
94
- transform(field_name, values[i])
95
- else
96
- values[i]
97
- end
109
+ unless (@skip_empty_columns && (values[i].nil? || values[i].empty?))
110
+ unless ignore_field?(i)
111
+ field_name = @columns[i] || "column#{i + 1}"
112
+ event[field_ref(field_name)] = transform(field_name, values[i])
98
113
  end
99
114
  end
100
115
  end
116
+
101
117
  filter_matched(event)
102
118
  rescue => e
103
- event.tag "_csvparsefailure"
104
- @logger.warn("Trouble parsing csv", :field => @source, :source => source, :exception => e)
119
+ event.tag("_csvparsefailure")
120
+ @logger.warn("Error parsing csv", :field => @source, :source => source, :exception => e)
105
121
  return
106
- end # begin
107
- end # if event
108
-
109
- @logger.debug("Event after csv filter", :event => event)
122
+ end
123
+ end
110
124
 
111
- end # def filter
125
+ @logger.debug? && @logger.debug("Event after csv filter", :event => event)
126
+ end
112
127
 
113
128
  private
114
129
 
115
- def ignore_field?(index)
116
- !@columns[index] && !@autogenerate_column_names
130
+ # construct the correct Event field reference for given field_name, taking into account @target
131
+ # @param field_name [String] the bare field name without brakets
132
+ # @return [String] fully qualified Event field reference also taking into account @target prefix
133
+ def field_ref(field_name)
134
+ "#{@target}[#{field_name}]"
117
135
  end
118
136
 
119
- def should_transform?(field_name)
120
- !@convert[field_name].nil?
137
+ def ignore_field?(index)
138
+ !@columns[index] && !@autogenerate_column_names
121
139
  end
122
140
 
123
141
  def transform(field_name, value)
124
- transformation = @convert[field_name].to_sym
125
- converters[transformation].call(value)
142
+ CONVERTERS[@convert_symbols[field_name]].call(value)
126
143
  end
127
-
128
- def converters
129
- @converters ||= {
130
- :integer => lambda do |value|
131
- CSV::Converters[:integer].call(value)
132
- end,
133
- :float => lambda do |value|
134
- CSV::Converters[:float].call(value)
135
-
136
- end,
137
- :date => lambda do |value|
138
- CSV::Converters[:date].call(value)
139
-
140
- end,
141
- :date_time => lambda do |value|
142
- CSV::Converters[:date_time].call(value)
143
- end,
144
- :boolean => lambda do |value|
145
- value = value.strip.downcase
146
- return false if value == "false"
147
- return true if value == "true"
148
- return value
149
- end
150
- }
151
- end
152
- end # class LogStash::Filters::Csv
153
-
144
+ end
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
 
3
3
  s.name = 'logstash-filter-csv'
4
- s.version = '2.1.0'
4
+ s.version = '2.1.1'
5
5
  s.licenses = ['Apache License (2.0)']
6
6
  s.summary = "The CSV filter takes an event field containing CSV data, parses it, and stores it as individual fields (can optionally specify the names)."
7
7
  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"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-filter-csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elastic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-01 00:00:00.000000000 Z
11
+ date: 2015-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logstash-core