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

Commit cb1743b

Browse files
authored
feat: add auto-inferred platform label (#886)
1 parent 692d0a7 commit cb1743b

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/agent/debuglet.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ interface SourceContext {
6868
[key: string]: string;
6969
}
7070

71+
/**
72+
* Environments that this system might be running in.
73+
* Helps provide platform-specific information and integration.
74+
*/
75+
export enum Platforms {
76+
/** Google Cloud Functions */
77+
CLOUD_FUNCTION = 'cloud_function',
78+
/** Any other platform. */
79+
DEFAULT = 'default',
80+
}
81+
7182
/**
7283
* Formats a breakpoint object prefixed with a provided message as a string
7384
* intended for logging.
@@ -544,6 +555,7 @@ export class Debuglet extends EventEmitter {
544555
'agent.name': packageInfo.name,
545556
'agent.version': packageInfo.version,
546557
projectid: projectId,
558+
platform: Debuglet.getPlatform(),
547559
};
548560

549561
if (serviceContext) {
@@ -604,6 +616,19 @@ export class Debuglet extends EventEmitter {
604616
return new Debuggee(properties);
605617
}
606618

619+
/**
620+
* Use environment vars to infer the current platform.
621+
* For now this is only Cloud Functions and other.
622+
*/
623+
private static getPlatform(): Platforms {
624+
const {FUNCTION_NAME, FUNCTION_TARGET} = process.env;
625+
// (In theory) only the Google Cloud Functions environment will have these env vars.
626+
if (FUNCTION_NAME || FUNCTION_TARGET) {
627+
return Platforms.CLOUD_FUNCTION;
628+
}
629+
return Platforms.DEFAULT;
630+
}
631+
607632
static runningOnGCP(): Promise<boolean> {
608633
return metadata.isAvailable();
609634
}

test/test-debuglet.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ import * as stackdriver from '../src/types/stackdriver';
2626

2727
DEFAULT_CONFIG.allowExpressions = true;
2828
DEFAULT_CONFIG.workingDirectory = path.join(__dirname, '..', '..');
29-
import {Debuglet, CachedPromise, FindFilesResult} from '../src/agent/debuglet';
29+
import {
30+
Debuglet,
31+
CachedPromise,
32+
FindFilesResult,
33+
Platforms,
34+
} from '../src/agent/debuglet';
3035
import {ScanResults} from '../src/agent/io/scanner';
3136
import * as extend from 'extend';
3237
import {Debug} from '../src/client/stackdriver/debug';
@@ -1542,6 +1547,48 @@ describe('Debuglet', () => {
15421547
);
15431548
assert.strictEqual(debuggee.canaryMode, 'CANARY_MODE_ALWAYS_DISABLED');
15441549
});
1550+
1551+
it('should correctly identify default platform.', () => {
1552+
const debuggee = Debuglet.createDebuggee(
1553+
'some project',
1554+
'id',
1555+
{service: 'some-service', version: 'production'},
1556+
{},
1557+
false,
1558+
packageInfo
1559+
);
1560+
assert.ok(debuggee.labels!.platform === Platforms.DEFAULT);
1561+
});
1562+
1563+
it('should correctly identify GCF (legacy) platform.', () => {
1564+
// GCF sets this env var on older runtimes.
1565+
process.env.FUNCTION_NAME = 'mock';
1566+
const debuggee = Debuglet.createDebuggee(
1567+
'some project',
1568+
'id',
1569+
{service: 'some-service', version: 'production'},
1570+
{},
1571+
false,
1572+
packageInfo
1573+
);
1574+
assert.ok(debuggee.labels!.platform === Platforms.CLOUD_FUNCTION);
1575+
delete process.env.FUNCTION_NAME; // Don't contaminate test environment.
1576+
});
1577+
1578+
it('should correctly identify GCF (modern) platform.', () => {
1579+
// GCF sets this env var on modern runtimes.
1580+
process.env.FUNCTION_TARGET = 'mock';
1581+
const debuggee = Debuglet.createDebuggee(
1582+
'some project',
1583+
'id',
1584+
{service: 'some-service', version: 'production'},
1585+
{},
1586+
false,
1587+
packageInfo
1588+
);
1589+
assert.ok(debuggee.labels!.platform === Platforms.CLOUD_FUNCTION);
1590+
delete process.env.FUNCTION_TARGET; // Don't contaminate test environment.
1591+
});
15451592
});
15461593

15471594
describe('_createUniquifier', () => {

0 commit comments

Comments
 (0)