Skip to content

Commit

Permalink
Merged PR 28496: Azure Monitor tracing for waste-tracking-gateway
Browse files Browse the repository at this point in the history
Azure Monitor tracing for waste-tracking-gateway

Added Application Insights tracing to _waste-tracking-gateway_ via [OpenTelemetry](https://opentelemetry.io/).

 - Downgraded Hapi from v21 to v20 because unfortunately instrumentation is [not yet provided for v21](open-telemetry/opentelemetry-js-contrib#1383). If this becomes a problem, we can forgo the Hapi layer and rely on just HTTP instrumentation.
 - Traces will be written to console in development.
 - Setting `APPINSIGHTS_CONNECTION_STRING` emits traces to Azure Monitor.
 - This required an additional _tracing.js_ entry point as instrumentation must be loaded first.
  • Loading branch information
iwalters committed Apr 11, 2023
1 parent dd05238 commit 21c6fed
Show file tree
Hide file tree
Showing 6 changed files with 1,831 additions and 535 deletions.
1 change: 1 addition & 0 deletions apps/waste-tracking-gateway/.env.build.production
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NODE_ENV=production
9 changes: 5 additions & 4 deletions apps/waste-tracking-gateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
FROM node:18-alpine

ENV NODE_ENV production
ENV PORT 3000
ENV NODE_ENV=production \
PORT=3000

WORKDIR /app
COPY --chown=node:node dist/apps/waste-tracking-gateway/main.js ./main.js
COPY --chown=node:node dist/apps/waste-tracking-gateway/tracing.js ./tracing.js
COPY --chown=node:node dist/apps/waste-tracking-gateway/package*.json ./

RUN npm ci

USER node
EXPOSE 3000
EXPOSE ${PORT}

CMD [ "main.js" ]
CMD [ "--require", "./tracing.js", "main.js" ]
14 changes: 12 additions & 2 deletions apps/waste-tracking-gateway/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
"assets": ["apps/waste-tracking-gateway/src/assets"],
"isolatedConfig": true,
"webpackConfig": "apps/waste-tracking-gateway/webpack.config.js",
"generatePackageJson": true
"generatePackageJson": true,
"additionalEntryPoints": [
{
"entryName": "tracing",
"entryPath": "apps/waste-tracking-gateway/src/tracing.ts"
}
]
},
"configurations": {
"development": {},
Expand All @@ -28,7 +34,11 @@
"executor": "@nrwl/js:node",
"defaultConfiguration": "development",
"options": {
"buildTarget": "waste-tracking-gateway:build"
"buildTarget": "waste-tracking-gateway:build",
"runtimeArgs": [
"--require",
"./dist/apps/waste-tracking-gateway/tracing.js"
]
},
"configurations": {
"development": {
Expand Down
36 changes: 36 additions & 0 deletions apps/waste-tracking-gateway/src/tracing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { AzureMonitorTraceExporter } from '@azure/monitor-opentelemetry-exporter';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { HapiInstrumentation } from '@opentelemetry/instrumentation-hapi';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { Resource } from '@opentelemetry/resources';
import {
BatchSpanProcessor,
ConsoleSpanExporter,
} from '@opentelemetry/sdk-trace-base';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';

const provider = new NodeTracerProvider({
resource: Resource.default().merge(
new Resource({
[SemanticResourceAttributes.SERVICE_NAME]:
process.env['APP_ID'] || 'waste-tracking-gateway',
})
),
});

if (process.env['NODE_ENV'] === 'development') {
provider.addSpanProcessor(new BatchSpanProcessor(new ConsoleSpanExporter()));
}

const connectionString = process.env['APPINSIGHTS_CONNECTION_STRING'];
if (connectionString !== undefined) {
provider.addSpanProcessor(
new BatchSpanProcessor(new AzureMonitorTraceExporter({ connectionString }))
);
}

provider.register();
registerInstrumentations({
instrumentations: [new HttpInstrumentation(), new HapiInstrumentation()],
});
Loading

0 comments on commit 21c6fed

Please sign in to comment.