Signed-off-by: Antonio Ereiz <antonio.ereiz@gmail.com>
This commit is contained in:
Antonio Ereiz
2024-02-24 22:08:05 +01:00
parent 5b8efa4e06
commit 863a4f5941
2 changed files with 38 additions and 25 deletions
+4 -25
View File
@@ -24,7 +24,7 @@ yarn --cwd packages/backend add @opentelemetry/sdk-node \
## Configure
In your `packages/backend` folder, create an `instrumentation.ts` file.
In your `packages/backend` folder, create an `instrumentation.js` file.
```typescript
const { NodeSDK } = require('@opentelemetry/sdk-node');
@@ -48,7 +48,7 @@ const sdk = new NodeSDK({
sdk.start();
```
Your probably won't need all the instrumentation inside `getNodeAutoInstrumentations()` so make sure to
You probably won't need all of the instrumentation inside `getNodeAutoInstrumentations()` so make sure to
check the [documentation](https://www.npmjs.com/package/@opentelemetry/auto-instrumentations-node) and tweak it properly.
It's important to setup the NodeSDK and the automatic instrumentation **before** importing any library.
@@ -59,31 +59,10 @@ flag when we start up the application.
In your `Dockerfile` add the `--require` flag which points to the `instrumentation.ts` file
```Dockerfile
FROM node:18-bookworm-slim
...
# More functionality goes here
...
WORKDIR /app
RUN chown node:node /app
USER node
ENV NODE_ENV production
COPY --chown=node:node .yarn ./.yarn
COPY --chown=node:node .yarnrc.yml ./
# We need the instrumentation file inside the Docker image so we can use it with --require
// highlight-add-next-line
COPY --chown=node:node packages/backend/instrumentation.ts ./
COPY --chown=node:node yarn.lock package.json packages/backend/dist/skeleton.tar.gz ./
RUN tar xzf skeleton.tar.gz && rm skeleton.tar.gz
RUN --mount=type=cache,target=/home/node/.yarn/berry/cache,sharing=locked,uid=1000,gid=1000 \
yarn workspaces focus --all --production
COPY --chown=node:node packages/backend/dist/bundle.tar.gz app-config*.yaml ./
RUN tar xzf bundle.tar.gz && rm bundle.tar.gz
COPY --chown=node:node packages/backend/instrumentation.js ./
// highlight-remove-next-line
CMD ["node", "packages/backend", "--config", "app-config.yaml"]
@@ -98,7 +77,7 @@ The above configuration will only work in production once your start a Docker co
To be able to test locally you can import the `./instrumentation.ts` file at the top (before all imports) of your backend `index.ts` file
```ts
import '../instrumentation.ts'
import '../instrumentation.js'
// Other imports
...
```
+34
View File
@@ -0,0 +1,34 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-node');
const {
getNodeAutoInstrumentations,
} = require('@opentelemetry/auto-instrumentations-node');
const {
PeriodicExportingMetricReader,
ConsoleMetricExporter,
} = require('@opentelemetry/sdk-metrics');
const sdk = new NodeSDK({
traceExporter: new ConsoleSpanExporter(),
metricReader: new PeriodicExportingMetricReader({
exporter: new ConsoleMetricExporter(),
}),
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start();