Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add options to set the cls mechanism to async-hooks or async-listener #741

Merged
merged 6 commits into from
May 17, 2018

Conversation

kjin
Copy link
Contributor

@kjin kjin commented May 7, 2018

This PR makes async_hooks-based Tracing the default option for tracing using Node 8+. To go back to using async-listener (via continuation-local-storage), new config.clsMechanism options are accepted: async-listener (and async-hooks).

This PR provides two new possible values for config.clsMechanism: async-listener and async-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-storage as late as possible. The constraints to satisfy are as following:

  • continuation-local-storage needs to be loaded as early as possible, and definitely before any modules that do I/O.
  • continuation-local-storage must be loaded after start() is called, so we know whether to load it (via the config option passed to start).

To accomplish this, most of the logic in index.ts has been moved to a new file, tracing.ts, where it is now encapsulated in a class Tracing. Barring the feat: 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#deactivate from 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.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label May 7, 2018
@codecov
Copy link

codecov bot commented May 7, 2018

Codecov Report

❗ No coverage uploaded for pull request base (master@000643f). Click here to learn what that means.
The diff coverage is 87.95%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #741   +/-   ##
=========================================
  Coverage          ?   91.15%           
=========================================
  Files             ?       31           
  Lines             ?     1595           
  Branches          ?      312           
=========================================
  Hits              ?     1454           
  Misses            ?       60           
  Partials          ?       81
Impacted Files Coverage Δ
src/trace-plugin-loader.ts 92.51% <ø> (ø)
src/config.ts 100% <ø> (ø)
src/util.ts 95.77% <100%> (ø)
src/tracing.ts 87.03% <87.03%> (ø)
src/index.ts 89.58% <88.46%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 000643f...e1c3b31. Read the comment docs.

src/index.ts Outdated
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.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@kjin kjin force-pushed the ah-default branch 4 times, most recently from 82bc9f7 to 1990e4a Compare May 12, 2018 00:43
@kjin kjin changed the title feat: making async_hooks tracing the default on Node 8+ feat: add options to set the cls mechanism to async-hooks or async-listener May 12, 2018
@kjin kjin requested review from ofrobots, jinwoo and DominicKramer May 14, 2018 20:03
src/index.ts Outdated
@@ -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.

src/index.ts Outdated
'Disabling trace agent.');
stop();
return traceAgent;
throw e;

This comment was marked as spam.

This comment was marked as spam.

src/index.ts Outdated
// 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.

@kjin kjin force-pushed the ah-default branch 3 times, most recently from 1fbcd68 to 409693a Compare May 14, 2018 22:50
src/tracing.ts Outdated
* 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.

src/tracing.ts Outdated
*/
enable(): void {
if (this.traceAgent.isActive()) {
// For unit tests only.

This comment was marked as spam.

This comment was marked as spam.

src/tracing.ts Outdated

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.

src/tracing.ts Outdated
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.

@kjin kjin mentioned this pull request May 16, 2018
Copy link
Contributor

@ofrobots ofrobots left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting there. One more round.

src/tracing.ts Outdated
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.

src/tracing.ts Outdated
const clsConfig: Forceable<TraceCLSConfig> = {
mechanism: this.config.clsMechanism as TraceCLSMechanism,
[FORCE_NEW]: this.config[FORCE_NEW]
};

This comment was marked as spam.

src/tracing.ts Outdated
this.disable();
}
});
} catch (e) {

This comment was marked as spam.

This comment was marked as spam.

@kjin kjin merged commit f34aac5 into googleapis:master May 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants