Skip to content

Commit

Permalink
fix #346. Fix dump_stats and allow configuration to disable
Browse files Browse the repository at this point in the history
  • Loading branch information
jcantrill committed Jul 12, 2022
1 parent e5a0fc7 commit 86f7f98
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -58,6 +58,7 @@ when true (default: `true`)
* `skip_container_metadata` - Skip some of the container data of the metadata. The metadata will not contain the container_image and container_image_id fields.
* `skip_master_url` - Skip the master_url field from the metadata.
* `skip_namespace_metadata` - Skip the namespace_id field from the metadata. The fetch_namespace_metadata function will be skipped. The plugin will be faster and cpu consumption will be less.
* 'stats_interval` - The interval to display cache stats (default: 30s). Set to 0 to disable stats collection and logging
* `watch_retry_interval` - The time interval in seconds for retry backoffs when watch connections fail. (default: `10`)


Expand Down
6 changes: 6 additions & 0 deletions lib/fluent/plugin/filter_kubernetes_metadata.rb
Expand Up @@ -170,7 +170,12 @@ def configure(conf)

require 'kubeclient'
require 'lru_redux'

@stats = KubernetesMetadata::Stats.new
if @stats_interval <= 0
@stats = KubernetesMetadata::NoOpStats.new
self.define_singleton_method(:dump_stats) {}
end

if @cache_ttl < 0
log.info 'Setting the cache TTL to :none because it was <= 0'
Expand Down Expand Up @@ -347,6 +352,7 @@ def filter(tag, time, record)
time, batch_miss_cache, record['docker']['container_id']))
metadata = k_metadata
end
dump_stats
metadata ? record.merge(metadata) : record
end

Expand Down
16 changes: 16 additions & 0 deletions lib/fluent/plugin/kubernetes_metadata_stats.rb
Expand Up @@ -43,4 +43,20 @@ def to_s
end.join(', ')
end
end
class NoOpStats
def initialize
end

def bump(key)
end

def set(key, value)
end

def [](key)
end

def to_s
end
end
end
19 changes: 19 additions & 0 deletions test/plugin/test_filter_kubernetes_metadata.rb
Expand Up @@ -41,6 +41,24 @@ def create_driver(conf = '')
assert_equal(1000, d.instance.cache_size)
end

sub_test_case 'stats_interval' do

test 'enables stats when greater than zero' do
d = create_driver('stats_interval 1')
assert_equal(1, d.instance.stats_interval)
d.instance.dump_stats
assert_false(d.instance.instance_variable_get("@curr_time").nil?)
end

test 'disables stats when <= zero' do
d = create_driver('stats_interval 0')
assert_equal(0, d.instance.stats_interval)
d.instance.dump_stats
assert_nil(d.instance.instance_variable_get("@curr_time"))
end

end

test 'check test_api_adapter' do
d = create_driver('test_api_adapter KubernetesMetadata::TestApiAdapter')
assert_equal('KubernetesMetadata::TestApiAdapter', d.instance.test_api_adapter)
Expand Down Expand Up @@ -620,6 +638,7 @@ def emit_with_tag(tag, msg = {}, config = '
kubernetes_url https://localhost:8443
watch false
cache_size 1
stats_interval 0
')
d.run do
d.feed(VAR_LOG_CONTAINER_TAG, msgpack_stream)
Expand Down

0 comments on commit 86f7f98

Please sign in to comment.