Skip to content

Instantly share code, notes, and snippets.

@Dletta
Created July 11, 2022 13:00
Show Gist options
  • Save Dletta/c8b348f87e1855302eb0d71246b95031 to your computer and use it in GitHub Desktop.
Save Dletta/c8b348f87e1855302eb0d71246b95031 to your computer and use it in GitHub Desktop.
Logsender - NodeJS, QRYN

Log Sender for QRYN

This gist outlines a logsender that can be used as a template to send logs to QRYN. This may be used to manipulate logs or create certain types of logs. In the example we assert 2 log types as an example:

JSON Log Stream

A JSON object that asserts some random values on each message.

Metric Stream

A Metric value asserted under the {type:cpu} label.

Other Streams

Other log formats are also possible, however were not included in this simple template. Logfmt, String and the Labels/Entries format for insertion are examples of other log types.

To run

Install node (v14+) and import the index.js file into a folder.

node index.js

to start.

/*
Log sender to artificially send logs to QRYN for testing inserts
*/
const axios = require('axios')
const messageInterval = 10000 // send a message every 10 seconds
function pushMsg () {
// create a fake log line
const value = (Math.random() * 100).toString()
const ramval = (Math.random() * 1000).toString()
const string1 = Math.random().toString(36).substring(7)
const string2 = Math.random().toString(36).substring(7)
var packet = 1
// http correct stream
const mObj = {
stream: '1',
cpu_percent: value,
stringarray: [string1, string2],
ram_mb: ramval,
packets: packet
}
// create a log string first
const logstring = JSON.stringify(mObj)
let msg = {}
// json log stream
msg = {
streams: [
{
stream: {
key: 'message1',
key2: 'message1'
},
values: [
[(Date.now() * 1000000).toString(), logstring]
]
}
]
}
const typeObj1 = { type: 'cpu' }
const value1 = Math.random() * 100
// log metric
const msg2 = {
streams: [
{
stream: {
key: 'metrics',
key2: 'metrics',
type: 'cpu_percent'
},
values: [
[(Date.now() * 1000000).toString(), JSON.stringify(typeObj1), value1]
]
}
]
}
// jsonify for sending
// console.log(JSON.stringify(msg))
const data = JSON.stringify(msg)
const data2 = JSON.stringify(msg2)
// console.log('sending', data)
postData(data)
// console.log('sending', data2)
postData(data2)
}
pushMsg()
setInterval(pushMsg, messageInterval)
async function postData (data) {
try {
var response = await axios.post('http://127.0.0.1:3100/loki/api/v1/push', data, {
headers: {
'Content-Type': 'application/json'
}
})
console.log('AXIOS Push', response.status, response.statusText)
} catch (err) {
console.log('ERROR AXIOS', err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment