Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
feat: add auto-inferred platform label (#886)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamohile committed Jun 25, 2020
1 parent 692d0a7 commit cb1743b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/agent/debuglet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ interface SourceContext {
[key: string]: string;
}

/**
* Environments that this system might be running in.
* Helps provide platform-specific information and integration.
*/
export enum Platforms {
/** Google Cloud Functions */
CLOUD_FUNCTION = 'cloud_function',
/** Any other platform. */
DEFAULT = 'default',
}

/**
* Formats a breakpoint object prefixed with a provided message as a string
* intended for logging.
Expand Down Expand Up @@ -544,6 +555,7 @@ export class Debuglet extends EventEmitter {
'agent.name': packageInfo.name,
'agent.version': packageInfo.version,
projectid: projectId,
platform: Debuglet.getPlatform(),
};

if (serviceContext) {
Expand Down Expand Up @@ -604,6 +616,19 @@ export class Debuglet extends EventEmitter {
return new Debuggee(properties);
}

/**
* Use environment vars to infer the current platform.
* For now this is only Cloud Functions and other.
*/
private static getPlatform(): Platforms {
const {FUNCTION_NAME, FUNCTION_TARGET} = process.env;
// (In theory) only the Google Cloud Functions environment will have these env vars.
if (FUNCTION_NAME || FUNCTION_TARGET) {
return Platforms.CLOUD_FUNCTION;
}
return Platforms.DEFAULT;
}

static runningOnGCP(): Promise<boolean> {
return metadata.isAvailable();
}
Expand Down
49 changes: 48 additions & 1 deletion test/test-debuglet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ import * as stackdriver from '../src/types/stackdriver';

DEFAULT_CONFIG.allowExpressions = true;
DEFAULT_CONFIG.workingDirectory = path.join(__dirname, '..', '..');
import {Debuglet, CachedPromise, FindFilesResult} from '../src/agent/debuglet';
import {
Debuglet,
CachedPromise,
FindFilesResult,
Platforms,
} from '../src/agent/debuglet';
import {ScanResults} from '../src/agent/io/scanner';
import * as extend from 'extend';
import {Debug} from '../src/client/stackdriver/debug';
Expand Down Expand Up @@ -1542,6 +1547,48 @@ describe('Debuglet', () => {
);
assert.strictEqual(debuggee.canaryMode, 'CANARY_MODE_ALWAYS_DISABLED');
});

it('should correctly identify default platform.', () => {
const debuggee = Debuglet.createDebuggee(
'some project',
'id',
{service: 'some-service', version: 'production'},
{},
false,
packageInfo
);
assert.ok(debuggee.labels!.platform === Platforms.DEFAULT);
});

it('should correctly identify GCF (legacy) platform.', () => {
// GCF sets this env var on older runtimes.
process.env.FUNCTION_NAME = 'mock';
const debuggee = Debuglet.createDebuggee(
'some project',
'id',
{service: 'some-service', version: 'production'},
{},
false,
packageInfo
);
assert.ok(debuggee.labels!.platform === Platforms.CLOUD_FUNCTION);
delete process.env.FUNCTION_NAME; // Don't contaminate test environment.
});

it('should correctly identify GCF (modern) platform.', () => {
// GCF sets this env var on modern runtimes.
process.env.FUNCTION_TARGET = 'mock';
const debuggee = Debuglet.createDebuggee(
'some project',
'id',
{service: 'some-service', version: 'production'},
{},
false,
packageInfo
);
assert.ok(debuggee.labels!.platform === Platforms.CLOUD_FUNCTION);
delete process.env.FUNCTION_TARGET; // Don't contaminate test environment.
});
});

describe('_createUniquifier', () => {
Expand Down

0 comments on commit cb1743b

Please sign in to comment.