Skip to content

Commit dc4eb4e

Browse files
authored
chore: add CI and container detection to telemetry (#2176)
1 parent 85938b3 commit dc4eb4e

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

packages/schema/src/telemetry.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import isDocker from './utils/is-docker';
1111
import { isWsl } from './utils/is-wsl';
1212
import { getMachineId } from './utils/machine-id-utils';
1313
import { getVersion } from './utils/version-utils';
14+
import { isInCi } from './utils/is-ci';
15+
import { isInContainer } from './utils/is-container';
1416

1517
/**
1618
* Telemetry events
@@ -44,6 +46,8 @@ export class Telemetry {
4446
private readonly prismaVersion = getPrismaVersion();
4547
private readonly isDocker = isDocker();
4648
private readonly isWsl = isWsl();
49+
private readonly isContainer = isInContainer();
50+
private readonly isCi = isInCi;
4751
private exitWait = 200;
4852

4953
constructor() {
@@ -108,6 +112,8 @@ export class Telemetry {
108112
prismaVersion: this.prismaVersion,
109113
isDocker: this.isDocker,
110114
isWsl: this.isWsl,
115+
isContainer: this.isContainer,
116+
isCi: this.isCi,
111117
...properties,
112118
};
113119
this.mixpanel.track(event, payload);

packages/schema/src/utils/is-ci.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {env} from 'node:process';
2+
export const isInCi = env.CI !== '0'
3+
&& env.CI !== 'false'
4+
&& (
5+
'CI' in env
6+
|| 'CONTINUOUS_INTEGRATION' in env
7+
|| Object.keys(env).some(key => key.startsWith('CI_'))
8+
);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import fs from 'node:fs';
2+
import isDocker from './is-docker';
3+
4+
let cachedResult: boolean | undefined;
5+
6+
// Podman detection
7+
const hasContainerEnv = () => {
8+
try {
9+
fs.statSync('/run/.containerenv');
10+
return true;
11+
} catch {
12+
return false;
13+
}
14+
};
15+
16+
export function isInContainer() {
17+
// TODO: Use `??=` when targeting Node.js 16.
18+
if (cachedResult === undefined) {
19+
cachedResult = hasContainerEnv() || isDocker();
20+
}
21+
22+
return cachedResult;
23+
}

0 commit comments

Comments
 (0)