Skip to content

Instantly share code, notes, and snippets.

@ericmustin
Created April 5, 2020 20:54
Show Gist options
  • Save ericmustin/c74a18ce52d85a49f82be6dbed81d2ea to your computer and use it in GitHub Desktop.
Save ericmustin/c74a18ce52d85a49f82be6dbed81d2ea to your computer and use it in GitHub Desktop.
msgpack_sample_request

msgpack sample

A brief demonstration of sending a messagepack encoded trace

requirements and testing notes

tested with Node v10.15.1 and NPM 6.4.1

setup

  • download repository
  • cd into the root of this directory
  • npm install
  • ensure datadog agent is installed, running locally, and listening for traces at port 8126, instructions here
  • node index.js
sample output
msgpack_sample_request$ node index.js
200 { rate_by_service: { 'service:,env:': 1 } }
919188a874726163655f6964cf1122110bbf049c00a77370616e5f6964cf112211096af8b800a46e616d65a772657175657374a87265736f75726365b46765746163636f756e7464657461696c5f74776fa57374617274cf1601d2b062e98f00a86475726174696f6ecf000000005fe7dd58a474797065a3776562a773657276696365b1636f6d6d657263652d64622d70726f7879
const axios = require('axios')
const msgpack = require('msgpack-lite')
const { Int64BE, Uint64BE } = require('int64-buffer')
const message = {
"trace_id": new Uint64BE(1234567990000000001),
"span_id": new Uint64BE(1234567980000000001),
"name": "request",
"resource": "getaccountdetail_two",
"start": new Uint64BE(1585780198826676000),
"duration": new Uint64BE(1609031000),
"type": "web",
"service": "commerce-db-proxy"}
const sendTrace = span => {
codec = msgpack.createCodec({ int64: true })
const output = msgpack.encode([[span]], { codec } )
axios.request({
method: 'PUT',
url: 'http://localhost:8126/v0.4/traces',
data: output,
headers: {
'Content-Type': 'application/msgpack',
"X-Datadog-Trace-Count": 1
}
}).then(function(x,y) {
console.log(x.status, x.data)
console.log(output.toString('hex'))
}).catch(function(x) {
console.warn('error', x.response.data)
})
}
sendTrace(message)
{
"name": "msgpack_test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"starrt": "node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.19.2",
"int64-buffer": "^0.99.1007",
"msgpack-lite": "^0.1.26"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment