Skip to content

Commit

Permalink
Merge pull request #548 from metrico/feature/grafana_profiles_plugin
Browse files Browse the repository at this point in the history
fix potential memory leaks
  • Loading branch information
akvlad authored Aug 26, 2024
2 parents 7d2e8a9 + fd82464 commit eee2508
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions pyroscope/render_diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,36 @@ const querierMessages = require('./querier_pb')
const types = require('./types/v1/types_pb')

const renderDiff = async (req, res) => {
const [leftQuery, leftFromTimeSec, leftToTimeSec] =
parseParams(req.query.leftQuery, req.query.leftFrom, req.query.leftUntil);
const [rightQuery, rightFromTimeSec, rightToTimeSec] =
parseParams(req.query.rightQuery, req.query.rightFrom, req.query.rightUntil);
if (leftQuery.typeId != rightQuery.typeId) {
res.code(400).send('Different type IDs')
return
}
const leftCtxIdx = newCtxIdx()
await importStackTraces(leftQuery.typeDesc, '{' + leftQuery.labelSelector + '}', leftFromTimeSec, leftToTimeSec, req.log, leftCtxIdx, true)
const rightCtxIdx = newCtxIdx()
await importStackTraces(rightQuery.typeDesc, '{' + rightQuery.labelSelector + '}', rightFromTimeSec, rightToTimeSec, req.log, rightCtxIdx, true)
const flamegraphDiffBin = pprofBin.diff_tree(leftCtxIdx, rightCtxIdx,
`${leftQuery.typeDesc.sampleType}:${leftQuery.typeDesc.sampleUnit}`)
const profileType = new types.ProfileType()
profileType.setId(leftQuery.typeId)
profileType.setName(leftQuery.typeDesc.type)
profileType.setSampleType(leftQuery.typeDesc.sampleType)
profileType.setSampleUnit(leftQuery.typeDesc.sampleUnit)
profileType.setPeriodType(leftQuery.typeDesc.periodType)
profileType.setPeriodUnit(leftQuery.typeDesc.periodUnit)
const diff = querierMessages.FlameGraphDiff.deserializeBinary(flamegraphDiffBin)
return res.code(200).send(diffToFlamegraph(diff, profileType).flamebearerProfileV1)
try {
const [leftQuery, leftFromTimeSec, leftToTimeSec] =
parseParams(req.query.leftQuery, req.query.leftFrom, req.query.leftUntil);
const [rightQuery, rightFromTimeSec, rightToTimeSec] =
parseParams(req.query.rightQuery, req.query.rightFrom, req.query.rightUntil);
if (leftQuery.typeId != rightQuery.typeId) {
res.code(400).send('Different type IDs')
return
}

await importStackTraces(leftQuery.typeDesc, '{' + leftQuery.labelSelector + '}', leftFromTimeSec, leftToTimeSec, req.log, leftCtxIdx, true)

await importStackTraces(rightQuery.typeDesc, '{' + rightQuery.labelSelector + '}', rightFromTimeSec, rightToTimeSec, req.log, rightCtxIdx, true)
const flamegraphDiffBin = pprofBin.diff_tree(leftCtxIdx, rightCtxIdx,
`${leftQuery.typeDesc.sampleType}:${leftQuery.typeDesc.sampleUnit}`)
const profileType = new types.ProfileType()
profileType.setId(leftQuery.typeId)
profileType.setName(leftQuery.typeDesc.type)
profileType.setSampleType(leftQuery.typeDesc.sampleType)
profileType.setSampleUnit(leftQuery.typeDesc.sampleUnit)
profileType.setPeriodType(leftQuery.typeDesc.periodType)
profileType.setPeriodUnit(leftQuery.typeDesc.periodUnit)
const diff = querierMessages.FlameGraphDiff.deserializeBinary(flamegraphDiffBin)
return res.code(200).send(diffToFlamegraph(diff, profileType).flamebearerProfileV1)
} finally {
pprofBin.drop_tree(leftCtxIdx)
pprofBin.drop_tree(rightCtxIdx)
}
}

/**
Expand Down

0 comments on commit eee2508

Please sign in to comment.