Skip to content

event based tracing #2869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.rs]
indent_size = 4

[*.md]
trim_trailing_whitespace = false
3 changes: 3 additions & 0 deletions benchmark/sirun/plugin-koa/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This creates 100,000 HTTP requests to a Koa server.

The variants are with the tracer, without it, and with a new internal tracer.
25 changes: 25 additions & 0 deletions benchmark/sirun/plugin-koa/agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

const express = require('express')
const bodyParser = require('body-parser')

const port = process.env.PORT || 8126
const app = express()

let requests = 0
let bytes = 0

app.use(bodyParser.raw({ limit: '50mb', type: () => true }))
app.use('*', (req, res) => {
requests++
bytes += req.body.length

// console.log(require('msgpack-lite').decode(req.body))

console.log(`Requests: ${requests}`) // eslint-disable-line no-console
console.log(`Bytes: ${bytes}`) // eslint-disable-line no-console

res.status(200).send()
})

app.listen(port)
92 changes: 92 additions & 0 deletions benchmark/sirun/plugin-koa/internal-tracer/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
'use strict'

const http = require('http')
const https = require('https')
const { dockerId, storage } = require('../../../../packages/datadog-core')
const tracerVersion = require('../../../../package.json').version

const httpAgent = new http.Agent({ keepAlive: true, maxSockets: 1 })
const httpsAgent = new https.Agent({ keepAlive: true, maxSockets: 1 })

const DD_TRACE_AGENT_URL = process.env.DD_TRACE_AGENT_URL || process.env.DD_TRACE_URL

class Client {
request (options, done) {
if (options.count === 0) return

const port = options.port || 8127
const url = new URL(DD_TRACE_AGENT_URL || `http://127.0.0.1:${port}`)
const isSecure = url.protocol === 'https:'
const isUnix = url.protocol === 'unix:'
const client = isSecure ? https : http
const agent = isSecure ? httpsAgent : httpAgent
const data = options.data
const timeout = 2000
const httpOptions = {
agent,
protocol: url.protocol,
hostname: url.hostname,
port: url.port,
socketPath: isUnix && url.pathname,
path: options.path,
method: 'PUT',
headers: {
'Content-Length': String(data.length),
'Content-Type': 'application/msgpack',
'Datadog-Container-ID': dockerId || '',
'Datadog-Meta-Lang': 'nodejs',
'Datadog-Meta-Lang-Version': process.version,
'Datadog-Meta-Lang-Interpreter': process.jsEngine || 'v8',
'Datadog-Meta-Tracer-Version': tracerVersion
},
timeout
}

const onResponse = res => {
let data = ''

res.setTimeout(timeout)
res.on('data', chunk => {
data += chunk
})
res.on('end', () => {
if (res.statusCode >= 200 && res.statusCode <= 299) {
try {
const response = data
done(null, response)
} catch (e) {
done(e)
}
} else {
const statusCode = res.statusCode
const statusText = http.STATUS_CODES[res.statusCode]
const error = new Error(`Error from the agent: ${statusCode} ${statusText}`)

error.status = statusCode

done(error, null)
}
})
}

const makeRequest = onError => {
const store = storage.getStore()

storage.enterWith({ noop: true })

const req = client.request(httpOptions, onResponse)

req.on('error', onError)

req.setTimeout(timeout, req.abort)
req.write(data)
req.end()

storage.enterWith(store)
}

makeRequest(() => makeRequest(done)) // retry once on error
}
}

module.exports = { Client }
19 changes: 19 additions & 0 deletions benchmark/sirun/plugin-koa/internal-tracer/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

const { id, zeroId } = require('./id')

class TraceContext {
constructor (childOf) {
if (childOf) {
this.traceId = childOf.traceId
this.spanId = id()
this.parentId = childOf.spanId
} else {
this.traceId = id()
this.spanId = this.traceId
this.parentId = zeroId
}
}
}

module.exports = { TraceContext }
Loading