Class: Fluent::FilterAggregate

Inherits:
Filter
  • Object
show all
Defined in:
lib/fluent/plugin/filter_aggregate.rb

Defined Under Namespace

Classes: TimerWatcher

Constant Summary collapse

VALID_AGGREGATIONS =
['sum','min','max','mean','median','variance','standard_deviation','bucket']

Instance Method Summary collapse

Constructor Details

#initializeFilterAggregate

Returns a new instance of FilterAggregate.



32
33
34
# File 'lib/fluent/plugin/filter_aggregate.rb', line 32

def initialize
  super
end

Instance Method Details

#aggregate_eventsObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/fluent/plugin/filter_aggregate.rb', line 160

def aggregate_events

  #log.trace @aggregator
  @aggregator_mutex.synchronize do
	data_aggregated = @data_operations.aggregate_events
    data_aggregated.each{|s_interval,data_aggregated|
      #log.trace data_aggregated
      es = MultiEventStream.new
      data_aggregated.each {|item|
         es.add(item['time'], item)
      }
      unless es.empty?
        tag="#{@aggregate_event_tag}.#{s_interval}"
        router.emit_stream(tag, es)
      end
    } if data_aggregated
  end

#rescue Exception => e
#  $log.error e
end

#configure(conf) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fluent/plugin/filter_aggregate.rb', line 36

def configure(conf)
  super

  require 'dataoperations-aggregate'

  @intervals = @interval unless @interval.nil?
  @hash_time_format = "%Y-%m-%dT%H"
  @interval_seconds = 3600
  @intervals[1..-1].each{|interval|
    case interval
    when 1,5,10,20,30,60,120,180,240,300,600,900,1200,1800,3600
	  if ! (interval % @intervals[0]) == 0
        raise Fluent::ConfigError, "interval must be multiple of default_aggregate_interval(#{@default_aggregate_interval}s)"
	  end
    else
      raise Fluent::ConfigError, "interval must set to 1s,5s,10s,20s,30s,1m,5m,10m"
    end
  }

  @group_field_names = @group_fields
  @aggregate_field_names = @aggregate_fields
  @aggregation_names = @aggregations
  @aggregator_name = "#{Socket.gethostname}"
  @aggregator_name = "#{@aggregator_name}-#{@aggregator_suffix_name}" unless @aggregator_suffix_name.nil?

  @aggregation_names.each {|operation|
    if ! VALID_AGGREGATIONS.include?(operation)
      raise Fluent::ConfigError, "aggregations must set any combination of sum,min,max,mean,median,variance,standard_deviation "
    end
  }

  if @aggregation_names.include?("bucket") && (buckets.empty? || bucket_metrics.empty?)
    log.warn "bucket aggregation disabled, need buckets & bucket_metrics parameters to work, please review documentation."
  else
    log.info "bucket aggregation enabled, bucket count bucket_metrics with values <= buckets parameter"
  end

  @aggregator = {}
  if load_temporarystatus_file_enabled && ! @temporary_status_file_path.nil? && File.exist?(@temporary_status_file_path) && file_status = File.open(@temporary_status_file_path,'r')
    begin
      @aggregator=eval(file_status.read)
      file_status.close
      File.delete(@temporary_status_file_path)
      log.info "Temporary information loaded from temporary_status_file_path:#{@temporary_status_file_path} before startup"
    rescue Exception => e
      log.warn "Failed to load temporary_status_file_path:#{@temporary_status_file_path}"
      log.warn e.message
      log.warn e.backtrace.inspect
    end
  end

  @aggregator = {} unless @aggregator.is_a?(Hash)

  log.warn "temporary_status_file_path is empty, is recomended using to avoid lost statistic information beetween restarts." if @temporary_status_file_path.nil?
  @aggregator_mutex = Mutex.new
  @processing_mode_type=@processing_mode=='batch' ? :batch : :online
  @time_started_mode_type=@time_started_mode=='first_event' ? :fist_event : :last_event
  @data_operations = DataOperations::Aggregate.new(aggregator: @aggregator,
                      time_format: @time_format,
                      time_field: @time_field,
                      output_time_format: @output_time_format,
                      intervals: @intervals,
                      flush_interval: @flush_interval,
                      keep_interval: @keep_interval,
                      field_no_data_value: @field_no_data_value,
                      processing_mode: @processing_mode_type,
                      time_started_mode: @time_started_mode_type,
                      log: log,
                      aggregator_name: @aggregator_name,
                      aggregation_names: @aggregation_names,
                      group_field_names: @group_field_names,
                      aggregate_field_names: @aggregate_field_names,
                      buckets: @buckets,
                      bucket_metrics: @bucket_metrics
                     )
end

#filter(tag, time, record) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fluent/plugin/filter_aggregate.rb', line 113

def filter(tag, time, record)
  result = nil
  begin
    result = record unless ! emit_original_message

    @data_operations.add_events(record)
  rescue => e
    log.warn "failed to filter events", :error_class => e.class, :error => e.message
    log.warn_backtrace
  end
  result
end

#runObject



134
135
136
137
138
139
# File 'lib/fluent/plugin/filter_aggregate.rb', line 134

def run
  @loop.run
rescue
  log.error "unexpected error", :error=>$!.to_s
  log.error_backtrace
end

#shutdownObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/fluent/plugin/filter_aggregate.rb', line 141

def shutdown
  @loop.stop
  @thread.join

  if load_temporarystatus_file_enabled && ! @temporary_status_file_path.nil? && file_status = File.open(@temporary_status_file_path,'w')
    begin
      file_status.write @aggregator
      file_status.close
      log.info "Temporary information stored in temporary_status_file_path:#{@temporary_status_file_path} before shutdown"
    rescue Exception => e
      log.warn "Failed to load temporary_status_file_path:#{@temporary_status_file_path}"
      log.warn e.message
      log.warn e.backtrace.inspect
    end
  end

  super
end

#startObject



126
127
128
129
130
131
132
# File 'lib/fluent/plugin/filter_aggregate.rb', line 126

def start
  super
  @loop = Coolio::Loop.new
  tw = TimerWatcher.new(@flush_interval, true, @log, &method(:aggregate_events))
  tw.attach(@loop)
  @thread = Thread.new(&method(:run))
end