Class: TreasureData::Logger::TreasureDataLogger

Inherits:
Fluent::Logger::LoggerBase
  • Object
show all
Extended by:
Finalizable
Defined in:
lib/td/logger/td_logger.rb

Defined Under Namespace

Modules: Finalizable Classes: Buffer

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Finalizable

finalizer, new

Constructor Details

#initialize(tag_prefix, options = {}) ⇒ TreasureDataLogger

Returns a new instance of TreasureDataLogger.



25
26
27
28
29
30
31
32
33
34
35
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
# File 'lib/td/logger/td_logger.rb', line 25

def initialize(tag_prefix, options={})
  defaults = {
    :auto_create_table => false,
  }
  options = defaults.merge!(options)

  @tag_prefix = tag_prefix
  @auto_create_table = !!options[:auto_create_table]

  apikey = options[:apikey]
  unless apikey
    raise ArgumentError, ":apikey options is required"
  end

  debug = !!options[:debug]

  require 'thread'
  require 'stringio'
  require 'zlib'
  require 'msgpack'
  require 'json'
  require 'time'
  require 'net/http'
  require 'cgi'
  require 'logger'
  require 'td-client'

  @logger = ::Logger.new(STDERR)
  if debug
    @logger.level = ::Logger::DEBUG
  else
    @logger.level = ::Logger::INFO
  end

  @client = TreasureData::Client.new(apikey)

  @mutex = Mutex.new
  @cond = ConditionVariable.new
  @map = {}  # (db,table) => buffer:String
  @queue = []

  @chunk_limit = 8*1024*1024
  @queue_limit = 50

  @flush_interval = 2
  @max_flush_interval = 300
  @retry_wait = 1.0
  @retry_limit = 12

  @finish = false
  @next_time = Time.now.to_i + @flush_interval
  @error_count = 0

  # start thread when the first post() is called for
  # Unicorn and Passenger.
  @upload_thread = nil
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



83
84
85
# File 'lib/td/logger/td_logger.rb', line 83

def logger
  @logger
end

Instance Method Details

#closeObject



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
112
113
114
# File 'lib/td/logger/td_logger.rb', line 85

def close
  unless @finish
    @finish = true
    @mutex.synchronize {
      @flush_now = true
      @cond.signal
    }
    @upload_thread.join if @upload_thread

    @queue.reject! {|db,table,data|
      begin
        upload(db, table, data)
        true
      rescue
        @logger.error "Failed to upload event logs to Treasure Data, trashed: #{$!}"
        false
      end
    }
    @map.reject! {|(db,table),buffer|
      data = buffer.flush!
      begin
        upload(db, table, data)
        true
      rescue
        @logger.error "Failed to upload event logs to Treasure Data, trashed: #{$!}"
        false
      end
    }
  end
end

#post_with_time(tag, record, time) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/td/logger/td_logger.rb', line 116

def post_with_time(tag, record, time)
  @logger.debug { "event: #{tag} #{record.to_json}" rescue nil }

  record[:time] ||= time.to_i

  tag = "#{@tag_prefix}.#{tag}" if @tag_prefix
  db, table = tag.split('.')[-2, 2]

  add(db, table, record)
end

#upload_mainObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/td/logger/td_logger.rb', line 127

def upload_main
  @mutex.lock
  until @finish
    now = Time.now.to_i

    if @next_time <= now || (@flush_now && @error_count == 0)
      flushed = try_flush
      @flush_now = false
    end

    if @error_count == 0
      if flushed && @flush_interval < @max_flush_interval
        @flush_interval = [@flush_interval ** 2, @max_flush_interval].min
      end
      next_wait = @flush_interval
    else
      next_wait = @retry_wait * (2 ** (@error_count-1))
    end
    @next_time = next_wait + now

    cond_wait(next_wait)
  end

rescue
  @logger.error "Unexpected error: #{$!}"
  $!.backtrace.each {|bt|
    @logger.info bt
  }
ensure
  @mutex.unlock
end