Skip to content

Commit 7b483ee

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

File tree

1 file changed

+44
-36
lines changed

1 file changed

+44
-36
lines changed

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

Lines changed: 44 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')
@@ -96,38 +95,6 @@ class Profiler extends EventEmitter {
9695
}
9796
}
9897

99-
try {
100-
const clevel = config.uploadCompression.level
101-
switch (config.uploadCompression.method) {
102-
case 'gzip':
103-
this._compressionFn = promisify(zlib.gzip)
104-
if (clevel !== undefined) {
105-
this._compressionOptions = {
106-
level: clevel
107-
}
108-
}
109-
break
110-
case 'zstd':
111-
if (typeof zlib.zstdCompress === 'function') {
112-
this._compressionFn = promisify(zlib.zstdCompress)
113-
if (clevel !== undefined) {
114-
this._compressionOptions = {
115-
params: {
116-
[zlib.constants.ZSTD_c_compressionLevel]: clevel
117-
}
118-
}
119-
}
120-
} else {
121-
const zstdCompress = require('@datadog/libdatadog').load('datadog-js-zstd').zstd_compress
122-
const level = clevel ?? 0 // 0 is zstd default compression level
123-
this._compressionFn = (buffer) => Promise.resolve(Buffer.from(zstdCompress(buffer, level)))
124-
}
125-
break
126-
}
127-
} catch (err) {
128-
this._logError(err)
129-
}
130-
13198
try {
13299
const start = new Date()
133100
for (const profiler of config.profilers) {
@@ -224,6 +191,43 @@ class Profiler extends EventEmitter {
224191
}
225192
}
226193

194+
_getCompressionFn () {
195+
if (this._compressionFn === undefined) {
196+
try {
197+
const clevel = this._config.uploadCompression.level
198+
switch (this._config.uploadCompression.method) {
199+
case 'gzip':
200+
this._compressionFn = promisify(require('zlib').gzip)
201+
if (clevel !== undefined) {
202+
this._compressionOptions = {
203+
level: clevel
204+
}
205+
}
206+
break
207+
case 'zstd':
208+
if (typeof zlib.zstdCompress === 'function') {
209+
this._compressionFn = promisify(require('zlib').zstdCompress)
210+
if (clevel !== undefined) {
211+
this._compressionOptions = {
212+
params: {
213+
[zlib.constants.ZSTD_c_compressionLevel]: clevel
214+
}
215+
}
216+
}
217+
} else {
218+
const zstdCompress = require('@datadog/libdatadog').load('datadog-js-zstd').zstd_compress
219+
const level = clevel ?? 0 // 0 is zstd default compression level
220+
this._compressionFn = (buffer) => Promise.resolve(Buffer.from(zstdCompress(buffer, level)))
221+
}
222+
break
223+
}
224+
} catch (err) {
225+
this._logError(err)
226+
}
227+
}
228+
return this._compressionFn
229+
}
230+
227231
async _collect (snapshotKind, restart = true) {
228232
if (!this._enabled) return
229233

@@ -259,9 +263,13 @@ class Profiler extends EventEmitter {
259263
await Promise.all(profiles.map(async ({ profiler, profile }) => {
260264
try {
261265
const encoded = await profiler.encode(profile)
262-
const compressed = encoded instanceof Buffer && this._compressionFn !== undefined
263-
? await this._compressionFn(encoded, this._compressionOptions)
264-
: encoded
266+
let compressed = encoded
267+
if (encoded instanceof Buffer) {
268+
const compressionFn = this._getCompressionFn()
269+
if (compressionFn !== undefined) {
270+
compressed = await compressionFn(encoded, this._compressionOptions)
271+
}
272+
}
265273
encodedProfiles[profiler.type] = compressed
266274
this._logger.debug(() => {
267275
const profileJson = JSON.stringify(profile, (key, value) => {

0 commit comments

Comments
 (0)