-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #832 from fluent/separate-plugin-id-class
separate PluginId module from config.rb
- Loading branch information
Showing
5 changed files
with
60 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# | ||
# Fluent | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
require 'set' | ||
|
||
module Fluent | ||
module PluginId | ||
@@configured_ids = Set.new | ||
|
||
def configure(conf) | ||
@id = conf['@id'] || conf['id'] | ||
@_id_configured = !!@id # plugin id is explicitly configured by users (or not) | ||
if @id | ||
@id = @id.to_s | ||
if @@configured_ids.include?(@id) && !plugin_id_for_test? | ||
raise Fluent::ConfigError, "Duplicated plugin id `#{@id}`. Check whole configuration and fix it." | ||
end | ||
@@configured_ids.add(@id) | ||
end | ||
|
||
super | ||
end | ||
|
||
def plugin_id_for_test? | ||
caller_locations.each do |location| | ||
# Thread::Backtrace::Location#path returns base filename or absolute path. | ||
# #absolute_path returns absolute_path always. | ||
# https://bugs.ruby-lang.org/issues/12159 | ||
if location.absolute_path =~ /\/test_[^\/]+\.rb$/ # location.path =~ /test_.+\.rb$/ | ||
return true | ||
end | ||
end | ||
false | ||
end | ||
|
||
def plugin_id_configured? | ||
@_id_configured | ||
end | ||
|
||
def plugin_id | ||
@id ? @id : "object:#{object_id.to_s(16)}" | ||
end | ||
end | ||
end |