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

Removed dependency on cloud-diagnostics-common #338

Merged
merged 8 commits into from
Jan 5, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 25 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ var filesLoadedBeforeTrace = Object.keys(require.cache);
require('continuation-local-storage');

var SpanData = require('./src/span-data.js');
var common = require('@google/cloud-diagnostics-common');
var common = require('@google-cloud/common');
var gcpMetadata = require('gcp-metadata');
var semver = require('semver');
var constants = require('./src/constants.js');
var util = require('./src/util.js');
Expand Down Expand Up @@ -74,7 +75,11 @@ var initConfig = function(projectConfig) {
util._extend(config, require('./config.js').trace);
util._extend(config, projectConfig);
if (process.env.hasOwnProperty('GCLOUD_TRACE_LOGLEVEL')) {
config.logLevel = process.env.GCLOUD_TRACE_LOGLEVEL;
try {
config.logLevel = parseInt(process.env.GCLOUD_TRACE_LOGLEVEL);
} catch (e) {
// Just use the original log level if GCLOUD_TRACE_LOGLEVEL is malformed.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

}
}
if (process.env.hasOwnProperty('GCLOUD_PROJECT')) {
config.projectId = process.env.GCLOUD_PROJECT;
Expand Down Expand Up @@ -125,7 +130,18 @@ var publicAgent = {
if (!config.enabled) {
return this;
}
var logger = common.logger.create(config.logLevel, '@google/cloud-trace');

var logLevel = config.logLevel;
if (logLevel < 0) {

This comment was marked as spam.

logLevel = 0;
} else if (logLevel >= common.logger.LEVELS.length) {
logLevel = common.logger.LEVELS.length - 1;
}
var logger = common.logger({
level: common.logger.LEVELS[logLevel],
tag: '@google/cloud-trace'
});

if (!semver.satisfies(process.versions.node, '>=0.12')) {
logger.error('Tracing is only supported on Node versions >=0.12');
return this;
Expand Down Expand Up @@ -153,7 +169,10 @@ var publicAgent = {
if (typeof config.projectId === 'undefined') {
// Queue the work to acquire the projectNumber (potentially from the
// network.)
common.utils.getProjectNumber(headers, function(err, project) {
gcpMetadata.project({
property: 'project-id',
headers: headers
}, function(err, response, projectId) {
if (err) {
// Fatal error. Disable the agent.
logger.error('Unable to acquire the project number from metadata ' +
Expand All @@ -163,13 +182,11 @@ var publicAgent = {
publicAgent.stop();
return;
}
config.projectId = project;
config.projectId = projectId;
});
} else if (typeof config.projectId === 'number') {
config.projectId = config.projectId.toString();
} else if (typeof config.projectId !== 'string') {
logger.error('config.projectId, if provided, must be' +
' a string or number. Disabling trace agent.');
' a string. Disabling trace agent.');
return this;
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
"tmp": "0.0.31"
},
"dependencies": {
"@google/cloud-diagnostics-common": "0.3.0",
"@google-cloud/common": "^0.11.0",
"continuation-local-storage": "^3.1.4",
"gcp-metadata": "^0.1.0",
"lodash.findindex": "^4.4.0",
"lodash.isequal": "^4.0.0",
"methods": "^1.1.1",
Expand Down
73 changes: 42 additions & 31 deletions src/trace-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

'use strict';

var utils = require('@google/cloud-diagnostics-common').utils;
var gcpMetadata = require('gcp-metadata');
var util = require('@google-cloud/common').util;
var traceLabels = require('./trace-labels.js');
var pjson = require('../package.json');
var constants = require('./constants.js');
Expand All @@ -42,7 +43,8 @@ function TraceWriter(logger, config) {
this.config_ = config;

/** @private {function} authenticated request function */
this.request_ = utils.authorizedRequestFactory(SCOPES, {
this.request_ = util.makeAuthenticatedRequestFactory({
scopes: SCOPES,
credentials: config.credentials,
keyFile: config.keyFilename
});
Expand All @@ -59,7 +61,7 @@ function TraceWriter(logger, config) {
// Schedule periodic flushing of the buffer, but only if we are able to get
// the project number (potentially from the network.)
var that = this;
that.getProjectNumber(function(err, project) {
that.getProjectId(function(err, project) {
if (err) { return; } // ignore as index.js takes care of this.
that.scheduleFlush_(project);
});
Expand Down Expand Up @@ -100,7 +102,10 @@ TraceWriter.prototype.stop = function() {

TraceWriter.prototype.getHostname = function(cb) {
var that = this;
utils.getHostname(headers, function(err, hostname) {
gcpMetadata.instance({
property: 'hostname',
headers: headers
}, function(err, response, hostname) {
if (err && err.code !== 'ENOTFOUND') {
// We are running on GCP.
that.logger_.warn('Unable to retrieve GCE hostname.', err);
Expand All @@ -111,7 +116,10 @@ TraceWriter.prototype.getHostname = function(cb) {

TraceWriter.prototype.getInstanceId = function(cb) {
var that = this;
utils.getInstanceId(headers, function(err, instanceId) {
gcpMetadata.instance({
property: 'id',
headers: headers
}, function(err, response, instanceId) {
if (err && err.code !== 'ENOTFOUND') {
// We are running on GCP.
that.logger_.warn('Unable to retrieve GCE instance id.', err);
Expand All @@ -120,6 +128,33 @@ TraceWriter.prototype.getInstanceId = function(cb) {
});
};

/**
* Returns the project ID if it has been cached and attempts to load
* it from the enviroment or network otherwise.
*
* @param {function(?, number):?} callback an (err, result) style callback
*/
TraceWriter.prototype.getProjectId = function(callback) {
var that = this;
if (that.config_.projectId) {
callback(null, that.config_.projectId);
return;
}

gcpMetadata.project({
property: 'project-id',
headers: headers
}, function(err, response, projectId) {
if (err) {
callback(err);
return;
}
that.logger_.info('Acquired ProjectId from metadata: ' + projectId);
that.config_.projectId = projectId;
callback(null, projectId);
});
};

/**
* Ensures that all sub spans of the provided spanData are
* closed and then queues the span data to be published.
Expand Down Expand Up @@ -151,7 +186,7 @@ TraceWriter.prototype.writeSpan = function(spanData) {
TraceWriter.prototype.queueTrace_ = function(trace) {
var that = this;

that.getProjectNumber(function(err, project) {
that.getProjectId(function(err, project) {
if (err) {
that.logger_.info('No project number, dropping trace.');
return; // ignore as index.js takes care of this.
Expand Down Expand Up @@ -213,7 +248,7 @@ TraceWriter.prototype.publish_ = function(projectId, json) {
var uri = 'https://cloudtrace.googleapis.com/v1/projects/' +
projectId + '/traces';

this.request_({
this.request_.makeAuthenticatedRequest({
method: 'PATCH',
uri: uri,
body: json,
Expand All @@ -228,30 +263,6 @@ TraceWriter.prototype.publish_ = function(projectId, json) {
});
};

/**
* Returns the project number if it has been cached and attempts to load
* it from the enviroment or network otherwise.
*
* @param {function(?, number):?} callback an (err, result) style callback
*/
TraceWriter.prototype.getProjectNumber = function(callback) {
var that = this;
if (that.config_.projectId) {
callback(null, that.config_.projectId);
return;
}

utils.getProjectNumber(headers, function(err, project) {
if (err) {
callback(err);
return;
}
that.logger_.info('Acquired ProjectId from metadata: ' + project);
that.config_.projectId = project;
callback(null, project);
});
};

/**
* Export TraceWriter.
* FIXME(ofrobots): TraceWriter should be a singleton. We should export
Expand Down
4 changes: 2 additions & 2 deletions test/hooks/test-hooks-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ describe('findModuleVersion', function() {
});

it('should work with namespaces', function() {
var modulePath = findModulePath('@google/cloud-diagnostics-common', module);
var modulePath = findModulePath('@google-cloud/common', module);
var truePackage =
require('../../node_modules/@google/cloud-diagnostics-common/package.json');
require('../../node_modules/@google-cloud/common/package.json');
assert.equal(findModuleVersion(modulePath, Module._load), truePackage.version);
});
});