Skip to content

Commit

Permalink
feat: collect emitter environment name and custom CI check (#290)
Browse files Browse the repository at this point in the history
* feat: check for SPS env variables

* fix: remove .only

* fix: split tests into two files

* fix(test): refactored code

* fix(test): mocked ci-info

* feat: hashing environment names

* fix: update SPS name

* fix: small doc update

* fix: specify the name is also hashed

* fix: update tests and payload
  • Loading branch information
IgnacioBecerra authored Jan 8, 2025
1 parent 21f5f07 commit 7068a5c
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 221 deletions.
165 changes: 10 additions & 155 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@commitlint/cli": "^19.4.1",
"@commitlint/config-conventional": "^19.4.1",
"@eslint-community/eslint-plugin-eslint-comments": "^4.4.0",
"@ibm/telemetry-attributes-js": "^4.1.2",
"@ibm/telemetry-attributes-js": "^4.2.0",
"@ibm/telemetry-config-schema": "^1.3.0",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/exporter-metrics-otlp-http": "^0.53.0",
Expand Down Expand Up @@ -78,7 +78,6 @@
"eslint-plugin-vitest": "^0.5.4",
"globals": "^15.9.0",
"husky": "^9.1.5",
"is-inside-container": "^1.0.0",
"js-yaml": "^4.1.0",
"lint-staged": "^15.2.10",
"lodash": "^4.17.21",
Expand Down
74 changes: 71 additions & 3 deletions src/main/core/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { isCI } from 'ci-info'
import isInsideContainer from 'is-inside-container'
import { createHash } from 'node:crypto'
import { existsSync, readFileSync } from 'node:fs'

import { isCI, name } from 'ci-info'

interface EnvironmentConfig {
cwd?: string
isCI?: boolean
isExportEnabled?: boolean
isTelemetryEnabled?: boolean
name?: string
}

const customEnvs = {
'Secure Pipelines Services': ['PIPELINE_RUN_URL', 'PIPELINE_RUN_ID', 'PIPELINE_ID']
}

/**
Expand All @@ -23,6 +30,11 @@ export class Environment {
*/
readonly isCI: boolean

/**
* The hashed name of the possible continuous integration environment the process is running in.
*/
name: string

/**
* Exporting can be disabled (for testing purposes) by exporting IBM_TELEMETRY_EXPORT_DISABLED as
* 'true'.
Expand All @@ -40,7 +52,8 @@ export class Environment {
readonly cwd: string

constructor(config?: EnvironmentConfig) {
this.isCI = isCI || isInsideContainer()
this.name = name ?? ''
this.isCI = isCI || this.customCICheck() || this.containerCheck()
this.isExportEnabled = process.env['IBM_TELEMETRY_EXPORT_DISABLED'] !== 'true'
this.isTelemetryEnabled = process.env['IBM_TELEMETRY_DISABLED'] !== 'true'
this.cwd = process.cwd()
Expand All @@ -59,5 +72,60 @@ export class Environment {
if (config?.cwd !== undefined) {
this.cwd = config.cwd
}

if (this.name != '') {
const hash = createHash('sha256')
hash.update(this.name)
this.name = hash.digest('hex')
}
}

/**
* Function to check for additional CIs that aren't covered by the ci-info package.
*
* @returns Whether env variables are found.
*/
public customCICheck(): boolean {
for (const [key, val] of Object.entries(customEnvs)) {
if (val.some((varName) => process.env[varName] !== undefined)) {
this.name = key // Set the matched name
return true
}
}
return false
}

/**
* Function to retrieve container name depending on file existence.
*
* TODO: Fork 'is-inside-container' to behave more like 'ci-info' by retrieving name,
* and even checking for other containers.
*
* @returns Whether environment is within container.
*/
public containerCheck(): boolean {
if (existsSync('/run/.containerenv')) {
this.name = 'Podman'
return true
}

if (existsSync('/.dockerenv')) {
this.name = 'Docker'
return true
}

if (existsSync('/proc/self/cgroup')) {
try {
const cgroupContent = readFileSync('/proc/self/cgroup', 'utf8')
if (cgroupContent.includes('docker')) {
this.name = 'Docker'
return true
}
} catch (error) {
console.log('Error reading /proc/self/cgroup:', error)
}
}

return false
}
}
Loading

0 comments on commit 7068a5c

Please sign in to comment.