Skip to content

Commit

Permalink
chore: address comments, pt 2
Browse files Browse the repository at this point in the history
  • Loading branch information
kjin committed May 14, 2018
1 parent db058d7 commit 409693a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
35 changes: 11 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2018 Google LLC
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,10 +26,11 @@ import * as PluginTypes from './plugin-types';
import {tracing, Tracing, NormalizedConfig} from './tracing';
import {Singleton, FORCE_NEW, Forceable} from './util';
import {Constants} from './constants';
import {TraceAgent} from './trace-api';

export {Config, PluginTypes};

let tracingSingleton: typeof tracing;
let traceAgent: TraceAgent;

/**
* Normalizes the user-provided configuration object by adding default values
Expand Down Expand Up @@ -110,14 +111,15 @@ export function start(config?: Config): PluginTypes.TraceAgent {
require('continuation-local-storage');
}

if (!tracingSingleton) {
tracingSingleton = require('./tracing').tracing;
if (!traceAgent) {
traceAgent = new (require('./trace-api').TraceAgent)();
}

try {
let tracing: Tracing;
try {
tracing = tracingSingleton.create(normalizedConfig, {});
tracing =
require('./tracing').tracing.create(normalizedConfig, traceAgent);
} catch (e) {
// An error could be thrown if create() is called multiple times.
// It's not a helpful error message for the end user, so make it more
Expand All @@ -126,9 +128,7 @@ export function start(config?: Config): PluginTypes.TraceAgent {
}
tracing.enable();
tracing.logModulesLoadedBeforeTrace(filesLoadedBeforeTrace);
return tracingSingleton.get().traceAgent;
} catch (e) {
throw e;
return traceAgent;
} finally {
// Stop storing these entries in memory
filesLoadedBeforeTrace.length = 0;
Expand All @@ -140,23 +140,10 @@ export function start(config?: Config): PluginTypes.TraceAgent {
* @returns An object exposing functions for creating custom spans.
*/
export function get(): PluginTypes.TraceAgent {
if (!tracingSingleton) {
tracingSingleton = require('./tracing').tracing;
}
if (tracingSingleton.exists()) {
return tracingSingleton.get().traceAgent;
} else {
// 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.

// Based on this assumption, and because we document that start() must be
// called first in an application, it's OK to create a permanently disabled
// Trace Agent here and assume that start() will never be called to enable
// it.
return tracingSingleton.create({enabled: false}, {}).traceAgent;
if (!traceAgent) {
traceAgent = new (require('./trace-api').TraceAgent)();
}
return traceAgent;
}

// If the module was --require'd from the command line, start the agent.
Expand Down
11 changes: 7 additions & 4 deletions src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,19 @@ export type NormalizedConfig =
*/
export class Tracing implements Component {
/** An object representing the custom span API. */
readonly traceAgent: TraceAgent = new TraceAgent('Custom Trace API');
private logger: common.Logger;
private config: Forceable<NormalizedConfig>;
private readonly traceAgent: TraceAgent;
/** A logger. */
private readonly logger: common.Logger;
/** The configuration object for this instance. */
private readonly config: Forceable<NormalizedConfig>;

/**
* Constructs a new Tracing instance.
* @param config The configuration for this instance.
*/
constructor(config: NormalizedConfig) {
constructor(config: NormalizedConfig, traceAgent: TraceAgent) {
this.config = config;
this.traceAgent = traceAgent;
let logLevel = config.enabled ? config.logLevel : 0;
// Clamp the logger level.
if (logLevel < 0) {
Expand Down

0 comments on commit 409693a

Please sign in to comment.