From 72ba88cb530458feb7aeed3e30bbd75e56d181e5 Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Mon, 8 Nov 2021 20:53:40 +0900 Subject: [PATCH] Add docker image containing nodejs auto instrumentation (#508) --- .../publish-autoinstrumentation-nodejs.yaml | 44 +++++++++++++++++++ .gitignore | 5 +++ autoinstrumentation/nodejs/.dockerignore | 2 + autoinstrumentation/nodejs/Dockerfile | 10 +++++ autoinstrumentation/nodejs/package.json | 22 ++++++++++ .../nodejs/src/autoinstrumentation.ts | 12 +++++ autoinstrumentation/nodejs/tsconfig.json | 33 ++++++++++++++ versions.txt | 4 ++ 8 files changed, 132 insertions(+) create mode 100644 .github/workflows/publish-autoinstrumentation-nodejs.yaml create mode 100644 autoinstrumentation/nodejs/.dockerignore create mode 100644 autoinstrumentation/nodejs/Dockerfile create mode 100644 autoinstrumentation/nodejs/package.json create mode 100644 autoinstrumentation/nodejs/src/autoinstrumentation.ts create mode 100644 autoinstrumentation/nodejs/tsconfig.json diff --git a/.github/workflows/publish-autoinstrumentation-nodejs.yaml b/.github/workflows/publish-autoinstrumentation-nodejs.yaml new file mode 100644 index 0000000000..157ffd5bbb --- /dev/null +++ b/.github/workflows/publish-autoinstrumentation-nodejs.yaml @@ -0,0 +1,44 @@ +name: "Publish NodeJS Auto-Instrumentation" + +on: + push: + paths: + - 'autoinstrumentation/nodejs/**' + - '.github/workflows/publish-autoinstrumentation-nodejs.yaml' + branches: + - main + workflow_dispatch: + +jobs: + publish: + runs-on: ubuntu-20.04 + + steps: + - uses: actions/checkout@v2.3.5 + + - name: Read version + run: echo VERSION=$(cat autoinstrumentation/nodejs/package.json | jq -r '.dependencies."@opentelemetry/sdk-node"') >> $GITHUB_ENV + + - name: Docker meta + id: meta + uses: docker/metadata-action@v3.6.0 + with: + images: ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-nodejs + tags: | + type=match,pattern=v(.*),group=1,value=v${{ env.VERSION }} + + - name: Login to GitHub Package Registry + uses: docker/login-action@v1.10.0 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push + uses: docker/build-push-action@v2.7.0 + with: + context: autoinstrumentation/nodejs + push: true + build-args: version=${{ env.VERSION }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/.gitignore b/.gitignore index 374dd36ea8..5ec29864b4 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,8 @@ local # test resources kubeconfig tests/_build/ + +# autoinstrumentation artifacts +build +node_modules +package-lock.json diff --git a/autoinstrumentation/nodejs/.dockerignore b/autoinstrumentation/nodejs/.dockerignore new file mode 100644 index 0000000000..48912d244b --- /dev/null +++ b/autoinstrumentation/nodejs/.dockerignore @@ -0,0 +1,2 @@ +build +node_modules \ No newline at end of file diff --git a/autoinstrumentation/nodejs/Dockerfile b/autoinstrumentation/nodejs/Dockerfile new file mode 100644 index 0000000000..85bc9f4c02 --- /dev/null +++ b/autoinstrumentation/nodejs/Dockerfile @@ -0,0 +1,10 @@ +FROM node:16 AS build + +WORKDIR /operator-build +COPY . . + +RUN npm install + +FROM busybox + +COPY --from=build /operator-build/build/workspace /autoinstrumentation diff --git a/autoinstrumentation/nodejs/package.json b/autoinstrumentation/nodejs/package.json new file mode 100644 index 0000000000..17853ed496 --- /dev/null +++ b/autoinstrumentation/nodejs/package.json @@ -0,0 +1,22 @@ +{ + "name": "@opentelemetry/k8s-autoinstrumentation", + "version": "0.0.1", + "private": true, + "scripts": { + "clean": "rimraf build/*", + "prepare": "npm run compile", + "compile": "tsc -p .", + "postcompile": "copyfiles -f 'build/src/**' build/workspace/ && copyfiles 'node_modules/**' build/workspace/" + }, + "devDependencies": { + "copyfiles": "^2.4.1", + "rimraf": "^3.0.2", + "typescript": "^4.4.4" + }, + "dependencies": { + "@opentelemetry/api": "1.0.3", + "@opentelemetry/auto-instrumentations-node": "0.26.0", + "@opentelemetry/exporter-otlp-grpc": "0.26.0", + "@opentelemetry/sdk-node": "0.26.0" + } +} diff --git a/autoinstrumentation/nodejs/src/autoinstrumentation.ts b/autoinstrumentation/nodejs/src/autoinstrumentation.ts new file mode 100644 index 0000000000..e03b533e3e --- /dev/null +++ b/autoinstrumentation/nodejs/src/autoinstrumentation.ts @@ -0,0 +1,12 @@ +import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'; +import { OTLPTraceExporter } from '@opentelemetry/exporter-otlp-grpc'; + +import { NodeSDK } from '@opentelemetry/sdk-node'; + +const sdk = new NodeSDK({ + autoDetectResources: true, + instrumentations: [getNodeAutoInstrumentations()], + traceExporter: new OTLPTraceExporter(), +}); + +sdk.start(); diff --git a/autoinstrumentation/nodejs/tsconfig.json b/autoinstrumentation/nodejs/tsconfig.json new file mode 100644 index 0000000000..83d94c8d13 --- /dev/null +++ b/autoinstrumentation/nodejs/tsconfig.json @@ -0,0 +1,33 @@ +{ + "compilerOptions": { + "rootDir": ".", + "outDir": "build", + + "allowUnreachableCode": false, + "allowUnusedLabels": false, + "composite": true, + "declaration": true, + "declarationMap": true, + "forceConsistentCasingInFileNames": true, + "incremental": true, + "inlineSources": true, + "module": "commonjs", + "newLine": "LF", + "noEmitOnError": true, + "noFallthroughCasesInSwitch": true, + "noImplicitOverride": true, + "noImplicitReturns": true, + "noUnusedLocals": true, + "pretty": true, + "sourceMap": true, + "strict": true, + "strictNullChecks": true, + "target": "es2017" + }, + "include": [ + "src/**/*.ts", + ], + "exclude": [ + "node_modules" + ] +} \ No newline at end of file diff --git a/versions.txt b/versions.txt index 06fb40f628..3da233d344 100644 --- a/versions.txt +++ b/versions.txt @@ -13,3 +13,7 @@ targetallocator=0.1.0 # Represents the current release of Java instrumentation. # Should match autoinstrumentation/java/version.txt autoinstrumentation-java=1.7.0 + +# Represents the current release of NodeJS instrumentation. +# Should match value in autoinstrumentation/nodejs/package.json +autoinstrumentation-nodejs=0.26.0 \ No newline at end of file