Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Allows for extra configuration with the 'graphite' key.
Browse files Browse the repository at this point in the history
A Chef node considers hash keys as strings, by having symbols and
strings the hash can not merge properly and as a result, adding another
`graphite` key to the `extra_config` results in a hash with two
`graphite` keys.
The result is that there is no way at the moment to add any more values to the `graphite`
key (see https://github.com/etsy/statsd/blob/master/docs/namespacing.md
for more examples)

To go around this problem the first thing to do is to remove symbols
from the `config_hash` hash and turn them into strings.
The second thing is to then do a "deep merge" since a regular merge will
only go down one level in the hash. To accomplish this we take advantage
of the Chef Mixins.

`config_hash` should really be a node attribute to prevent this type of
behavior and let Chef handle the deep merges but for the sake of
backward compatibility it was left as is. However this should be
considered when bumping the cookbook to a major release.

This allows the user to do things like this:

```ruby
node.default['statsd']['extra_config'] = {
  'graphite' => {
    'globalPrefix' => 'something'
  },
}
```

And the output of the config.js will be

```
  "graphite": {
    "legacyNamespace": false,
    "globalPrefix": "something"
  },

```
  • Loading branch information
Martin Cozzi committed Sep 4, 2014
1 parent cb55745 commit 713d3e6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
license 'Apache 2.0'
description 'Installs/Configures statsd'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version '0.3.2'
version '0.3.3'

depends 'build-essential'
depends 'git'
Expand Down
9 changes: 5 additions & 4 deletions recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'chef/mixin/deep_merge'

include_recipe 'nodejs'
include_recipe 'git'
Expand Down Expand Up @@ -98,12 +99,12 @@
}

if node['statsd']['graphite_enabled']
config_hash[:graphite] = { legacyNamespace: node['statsd']['legacyNamespace'] }
config_hash[:graphitePort] = node['statsd']['graphite_port']
config_hash[:graphiteHost] = node['statsd']['graphite_host']
config_hash['graphite'] = { 'legacyNamespace' => node['statsd']['legacyNamespace'] }
config_hash['graphitePort'] = node['statsd']['graphite_port']
config_hash['graphiteHost'] = node['statsd']['graphite_host']
end

config_hash = config_hash.merge(node['statsd']['extra_config'])
Chef::Mixin::DeepMerge.deep_merge!(node['statsd']['extra_config'], config_hash)
variables config_hash: config_hash
notifies :restart, 'service[statsd]', :delayed
end
Expand Down

0 comments on commit 713d3e6

Please sign in to comment.