From f5e92be356b5a96f555b37dc7cd910d34016e6ea Mon Sep 17 00:00:00 2001 From: Bastian Krol Date: Wed, 13 Mar 2024 13:26:50 +0100 Subject: [PATCH] [dash0] frontend: convert k8s resource detector from ts to js The resource detectors are configured in a file loaded via a --require hook (in utils/telemetry/Instrumentation.js), which needs to be pure JS. --- .github/actions/update-env-file/action.yaml | 1 - .licenserc.json | 2 + src/frontend/package-lock.json | 119 ++++++------------ src/frontend/package.json | 2 - ...ector.ts => KubernetesResourceDetector.js} | 22 ++-- ....ts => KubernetesResourceDetector.test.js} | 12 +- 6 files changed, 56 insertions(+), 102 deletions(-) rename src/frontend/utils/telemetry/{KubernetesResourceDetector.ts => KubernetesResourceDetector.js} (82%) rename src/frontend/utils/telemetry/{KubernetesResourceDetector.test.ts => KubernetesResourceDetector.test.js} (95%) diff --git a/.github/actions/update-env-file/action.yaml b/.github/actions/update-env-file/action.yaml index c0c338986d..9f07081ff4 100644 --- a/.github/actions/update-env-file/action.yaml +++ b/.github/actions/update-env-file/action.yaml @@ -33,4 +33,3 @@ runs: git commit -a -m"[dash0] update image version in .env to ${{ inputs.containerImageVersion }}" -m"[ci skip]" git status git push - diff --git a/.licenserc.json b/.licenserc.json index 55e6c5c332..239a79bba1 100644 --- a/.licenserc.json +++ b/.licenserc.json @@ -44,6 +44,8 @@ "src/checkoutservice/genproto/", "src/featureflagservice/assets/vendor/", "src/featureflagservice/priv/", + "src/frontend/utils/telemetry/KubernetesResourceDetector.js", + "src/frontend/utils/telemetry/KubernetesResourceDetector.test.js", "src/productcatalogservice/genproto/", "internal/tools/", ".github/actions/", diff --git a/src/frontend/package-lock.json b/src/frontend/package-lock.json index ee9d7b39c9..3943771dc5 100644 --- a/src/frontend/package-lock.json +++ b/src/frontend/package-lock.json @@ -59,8 +59,6 @@ "jest": "^29.7.0", "mock-fs": "^5.2.0", "openapi-typescript": "6.7.1", - "ts-jest": "^29.1.2", - "ts-node": "^10.9.2", "typescript": "5.2.2" } }, @@ -789,6 +787,8 @@ "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==", "dev": true, + "optional": true, + "peer": true, "dependencies": { "@jridgewell/trace-mapping": "0.3.9" }, @@ -801,6 +801,8 @@ "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", "dev": true, + "optional": true, + "peer": true, "dependencies": { "@jridgewell/resolve-uri": "^3.0.3", "@jridgewell/sourcemap-codec": "^1.4.10" @@ -3667,25 +3669,33 @@ "version": "1.0.9", "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.9.tgz", "integrity": "sha512-jNsYVVxU8v5g43Erja32laIDHXeoNvFEpX33OK4d6hljo3jDhCBDhx5dhCCTMWUojscpAagGiRkBKxpdl9fxqA==", - "dev": true + "dev": true, + "optional": true, + "peer": true }, "node_modules/@tsconfig/node12": { "version": "1.0.11", "resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.11.tgz", "integrity": "sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==", - "dev": true + "dev": true, + "optional": true, + "peer": true }, "node_modules/@tsconfig/node14": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.3.tgz", "integrity": "sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==", - "dev": true + "dev": true, + "optional": true, + "peer": true }, "node_modules/@tsconfig/node16": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz", "integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==", - "dev": true + "dev": true, + "optional": true, + "peer": true }, "node_modules/@types/accepts": { "version": "1.3.7", @@ -4389,6 +4399,8 @@ "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.3.2.tgz", "integrity": "sha512-cjkyv4OtNCIeqhHrfS81QWXoCBPExR/J62oyEqepVw8WaQeSqpW2uhuLPh1m9eWhDuOo/jUXVTlifvesOWp/4A==", "dev": true, + "optional": true, + "peer": true, "engines": { "node": ">=0.4.0" } @@ -4528,7 +4540,9 @@ "version": "4.1.3", "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==", - "dev": true + "dev": true, + "optional": true, + "peer": true }, "node_modules/argparse": { "version": "2.0.1", @@ -5008,18 +5022,6 @@ "node": "^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7" } }, - "node_modules/bs-logger": { - "version": "0.2.6", - "resolved": "https://registry.npmjs.org/bs-logger/-/bs-logger-0.2.6.tgz", - "integrity": "sha512-pd8DCoxmbgc7hyPKOvxtqNcjYoOsABPQdcCUjGp3d42VR2CX1ORhk2A87oqqu5R1kk+76nsxZupkmyd+MVtCog==", - "dev": true, - "dependencies": { - "fast-json-stable-stringify": "2.x" - }, - "engines": { - "node": ">= 6" - } - }, "node_modules/bser": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/bser/-/bser-2.1.1.tgz", @@ -5484,7 +5486,9 @@ "version": "1.1.1", "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==", - "dev": true + "dev": true, + "optional": true, + "peer": true }, "node_modules/cross-spawn": { "version": "7.0.3", @@ -5731,6 +5735,8 @@ "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", "dev": true, + "optional": true, + "peer": true, "engines": { "node": ">=0.3.1" } @@ -8683,12 +8689,6 @@ "resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz", "integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==" }, - "node_modules/lodash.memoize": { - "version": "4.1.2", - "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz", - "integrity": "sha512-t7j+NzmgnQzTAYXcsHYLgimltOV1MXHtlOWf6GjL9Kj8GK5FInw5JotxvbOs+IvV1/Dzo04/fCGfLVs7aXb4Ag==", - "dev": true - }, "node_modules/lodash.merge": { "version": "4.6.2", "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", @@ -8811,7 +8811,9 @@ "version": "1.3.6", "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz", "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==", - "dev": true + "dev": true, + "optional": true, + "peer": true }, "node_modules/makeerror": { "version": "1.0.12", @@ -10684,66 +10686,13 @@ "typescript": ">=4.2.0" } }, - "node_modules/ts-jest": { - "version": "29.1.2", - "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-29.1.2.tgz", - "integrity": "sha512-br6GJoH/WUX4pu7FbZXuWGKGNDuU7b8Uj77g/Sp7puZV6EXzuByl6JrECvm0MzVzSTkSHWTihsXt+5XYER5b+g==", - "dev": true, - "dependencies": { - "bs-logger": "0.x", - "fast-json-stable-stringify": "2.x", - "jest-util": "^29.0.0", - "json5": "^2.2.3", - "lodash.memoize": "4.x", - "make-error": "1.x", - "semver": "^7.5.3", - "yargs-parser": "^21.0.1" - }, - "bin": { - "ts-jest": "cli.js" - }, - "engines": { - "node": "^16.10.0 || ^18.0.0 || >=20.0.0" - }, - "peerDependencies": { - "@babel/core": ">=7.0.0-beta.0 <8", - "@jest/types": "^29.0.0", - "babel-jest": "^29.0.0", - "jest": "^29.0.0", - "typescript": ">=4.3 <6" - }, - "peerDependenciesMeta": { - "@babel/core": { - "optional": true - }, - "@jest/types": { - "optional": true - }, - "babel-jest": { - "optional": true - }, - "esbuild": { - "optional": true - } - } - }, - "node_modules/ts-jest/node_modules/json5": { - "version": "2.2.3", - "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", - "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", - "dev": true, - "bin": { - "json5": "lib/cli.js" - }, - "engines": { - "node": ">=6" - } - }, "node_modules/ts-node": { "version": "10.9.2", "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.2.tgz", "integrity": "sha512-f0FFpIdcHgn8zcPSbf1dRevwt047YMnaiJM3u2w2RewrB+fob/zePZcrOyQoLMMO7aBIddLcQIEK5dYjkLnGrQ==", "dev": true, + "optional": true, + "peer": true, "dependencies": { "@cspotcode/source-map-support": "^0.8.0", "@tsconfig/node10": "^1.0.7", @@ -11082,7 +11031,9 @@ "version": "3.0.1", "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", "integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==", - "dev": true + "dev": true, + "optional": true, + "peer": true }, "node_modules/v8-to-istanbul": { "version": "9.2.0", @@ -11322,6 +11273,8 @@ "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz", "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==", "dev": true, + "optional": true, + "peer": true, "engines": { "node": ">=6" } diff --git a/src/frontend/package.json b/src/frontend/package.json index 6292d8d917..a8a5a42e6b 100644 --- a/src/frontend/package.json +++ b/src/frontend/package.json @@ -63,8 +63,6 @@ "jest": "^29.7.0", "mock-fs": "^5.2.0", "openapi-typescript": "6.7.1", - "ts-jest": "^29.1.2", - "ts-node": "^10.9.2", "typescript": "5.2.2" } } diff --git a/src/frontend/utils/telemetry/KubernetesResourceDetector.ts b/src/frontend/utils/telemetry/KubernetesResourceDetector.js similarity index 82% rename from src/frontend/utils/telemetry/KubernetesResourceDetector.ts rename to src/frontend/utils/telemetry/KubernetesResourceDetector.js index ab5c14bffb..43a4349638 100644 --- a/src/frontend/utils/telemetry/KubernetesResourceDetector.ts +++ b/src/frontend/utils/telemetry/KubernetesResourceDetector.js @@ -1,13 +1,13 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ -import { readFile } from 'fs/promises'; -import { Detector, Resource, ResourceDetectionConfig } from '@opentelemetry/resources'; -import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; +const { readFile } = require('fs/promises'); +const { Detector, Resource, ResourceDetectionConfig } = require('@opentelemetry/resources'); +const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions'); const POD_ID_LENGTH = 36; const CONTAINER_ID_LENGTH = 64; -export class KubernetesResourceDetector implements Detector { - async detect(_config?: ResourceDetectionConfig): Promise { +class KubernetesResourceDetector { + async detect(_config) { try { const hostFileContent = await readFile('/etc/hosts', { encoding: 'utf8', @@ -27,7 +27,7 @@ export class KubernetesResourceDetector implements Detector { return Resource.EMPTY; } - let podUid: string | null | undefined = null; + let podUid = null; try { podUid = await readPodUidCgroupsV1(); } catch (err) { @@ -49,7 +49,7 @@ export class KubernetesResourceDetector implements Detector { } } -const readPodUidCgroupsV1 = async (): Promise => { +async function readPodUidCgroupsV1() { const mountinfo = await readFile('/proc/self/mountinfo', { encoding: 'utf8', }); @@ -66,9 +66,9 @@ const readPodUidCgroupsV1 = async (): Promise => { } return podMountInfoEntry.split('/pods/')[1].substring(0, POD_ID_LENGTH); -}; +} -const readPodUidCgroupsV2 = async (): Promise => { +async function readPodUidCgroupsV2() { const cgroups = await readFile('/proc/self/cgroup', { encoding: 'utf8', }); @@ -94,4 +94,6 @@ const readPodUidCgroupsV2 = async (): Promise => { return segments[segments.length - 2]; }) .find(podUid => !!podUid); -}; +} + +module.exports = KubernetesResourceDetector; diff --git a/src/frontend/utils/telemetry/KubernetesResourceDetector.test.ts b/src/frontend/utils/telemetry/KubernetesResourceDetector.test.js similarity index 95% rename from src/frontend/utils/telemetry/KubernetesResourceDetector.test.ts rename to src/frontend/utils/telemetry/KubernetesResourceDetector.test.js index e24a311a8f..dbb5016769 100644 --- a/src/frontend/utils/telemetry/KubernetesResourceDetector.test.ts +++ b/src/frontend/utils/telemetry/KubernetesResourceDetector.test.js @@ -1,14 +1,14 @@ -import { KubernetesResourceDetector } from './KubernetesResourceDetector'; -import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; -import {expect, jest, test} from '@jest/globals'; -import mock from 'mock-fs'; +const KubernetesResourceDetector = require('./KubernetesResourceDetector'); +const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions'); +const { expect, test } = require('@jest/globals'); +const mock = require('mock-fs'); const K8S_POD_ID = '6189e731-8c9a-4c3a-ba6f-9796664788a8'; -const trimMultiline = (s: string) => +const trimMultiline = s => s .split('\n') - .map((s) => s.trim()) + .map(s => s.trim()) .join('\n'); describe('KubernetesResourceDetector', () => {