@azure/mcp-linux-arm64 2.0.0-beta.9 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE.txt +5683 -4849
- package/README.md +148 -27
- package/dist/Azure.Mcp.Tools.AzureMigrate.xml +1060 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/ActivityProcessors.md +119 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/AddApplicationInsightsTelemetry.md +129 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/AddApplicationInsightsTelemetryWorkerService.md +115 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/AddOpenTelemetry.md +153 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/ApplicationInsightsWeb.md +103 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/AzureMonitorExporter.md +137 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/ConfigureOpenTelemetryProvider.md +218 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/ConfigureResource.md +119 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/ConsoleExporter.md +47 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/EntityFrameworkInstrumentation.md +56 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/HttpInstrumentation.md +109 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/LogProcessors.md +101 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/OpenTelemetrySdkCreate.md +146 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/OtlpExporter.md +88 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/RedisInstrumentation.md +63 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/Sampling.md +86 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/SdkCreateTracerProviderBuilder.md +127 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/SqlClientInstrumentation.md +53 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/TelemetryClient.md +122 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/TelemetryConfigurationBuilder.md +173 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/UseAzureMonitor.md +96 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/UseAzureMonitorExporter.md +146 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/WithLogging.md +109 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/WithMetrics.md +105 -0
- package/dist/Instrumentation/Resources/api-reference/dotnet/WithTracing.md +91 -0
- package/dist/Instrumentation/Resources/concepts/dotnet/appinsights-aspnetcore.md +113 -0
- package/dist/Instrumentation/Resources/concepts/dotnet/aspnet-classic-appinsights.md +95 -0
- package/dist/Instrumentation/Resources/concepts/dotnet/azure-monitor-distro.md +102 -0
- package/dist/Instrumentation/Resources/concepts/dotnet/opentelemetry-pipeline.md +57 -0
- package/dist/Instrumentation/Resources/concepts/nodejs/azure-monitor-overview.md +106 -0
- package/dist/Instrumentation/Resources/concepts/nodejs/opentelemetry-pipeline.md +201 -0
- package/dist/Instrumentation/Resources/concepts/python/azure-monitor-overview.md +122 -0
- package/dist/Instrumentation/Resources/concepts/python/opentelemetry-pipeline.md +154 -0
- package/dist/Instrumentation/Resources/examples/dotnet/aspnet-classic-setup.md +80 -0
- package/dist/Instrumentation/Resources/examples/dotnet/aspnetcore-distro-setup.md +156 -0
- package/dist/Instrumentation/Resources/examples/dotnet/aspnetcore-setup.md +160 -0
- package/dist/Instrumentation/Resources/examples/dotnet/workerservice-setup.md +154 -0
- package/dist/Instrumentation/Resources/examples/nodejs/bunyan-setup.md +301 -0
- package/dist/Instrumentation/Resources/examples/nodejs/console-setup.md +284 -0
- package/dist/Instrumentation/Resources/examples/nodejs/express-setup.md +169 -0
- package/dist/Instrumentation/Resources/examples/nodejs/fastify-setup.md +237 -0
- package/dist/Instrumentation/Resources/examples/nodejs/langchain-js-setup.md +310 -0
- package/dist/Instrumentation/Resources/examples/nodejs/mongodb-setup.md +185 -0
- package/dist/Instrumentation/Resources/examples/nodejs/mysql-setup.md +231 -0
- package/dist/Instrumentation/Resources/examples/nodejs/nestjs-setup.md +184 -0
- package/dist/Instrumentation/Resources/examples/nodejs/nextjs-setup.md +320 -0
- package/dist/Instrumentation/Resources/examples/nodejs/postgres-setup.md +147 -0
- package/dist/Instrumentation/Resources/examples/nodejs/redis-setup.md +198 -0
- package/dist/Instrumentation/Resources/examples/nodejs/winston-setup.md +260 -0
- package/dist/Instrumentation/Resources/examples/python/console-setup.md +392 -0
- package/dist/Instrumentation/Resources/examples/python/django-setup.md +269 -0
- package/dist/Instrumentation/Resources/examples/python/fastapi-setup.md +256 -0
- package/dist/Instrumentation/Resources/examples/python/flask-setup.md +218 -0
- package/dist/Instrumentation/Resources/examples/python/genai-setup.md +214 -0
- package/dist/Instrumentation/Resources/examples/python/generic-setup.md +164 -0
- package/dist/Instrumentation/Resources/migration/dotnet/aad-authentication-migration.md +150 -0
- package/dist/Instrumentation/Resources/migration/dotnet/appinsights-2x-to-3x-code-migration.md +164 -0
- package/dist/Instrumentation/Resources/migration/dotnet/appinsights-2x-to-3x-no-code-change.md +92 -0
- package/dist/Instrumentation/Resources/migration/dotnet/aspnet-classic-2x-to-3x-code-migration.md +190 -0
- package/dist/Instrumentation/Resources/migration/dotnet/console-2x-to-3x-code-migration.md +106 -0
- package/dist/Instrumentation/Resources/migration/dotnet/ilogger-migration.md +54 -0
- package/dist/Instrumentation/Resources/migration/dotnet/workerservice-2x-to-3x-code-migration.md +126 -0
- package/dist/Instrumentation/Resources/migration/dotnet/workerservice-2x-to-3x-no-code-change.md +102 -0
- package/dist/azmcp +0 -0
- package/package.json +1 -1
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# Basic Azure Monitor Setup for Node.js with MongoDB
|
|
2
|
+
|
|
3
|
+
This guide shows how to add Azure Monitor OpenTelemetry to a Node.js application using MongoDB.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- Node.js 14.x or higher
|
|
8
|
+
- npm or yarn
|
|
9
|
+
- Node.js application with MongoDB (`mongodb` package)
|
|
10
|
+
- Azure Application Insights resource
|
|
11
|
+
|
|
12
|
+
## Step 1: Install Package
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @azure/monitor-opentelemetry
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Step 2: Initialize at Startup
|
|
19
|
+
|
|
20
|
+
Create or update your main entry point (typically `index.js` or `server.js`):
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
// IMPORTANT: This must be the first line, before any other imports
|
|
24
|
+
const { useAzureMonitor } = require('@azure/monitor-opentelemetry');
|
|
25
|
+
|
|
26
|
+
// Initialize Azure Monitor - MongoDB operations will be automatically instrumented
|
|
27
|
+
useAzureMonitor({
|
|
28
|
+
azureMonitorExporterOptions: {
|
|
29
|
+
connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Now load your application code
|
|
34
|
+
const express = require('express');
|
|
35
|
+
const { MongoClient } = require('mongodb');
|
|
36
|
+
|
|
37
|
+
const app = express();
|
|
38
|
+
const port = process.env.PORT || 3000;
|
|
39
|
+
|
|
40
|
+
// MongoDB connection
|
|
41
|
+
const mongoUrl = process.env.MONGODB_URL || 'mongodb://localhost:27017';
|
|
42
|
+
const dbName = process.env.MONGODB_DB || 'mydb';
|
|
43
|
+
let db;
|
|
44
|
+
|
|
45
|
+
async function connectToDatabase() {
|
|
46
|
+
const client = new MongoClient(mongoUrl);
|
|
47
|
+
await client.connect();
|
|
48
|
+
db = client.db(dbName);
|
|
49
|
+
console.log('Connected to MongoDB');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
app.use(express.json());
|
|
53
|
+
|
|
54
|
+
app.get('/api/users', async (req, res) => {
|
|
55
|
+
// This query will be automatically tracked as a dependency
|
|
56
|
+
const users = await db.collection('users').find({}).limit(10).toArray();
|
|
57
|
+
res.json(users);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
connectToDatabase().then(() => {
|
|
61
|
+
app.listen(port, () => {
|
|
62
|
+
console.log(`Server listening on port ${port}`);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Step 3: Configure Connection String
|
|
68
|
+
|
|
69
|
+
Create a `.env` file in your project root:
|
|
70
|
+
|
|
71
|
+
```env
|
|
72
|
+
APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://...
|
|
73
|
+
MONGODB_URL=mongodb://localhost:27017
|
|
74
|
+
MONGODB_DB=mydb
|
|
75
|
+
PORT=3000
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Install `dotenv` to load environment variables:
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
npm install dotenv
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Load it at the very top of your entry file:
|
|
85
|
+
|
|
86
|
+
```javascript
|
|
87
|
+
require('dotenv').config();
|
|
88
|
+
const { useAzureMonitor } = require('@azure/monitor-opentelemetry');
|
|
89
|
+
// ... rest of code
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## What Gets Instrumented Automatically
|
|
93
|
+
|
|
94
|
+
With Azure Monitor OpenTelemetry, the following MongoDB operations are automatically tracked:
|
|
95
|
+
|
|
96
|
+
- **Operations**: find, insert, update, delete, aggregate, etc.
|
|
97
|
+
- **Operation duration**: Time taken for each database operation
|
|
98
|
+
- **Collection name**: Which collection was accessed
|
|
99
|
+
- **Database name**: Which database was used
|
|
100
|
+
- **Success/failure**: Operation status
|
|
101
|
+
|
|
102
|
+
## Step 4: Add Custom Telemetry (Optional)
|
|
103
|
+
|
|
104
|
+
```javascript
|
|
105
|
+
const { trace } = require('@opentelemetry/api');
|
|
106
|
+
|
|
107
|
+
app.post('/api/users', async (req, res) => {
|
|
108
|
+
const tracer = trace.getTracer('my-app');
|
|
109
|
+
|
|
110
|
+
await tracer.startActiveSpan('create-user', async (span) => {
|
|
111
|
+
try {
|
|
112
|
+
span.setAttribute('user.email', req.body.email);
|
|
113
|
+
|
|
114
|
+
const result = await db.collection('users').insertOne({
|
|
115
|
+
name: req.body.name,
|
|
116
|
+
email: req.body.email,
|
|
117
|
+
createdAt: new Date()
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
span.setAttribute('user.id', result.insertedId.toString());
|
|
121
|
+
res.status(201).json({ _id: result.insertedId, ...req.body });
|
|
122
|
+
} catch (error) {
|
|
123
|
+
span.recordException(error);
|
|
124
|
+
span.setStatus({ code: 2, message: error.message });
|
|
125
|
+
res.status(500).json({ error: 'Database error' });
|
|
126
|
+
} finally {
|
|
127
|
+
span.end();
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Using with Mongoose
|
|
134
|
+
|
|
135
|
+
If you're using Mongoose ODM, the setup is the same:
|
|
136
|
+
|
|
137
|
+
```javascript
|
|
138
|
+
require('dotenv').config();
|
|
139
|
+
const { useAzureMonitor } = require('@azure/monitor-opentelemetry');
|
|
140
|
+
|
|
141
|
+
useAzureMonitor({
|
|
142
|
+
azureMonitorExporterOptions: {
|
|
143
|
+
connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const mongoose = require('mongoose');
|
|
148
|
+
const express = require('express');
|
|
149
|
+
|
|
150
|
+
// Mongoose operations will automatically be instrumented
|
|
151
|
+
mongoose.connect(process.env.MONGODB_URL);
|
|
152
|
+
|
|
153
|
+
const UserSchema = new mongoose.Schema({
|
|
154
|
+
name: String,
|
|
155
|
+
email: String
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const User = mongoose.model('User', UserSchema);
|
|
159
|
+
|
|
160
|
+
const app = express();
|
|
161
|
+
|
|
162
|
+
app.get('/api/users', async (req, res) => {
|
|
163
|
+
const users = await User.find().limit(10);
|
|
164
|
+
res.json(users);
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Viewing Telemetry in Azure Portal
|
|
169
|
+
|
|
170
|
+
1. Open your Application Insights resource in Azure Portal
|
|
171
|
+
2. Navigate to "Application Map" to see MongoDB as a dependency
|
|
172
|
+
3. Use "Transaction search" to find specific database operations
|
|
173
|
+
4. Check "Dependencies" under "Investigate" to see operation performance
|
|
174
|
+
|
|
175
|
+
## Troubleshooting
|
|
176
|
+
|
|
177
|
+
### MongoDB operations not appearing
|
|
178
|
+
|
|
179
|
+
1. Ensure `useAzureMonitor()` is called **before** importing `mongodb` or `mongoose`
|
|
180
|
+
2. Verify the connection string is set correctly
|
|
181
|
+
3. Check that operations are being executed (not just connections)
|
|
182
|
+
|
|
183
|
+
### High cardinality in telemetry
|
|
184
|
+
|
|
185
|
+
MongoDB instrumentation captures collection names. For applications with dynamic collections, consider using a consistent naming pattern.
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
# Basic Azure Monitor Setup for Node.js with MySQL
|
|
2
|
+
|
|
3
|
+
This guide shows how to add Azure Monitor OpenTelemetry to a Node.js application using MySQL.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- Node.js 14.x or higher
|
|
8
|
+
- npm or yarn
|
|
9
|
+
- Node.js application with MySQL (`mysql2` package)
|
|
10
|
+
- Azure Application Insights resource
|
|
11
|
+
|
|
12
|
+
## Step 1: Install Package
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @azure/monitor-opentelemetry
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Step 2: Initialize at Startup
|
|
19
|
+
|
|
20
|
+
Create or update your main entry point (typically `index.js` or `server.js`):
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
// IMPORTANT: This must be the first line, before any other imports
|
|
24
|
+
const { useAzureMonitor } = require('@azure/monitor-opentelemetry');
|
|
25
|
+
|
|
26
|
+
// Initialize Azure Monitor - MySQL queries will be automatically instrumented
|
|
27
|
+
useAzureMonitor({
|
|
28
|
+
azureMonitorExporterOptions: {
|
|
29
|
+
connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Now load your application code
|
|
34
|
+
const express = require('express');
|
|
35
|
+
const mysql = require('mysql2/promise');
|
|
36
|
+
|
|
37
|
+
const app = express();
|
|
38
|
+
const port = process.env.PORT || 3000;
|
|
39
|
+
|
|
40
|
+
// MySQL connection pool
|
|
41
|
+
let pool;
|
|
42
|
+
|
|
43
|
+
async function createPool() {
|
|
44
|
+
pool = mysql.createPool({
|
|
45
|
+
host: process.env.MYSQL_HOST || 'localhost',
|
|
46
|
+
port: process.env.MYSQL_PORT || 3306,
|
|
47
|
+
user: process.env.MYSQL_USER || 'root',
|
|
48
|
+
password: process.env.MYSQL_PASSWORD || '',
|
|
49
|
+
database: process.env.MYSQL_DATABASE || 'mydb',
|
|
50
|
+
waitForConnections: true,
|
|
51
|
+
connectionLimit: 10
|
|
52
|
+
});
|
|
53
|
+
console.log('MySQL connection pool created');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
app.use(express.json());
|
|
57
|
+
|
|
58
|
+
app.get('/api/users', async (req, res) => {
|
|
59
|
+
// This query will be automatically tracked as a dependency
|
|
60
|
+
const [rows] = await pool.execute('SELECT * FROM users LIMIT 10');
|
|
61
|
+
res.json(rows);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
createPool().then(() => {
|
|
65
|
+
app.listen(port, () => {
|
|
66
|
+
console.log(`Server listening on port ${port}`);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Step 3: Configure Connection String
|
|
72
|
+
|
|
73
|
+
Create a `.env` file in your project root:
|
|
74
|
+
|
|
75
|
+
```env
|
|
76
|
+
APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://...
|
|
77
|
+
MYSQL_HOST=localhost
|
|
78
|
+
MYSQL_PORT=3306
|
|
79
|
+
MYSQL_USER=root
|
|
80
|
+
MYSQL_PASSWORD=your_password
|
|
81
|
+
MYSQL_DATABASE=mydb
|
|
82
|
+
PORT=3000
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Install `dotenv` to load environment variables:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
npm install dotenv
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Load it at the very top of your entry file:
|
|
92
|
+
|
|
93
|
+
```javascript
|
|
94
|
+
require('dotenv').config();
|
|
95
|
+
const { useAzureMonitor } = require('@azure/monitor-opentelemetry');
|
|
96
|
+
// ... rest of code
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## What Gets Instrumented Automatically
|
|
100
|
+
|
|
101
|
+
With Azure Monitor OpenTelemetry, the following MySQL operations are automatically tracked:
|
|
102
|
+
|
|
103
|
+
- **Queries**: All SQL queries executed via `mysql2` client
|
|
104
|
+
- **Query duration**: Time taken for each database operation
|
|
105
|
+
- **Query results**: Success/failure status
|
|
106
|
+
- **Connection info**: Database name and server details
|
|
107
|
+
|
|
108
|
+
## Step 4: Add Custom Telemetry (Optional)
|
|
109
|
+
|
|
110
|
+
```javascript
|
|
111
|
+
const { trace } = require('@opentelemetry/api');
|
|
112
|
+
|
|
113
|
+
app.post('/api/users', async (req, res) => {
|
|
114
|
+
const tracer = trace.getTracer('my-app');
|
|
115
|
+
|
|
116
|
+
await tracer.startActiveSpan('create-user', async (span) => {
|
|
117
|
+
try {
|
|
118
|
+
span.setAttribute('user.email', req.body.email);
|
|
119
|
+
|
|
120
|
+
const [result] = await pool.execute(
|
|
121
|
+
'INSERT INTO users (name, email) VALUES (?, ?)',
|
|
122
|
+
[req.body.name, req.body.email]
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
span.setAttribute('user.id', result.insertId);
|
|
126
|
+
res.status(201).json({ id: result.insertId, ...req.body });
|
|
127
|
+
} catch (error) {
|
|
128
|
+
span.recordException(error);
|
|
129
|
+
span.setStatus({ code: 2, message: error.message });
|
|
130
|
+
res.status(500).json({ error: 'Database error' });
|
|
131
|
+
} finally {
|
|
132
|
+
span.end();
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Using with Azure Database for MySQL
|
|
139
|
+
|
|
140
|
+
For Azure Database for MySQL, update your configuration:
|
|
141
|
+
|
|
142
|
+
```env
|
|
143
|
+
MYSQL_HOST=your-server.mysql.database.azure.com
|
|
144
|
+
MYSQL_PORT=3306
|
|
145
|
+
MYSQL_USER=your_user@your-server
|
|
146
|
+
MYSQL_PASSWORD=your_password
|
|
147
|
+
MYSQL_DATABASE=mydb
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
And enable SSL in your connection:
|
|
151
|
+
|
|
152
|
+
```javascript
|
|
153
|
+
const pool = mysql.createPool({
|
|
154
|
+
host: process.env.MYSQL_HOST,
|
|
155
|
+
port: process.env.MYSQL_PORT,
|
|
156
|
+
user: process.env.MYSQL_USER,
|
|
157
|
+
password: process.env.MYSQL_PASSWORD,
|
|
158
|
+
database: process.env.MYSQL_DATABASE,
|
|
159
|
+
ssl: {
|
|
160
|
+
rejectUnauthorized: true
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Using with Sequelize ORM
|
|
166
|
+
|
|
167
|
+
If you're using Sequelize, the setup is similar:
|
|
168
|
+
|
|
169
|
+
```javascript
|
|
170
|
+
require('dotenv').config();
|
|
171
|
+
const { useAzureMonitor } = require('@azure/monitor-opentelemetry');
|
|
172
|
+
|
|
173
|
+
useAzureMonitor({
|
|
174
|
+
azureMonitorExporterOptions: {
|
|
175
|
+
connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
const { Sequelize, DataTypes } = require('sequelize');
|
|
180
|
+
const express = require('express');
|
|
181
|
+
|
|
182
|
+
const sequelize = new Sequelize(
|
|
183
|
+
process.env.MYSQL_DATABASE,
|
|
184
|
+
process.env.MYSQL_USER,
|
|
185
|
+
process.env.MYSQL_PASSWORD,
|
|
186
|
+
{
|
|
187
|
+
host: process.env.MYSQL_HOST,
|
|
188
|
+
dialect: 'mysql'
|
|
189
|
+
}
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
const User = sequelize.define('User', {
|
|
193
|
+
name: DataTypes.STRING,
|
|
194
|
+
email: DataTypes.STRING
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const app = express();
|
|
198
|
+
|
|
199
|
+
app.get('/api/users', async (req, res) => {
|
|
200
|
+
const users = await User.findAll({ limit: 10 });
|
|
201
|
+
res.json(users);
|
|
202
|
+
});
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## Viewing Telemetry in Azure Portal
|
|
206
|
+
|
|
207
|
+
1. Open your Application Insights resource in Azure Portal
|
|
208
|
+
2. Navigate to "Application Map" to see MySQL as a dependency
|
|
209
|
+
3. Use "Transaction search" to find specific database operations
|
|
210
|
+
4. Check "Dependencies" under "Investigate" to see query performance
|
|
211
|
+
|
|
212
|
+
## Troubleshooting
|
|
213
|
+
|
|
214
|
+
### MySQL queries not appearing
|
|
215
|
+
|
|
216
|
+
1. Ensure `useAzureMonitor()` is called **before** importing `mysql2`
|
|
217
|
+
2. Verify the connection string is set correctly
|
|
218
|
+
3. Check that queries are being executed (not just connections)
|
|
219
|
+
|
|
220
|
+
### High latency in telemetry
|
|
221
|
+
|
|
222
|
+
MySQL instrumentation captures all queries. For high-throughput applications, consider:
|
|
223
|
+
|
|
224
|
+
```javascript
|
|
225
|
+
useAzureMonitor({
|
|
226
|
+
azureMonitorExporterOptions: {
|
|
227
|
+
connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
|
|
228
|
+
},
|
|
229
|
+
samplingRatio: 0.5 // Sample 50% of requests
|
|
230
|
+
});
|
|
231
|
+
```
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# Basic Azure Monitor Setup for NestJS
|
|
2
|
+
|
|
3
|
+
This guide shows how to add Azure Monitor OpenTelemetry to a NestJS application.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- Node.js 18.x or higher
|
|
8
|
+
- npm or yarn
|
|
9
|
+
- NestJS application
|
|
10
|
+
- Azure Application Insights resource
|
|
11
|
+
|
|
12
|
+
## Step 1: Install Package
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @azure/monitor-opentelemetry
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Step 2: Create Tracing File
|
|
19
|
+
|
|
20
|
+
Create a new file `src/tracing.ts` for OpenTelemetry initialization:
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { useAzureMonitor } from '@azure/monitor-opentelemetry';
|
|
24
|
+
|
|
25
|
+
// Enable Azure Monitor integration
|
|
26
|
+
// This must be called before any other imports to ensure proper instrumentation
|
|
27
|
+
useAzureMonitor({
|
|
28
|
+
azureMonitorExporterOptions: {
|
|
29
|
+
connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Step 3: Import Tracing in main.ts
|
|
35
|
+
|
|
36
|
+
Update your `src/main.ts` to import tracing **as the very first line**:
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import './tracing'; // MUST be the first import
|
|
40
|
+
|
|
41
|
+
import { NestFactory } from '@nestjs/core';
|
|
42
|
+
import { AppModule } from './app.module';
|
|
43
|
+
|
|
44
|
+
async function bootstrap() {
|
|
45
|
+
const app = await NestFactory.create(AppModule);
|
|
46
|
+
await app.listen(3000);
|
|
47
|
+
}
|
|
48
|
+
bootstrap();
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
> **Important**: The tracing import must be before all other imports to ensure proper instrumentation of HTTP modules and other dependencies.
|
|
52
|
+
|
|
53
|
+
## Step 4: Configure Connection String
|
|
54
|
+
|
|
55
|
+
Create a `.env` file in your project root:
|
|
56
|
+
|
|
57
|
+
```env
|
|
58
|
+
APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://...
|
|
59
|
+
PORT=3000
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Install and configure `@nestjs/config` for environment variables:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
npm install @nestjs/config
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Update `app.module.ts`:
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import { Module } from '@nestjs/common';
|
|
72
|
+
import { ConfigModule } from '@nestjs/config';
|
|
73
|
+
|
|
74
|
+
@Module({
|
|
75
|
+
imports: [
|
|
76
|
+
ConfigModule.forRoot({
|
|
77
|
+
isGlobal: true,
|
|
78
|
+
}),
|
|
79
|
+
],
|
|
80
|
+
// ... your other modules
|
|
81
|
+
})
|
|
82
|
+
export class AppModule {}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Step 5: Add Custom Telemetry (Optional)
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import { Controller, Get, Param } from '@nestjs/common';
|
|
89
|
+
import { trace } from '@opentelemetry/api';
|
|
90
|
+
|
|
91
|
+
@Controller('users')
|
|
92
|
+
export class UsersController {
|
|
93
|
+
@Get(':id')
|
|
94
|
+
async findOne(@Param('id') id: string) {
|
|
95
|
+
const span = trace.getActiveSpan();
|
|
96
|
+
|
|
97
|
+
// Add custom attributes to the current span
|
|
98
|
+
span?.setAttribute('user.id', id);
|
|
99
|
+
span?.setAttribute('operation.type', 'user-lookup');
|
|
100
|
+
|
|
101
|
+
try {
|
|
102
|
+
const user = await this.userService.findById(id);
|
|
103
|
+
return user;
|
|
104
|
+
} catch (error) {
|
|
105
|
+
// Exceptions are automatically tracked
|
|
106
|
+
span?.recordException(error);
|
|
107
|
+
throw error;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## What Gets Tracked Automatically
|
|
114
|
+
|
|
115
|
+
✅ **HTTP Requests**: All incoming requests to your NestJS controllers
|
|
116
|
+
✅ **Dependencies**: Outgoing HTTP calls, database queries (TypeORM, Prisma, etc.)
|
|
117
|
+
✅ **Exceptions**: Unhandled errors and NestJS exceptions
|
|
118
|
+
✅ **Performance**: Response times, request counts, and latency
|
|
119
|
+
✅ **Custom Logs**: Console statements are captured as traces
|
|
120
|
+
|
|
121
|
+
## Verify It Works
|
|
122
|
+
|
|
123
|
+
1. Start your application:
|
|
124
|
+
```bash
|
|
125
|
+
npm run start:dev
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
2. Make some HTTP requests:
|
|
129
|
+
```bash
|
|
130
|
+
curl http://localhost:3000/
|
|
131
|
+
curl http://localhost:3000/users/1
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
3. Check Azure Portal:
|
|
135
|
+
- Navigate to your Application Insights resource
|
|
136
|
+
- Go to "Transaction search" or "Live Metrics"
|
|
137
|
+
- You should see requests appearing within 1-2 minutes
|
|
138
|
+
|
|
139
|
+
## Complete package.json Example
|
|
140
|
+
|
|
141
|
+
```json
|
|
142
|
+
{
|
|
143
|
+
"name": "nestjs-azure-monitor-demo",
|
|
144
|
+
"version": "1.0.0",
|
|
145
|
+
"scripts": {
|
|
146
|
+
"build": "nest build",
|
|
147
|
+
"start": "nest start",
|
|
148
|
+
"start:dev": "nest start --watch",
|
|
149
|
+
"start:prod": "node dist/main"
|
|
150
|
+
},
|
|
151
|
+
"dependencies": {
|
|
152
|
+
"@azure/monitor-opentelemetry": "^1.0.0",
|
|
153
|
+
"@nestjs/common": "^10.0.0",
|
|
154
|
+
"@nestjs/config": "^3.0.0",
|
|
155
|
+
"@nestjs/core": "^10.0.0",
|
|
156
|
+
"@nestjs/platform-express": "^10.0.0",
|
|
157
|
+
"reflect-metadata": "^0.1.13",
|
|
158
|
+
"rxjs": "^7.8.0"
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Troubleshooting
|
|
164
|
+
|
|
165
|
+
**No telemetry appearing?**
|
|
166
|
+
- Verify the tracing import is the FIRST line in `main.ts`
|
|
167
|
+
- Check that connection string is correct
|
|
168
|
+
- Ensure environment variables are loaded before `useAzureMonitor()` is called
|
|
169
|
+
- Wait 2-3 minutes for initial data to appear
|
|
170
|
+
|
|
171
|
+
**TypeScript compilation errors?**
|
|
172
|
+
- Ensure `@types/node` is installed
|
|
173
|
+
- Add `"esModuleInterop": true` to tsconfig.json if needed
|
|
174
|
+
|
|
175
|
+
**Performance impact?**
|
|
176
|
+
- Azure Monitor has minimal overhead (<5% in most cases)
|
|
177
|
+
- Use sampling for high-traffic applications
|
|
178
|
+
|
|
179
|
+
## Next Steps
|
|
180
|
+
|
|
181
|
+
- Configure custom dimensions and metrics
|
|
182
|
+
- Set up alerts and dashboards in Azure Portal
|
|
183
|
+
- Enable distributed tracing across microservices
|
|
184
|
+
- Add interceptors for custom span attributes
|