log2json 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/bin/redis2es +37 -8
  2. data/bin/tail-log.sh +10 -5
  3. data/log2json.gemspec +1 -1
  4. metadata +3 -3
data/bin/redis2es CHANGED
@@ -111,24 +111,53 @@ def enqueue(logstr)
111
111
  @queue << log.to_json
112
112
  end
113
113
 
114
+ def load_redis_script
115
+ # totaly stolen from logstash
116
+ redis_script = <<EOF
117
+ local i = tonumber(ARGV[1])
118
+ local res = {}
119
+ local length = redis.call('llen',KEYS[1])
120
+ if length < i then i = length end
121
+ while (i > 0) do
122
+ local item = redis.call("lpop", KEYS[1])
123
+ if (not item) then
124
+ break
125
+ end
126
+ table.insert(res, item)
127
+ i = i-1
128
+ end
129
+ return res
130
+ EOF
131
+ @redis_script_sha = @redis.script(:load, redis_script)
132
+ end
133
+
114
134
  def main
135
+ load_redis_script()
115
136
  time_start = Time.now
116
137
  loop do
117
138
  # wait for input from the redis queue
118
139
  ret = @redis.blpop(REDIS_QUEUE, timeout: FLUSH_TIMEOUT/2)
119
140
  enqueue(ret[1]) if ret != nil
120
141
 
121
- # try to queue up to FLUSH_SIZE
122
- while @queue.size < FLUSH_SIZE do
123
- # Logstash's redis input actually uses a Lua script to do the lpop in one request,
124
- # but let's keep it simple and stupid first here.
125
- body = @redis.lpop(REDIS_QUEUE)
126
- break if body.nil?
127
- enqueue(body)
142
+ # try to queue up to FLUSH_SIZE.
143
+ begin
144
+ length = FLUSH_SIZE - @queue.length
145
+ @redis.evalsha(@redis_script_sha, [REDIS_QUEUE], length).each do |item|
146
+ enqueue(item)
147
+ end
148
+ rescue Redis::CommandError => e
149
+ #Note: redis script reload logic and code are copied from logstash.
150
+ if e.to_s =~ /NOSCRIPT/ then
151
+ @logger.warn("Redis may have been restarted, reloading redis batch EVAL script", :exception => e);
152
+ load_redis_script()
153
+ retry
154
+ else
155
+ raise e
156
+ end
128
157
  end
129
158
 
130
159
  # flush when the queue is full or when time is up.
131
- if @queue.size == FLUSH_SIZE or (Time.now - time_start) >= FLUSH_TIMEOUT
160
+ if @queue.size >= FLUSH_SIZE or (Time.now - time_start) >= FLUSH_TIMEOUT
132
161
  time_start = Time.now # reset timer upon a flush or timeout
133
162
  flush_queue()
134
163
  end
data/bin/tail-log.sh CHANGED
@@ -27,6 +27,9 @@ IFS=$OIFS
27
27
  SINCEDB_DIR=${SINCEDB_DIR:-~/.tail-log}
28
28
  mkdir -p "$SINCEDB_DIR" || true
29
29
 
30
+ # Whether to tail from the end instead of from the first line when there's record
31
+ # of the file in SINCEDB.
32
+ SINCE_EOF=${SINCE_EOF:-""}
30
33
 
31
34
  # Helper to build the arguments to tail.
32
35
  # Specifically, we expect the use of GNU tail as found in GNU coreutils.
@@ -51,11 +54,13 @@ build_tail_args() {
51
54
  continue
52
55
  fi
53
56
  fi
54
- TAIL_ARGS[$((i++))]="-n+$(($(wc -l "$fpath" | cut -d' ' -f1) + 1))"
55
- # Note: we can't just ask tail to seek to the end here(ie, with -n0) since
56
- # then we'd lose track of the line count.
57
- # Note: if fpath doesn't exist yet, then the above evaluates to "-n+1", which
58
- # is fine.
57
+ if [ ! "$SINCE_EOF" ]; then
58
+ TAIL_ARGS[$((i++))]="-n+$(($(wc -l "$fpath" | cut -d' ' -f1) + 1))"
59
+ # Note: we can't just ask tail to seek to the end here(ie, with -n0) since
60
+ # then we'd lose track of the line count.
61
+ # Note: if fpath doesn't exist yet, then the above evaluates to "-n+1", which
62
+ # is fine.
63
+ fi
59
64
  TAIL_ARGS[$((i++))]=$fpath
60
65
  done
61
66
  }
data/log2json.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'log2json'
3
- s.version = '0.1.5'
3
+ s.version = '0.1.6'
4
4
  s.summary = "Read, filter and ship logs. ie, poor man's roll-your-own, light-weight logstash replacement."
5
5
  s.description = IO.read(File.join(File.dirname(__FILE__), 'README'))
6
6
  s.authors = ['Jack Kuan']
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 5
9
- version: 0.1.5
8
+ - 6
9
+ version: 0.1.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jack Kuan
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2013-09-13 00:00:00 -04:00
17
+ date: 2013-09-17 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency