Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
67 changes: 22 additions & 45 deletions packages/logging/src/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ function Metadata(logging) {
/**
* Create a descriptor for Cloud Functions.
*
* @param {string} projectId - The project ID.
* @returns {object}
*/
Metadata.getCloudFunctionDescriptor = function(projectId) {
Metadata.getCloudFunctionDescriptor = function() {
return {
type: 'cloud_function',
labels: {
project_id: projectId,
function_name: process.env.FUNCTION_NAME,
region: process.env.SUPERVISOR_REGION
}
Expand All @@ -60,14 +58,12 @@ Metadata.getCloudFunctionDescriptor = function(projectId) {
/**
* Create a descriptor for Google App Engine.
*
* @param {string} projectId - The project ID.
* @returns {object}
*/
Metadata.getGAEDescriptor = function(projectId) {
Metadata.getGAEDescriptor = function() {
return {
type: 'gae_app',
labels: {
project_id: projectId,
module_id: process.env.GAE_SERVICE || process.env.GAE_MODULE_NAME,
version_id: process.env.GAE_VERSION
}
Expand All @@ -79,15 +75,11 @@ Metadata.getGAEDescriptor = function(projectId) {
*
* @private
*
* @param {string} projectId - The project ID.
* @return {object}
*/
Metadata.getGCEDescriptor = function(projectId) {
Metadata.getGCEDescriptor = function() {
return {
type: 'gce_instance',
labels: {
project_id: projectId
}
type: 'gce_instance'
};
};

Expand All @@ -96,11 +88,10 @@ Metadata.getGCEDescriptor = function(projectId) {
*
* @private
*
* @param {string} projectId - The project ID.
* @param {function} callback - The callback function.
* @return {object}
*/
Metadata.getGKEDescriptor = function(projectId, callback) {
Metadata.getGKEDescriptor = function(callback) {
gcpMetadata.instance('attributes/clusterName', function(err, _, clusterName) {
if (err) {
callback(err);
Expand All @@ -111,8 +102,7 @@ Metadata.getGKEDescriptor = function(projectId, callback) {
type: 'container',
labels: {
// TODO(ofrobots): it would be good to include the namespace_id as well.
cluster_name: clusterName,
project_id: projectId
cluster_name: clusterName
}
});
});
Expand All @@ -123,15 +113,11 @@ Metadata.getGKEDescriptor = function(projectId, callback) {
*
* @private
*
* @param {string} projectId - The project ID.
* @returns {object}
*/
Metadata.getGlobalDescriptor = function(projectId) {
Metadata.getGlobalDescriptor = function() {
return {
type: 'global',
labels: {
project_id: projectId
}
type: 'global'
};
};

Expand All @@ -141,34 +127,25 @@ Metadata.getGlobalDescriptor = function(projectId) {
* @param {function} callback - The callback function.
*/
Metadata.prototype.getDefaultResource = function(callback) {
var self = this;

this.logging.auth.getProjectId(function(err, projectId) {
if (err) {
callback(err);
this.logging.auth.getEnvironment(function(err, env) {
if (env.IS_CONTAINER_ENGINE) {
Metadata.getGKEDescriptor(callback);
return;
}

self.logging.auth.getEnvironment(function(err, env) {
if (env.IS_CONTAINER_ENGINE) {
Metadata.getGKEDescriptor(projectId, callback);
return;
}

var defaultResource;
var defaultResource;

if (env.IS_APP_ENGINE) {
defaultResource = Metadata.getGAEDescriptor(projectId);
} else if (env.IS_CLOUD_FUNCTION) {
defaultResource = Metadata.getCloudFunctionDescriptor(projectId);
} else if (env.IS_COMPUTE_ENGINE) {
defaultResource = Metadata.getGCEDescriptor(projectId);
} else {
defaultResource = Metadata.getGlobalDescriptor(projectId);
}
if (env.IS_APP_ENGINE) {
defaultResource = Metadata.getGAEDescriptor();
} else if (env.IS_CLOUD_FUNCTION) {
defaultResource = Metadata.getCloudFunctionDescriptor();
} else if (env.IS_COMPUTE_ENGINE) {
defaultResource = Metadata.getGCEDescriptor();
} else {
defaultResource = Metadata.getGlobalDescriptor();
}

callback(null, defaultResource);
});
callback(null, defaultResource);
});
};

Expand Down
74 changes: 15 additions & 59 deletions packages/logging/test/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ describe('metadata', function() {
var Metadata;
var metadata;

var PROJECT_ID = 'project-id';
var LOGGING;

var ENV_CACHED = extend({}, process.env);
Expand Down Expand Up @@ -77,10 +76,9 @@ describe('metadata', function() {
});

it('should return the correct descriptor', function() {
assert.deepEqual(Metadata.getCloudFunctionDescriptor(PROJECT_ID), {
assert.deepEqual(Metadata.getCloudFunctionDescriptor(), {
type: 'cloud_function',
labels: {
project_id: PROJECT_ID,
function_name: FUNCTION_NAME,
region: SUPERVISOR_REGION
}
Expand All @@ -100,10 +98,9 @@ describe('metadata', function() {
});

it('should return the correct descriptor', function() {
assert.deepEqual(Metadata.getGAEDescriptor(PROJECT_ID), {
assert.deepEqual(Metadata.getGAEDescriptor(), {
type: 'gae_app',
labels: {
project_id: PROJECT_ID,
module_id: GAE_SERVICE,
version_id: GAE_VERSION
}
Expand All @@ -113,7 +110,7 @@ describe('metadata', function() {
it('should use GAE_MODULE_NAME for module_id', function() {
delete process.env.GAE_SERVICE;

var moduleId = Metadata.getGAEDescriptor(PROJECT_ID).labels.module_id;
var moduleId = Metadata.getGAEDescriptor().labels.module_id;
assert.strictEqual(moduleId, GAE_MODULE_NAME);
});
});
Expand All @@ -124,13 +121,12 @@ describe('metadata', function() {
it('should return the correct descriptor', function(done) {
instanceArgsOverride = [null, null, CLUSTER_NAME];

Metadata.getGKEDescriptor(PROJECT_ID, function(err, descriptor) {
Metadata.getGKEDescriptor(function(err, descriptor) {
assert.ifError(err);
assert.deepEqual(descriptor, {
type: 'container',
labels: {
cluster_name: CLUSTER_NAME,
project_id: PROJECT_ID
cluster_name: CLUSTER_NAME
}
});
done();
Expand All @@ -141,7 +137,7 @@ describe('metadata', function() {
var FAKE_ERROR = new Error();
instanceArgsOverride = [ FAKE_ERROR ];

Metadata.getGKEDescriptor(PROJECT_ID, function(err) {
Metadata.getGKEDescriptor(function(err) {
assert.strictEqual(err, FAKE_ERROR);
done();
});
Expand All @@ -150,56 +146,21 @@ describe('metadata', function() {

describe('getGCEDescriptor', function() {
it('should return the correct descriptor', function() {
assert.deepEqual(Metadata.getGCEDescriptor(PROJECT_ID), {
type: 'gce_instance',
labels: {
project_id: PROJECT_ID
}
assert.deepEqual(Metadata.getGCEDescriptor(), {
type: 'gce_instance'
});
});
});

describe('getGlobalDescriptor', function() {
it('should return the correct descriptor', function() {
assert.deepEqual(Metadata.getGlobalDescriptor(PROJECT_ID), {
type: 'global',
labels: {
project_id: PROJECT_ID
}
assert.deepEqual(Metadata.getGlobalDescriptor(), {
type: 'global'
});
});
});

describe('getDefaultResource', function() {
var RETURNED_PROJECT_ID = 'project-id';

beforeEach(function() {
metadata.logging.auth.getProjectId = function(callback) {
callback(null, RETURNED_PROJECT_ID);
};
});

it('should get the project ID', function(done) {
metadata.logging.auth.getProjectId = function() {
done();
};

metadata.getDefaultResource(assert.ifError);
});

it('should return error from getProjectId', function(done) {
var error = new Error('Error.');

metadata.logging.auth.getProjectId = function(callback) {
callback(error);
};

metadata.getDefaultResource(function(err) {
assert.strictEqual(err, error);
done();
});
});

it('should get the environment from auth client', function(done) {
metadata.logging.auth.getEnvironment = function() {
done();
Expand All @@ -213,8 +174,7 @@ describe('metadata', function() {
it('should return correct descriptor', function(done) {
var DESCRIPTOR = {};

Metadata.getGAEDescriptor = function(projectId) {
assert.strictEqual(projectId, RETURNED_PROJECT_ID);
Metadata.getGAEDescriptor = function() {
return DESCRIPTOR;
};

Expand All @@ -237,8 +197,7 @@ describe('metadata', function() {
it('should return correct descriptor', function(done) {
var DESCRIPTOR = {};

Metadata.getCloudFunctionDescriptor = function(projectId) {
assert.strictEqual(projectId, RETURNED_PROJECT_ID);
Metadata.getCloudFunctionDescriptor = function() {
return DESCRIPTOR;
};

Expand All @@ -261,8 +220,7 @@ describe('metadata', function() {
it('should return correct descriptor', function(done) {
var DESCRIPTOR = {};

Metadata.getGCEDescriptor = function(projectId) {
assert.strictEqual(projectId, RETURNED_PROJECT_ID);
Metadata.getGCEDescriptor = function() {
return DESCRIPTOR;
};

Expand Down Expand Up @@ -297,8 +255,7 @@ describe('metadata', function() {
assert.deepStrictEqual(defaultResource, {
type: 'container',
labels: {
cluster_name: CLUSTER_NAME,
project_id: RETURNED_PROJECT_ID
cluster_name: CLUSTER_NAME
}
});
done();
Expand All @@ -310,8 +267,7 @@ describe('metadata', function() {
it('should return correct descriptor', function(done) {
var DESCRIPTOR = {};

Metadata.getGlobalDescriptor = function(projectId) {
assert.strictEqual(projectId, RETURNED_PROJECT_ID);
Metadata.getGlobalDescriptor = function() {
return DESCRIPTOR;
};

Expand Down