Skip to content
This repository was archived by the owner on Jan 21, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
},
"devDependencies": {
"@types/builtin-modules": "^2.0.0",
"@types/console-log-level": "^1.4.0",
"@types/continuation-local-storage": "^3.2.1",
"@types/extend": "^3.0.0",
"@types/glob": "^5.0.32",
Expand Down Expand Up @@ -83,7 +84,6 @@
"nyc": "^13.0.0",
"once": "^1.4.0",
"pify": "^4.0.0",
"request": "^2.83.0",
"retry-axios": "^0.3.2",
"rimraf": "^2.6.2",
"source-map-support": "^0.5.6",
Expand All @@ -94,14 +94,16 @@
"typescript": "~3.0.0"
},
"dependencies": {
"@google-cloud/common": "^0.20.3",
"@google-cloud/common": "^0.23.0",
"builtin-modules": "^3.0.0",
"console-log-level": "^1.4.0",
"continuation-local-storage": "^3.2.1",
"extend": "^3.0.0",
"gcp-metadata": "^0.7.0",
"hex2dec": "^1.0.1",
"is": "^3.2.0",
"methods": "^1.1.1",
"request": "^2.83.0",
"require-in-the-middle": "^3.0.0",
"semver": "^5.4.1",
"shimmer": "^1.2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/cls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import {Logger} from '@google-cloud/common';
import {EventEmitter} from 'events';
import * as semver from 'semver';

Expand All @@ -24,6 +23,7 @@ import {CLS, Func} from './cls/base';
import {NullCLS} from './cls/null';
import {SingularCLS} from './cls/singular';
import {SpanType} from './constants';
import {Logger} from './logger';
import {RootSpan} from './plugin-types';
import {UNCORRELATED_ROOT_SPAN, UNTRACED_ROOT_SPAN} from './span-data';
import {Trace, TraceSpan} from './trace';
Expand Down
96 changes: 96 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as consoleLogLevel from 'console-log-level';
import {defaultConfig} from './config';

export type ConsoleLogLevel = 'error'|'warn'|'info'|'debug';
export type LogLevel = 'silent'|ConsoleLogLevel;

/**
* The list of log levels.
*/
export const LEVELS: ReadonlyArray<LogLevel> =
['silent', 'error', 'warn', 'info', 'debug'];

export interface LoggerConfig {
/**
* The minimum log level that will print to the console.
*/
level: string|false;

/**
* A tag to use in log messages.
*/
tag: string;
}

function logLevelToName(level?: number): LogLevel {
if (typeof level === 'string') {
level = Number(level);
}
if (typeof level !== 'number') {
level = defaultConfig.logLevel;
}
if (level < 0) level = 0;
if (level >= LEVELS.length) level = LEVELS.length - 1;
return LEVELS[level];
}

export class Logger {
private logger: consoleLogLevel.Logger|null;

constructor(opts?: Partial<LoggerConfig>) {
const levelName = opts && opts.level !== undefined ?
opts.level :
logLevelToName(defaultConfig.logLevel);

if (levelName === false || levelName === 'silent') {
this.logger = null;
return;
}

this.logger = consoleLogLevel({
stderr: true,
prefix: opts && opts.tag ? opts.tag : '@google-cloud/trace-agent',
level: levelName as ConsoleLogLevel
});
}

error(...args: Array<{}>): void {
if (this.logger) {
this.logger.error(args);
}
}

warn(...args: Array<{}>): void {
if (this.logger) {
this.logger.warn(args);
}
}

debug(...args: Array<{}>): void {
if (this.logger) {
this.logger.debug(args);
}
}

info(...args: Array<{}>): void {
if (this.logger) {
this.logger.info(args);
}
}
}
2 changes: 1 addition & 1 deletion src/trace-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

import {Logger} from '@google-cloud/common';
import * as is from 'is';
import * as uuid from 'uuid';

import {cls, RootContext} from './cls';
import {Constants, SpanType} from './constants';
import {Logger} from './logger';
import {Func, RootSpan, RootSpanOptions, Span, SpanOptions, Tracer} from './plugin-types';
import {RootSpanData, UNCORRELATED_CHILD_SPAN, UNCORRELATED_ROOT_SPAN, UNTRACED_CHILD_SPAN, UNTRACED_ROOT_SPAN} from './span-data';
import {TraceLabels} from './trace-labels';
Expand Down
2 changes: 1 addition & 1 deletion src/trace-plugin-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

import {Logger} from '@google-cloud/common';
import * as builtinModules from 'builtin-modules';
import * as path from 'path';
import * as hook from 'require-in-the-middle';
import * as semver from 'semver';

import {Logger} from './logger';
import {Intercept, Monkeypatch, Plugin} from './plugin-types';
import {StackdriverTracer, StackdriverTracerConfig} from './trace-api';
import {Singleton} from './util';
Expand Down
5 changes: 4 additions & 1 deletion src/trace-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import {AxiosError} from 'axios';
import * as gcpMetadata from 'gcp-metadata';
import {OutgoingHttpHeaders} from 'http';
import * as os from 'os';
import * as request from 'request';

import {Constants} from './constants';
import {Logger} from './logger';
import {SpanKind, Trace} from './trace';
import {TraceLabels} from './trace-labels';
import {Singleton} from './util';
Expand Down Expand Up @@ -74,9 +76,10 @@ export class TraceWriter extends common.Service {
*/
constructor(
private readonly config: TraceWriterConfig,
private readonly logger: common.Logger) {
private readonly logger: Logger) {
super(
{
requestModule: request,

This comment was marked as spam.

This comment was marked as spam.

packageJson: pjson,
projectIdRequired: false,
baseUrl: 'https://cloudtrace.googleapis.com/v1',
Expand Down
6 changes: 2 additions & 4 deletions src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* limitations under the License.
*/

import {Logger, logger} from '@google-cloud/common';
import * as path from 'path';

import {cls, TraceCLSConfig, TraceCLSMechanism} from './cls';
import {CLSMechanism} from './config';
import {LEVELS, Logger} from './logger';
import {StackdriverTracer} from './trace-api';
import {pluginLoader, PluginLoaderConfig} from './trace-plugin-loader';
import {traceWriter, TraceWriterConfig} from './trace-writer';
Expand Down Expand Up @@ -54,9 +54,7 @@ export class Tracing implements Component {
this.config = config;
let logLevel = config.enabled ? config.logLevel : 0;
// Clamp the logger level.
// TODO(kjin): When @google-cloud/common@0.19.2 is released, use
// Logger.LEVELS instead.
const defaultLevels = logger.LEVELS;
const defaultLevels = LEVELS;
if (logLevel < 0) {
logLevel = 0;
} else if (logLevel >= defaultLevels.length) {
Expand Down
9 changes: 4 additions & 5 deletions test/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {Logger, logger, LoggerConfig} from '@google-cloud/common';
import {LEVELS, Logger, LoggerConfig} from '../src/logger';

const PASS_THROUGH_LOG_LEVEL = Number(process.env.GCLOUD_TEST_LOG_LEVEL || 0);
// Capture the value of common.Logger so that we don't enter an infinite loop
Expand All @@ -26,10 +26,10 @@ const OriginalLogger = Logger;
type LoggerFunction<R> = (message: any, ...args: any[]) => R;

export class TestLogger extends Logger {
private logs: {[k in keyof Logger]: string[]} =
{silent: [], error: [], warn: [], info: [], debug: [], silly: []};
private logs: {[k in keyof Logger]:
string[]} = {error: [], warn: [], info: [], debug: []};
private innerLogger =
new OriginalLogger({level: logger.LEVELS[PASS_THROUGH_LOG_LEVEL]});
new OriginalLogger({level: LEVELS[PASS_THROUGH_LOG_LEVEL]});

constructor(options?: Partial<LoggerConfig>) {
super(options);
Expand All @@ -50,7 +50,6 @@ export class TestLogger extends Logger {
warn = this.makeLoggerFn('warn');
info = this.makeLoggerFn('info');
debug = this.makeLoggerFn('debug');
silly = this.makeLoggerFn('silly');

getLogs(logLevel: keyof Logger): string[] {
return this.logs[logLevel];
Expand Down
2 changes: 1 addition & 1 deletion test/test-config-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

import {Logger} from '@google-cloud/common';
import * as assert from 'assert';

import {defaultConfig} from '../src/config';
import {Logger} from '../src/logger';
import {PluginLoader, PluginLoaderConfig} from '../src/trace-plugin-loader';

import * as testTraceModule from './trace';
Expand Down
2 changes: 1 addition & 1 deletion test/test-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import {Logger} from '@google-cloud/common';
import * as assert from 'assert';
import * as semver from 'semver';
import * as util from 'util';
Expand All @@ -24,6 +23,7 @@ import {TraceCLSConfig, TraceCLSMechanism} from '../src/cls';
import * as testTraceModule from './trace';
import { NormalizedConfig } from '../src/tracing';
import { StackdriverTracer } from '../src/trace-api';
import {Logger} from '../src/logger';

describe('Behavior set by config for context propagation mechanism', () => {
const useAH = semver.satisfies(process.version, '>=8');
Expand Down
13 changes: 7 additions & 6 deletions test/test-env-log-level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as common from '@google-cloud/common';
import * as assert from 'assert';
import * as shimmer from 'shimmer';

import * as logger from '../src/logger';
import {FORCE_NEW} from '../src/util';

import {TestLogger} from './logger';
Expand All @@ -40,12 +41,12 @@ describe('should respect environment variables', () => {
process.env.GCLOUD_TRACE_LOGLEVEL = '4';
// Wrap logger constructor so that the log level (string) is saved
// in logLevel
shimmer.wrap(common, 'Logger', () => CaptureLogLevelTestLogger);
shimmer.wrap(logger, 'Logger', () => CaptureLogLevelTestLogger);
});

after(() => {
delete process.env.GCLOUD_TRACE_LOGLEVEL;
shimmer.unwrap(common, 'logger');
shimmer.unwrap(logger, 'Logger');
});

afterEach(() => {
Expand All @@ -54,20 +55,20 @@ describe('should respect environment variables', () => {

it('should respect GCLOUD_TRACE_LOGLEVEL', () => {
traceTestModule.start();
assert.strictEqual(logLevel, common.logger.LEVELS[4]);
assert.strictEqual(logLevel, logger.LEVELS[4]);
});

it('should prefer env to config', () => {
traceTestModule.start({logLevel: 2});
assert.strictEqual(logLevel, common.logger.LEVELS[4]);
assert.strictEqual(logLevel, logger.LEVELS[4]);
});

it('should fix out of bounds log level', () => {
process.env.GCLOUD_TRACE_LOGLEVEL = '-5';
traceTestModule.start();
assert.strictEqual(logLevel, common.logger.LEVELS[0]);
assert.strictEqual(logLevel, logger.LEVELS[0]);
process.env.GCLOUD_TRACE_LOGLEVEL = '300';
traceTestModule.start();
assert.strictEqual(logLevel, common.logger.LEVELS[5]);
assert.strictEqual(logLevel, logger.LEVELS[5]);
});
});
6 changes: 4 additions & 2 deletions test/test-modules-loaded-before-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import * as common from '@google-cloud/common';
import * as assert from 'assert';
import * as shimmer from 'shimmer';

import * as log from '../src/logger';

import {TestLogger} from './logger';
import * as testTraceModule from './trace';

Expand All @@ -32,11 +34,11 @@ describe('modules loaded before agent', () => {
}

before(() => {
shimmer.wrap(common, 'Logger', () => CaptureTestLogger);
shimmer.wrap(log, 'Logger', () => CaptureTestLogger);
});

after(() => {
shimmer.unwrap(common, 'Logger');
shimmer.unwrap(log, 'Logger');
});

it('should log if modules were loaded before agent', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/test-plugin-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

import {Logger} from '@google-cloud/common';
import * as assert from 'assert';
import * as path from 'path';
import * as hook from 'require-in-the-middle';
import * as shimmer from 'shimmer';

import {Logger} from '../src/logger';
import {PluginLoader, PluginLoaderState, PluginWrapper} from '../src/trace-plugin-loader';

import {TestLogger} from './logger';
Expand Down
2 changes: 1 addition & 1 deletion test/test-trace-uncaught-exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

import {Logger} from '@google-cloud/common';
import * as assert from 'assert';

import {Logger} from '../src/logger';
import {TraceWriterConfig} from '../src/trace-writer';

import {TestLogger} from './logger';
Expand Down
2 changes: 1 addition & 1 deletion test/test-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* limitations under the License.
*/

import {Logger} from '@google-cloud/common';
import * as assert from 'assert';
import {inspect} from 'util';

import {Constants} from '../src/constants';
import {Logger} from '../src/logger';
import * as util from '../src/util';

import {TestLogger} from './logger';
Expand Down
Loading