Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sprintf-formatting of column names in coerce_values #89

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 5.0.6
- Column names in coerce_values now supports sprintf formatting

## 5.0.5
- Added clarifying information for `send_as_tags` config option [#80](https://github.com/logstash-plugins/logstash-output-influxdb/pull/80)

Expand Down
39 changes: 30 additions & 9 deletions lib/logstash/outputs/influxdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class LogStash::Outputs::InfluxDB < LogStash::Outputs::Base
#
# currently supported datatypes are `integer` and `float`
#
# supports sprintf-formatting in column names
#
config :coerce_values, :validate => :hash, :default => {}

# Automatically use fields from the event as the data points sent to Influxdb
Expand Down Expand Up @@ -155,17 +157,20 @@ def receive(event)
time = timestamp_at_precision(event.timestamp, @time_precision.to_sym)
point = create_point_from_event(event)
exclude_fields!(point)
coerce_values!(point)
coerce_values!(point, event)

if point.has_key?('time')
unless @allow_time_override
logger.error("Cannot override value of time without 'allow_time_override'. Using event timestamp")
@logger.error("Cannot override value of time without 'allow_time_override'. Using event timestamp")
else
time = point.delete("time")
end
end


if point.empty?
@logger.debug? and @logger.debug("Received data points empty")
return
end

tags, point = extract_tags(point)

Expand Down Expand Up @@ -217,12 +222,18 @@ def create_point_from_event(event)
# foreknowledge of what's in the data point, which is less than ideal. An
# alternative is to use a `code` filter and manipulate the individual point's
# data before sending to the output pipeline
def coerce_values!(event_data)
def coerce_values!(event_data, event)
@coerce_values.each do |column, value_type|
column = event.sprintf(column)
if event_data.has_key?(column)
begin
@logger.debug? and @logger.debug("Converting column #{column} to type #{value_type}: Current value: #{event_data[column]}")
event_data[column] = coerce_value(value_type, event_data[column])
coerced_value, ok = coerce_value(value_type, event_data[column])
if ok
event_data[column] = coerced_value
else
event_data.delete(column)
end

rescue => e
@logger.error("Unhandled exception", :error => e.message)
Expand All @@ -237,17 +248,27 @@ def coerce_values!(event_data)
def coerce_value(value_type, value)
case value_type.to_sym
when :integer
value.to_i
number = Integer(value) rescue nil
if number.nil?
[value, false]
else
[number, true]
end

when :float
value.to_f
number = Float(value) rescue nil
if number.nil?
[value, false]
else
[number, true]
end

when :string
value.to_s
[value.to_s, true]

else
@logger.warn("Don't know how to convert to #{value_type}. Returning value unchanged")
value
[value, true]
end
end

Expand Down
2 changes: 1 addition & 1 deletion logstash-output-influxdb.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'logstash-output-influxdb'
s.version = '5.0.5'
s.version = '5.0.6'
s.licenses = ['Apache License (2.0)']
s.summary = "Writes metrics to InfluxDB"
s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
Expand Down