Description
setAutoCollectRequests is not logging on applicationInsights until not manually triggered.
My main.ts looks like below:
import * as appInsights from "applicationinsights";
import express from "express";
import * as dotenv from "dotenv";
const CONNECTION_STRING = conn_string;
if (!CONNECTION_STRING) {
console.error("Application Insights connection string is missing.");
process.exit(1);
}
dotenv.config();
// Initialize Application Insights
appInsights.setup(CONNECTION_STRING)
.setAutoCollectRequests(true)
.setAutoCollectPerformance(true, true)
.setAutoCollectExceptions(true)
.setSendLiveMetrics(true)
.setInternalLogging(true, true)
.start();
const client = appInsights.defaultClient;
const app = express();
const PORT = 3000;
// Middleware to log requests manually (if auto-collection fails)
app.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
const startTime = Date.now();
res.on("finish", () => {
const duration = Date.now() - startTime;
client.trackRequest({
name: ${req.method} ${req.url}
,
url: req.url,
duration,
resultCode: res.statusCode.toString(),
success: res.statusCode < 400,
properties: {
method: req.method,
userAgent: req.headers["user-agent"] || "unknown",
},
});
});
next();
});
app.get("/", (req: express.Request, res: express.Response) => {
res.send("Hello, World!");
client.trackTrace({ message: "Home page accessed" });
});
app.get("/error", (req: express.Request, res: express.Response) => {
try {
throw new Error("Test error");
} catch (error) {
client.trackException({ exception: error as Error });
res.status(500).send("An error occurred");
}
});
app.listen(PORT, () => {
console.log(Server running on port ${PORT}
);
});
package.json
{ "dependencies": { "@azure/core-tracing": "^1.2.0", "@azure/opentelemetry-instrumentation-azure-sdk": "^1.0.0-beta.8", "applicationinsights": "^3.5.0", "dotenv": "^16.4.7", "express": "^5.0.1" }, "devDependencies": { "@types/express": "^5.0.0", "@types/node": "^22.13.5", "typescript": "^5.7.3" } }
Am I doing anything wrong or is there a bug?