How to save next line with ruby filter

I wanted to know if it's possible to write an advanced ruby code into a logstash filter in order to keep specific value.
I have something like:

ruby {
	code => "		
		_isInCyclic = false
		_isJump = false
		_loggerName = ''
		
		_currentLine = event.get('message')
		
		if _loggerName == ''
			if _currentLine['[SynchronizedThreadPoolStatistics] Cyclic thread pool statistics']
				_loggerName = 'SynchronizedThreadPoolStatistics'
				event.tag('inCyclicSynchro')
			else if _currentLine['[ThreadPoolStatistics] Cyclic thread pool statistics']
				_loggerName = 'ThreadPoolStatistics'
				event.tag('inCyclicThread')
				end
			end
		end

		if !_isInCyclic && _currentLine['[' + _loggerName + '] Cyclic thread pool statistics']
			print(_loggerName)
			_isInCyclic = true
			_isJump = true
		elsif _isInCyclic && _currentLine['Total thread pool statistics']
			_isInCyclic = false
		elsif _isInCyclic && _currentLine['Cyclic thread pool statistics']
			_isJump = true
		elsif _isInCyclic
			if _isJump
				_isJump = false
			else 
				if _currentLine['[' + _loggerName + ']' + '  ']
					event.tag('LineIwantToKeep')
				end
			end
		end
		"
}

When I run the code, logstash works normaly but it seems like he never going through the second if statement "if !_isInCyclic && _currentLine['[' + _loggerName......" so I don't see my tag "LineIwantToKeep" appears on lines that I want to send to ES.

I thought that this is because a filter is apply line by line no? Is it possible to tell logstash that we want have variables outside of the loop?

Yes. You need to declare those variables in a init block. They need to be instance variable prefix @. The others can be local variables.

    ruby {
      init => "
        @isInCyclic = false
        @isJump = false
      "
      code => "
        _loggerName = ''
        _currentLine = event.get('message')
        
        if _loggerName == ''
          if _currentLine['[SynchronizedThreadPoolStatistics] Cyclic thread pool statistics']
            _loggerName = 'SynchronizedThreadPoolStatistics'
            event.tag('inCyclicSynchro')
          else if _currentLine['[ThreadPoolStatistics] Cyclic thread pool statistics']
            _loggerName = 'ThreadPoolStatistics'
            event.tag('inCyclicThread')
            end
          end
        end

        if !@isInCyclic && _currentLine['[' + _loggerName + '] Cyclic thread pool statistics']
          print(_loggerName)
          @isInCyclic = true
          @isJump = true
        elsif @isInCyclic && _currentLine['Total thread pool statistics']
          @isInCyclic = false
        elsif @isInCyclic && _currentLine['Cyclic thread pool statistics']
          @isJump = true
        elsif @isInCyclic
          if @isJump
            @isJump = false
          else 
            if _currentLine['[' + _loggerName + ']' + '  ']
              event.tag('LineIwantToKeep')
            end
          end
        end
      "
    }

Thanks. That's exactly what I needed

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.