Class: TreasureData::Logger::TreasureDataLogger

Inherits:
Fluent::Logger::LoggerBase
  • Object
show all
Defined in:
lib/td/logger/tdlog.rb

Overview

TODO shutdown handler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apikey, tag, auto_create_table) ⇒ TreasureDataLogger

Returns a new instance of TreasureDataLogger.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/td/logger/tdlog.rb', line 8

def initialize(apikey, tag, auto_create_table)
  require 'thread'
  require 'stringio'
  require 'zlib'
  require 'msgpack'
  require 'time'
  require 'net/http'
  require 'cgi'
  require 'logger'

  @tag = tag
  @auto_create_table = auto_create_table
  @logger = ::Logger.new(STDOUT)

  # TODO
  gem 'td'
  require 'td/client'
  @client = TreasureData::Client.new(apikey)

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

  @chunk_limit = 8*1024*1024
  @flush_interval = 60
  @retry_wait = 1.0
  @retry_limit = 8

  @finish = false
  @next_time = Time.now.to_i + @flush_interval
  @error_count = 0
  @upload_thread = Thread.new(&method(:upload_main))
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



43
44
45
# File 'lib/td/logger/tdlog.rb', line 43

def logger
  @logger
end

Instance Method Details

#closeObject



45
46
47
48
49
50
51
# File 'lib/td/logger/tdlog.rb', line 45

def close
  @finish = true
  @mutex.synchronize {
    @flush_now = true
    @cond.signal
  }
end

#post(tag, record) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/td/logger/tdlog.rb', line 53

def post(tag, record)
  tag = "#{@tag}.#{tag}"
  db, table = tag.split('.')[-2, 2]

  record['time'] ||= Time.now.to_i

  key = [db, table]
  @mutex.synchronize do
    buffer = (@map[key] ||= '')
    record.to_msgpack(buffer)

    if buffer.size > @chunk_limit
      @queue << [db, table, buffer]
      @map.delete(key)
      @cond.signal
    end
  end

  nil
end

#upload_mainObject



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
# File 'lib/td/logger/tdlog.rb', line 74

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

  if @next_time <= now || (@flush_now && @error_count == 0)
    @mutex.unlock
    begin
      try_flush
    ensure
      @mutex.lock
    end
    @flush_now = false
  end

  if @error_count == 0
    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