@google-cloud/trace-agent
Advanced tools
Comparing version 3.3.0 to 3.3.1
@@ -108,3 +108,3 @@ "use strict"; | ||
super.endSpan(timestamp); | ||
trace_writer_1.traceWriter.get().writeSpan(this.trace); | ||
trace_writer_1.traceWriter.get().writeTrace(this.trace); | ||
} | ||
@@ -111,0 +111,0 @@ } |
@@ -147,2 +147,6 @@ "use strict"; | ||
if (parsedContext) { | ||
if (parsedContext.options === undefined) { | ||
// If there are no incoming option flags, default to 0x1. | ||
parsedContext.options = 1; | ||
} | ||
Object.assign(incomingTraceContext, parsedContext); | ||
@@ -149,0 +153,0 @@ } |
@@ -59,6 +59,6 @@ /** | ||
stop(): void; | ||
initialize(cb: (err?: Error) => void): void; | ||
getConfig(): TraceWriterConfig; | ||
getHostname(cb: (hostname: string) => void): void; | ||
getInstanceId(cb: (instanceId?: number) => void): void; | ||
initialize(): Promise<void>; | ||
private getHostname; | ||
private getInstanceId; | ||
getProjectId(): Promise<string>; | ||
@@ -71,29 +71,18 @@ /** | ||
*/ | ||
writeSpan(trace: Trace): void; | ||
writeTrace(trace: Trace): void; | ||
/** | ||
* Buffers the provided trace to be published. | ||
* | ||
* @private | ||
* @param trace The trace to be queued. | ||
* Flushes the buffer of traces at a regular interval controlled by the | ||
* flushDelay property of this TraceWriter's config. | ||
*/ | ||
queueTrace(trace: Trace): void; | ||
private scheduleFlush; | ||
/** | ||
* Flushes the buffer of traces at a regular interval | ||
* controlled by the flushDelay property of this | ||
* TraceWriter's config. | ||
* @private | ||
*/ | ||
scheduleFlush(): void; | ||
/** | ||
* Serializes the buffered traces to be published asynchronously. | ||
* @private | ||
*/ | ||
flushBuffer(): void; | ||
private flushBuffer; | ||
/** | ||
* Publishes flushed traces to the network. | ||
* @private | ||
* @param json The stringified json representation of the queued traces. | ||
*/ | ||
publish(json: string): void; | ||
protected publish(json: string): void; | ||
} | ||
export declare const traceWriter: Singleton<TraceWriter, TraceWriterConfig, Logger>; |
@@ -17,2 +17,10 @@ "use strict"; | ||
*/ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -81,77 +89,77 @@ const common = require("@google-cloud/common"); | ||
} | ||
initialize(cb) { | ||
// Ensure that cb is called only once. | ||
let pendingOperations = 2; | ||
// Schedule periodic flushing of the buffer, but only if we are able to get | ||
// the project number (potentially from the network.) | ||
this.getProjectId().then(() => { | ||
this.scheduleFlush(); | ||
if (--pendingOperations === 0) { | ||
cb(); | ||
getConfig() { | ||
return this.config; | ||
} | ||
initialize() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// Schedule periodic flushing of the buffer, but only if we are able to get | ||
// the project number (potentially from the network.) | ||
const getProjectIdAndScheduleFlush = () => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
yield this.getProjectId(); | ||
} | ||
catch (err) { | ||
this.logger.error('TraceWriter#initialize: Unable to acquire the project number', 'automatically from the GCP metadata service. Please provide a', 'valid project ID as environmental variable GCLOUD_PROJECT, or', `as config.projectId passed to start. Original error: ${err}`); | ||
throw err; | ||
} | ||
this.scheduleFlush(); | ||
}); | ||
// getProjectIdAndScheduleFlush has no return value, so no need to capture | ||
// it on the left-hand side. | ||
const [hostname, instanceId] = yield Promise.all([ | ||
this.getHostname(), this.getInstanceId(), getProjectIdAndScheduleFlush() | ||
]); | ||
const addDefaultLabel = (key, value) => { | ||
this.defaultLabels[key] = `${value}`; | ||
}; | ||
this.defaultLabels = {}; | ||
addDefaultLabel(trace_labels_1.TraceLabels.AGENT_DATA, `node ${pjson.name} v${pjson.version}`); | ||
addDefaultLabel(trace_labels_1.TraceLabels.GCE_HOSTNAME, hostname); | ||
if (instanceId) { | ||
addDefaultLabel(trace_labels_1.TraceLabels.GCE_INSTANCE_ID, instanceId); | ||
} | ||
}, (err) => { | ||
this.logger.error('TraceWriter#initialize: Unable to acquire the project number', 'automatically from the GCP metadata service. Please provide a', 'valid project ID as environmental variable GCLOUD_PROJECT, or', `as config.projectId passed to start. Original error: ${err}`); | ||
cb(err); | ||
}); | ||
this.getHostname((hostname) => { | ||
this.getInstanceId((instanceId) => { | ||
// tslint:disable-next-line:no-any | ||
const addDefaultLabel = (key, value) => { | ||
this.defaultLabels[key] = `${value}`; | ||
}; | ||
this.defaultLabels = {}; | ||
addDefaultLabel(trace_labels_1.TraceLabels.AGENT_DATA, `node ${pjson.name} v${pjson.version}`); | ||
addDefaultLabel(trace_labels_1.TraceLabels.GCE_HOSTNAME, hostname); | ||
if (instanceId) { | ||
addDefaultLabel(trace_labels_1.TraceLabels.GCE_INSTANCE_ID, instanceId); | ||
} | ||
const moduleName = this.config.serviceContext.service || hostname; | ||
addDefaultLabel(trace_labels_1.TraceLabels.GAE_MODULE_NAME, moduleName); | ||
const moduleVersion = this.config.serviceContext.version; | ||
if (moduleVersion) { | ||
addDefaultLabel(trace_labels_1.TraceLabels.GAE_MODULE_VERSION, moduleVersion); | ||
const minorVersion = this.config.serviceContext.minorVersion; | ||
if (minorVersion) { | ||
let versionLabel = ''; | ||
if (moduleName !== 'default') { | ||
versionLabel = moduleName + ':'; | ||
} | ||
versionLabel += moduleVersion + '.' + minorVersion; | ||
addDefaultLabel(trace_labels_1.TraceLabels.GAE_VERSION, versionLabel); | ||
const moduleName = this.config.serviceContext.service || hostname; | ||
addDefaultLabel(trace_labels_1.TraceLabels.GAE_MODULE_NAME, moduleName); | ||
const moduleVersion = this.config.serviceContext.version; | ||
if (moduleVersion) { | ||
addDefaultLabel(trace_labels_1.TraceLabels.GAE_MODULE_VERSION, moduleVersion); | ||
const minorVersion = this.config.serviceContext.minorVersion; | ||
if (minorVersion) { | ||
let versionLabel = ''; | ||
if (moduleName !== 'default') { | ||
versionLabel = moduleName + ':'; | ||
} | ||
versionLabel += moduleVersion + '.' + minorVersion; | ||
addDefaultLabel(trace_labels_1.TraceLabels.GAE_VERSION, versionLabel); | ||
} | ||
Object.freeze(this.defaultLabels); | ||
if (--pendingOperations === 0) { | ||
cb(); | ||
} | ||
}); | ||
} | ||
Object.freeze(this.defaultLabels); | ||
}); | ||
} | ||
getConfig() { | ||
return this.config; | ||
} | ||
getHostname(cb) { | ||
gcpMetadata.instance({ property: 'hostname', headers }) | ||
.then((data) => { | ||
cb(data); // hostname | ||
}) | ||
.catch((err) => { | ||
if (err.code !== 'ENOTFOUND') { | ||
// We are running on GCP. | ||
this.logger.warn('TraceWriter#getHostname: Encountered an error while', 'retrieving GCE hostname from the GCP metadata service', `(metadata.google.internal): ${err}`); | ||
getHostname() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
return yield gcpMetadata.instance({ property: 'hostname', headers }); | ||
} | ||
cb(os.hostname()); | ||
catch (err) { | ||
if (err.code !== 'ENOTFOUND') { | ||
// We are running on GCP. | ||
this.logger.warn('TraceWriter#getHostname: Encountered an error while', 'retrieving GCE hostname from the GCP metadata service', `(metadata.google.internal): ${err}`); | ||
} | ||
return os.hostname(); | ||
} | ||
}); | ||
} | ||
getInstanceId(cb) { | ||
gcpMetadata.instance({ property: 'id', headers }) | ||
.then((data) => { | ||
cb(data); // instance ID | ||
}) | ||
.catch((err) => { | ||
if (err.code !== 'ENOTFOUND') { | ||
// We are running on GCP. | ||
this.logger.warn('TraceWriter#getInstanceId: Encountered an error while', 'retrieving GCE instance ID from the GCP metadata service', `(metadata.google.internal): ${err}`); | ||
getInstanceId() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
return yield gcpMetadata.instance({ property: 'id', headers }); | ||
} | ||
cb(); | ||
catch (err) { | ||
if (err.code !== 'ENOTFOUND') { | ||
// We are running on GCP. | ||
this.logger.warn('TraceWriter#getInstanceId: Encountered an error while', 'retrieving GCE instance ID from the GCP metadata service', `(metadata.google.internal): ${err}`); | ||
} | ||
return null; | ||
} | ||
}); | ||
@@ -174,3 +182,3 @@ } | ||
*/ | ||
writeSpan(trace) { | ||
writeTrace(trace) { | ||
for (const span of trace.spans) { | ||
@@ -187,18 +195,9 @@ if (span.endTime === '') { | ||
}); | ||
this.queueTrace(trace); | ||
} | ||
/** | ||
* Buffers the provided trace to be published. | ||
* | ||
* @private | ||
* @param trace The trace to be queued. | ||
*/ | ||
queueTrace(trace) { | ||
const afterProjectId = (projectId) => { | ||
trace.projectId = projectId; | ||
this.buffer.push(JSON.stringify(trace)); | ||
this.logger.info(`TraceWriter#queueTrace: buffer.size = ${this.buffer.length}`); | ||
this.logger.info(`TraceWriter#writeTrace: buffer.size = ${this.buffer.length}`); | ||
// Publish soon if the buffer is getting big | ||
if (this.buffer.length >= this.config.bufferSize) { | ||
this.logger.info('TraceWriter#queueTrace: Trace buffer full, flushing.'); | ||
this.logger.info('TraceWriter#writeTrace: Trace buffer full, flushing.'); | ||
setImmediate(() => this.flushBuffer()); | ||
@@ -225,6 +224,4 @@ } | ||
/** | ||
* Flushes the buffer of traces at a regular interval | ||
* controlled by the flushDelay property of this | ||
* TraceWriter's config. | ||
* @private | ||
* Flushes the buffer of traces at a regular interval controlled by the | ||
* flushDelay property of this TraceWriter's config. | ||
*/ | ||
@@ -246,3 +243,2 @@ scheduleFlush() { | ||
* Serializes the buffered traces to be published asynchronously. | ||
* @private | ||
*/ | ||
@@ -261,3 +257,2 @@ flushBuffer() { | ||
* Publishes flushed traces to the network. | ||
* @private | ||
* @param json The stringified json representation of the queued traces. | ||
@@ -264,0 +259,0 @@ */ |
@@ -89,7 +89,5 @@ "use strict"; | ||
} | ||
trace_writer_1.traceWriter.get().initialize((err) => { | ||
if (err) { | ||
this.logger.error('StackdriverTracer#start: Disabling the Trace Agent for the', `following reason: ${err.message}`); | ||
this.disable(); | ||
} | ||
trace_writer_1.traceWriter.get().initialize().catch((err) => { | ||
this.logger.error('StackdriverTracer#start: Disabling the Trace Agent for the', `following reason: ${err.message}`); | ||
this.disable(); | ||
}); | ||
@@ -96,0 +94,0 @@ cls_1.cls.get().enable(); |
{ | ||
"name": "@google-cloud/trace-agent", | ||
"version": "3.3.0", | ||
"version": "3.3.1", | ||
"description": "Node.js Support for StackDriver Trace", | ||
@@ -56,3 +56,3 @@ "main": "build/src/index.js", | ||
"@types/glob": "^7.0.0", | ||
"@types/is": "0.0.20", | ||
"@types/is": "0.0.21", | ||
"@types/methods": "^1.1.0", | ||
@@ -59,0 +59,0 @@ "@types/mocha": "^5.2.5", |
@@ -37,3 +37,3 @@ # Stackdriver Trace Agent for Node.js | ||
require('@google-cloud/trace-agent').start({ | ||
samplingRate: 500, // sample one trace every half-second. | ||
samplingRate: 5, // sample 5 traces per second, or at most 1 every 200 milliseconds. | ||
ignoreUrls: [ /^\/ignore-me#/ ] // ignore the "/ignore-me" endpoint. | ||
@@ -40,0 +40,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
301346
6506