forked from pinpoint-apm/pinpoint-node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pinpoint-apm#140] Support AsyncLocalStorage [email protected]
- Loading branch information
Showing
14 changed files
with
231 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
lib/instrumentation/context/async-context-local-storage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Pinpoint Node.js Agent | ||
* Copyright 2023-present NAVER Corp. | ||
* Apache License v2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const { AsyncLocalStorage } = require('node:async_hooks') | ||
|
||
class AsyncContextLocalStorage { | ||
constructor() { | ||
this.storage = new AsyncLocalStorage() | ||
} | ||
|
||
run(store, callback) { | ||
this.storage.run(store, callback) | ||
} | ||
|
||
getStore() { | ||
return this.storage.getStore() | ||
} | ||
} | ||
|
||
module.exports = AsyncContextLocalStorage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Pinpoint Node.js Agent | ||
* Copyright 2023-present NAVER Corp. | ||
* Apache License v2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const contextManager = require('../../context/context-manager') | ||
|
||
class AsyncHooksLocalStorage { | ||
constructor() { | ||
contextManager.start() | ||
} | ||
|
||
run(store, callback) { | ||
contextManager.setObject(store) | ||
callback() | ||
} | ||
|
||
getStore() { | ||
return contextManager.getObject() | ||
} | ||
} | ||
|
||
module.exports = AsyncHooksLocalStorage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Pinpoint Node.js Agent | ||
* Copyright 2023-present NAVER Corp. | ||
* Apache License v2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const ServiceType = require('../../context/service-type') | ||
|
||
module.exports = new ServiceType(100, 'ASYNC') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Pinpoint Node.js Agent | ||
* Copyright 2023-present NAVER Corp. | ||
* Apache License v2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const semver = require('semver') | ||
const AsyncContextLocalStorage = require('./async-context-local-storage') | ||
const AsyncHooksLocalStorage = require('./async-hooks-local-storage') | ||
|
||
class LocalStorage { | ||
constructor() { | ||
this.storage = this.createLocalStorage() | ||
} | ||
|
||
createLocalStorage() { | ||
if (!semver.satisfies(process.version, '>=16.4.0')) { | ||
return new AsyncHooksLocalStorage() | ||
} | ||
return new AsyncContextLocalStorage() | ||
} | ||
|
||
run(store, callback) { | ||
return this.storage.run(store, callback) | ||
} | ||
|
||
getStore() { | ||
return this.storage.getStore() | ||
} | ||
} | ||
|
||
module.exports = new LocalStorage() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Pinpoint Node.js Agent | ||
* Copyright 2023-present NAVER Corp. | ||
* Apache License v2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const Span = require('../../context/span') | ||
const SpanRecorder = require('../../context/span-recorder') | ||
const SpanChunk = require('../../context/span-chunk') | ||
const BufferedStorage = require('../../context/buffered-storage') | ||
const AsyncTrace = require('../../context/async-trace') | ||
const DisableAsyncTrace = require('../../context/disable-async-trace') | ||
const serviceType = require('./async-service-type') | ||
const defaultPredefinedMethodDescriptorRegistry = require('../../constant/default-predefined-method-descriptor-registry') | ||
|
||
class TraceBuilder { | ||
constructor(traceId, agentInfo, dataSender, sampling) { | ||
this.traceId = traceId | ||
this.agentInfo = agentInfo | ||
this.dataSender = dataSender | ||
this.sampling = sampling | ||
|
||
this.span = new Span(traceId, agentInfo) | ||
this.spanRecorder = new SpanRecorder(this.span) | ||
|
||
this.callStack = [] | ||
this.sequence = 0 | ||
|
||
const createSpanChunk = SpanChunk.getFactoryMethod(traceId, agentInfo) | ||
this.storage = new BufferedStorage(dataSender, createSpanChunk) | ||
} | ||
|
||
static valueOfTrace(trace) { | ||
const value = new TraceBuilder(trace.traceId, trace.agentInfo, trace.dataSender, trace.sampling) | ||
value.span = trace.span | ||
value.spanRecorder = trace.spanRecorder | ||
value.callStack = trace.callStack | ||
value.sequence = trace.sequence | ||
value.storage = trace.storage | ||
return value | ||
} | ||
|
||
buildAsyncTrace(asyncId) { | ||
if (!asyncId) { | ||
return new DisableAsyncTrace() | ||
} | ||
|
||
const value = new AsyncTrace(this.span, asyncId, this.traceId, this.agentInfo, this.dataSender, this.sampling) | ||
const spanEventRecorder = value.traceAsyncBegin() | ||
spanEventRecorder.recordServiceType(serviceType) | ||
spanEventRecorder.recordApiId(defaultPredefinedMethodDescriptorRegistry.asyncInvocationDescriptor.getApiId()) | ||
spanEventRecorder.spanEvent.endPoint = null | ||
spanEventRecorder.spanEvent.destinationId = null | ||
value.storage.storeSpanEvent(spanEventRecorder.spanEvent) | ||
return value | ||
} | ||
} | ||
|
||
module.exports = TraceBuilder |
Oops, something went wrong.