Skip to content

Commit 7bb6fe8

Browse files
committed
feat: provide cluster_name on GKE resources
For better log categorization in the Logging UI, we need to include the cluster name as part of the resource descriptor.
1 parent a64518e commit 7bb6fe8

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

packages/logging/src/metadata.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,16 @@ Metadata.getGCEDescriptor = function(projectId) {
9595
* @private
9696
*
9797
* @param {string} projectId - The project ID.
98+
* @param {string} clusterName - GKE cluster name.
9899
* @return {object}
99100
*/
100-
Metadata.getGKEDescriptor = function(projectId) {
101+
Metadata.getGKEDescriptor = function(projectId, clusterName) {
101102
return {
102103
type: 'container',
103104
labels: {
104-
project_id: projectId,
105+
// TODO(ofrobots): it would be good to include the namespace_id as well.
106+
cluster_name: clusterName,
107+
project_id: projectId
105108
}
106109
};
107110
};
@@ -145,7 +148,7 @@ Metadata.prototype.getDefaultResource = function(callback) {
145148
} else if (env.IS_CLOUD_FUNCTION) {
146149
defaultResource = Metadata.getCloudFunctionDescriptor(projectId);
147150
} else if (env.IS_CONTAINER_ENGINE) {
148-
defaultResource = Metadata.getGKEDescriptor(projectId);
151+
defaultResource = Metadata.getGKEDescriptor(projectId, env.clusterName);
149152
} else if (env.IS_COMPUTE_ENGINE) {
150153
defaultResource = Metadata.getGCEDescriptor(projectId);
151154
} else {

packages/logging/test/metadata.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,13 @@ describe('metadata', function() {
104104
});
105105

106106
describe('getGKEDescriptor', function() {
107+
var CLUSTER_NAME = 'gke-cluster-name';
108+
107109
it('should return the correct descriptor', function() {
108-
assert.deepEqual(Metadata.getGKEDescriptor(PROJECT_ID), {
110+
assert.deepEqual(Metadata.getGKEDescriptor(PROJECT_ID, CLUSTER_NAME), {
109111
type: 'container',
110112
labels: {
113+
cluster_name: CLUSTER_NAME,
111114
project_id: PROJECT_ID
112115
}
113116
});
@@ -246,16 +249,19 @@ describe('metadata', function() {
246249

247250
describe('container engine', function() {
248251
it('should return correct descriptor', function(done) {
252+
var RETURNED_CLUSTER_NAME = 'fake-cluster';
249253
var DESCRIPTOR = {};
250254

251-
Metadata.getGKEDescriptor = function(projectId) {
255+
Metadata.getGKEDescriptor = function(projectId, clusterName) {
252256
assert.strictEqual(projectId, RETURNED_PROJECT_ID);
257+
assert.strictEqual(clusterName, RETURNED_CLUSTER_NAME);
253258
return DESCRIPTOR;
254259
};
255260

256261
metadata.logging.auth.getEnvironment = function(callback) {
257262
callback(null, {
258-
IS_CONTAINER_ENGINE: true
263+
IS_CONTAINER_ENGINE: true,
264+
clusterName: RETURNED_CLUSTER_NAME
259265
});
260266
};
261267

0 commit comments

Comments
 (0)