{"record":{"id":"d82fda70144ca587","repo":"fluent/fluentd","slug":"regexp-i-contains-a-duplicated-key-key","errorCode":null,"errorMessage":"regexp#{i} contains a duplicated key, #{key}","messagePattern":"regexp#(.+?) contains a duplicated key, #(.+?)","errorType":"validation","errorClass":"Fluent::ConfigError","httpStatus":null,"severity":"error","filePath":"lib/fluent/plugin/filter_grep.rb","lineNumber":100,"sourceCode":"        config_param :key, :string\n        desc \"The regular expression.\"\n        config_param :pattern, :regexp\n      end\n    end\n\n    def configure(conf)\n      super\n\n      regexp_and_conditions = {}\n      regexp_or_conditions = {}\n      exclude_and_conditions = {}\n      exclude_or_conditions = {}\n\n      (1..REGEXP_MAX_NUM).each do |i|\n        next unless conf[\"regexp#{i}\"]\n        key, regexp = conf[\"regexp#{i}\"].split(/ /, 2)\n        raise Fluent::ConfigError, \"regexp#{i} does not contain 2 parameters\" unless regexp\n        raise Fluent::ConfigError, \"regexp#{i} contains a duplicated key, #{key}\" if regexp_and_conditions[key]\n        regexp_and_conditions[key] = Expression.new(record_accessor_create(key), Regexp.compile(regexp))\n      end\n\n      (1..REGEXP_MAX_NUM).each do |i|\n        next unless conf[\"exclude#{i}\"]\n        key, exclude = conf[\"exclude#{i}\"].split(/ /, 2)\n        raise Fluent::ConfigError, \"exclude#{i} does not contain 2 parameters\" unless exclude\n        raise Fluent::ConfigError, \"exclude#{i} contains a duplicated key, #{key}\" if exclude_or_conditions[key]\n        exclude_or_conditions[key] = Expression.new(record_accessor_create(key), Regexp.compile(exclude))\n      end\n\n      if @regexps.size > 1\n        log.info \"Top level multiple <regexp> is interpreted as 'and' condition\"\n      end\n      @regexps.each do |e|\n        raise Fluent::ConfigError, \"Duplicate key: #{e.key}\" if regexp_and_conditions.key?(e.key)\n        regexp_and_conditions[e.key] = Expression.new(record_accessor_create(e.key), e.pattern)\n      end","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/fluent/fluentd/blob/dd45c6e18dc7be33b5e5a0f0767bf46307ff5626/lib/fluent/plugin/filter_grep.rb#L82-L118","documentation":"While parsing the deprecated numbered grep directives, configure builds a hash keyed by record field name. If two regexpN lines reference the same field key, the second is rejected with Fluent::ConfigError because each key may only carry one AND-condition pattern in this syntax.","triggerScenarios":"Two lines like regexp1 message /a/ and regexp2 message /b/ — same key 'message' parsed from different regexpN indices; also a single line duplicated by a config generator or copy-paste.","commonSituations":"Trying to express 'field matches X OR Y' with two regexpN lines (this syntax is AND, and duplicate keys are disallowed — the fix is one combined regex or <or> sections); templated configs that stamp the same key repeatedly.","solutions":["Combine the two patterns into one regex: regexp1 message /(a|b)/","Use different field keys if the conditions really apply to different fields","Migrate to <or> sections for OR semantics over the same key","Validate with fluentd --dry-run"],"exampleFix":"# before\nregexp1 message /error/\nregexp2 message /warning/\n\n# after\n<or>\n  <regexp>\n    key message\n    pattern /error/\n  </regexp>\n  <regexp>\n    key message\n    pattern /warning/\n  </regexp>\n</or>","handlingStrategy":"validation","validationCode":"keys = (1..20).filter_map { |i| conf[\"regexp#{i}\"]&.split(/ /, 2)&.first }\ndupes = keys.tally.select { |_, n| n > 1 }.keys\nabort \"duplicate regexpN keys: #{dupes.join(', ')}\" unless dupes.empty?","typeGuard":null,"tryCatchPattern":"begin\n  driver = Fluent::Test::Driver::Filter.new(Fluent::Plugin::GrepFilter).configure(conf)\nrescue Fluent::ConfigError => e\n  # e.message contains the duplicated key; merge into one alternation regex and reconfigure\n  raise\nend","preventionTips":["One regexpN line per field key; combine alternatives with (a|b)","For OR over the same field use <or> sections or one alternation regex","Dry-run configs in CI","Avoid copy-pasting grep lines without checking the key token"],"tags":["fluentd","grep-filter","duplicate-key","configuration","configerror"],"backgroundTag":"duplicate-config-key","analyzedSha":"dd45c6e18dc7be33b5e5a0f0767bf46307ff5626","analyzedAt":"2026-08-21T16:22:07.332Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}