Skip to content

Commit fb4be89

Browse files
authored
chore(refactor): rename some files and classes for ease of understanding (#6934)
* Chunk -> MsgpackChunk * format.js -> span_format.js * Writer -> AgentWriter * remove setHeader()
1 parent b55ffd2 commit fb4be89

File tree

12 files changed

+84
-92
lines changed

12 files changed

+84
-92
lines changed

benchmark/core.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const Writer = proxyquire('../packages/dd-trace/src/exporters/agent/writer', {
1919
}
2020
})
2121
const Sampler = require('../packages/dd-trace/src/sampler')
22-
const format = require('../packages/dd-trace/src/format')
22+
const spanFormat = require('../packages/dd-trace/src/span_format')
2323
const { AgentEncoder: Agent04Encoder } = require('../packages/dd-trace/src/encode/0.4')
2424
const { AgentEncoder: Agent05Encoder } = require('../packages/dd-trace/src/encode/0.5')
2525
const config = getConfig({ service: 'benchmark' })
@@ -42,7 +42,7 @@ let writer
4242
let sampler
4343

4444
const spanStub = require('./stubs/span')
45-
const span = format(spanStub)
45+
const span = spanFormat(spanStub)
4646

4747
suite
4848
.add('DatadogTracer#startSpan', {
@@ -96,9 +96,9 @@ suite
9696
sampler.isSampled(span)
9797
}
9898
})
99-
.add('format', {
99+
.add('spanFormat', {
100100
fn () {
101-
format(spanStub)
101+
spanFormat(spanStub)
102102
}
103103
})
104104
.add('encode (0.4)', {

packages/dd-trace/src/encode/0.4.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const { truncateSpan, normalizeSpan } = require('./tags-processors')
4-
const { Chunk, MsgpackEncoder } = require('../msgpack')
4+
const { MsgpackChunk, MsgpackEncoder } = require('../msgpack')
55
const log = require('../log')
66
const { isTrue } = require('../util')
77
const { memoize } = require('../log/utils')
@@ -27,8 +27,8 @@ class AgentEncoder {
2727
constructor (writer, limit = SOFT_LIMIT) {
2828
this._msgpack = new MsgpackEncoder()
2929
this._limit = limit
30-
this._traceBytes = new Chunk()
31-
this._stringBytes = new Chunk()
30+
this._traceBytes = new MsgpackChunk()
31+
this._stringBytes = new MsgpackChunk()
3232
this._writer = writer
3333
this._reset()
3434
this._debugEncoding = isTrue(getEnvironmentVariable('DD_TRACE_ENCODING_DEBUG'))

packages/dd-trace/src/encode/coverage-ci-visibility.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22
const { AgentEncoder } = require('./0.4')
3-
const { Chunk } = require('../msgpack')
3+
const { MsgpackChunk } = require('../msgpack')
44

55
const {
66
distributionMetric,
@@ -15,7 +15,7 @@ const COVERAGE_KEYS_LENGTH = 2
1515
class CoverageCIVisibilityEncoder extends AgentEncoder {
1616
constructor () {
1717
super(...arguments)
18-
this._coverageBytes = new Chunk()
18+
this._coverageBytes = new MsgpackChunk()
1919
this.form = new FormData()
2020
this._coveragesCount = 0
2121
this.reset()

packages/dd-trace/src/exporters/agent/writer.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const BaseWriter = require('../common/writer')
99

1010
const METRIC_PREFIX = 'datadog.tracer.node.exporter.agent'
1111

12-
class Writer extends BaseWriter {
12+
class AgentWriter extends BaseWriter {
1313
constructor ({ prioritySampler, lookup, protocolVersion, headers, config = {} }) {
1414
super(...arguments)
1515
const AgentEncoder = getEncoder(protocolVersion)
@@ -62,12 +62,6 @@ class Writer extends BaseWriter {
6262
}
6363
}
6464

65-
function setHeader (headers, key, value) {
66-
if (value) {
67-
headers[key] = value
68-
}
69-
}
70-
7165
function getEncoder (protocolVersion) {
7266
return protocolVersion === '0.5'
7367
? require('../../encode/0.5').AgentEncoder
@@ -82,16 +76,15 @@ function makeRequest (version, data, count, url, headers, lookup, needsStartupLo
8276
...headers,
8377
'Content-Type': 'application/msgpack',
8478
'Datadog-Meta-Tracer-Version': tracerVersion,
85-
'X-Datadog-Trace-Count': String(count)
79+
'X-Datadog-Trace-Count': String(count),
80+
'Datadog-Meta-Lang': 'nodejs',
81+
'Datadog-Meta-Lang-Version': process.version,
82+
'Datadog-Meta-Lang-Interpreter': process.jsEngine || 'v8'
8683
},
8784
lookup,
8885
url
8986
}
9087

91-
setHeader(options.headers, 'Datadog-Meta-Lang', 'nodejs')
92-
setHeader(options.headers, 'Datadog-Meta-Lang-Version', process.version)
93-
setHeader(options.headers, 'Datadog-Meta-Lang-Interpreter', process.jsEngine || 'v8')
94-
9588
log.debug('Request to the agent: %j', options)
9689

9790
request(data, options, (err, res, status) => {
@@ -105,4 +98,4 @@ function makeRequest (version, data, count, url, headers, lookup, needsStartupLo
10598
})
10699
}
107100

108-
module.exports = Writer
101+
module.exports = AgentWriter

packages/dd-trace/src/msgpack/chunk.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const DEFAULT_MIN_SIZE = 2 * 1024 * 1024 // 2MB
77
* interfaces so that it can be used seamlessly by any encoder code that expects
88
* either.
99
*/
10-
class Chunk {
10+
class MsgpackChunk {
1111
constructor (minSize = DEFAULT_MIN_SIZE) {
1212
this.buffer = Buffer.allocUnsafe(minSize)
1313
this.view = new DataView(this.buffer.buffer)
@@ -66,4 +66,4 @@ class Chunk {
6666
}
6767
}
6868

69-
module.exports = Chunk
69+
module.exports = MsgpackChunk

packages/dd-trace/src/msgpack/encoder.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
'use strict'
22

3-
const Chunk = require('./chunk')
3+
const MsgpackChunk = require('./chunk')
44

55
class MsgpackEncoder {
66
encode (value) {
7-
const bytes = new Chunk()
8-
7+
const bytes = new MsgpackChunk()
98
this.encodeValue(bytes, value)
109

1110
return bytes.buffer.subarray(0, bytes.length)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const Chunk = require('./chunk')
3+
const MsgpackChunk = require('./chunk')
44
const { MsgpackEncoder } = require('./encoder')
55

6-
module.exports = { Chunk, MsgpackEncoder }
6+
module.exports = { MsgpackChunk, MsgpackEncoder }
File renamed without changes.

packages/dd-trace/src/span_processor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const log = require('./log')
4-
const format = require('./format')
4+
const spanFormat = require('./span_format')
55
const SpanSampler = require('./span_sampler')
66
const GitMetadataTagger = require('./git_metadata_tagger')
77
const { getEnvironmentVariable } = require('./config-helper')
@@ -53,7 +53,7 @@ class SpanProcessor {
5353
if (span._duration === undefined) {
5454
active.push(span)
5555
} else {
56-
const formattedSpan = format(span, isChunkRoot)
56+
const formattedSpan = spanFormat(span, isChunkRoot)
5757
isChunkRoot = false
5858
this._stats?.onSpanFinished(formattedSpan)
5959
formatted.push(formattedSpan)

packages/dd-trace/test/opentelemetry/span.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const { NoopSpanProcessor } = require('../../src/opentelemetry/span_processor')
1919
const { ERROR_MESSAGE, ERROR_STACK, ERROR_TYPE, IGNORE_OTEL_ERROR } = require('../../src/constants')
2020
const { SERVICE_NAME, RESOURCE_NAME, SPAN_KIND } = require('../../../../ext/tags')
2121
const kinds = require('../../../../ext/kinds')
22-
const format = require('../../src/format')
22+
const spanFormat = require('../../src/span_format')
2323

2424
const spanKindNames = {
2525
[api.SpanKind.INTERNAL]: kinds.INTERNAL,
@@ -406,20 +406,20 @@ describe('OTel Span', () => {
406406
startTime: datenow
407407
}])
408408

409-
let formatted = format(span._ddSpan)
409+
let formatted = spanFormat(span._ddSpan)
410410
expect(formatted).to.have.property('error', 0)
411411
expect(formatted.meta).to.not.have.property('doNotSetTraceError')
412412

413413
// Set error code
414414
span.setStatus({ code: 2, message: 'error' })
415415

416-
formatted = format(span._ddSpan)
416+
formatted = spanFormat(span._ddSpan)
417417
expect(formatted).to.have.property('error', 1)
418418

419419
span.recordException(new Error('foobar'), Date.now())
420420

421421
// Keep the error set to 1
422-
formatted = format(span._ddSpan)
422+
formatted = spanFormat(span._ddSpan)
423423
expect(formatted).to.have.property('error', 1)
424424
expect(formatted).to.have.property('meta')
425425
expect(formatted.meta).to.have.property('error.message', 'foobar')

0 commit comments

Comments
 (0)