Skip to content

Commit 0858a12

Browse files
committed
Delay compression function instantiation out of sync start path – it'll only be needed first time profile is submitted.
1 parent bf9be5a commit 0858a12

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

packages/dd-trace/src/profiling/profiler.js

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const dc = require('dc-polyfill')
99
const crashtracker = require('../crashtracking')
1010

1111
const { promisify } = require('util')
12-
const zlib = require('zlib')
1312

1413
const profileSubmittedChannel = dc.channel('datadog:profiling:profile-submitted')
1514
const spanFinishedChannel = dc.channel('dd-trace:span:finish')
@@ -104,38 +103,6 @@ class Profiler extends EventEmitter {
104103
this._sourceMapsLoaded = Promise.resolve()
105104
}
106105

107-
try {
108-
const clevel = config.uploadCompression.level
109-
switch (config.uploadCompression.method) {
110-
case 'gzip':
111-
this._compressionFn = promisify(zlib.gzip)
112-
if (clevel !== undefined) {
113-
this._compressionOptions = {
114-
level: clevel
115-
}
116-
}
117-
break
118-
case 'zstd':
119-
if (typeof zlib.zstdCompress === 'function') {
120-
this._compressionFn = promisify(zlib.zstdCompress)
121-
if (clevel !== undefined) {
122-
this._compressionOptions = {
123-
params: {
124-
[zlib.constants.ZSTD_c_compressionLevel]: clevel
125-
}
126-
}
127-
}
128-
} else {
129-
const zstdCompress = require('@datadog/libdatadog').load('datadog-js-zstd').zstd_compress
130-
const level = clevel ?? 0 // 0 is zstd default compression level
131-
this._compressionFn = (buffer) => Promise.resolve(Buffer.from(zstdCompress(buffer, level)))
132-
}
133-
break
134-
}
135-
} catch (err) {
136-
this._logError(err)
137-
}
138-
139106
try {
140107
const start = new Date()
141108
for (const profiler of config.profilers) {
@@ -232,6 +199,45 @@ class Profiler extends EventEmitter {
232199
}
233200
}
234201

202+
_getCompressionFn () {
203+
if (this._compressionFn === undefined) {
204+
try {
205+
const method = this._config.uploadCompression.method
206+
if (method === 'off') {
207+
return
208+
}
209+
const zlib = require('zlib')
210+
const clevel = this._config.uploadCompression.level
211+
if (method === 'gzip') {
212+
this._compressionFn = promisify(zlib.gzip)
213+
if (clevel !== undefined) {
214+
this._compressionOptions = {
215+
level: clevel
216+
}
217+
}
218+
} else if (method === 'zstd') {
219+
if (typeof zlib.zstdCompress === 'function') {
220+
this._compressionFn = promisify(zlib.zstdCompress)
221+
if (clevel !== undefined) {
222+
this._compressionOptions = {
223+
params: {
224+
[zlib.constants.ZSTD_c_compressionLevel]: clevel
225+
}
226+
}
227+
}
228+
} else {
229+
const zstdCompress = require('@datadog/libdatadog').load('datadog-js-zstd').zstd_compress
230+
const level = clevel ?? 0 // 0 is zstd default compression level
231+
this._compressionFn = (buffer) => Promise.resolve(Buffer.from(zstdCompress(buffer, level)))
232+
}
233+
}
234+
} catch (err) {
235+
this._logError(err)
236+
}
237+
}
238+
return this._compressionFn
239+
}
240+
235241
async _collect (snapshotKind, restart = true) {
236242
if (!this._enabled) return
237243

@@ -267,9 +273,13 @@ class Profiler extends EventEmitter {
267273
await Promise.all(profiles.map(async ({ profiler, profile }) => {
268274
try {
269275
const encoded = await profiler.encode(profile)
270-
const compressed = encoded instanceof Buffer && this._compressionFn !== undefined
271-
? await this._compressionFn(encoded, this._compressionOptions)
272-
: encoded
276+
let compressed = encoded
277+
if (encoded instanceof Buffer) {
278+
const compressionFn = this._getCompressionFn()
279+
if (compressionFn !== undefined) {
280+
compressed = await compressionFn(encoded, this._compressionOptions)
281+
}
282+
}
273283
encodedProfiles[profiler.type] = compressed
274284
this._logger.debug(() => {
275285
const profileJson = JSON.stringify(profile, (key, value) => {

0 commit comments

Comments
 (0)