Skip to content

Commit

Permalink
Merge pull request #22 from tdeo/master
Browse files Browse the repository at this point in the history
Allow for Date in index
  • Loading branch information
mcollina committed Feb 6, 2019
2 parents 7519ab5 + b1082f3 commit dc0ecb3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ npm install pino-elasticsearch -g
-H | --host the IP address of elasticsearch; default: 127.0.0.1
-p | --port the port of elasticsearch; default: 9200
-i | --index the name of the index to use; default: pino
will replace %{DATE} with the YYYY-MM-DD date
-t | --type the name of the type to use; default: log
-b | --size the number of documents for each bulk insert
-l | --trace-level trace level for the elasticsearch client, default 'error' (info, debug, trace).
Expand Down
10 changes: 8 additions & 2 deletions pino-elasticsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ function pinoElasticSearch (opts) {
for (var i = 0; i < docs.length; i++) {
if (i % 2 === 0) {
// add the header
docs[i] = { index: { _index: index, _type: type } }
docs[i] = {
index: {
_type: type,
_index: index.replace('%{DATE}', chunks[Math.floor(i / 2)].chunk.time.substring(0, 10))
}
}
} else {
// add the chunk
docs[i] = chunks[Math.floor(i / 2)].chunk
Expand All @@ -83,7 +88,8 @@ function pinoElasticSearch (opts) {
})
},
write: function (body, enc, cb) {
const obj = { index, type, body }
var idx = index.replace('%{DATE}', body.time.substring(0, 10))
const obj = { index: idx, type, body }
client.index(obj, function (err, data) {
if (!err) {
splitter.emit('insert', data, body)
Expand Down
51 changes: 51 additions & 0 deletions test/acceptance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,54 @@ test('store lines in bulk', { timeout }, (t) => {
}, refreshInterval)
})
})

test('replaces date in index', { timeout }, (t) => {
t.plan(3)
const index = 'pinotest-%{DATE}'

const instance = elastic({ index, type, consistency, host, port })
const log = pino(instance)

log.info('hello world')

instance.on('insert', (obj, body) => {
t.ok('data uploaded')

client.get({
index: index.replace('%{DATE}', new Date().toISOString().substring(0, 10)),
type,
id: obj._id
}, (err, response) => {
t.error(err)
t.deepEqual(response._source, body, 'obj matches')
})
})
})

test('replaces date in index during bulk insert', { timeout }, (t) => {
t.plan(15)

const index = 'pinotest-%{DATE}'
const instance = elastic({ index, type, consistency, host, port })
const log = pino(instance)

log.info('hello world')
log.info('hello world')
log.info('hello world')
log.info('hello world')
log.info('hello world')

instance.on('insert', (obj, body) => {
t.ok(obj, 'data uploaded')
setTimeout(function () {
client.get({
index: index.replace('%{DATE}', new Date().toISOString().substring(0, 10)),
type,
id: obj._id
}, (err, response) => {
t.error(err)
t.deepEqual(response._source, body, 'obj matches')
})
}, refreshInterval)
})
})
2 changes: 1 addition & 1 deletion usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
-H | --host the IP address of elasticsearch; default: 127.0.0.1
-p | --port the port of elasticsearch; default: 9200
-i | --index the name of the index to use; default: pino
will replace %{DATE} with the YYYY-MM-DD date
-t | --type the name of the type to use; default: log
-c | --consistency the consistency of the write; default: one
-b | --size the number of documents for each bulk insert
-l | --trace-level trace level for the elasticsearch client, default 'error' (info, debug, trace).
-c | --aws-credentials path to aws_config.json (is using AWS Elasticsearch)
Expand Down

0 comments on commit dc0ecb3

Please sign in to comment.