feat: add options to set the cls mechanism to async-hooks or async-listener#741
feat: add options to set the cls mechanism to async-hooks or async-listener#741kjin merged 6 commits intogoogleapis:masterfrom
Conversation
Codecov Report
@@ Coverage Diff @@
## master #741 +/- ##
=========================================
Coverage ? 91.15%
=========================================
Files ? 31
Lines ? 1595
Branches ? 312
=========================================
Hits ? 1454
Misses ? 60
Partials ? 81
Continue to review full report at Codecov.
|
| const agentEnabled = !config || config.enabled !== false; | ||
| const alAutoPreferred = | ||
| !ahAvailable && (!config || config.clsMechanism === 'auto'); | ||
| const alUserPreferred = config && (config.clsMechanism === 'async-listener'); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
82bc9f7 to
1990e4a
Compare
| @@ -1,5 +1,5 @@ | |||
| /** | |||
| * Copyright 2015 Google Inc. All Rights Reserved. | |||
| * Copyright 2018 Google LLC | |||
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| 'Disabling trace agent.'); | ||
| stop(); | ||
| return traceAgent; | ||
| throw e; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| // This code path maintains the current contract that calling get() before | ||
| // start() yields a disabled custom span API. It assumes that the use case | ||
| // for doing so (instead of returning null) is when get() is called in | ||
| // a file where it is unknown whether start() has been called. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
1fbcd68 to
409693a
Compare
| * Constructs a new Tracing instance. | ||
| * @param config The configuration for this instance. | ||
| */ | ||
| constructor(config: NormalizedConfig, traceAgent: TraceAgent) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| */ | ||
| enable(): void { | ||
| if (this.traceAgent.isActive()) { | ||
| // For unit tests only. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
|
||
| this.traceAgent.enable(this.config, this.logger); | ||
|
|
||
| pluginLoader.create(this.config, this.logger).activate(); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| this.traceAgent.enable(this.config, this.logger); | ||
|
|
||
| pluginLoader.create(this.config, this.logger).activate(); | ||
| } catch (e) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
ofrobots
left a comment
There was a problem hiding this comment.
Getting there. One more round.
| constructor( | ||
| config: NormalizedConfig, private readonly traceAgent: TraceAgent) { | ||
| this.config = config; | ||
| this.traceAgent = traceAgent; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| const clsConfig: Forceable<TraceCLSConfig> = { | ||
| mechanism: this.config.clsMechanism as TraceCLSMechanism, | ||
| [FORCE_NEW]: this.config[FORCE_NEW] | ||
| }; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| this.disable(); | ||
| } | ||
| }); | ||
| } catch (e) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This PR makesasync_hooks-based Tracing the default option for tracing using Node 8+. To go back to usingasync-listener(viacontinuation-local-storage), newconfig.clsMechanismoptions are accepted:async-listener(andasync-hooks).This PR provides two new possible values for
config.clsMechanism:async-listenerandasync-hooks.Because context tracking is now specified as a config option rather than strictly as an environmental variable, a fair bit of refactoring is needed to support conditionally loading
continuation-local-storageas late as possible. The constraints to satisfy are as following:continuation-local-storageneeds to be loaded as early as possible, and definitely before any modules that do I/O.continuation-local-storagemust be loaded afterstart()is called, so we know whether to load it (via the config option passed tostart).To accomplish this, most of the logic in
index.tshas been moved to a new file,tracing.ts, where it is now encapsulated in a classTracing. Barring thefeat:change, there should be no behavioral changes; however there are some caveats that hopefully are captured in the added comments.There is also a small change to prevent
PluginLoader#deactivatefrom throwing when being called on an already deactivated plugin loader. This is only required to prevent large-scale changes to tests, as the case when it might be called when the plugin loader is de-activated is from test code that sets the[FORCE_NEW]flag in configuration objects.