Skip to content

Commit

Permalink
[pinpoint-apm#86] fix span recorder error method crash issue
Browse files Browse the repository at this point in the history
  • Loading branch information
feelform committed Sep 12, 2023
1 parent dd88f6c commit 9b2a239
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
7 changes: 3 additions & 4 deletions lib/context/api-meta-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ class StringMetaCache {
cacheApiWithBuilder(builder) {
if (!builder) return

const fullName = builder.getFullName()
const cachedValue = this.cache.get(fullName)

const cacheId = builder.getCacheId()
const cachedValue = this.cache.get(cacheId)
if (cachedValue === null) {
builder.setApiId(apiMetaCacheKeyGenerator.next)
const methodDescriptor = builder.build()
this.cache.put(fullName, methodDescriptor)
this.cache.put(cacheId, methodDescriptor)
this.sendApiMetaInfo(methodDescriptor)
return methodDescriptor
}
Expand Down
10 changes: 10 additions & 0 deletions lib/context/method-descriptor-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class MethodDescriptorBuilder {
.setLineNumber(makeLineNumber(namedGroup))
.setClassName(namedGroup.type)
.setLocation(`${namedGroup.location}${namedGroup.fileName}`)
.setFileName(namedGroup.fileName)
return builder
}

Expand Down Expand Up @@ -104,6 +105,11 @@ class MethodDescriptorBuilder {
return this
}

setFileName(fileName) {
this.fileName = fileName
return this
}

build() {
const apiDescriptor = this.apiDescriptor || this.computedApiDescriptor
const fullName = this.fullName || this.computedFullName
Expand Down Expand Up @@ -167,6 +173,10 @@ class MethodDescriptorBuilder {
this.computedFullName = this.makeFullName(this.moduleName, this.objectPath)
return this
}

getCacheId() {
return `${this.getFullName()}:${this.lineNumber}:${this.fileName}`
}
}

function makeFunctionName(namedGroup) {
Expand Down
1 change: 1 addition & 0 deletions test/context/method-descriptor-builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test('callstack', (t) => {
t.equal(actual.functionName, 'app.get', 'functionName')
t.equal(actual.className, 'Function', 'className')
t.equal(actual.lineNumber, 481, 'lineNumber')
t.equal(actual.getCacheId(), 'express.app.get():481:application.js', `cache ID check`)

let actualMethodDescriptor = actual.build()
t.equal(actualMethodDescriptor.getModuleName(), 'express', 'MethodDescriptor moduleName')
Expand Down
10 changes: 7 additions & 3 deletions test/instrumentation/module/express.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ test(`${testName1} Should record request in basic route`, function (t) {
t.equal(trace.span.annotations[0].value.stringValue, 'api=test&test1=test', 'HTTP param value match')

const actualBuilder = new MethodDescriptorBuilder('express', 'app.get')
.setFullName('express.app.get(path, callback)')
.setParameterDescriptor('(path, callback)')
.setLineNumber(481)
.setFileName('application.js')
const actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder)
const spanEvent = trace.storage.storage[1]
t.equal(actualMethodDescriptor.apiId, spanEvent.apiId, 'apiId')
Expand All @@ -63,8 +65,10 @@ test(`${testName1} Should record request in basic route`, function (t) {
const trace = agent.traceContext.currentTraceObject()
t.false(trace.span.annotations[0], 'HTTP param undefined case')

const actualBuilder = new MethodDescriptorBuilder('express', 'app.post()')
.setFullName('express.app.post(path, callback)')
const actualBuilder = new MethodDescriptorBuilder('express', 'app.post')
.setParameterDescriptor('(path, callback)')
.setLineNumber(481)
.setFileName('application.js')
const actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder)
const spanEvent = trace.storage.storage[1]
t.equal(actualMethodDescriptor.apiId, spanEvent.apiId, 'apiId')
Expand Down

0 comments on commit 9b2a239

Please sign in to comment.