Skip to content

Commit

Permalink
[dash0] frontend: convert k8s resource detector from ts to js
Browse files Browse the repository at this point in the history
The resource detectors are configured in a file loaded via a
--require hook (in utils/telemetry/Instrumentation.js), which needs
to be pure JS.
  • Loading branch information
basti1302 committed Mar 13, 2024
1 parent 5a45834 commit 1d52ea4
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 108 deletions.
1 change: 0 additions & 1 deletion .github/actions/update-env-file/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions .licenserc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ RUN adduser -S nextjs -u 1001

COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/utils/telemetry/Instrumentation.js ./
COPY --from=builder /app/utils/telemetry/KubernetesResourceDetector.ts ./
COPY --from=builder /app/utils/telemetry/KubernetesResourceDetector.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
COPY --from=deps /app/node_modules ./node_modules
Expand Down
7 changes: 2 additions & 5 deletions src/frontend/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/**
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
*/
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

import type { Config } from 'jest';

const config: Config = {
preset: 'ts-jest',
testEnvironment: 'node',
clearMocks: true,
coverageProvider: 'v8',
Expand Down
119 changes: 36 additions & 83 deletions src/frontend/package-lock.json

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

2 changes: 0 additions & 2 deletions src/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -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<Resource> {
class KubernetesResourceDetector {
async detect(_config) {
try {
const hostFileContent = await readFile('/etc/hosts', {
encoding: 'utf8',
Expand All @@ -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) {
Expand All @@ -49,7 +49,7 @@ export class KubernetesResourceDetector implements Detector {
}
}

const readPodUidCgroupsV1 = async (): Promise<string> => {
async function readPodUidCgroupsV1() {
const mountinfo = await readFile('/proc/self/mountinfo', {
encoding: 'utf8',
});
Expand All @@ -66,9 +66,9 @@ const readPodUidCgroupsV1 = async (): Promise<string> => {
}

return podMountInfoEntry.split('/pods/')[1].substring(0, POD_ID_LENGTH);
};
}

const readPodUidCgroupsV2 = async (): Promise<string | null | undefined> => {
async function readPodUidCgroupsV2() {
const cgroups = await readFile('/proc/self/cgroup', {
encoding: 'utf8',
});
Expand All @@ -94,4 +94,6 @@ const readPodUidCgroupsV2 = async (): Promise<string | null | undefined> => {
return segments[segments.length - 2];
})
.find(podUid => !!podUid);
};
}

module.exports = KubernetesResourceDetector;
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down

0 comments on commit 1d52ea4

Please sign in to comment.