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

fix: serviceContext detection for Cloud Function and Cloud Run #1544

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion src/utils/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ export async function detectServiceContext(
};
case GCPEnv.CLOUD_FUNCTIONS:
return {
service: process.env.FUNCTION_NAME,
service: process.env.K_SERVICE || process.env.FUNCTION_NAME,
version: process.env.K_REVISION,
};
// On Kubernetes we use the pod-name to describe the service. Currently,
// we acquire the pod-name from within the pod through env var `HOSTNAME`.
Expand All @@ -233,6 +234,7 @@ export async function detectServiceContext(
case GCPEnv.CLOUD_RUN:
return {
service: process.env.K_SERVICE,
version: process.env.K_REVISION,
};
case GCPEnv.COMPUTE_ENGINE:
return null;
Expand Down
18 changes: 16 additions & 2 deletions test/utils/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,8 @@ describe('metadata', () => {

it('should return the correct descriptor for Cloud Functions', async () => {
const FUNCTION_NAME = (process.env.FUNCTION_NAME = 'function-name');
const K_SERVICE = (process.env.K_SERVICE = 'k-service');
const K_REVISION = (process.env.K_REVISION = 'k-revision');

const fakeAuth = {
async getEnv() {
Expand All @@ -595,12 +597,20 @@ describe('metadata', () => {
};

const sc1 = await metadata.detectServiceContext(fakeAuth);
assert.deepStrictEqual(sc1, {service: FUNCTION_NAME});
assert.deepStrictEqual(sc1, {service: K_SERVICE, version: K_REVISION});

delete process.env.K_SERVICE;
const sc2 = await metadata.detectServiceContext(fakeAuth);
assert.deepStrictEqual(sc2, {
service: FUNCTION_NAME,
version: K_REVISION,
});
});

it('should return the correct descriptor for Cloud Run', async () => {
process.env.K_CONFIGURATION = 'hello-world';
const SERVICE_NAME = (process.env.K_SERVICE = 'hello-world');
const SERVICE_VERSION = (process.env.K_REVISION = 'hello-world.1');

const fakeAuth = {
async getEnv() {
Expand All @@ -609,10 +619,14 @@ describe('metadata', () => {
};

const sc1 = await metadata.detectServiceContext(fakeAuth);
assert.deepStrictEqual(sc1, {service: SERVICE_NAME});
assert.deepStrictEqual(sc1, {
service: SERVICE_NAME,
version: SERVICE_VERSION,
});

delete process.env['K_CONFIGURATION'];
delete process.env['K_SERVICE'];
delete process.env['K_REVISION'];
});

it('should return the correct descriptor for GKE', async () => {
Expand Down