Skip to content
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ Notice: If you use some plugins in your sources, you should install it before yo
| type | Type of source. This is name of input plugin. |
| tag | Tag, what uses in fluentd routing. |
| parameters | Parameters of source. Hash. |
| _raw_options | Use attribute value as is. |

### Example

Expand All @@ -177,6 +178,21 @@ td_agent_source 'test_in_tail' do
end
```

Use attribute `_raw_options` for attribute that using value such as `Array` or `Hash`. For example when you using [fluentd systemd plugin](https://github.com/reevoo/fluent-plugin-systemd).
Just add the attribute name as an array item of `_raw_options`

```ruby
td_agent_source 'tail_journalctl' do
type 'systemd'
tag 'journalctl'
parameters(
matches: [{"_SYSTEMD_UNIT": "syslog.service"}]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly speaking, I'm skeptical if this is so useful. If I understand the situation correctly, just passing the parameter as a String must be sufficient for that purpose.

td_agent_source 'tail_journalctl' do
  ( ... )
  parameters(
    matches: ::JSON.generate([{"_SYSTEMD_UNIT": "syslog.service"}])
    read_from_head: true
  )
end

I know the TdAgent::Helpers.params_to_text isn't well implemented, but this change would make the situation more complicated with very few actual benefit, I think 🤔

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @yyuu thanks for reviewing my PR. I will try your approach and will tell you soon if this works on my case.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @yyuu so I've been thinking about your approach. But I found a problem when using this method. How can user set this attributes from nodes.json files where we can't use ruby code? This will be okay if the user set the attribute directly in attributes files.

Thanks in advance.

read_from_head: true
)
_raw_options ["matches"]
end
```

## td_agent_match

Create file with match definition in `/etc/td-agent/conf.d` directory. It works only if `node[:td_agent][:includes]` is `true`
Expand Down
14 changes: 10 additions & 4 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
module TdAgent
# A set of helper methods for the td-agent cookbook
module Helpers
def self.params_to_text(parameters)
def self.params_to_text(parameters, raw_options = [])
body = ''
parameters.each do |param_key, param_value|
if param_value.is_a?(Hash)
is_raw_options = raw_options.include?(param_key)

if param_value.is_a?(Hash) && !is_raw_options
body += "<#{param_key}>\n"
body += params_to_text(param_value)
body += "</#{param_key}>\n"
elsif param_value.is_a?(Array)
elsif param_value.is_a?(Array) && !is_raw_options
if param_value.all? { |array_value| array_value.is_a?(Hash) }
body += param_value.map { |array_value|
"<#{param_key}>\n#{params_to_text(array_value)}</#{param_key}>\n"
Expand All @@ -17,7 +19,11 @@ def self.params_to_text(parameters)
body += "#{param_key} [#{param_value.map { |array_value| array_value.to_s.dump }.join(", ")}]\n"
end
else
body += "#{param_key} #{param_value}\n"
if param_value.is_a?(Hash) || param_value.is_a?(Array)
body += "#{param_key} #{param_value.to_json}\n"
else
body += "#{param_key} #{param_value}\n"
end
end
end
indent = ' '
Expand Down
2 changes: 1 addition & 1 deletion providers/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
end

variables(type: new_resource.type,
parameters: TdAgent::Helpers.params_to_text(parameters),
parameters: TdAgent::Helpers.params_to_text(parameters, new_resource._raw_options),
tag: new_resource.tag)
cookbook new_resource.template_source
notifies reload_action, 'service[td-agent]'
Expand Down
1 change: 1 addition & 0 deletions resources/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
attribute :type, :kind_of => String, :required => true
attribute :tag, :kind_of => String
attribute :parameters, :kind_of => Hash, :default => {}
attribute :_raw_options, :kind_of => Array, :default => []

# Workaround for backward compatibility for Chef pre-13 (#99)
if TdAgent::Helpers.apply_params_kludge?
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/recipes/configure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
it 'creates td-agent.conf' do
expect(chef_run).to create_template('/etc/td-agent/td-agent.conf')
end

it 'starts and enables the td-agent service' do
expect(chef_run).to start_service('td-agent')
expect(chef_run).to enable_service('td-agent')
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/recipes/install_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
let(:platform) do
{ platform: 'ubuntu', version: '14.04' }
end

let(:node_attributes) do
{
'td_agent' => {
Expand Down