We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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", }, }); });
${req.method} ${req.url}
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}); });
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" } }
{ "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?
The text was updated successfully, but these errors were encountered:
@sajidhashmi As per https://github.com/microsoft/ApplicationInsights-node.js?tab=readme-ov-file#get-started it's important to initialize the SDK as soon as possible. If you initialize after importing express it will not instrument the express package (hence why you're not seeing any auto-collected requests.
express
Sorry, something went wrong.
No branches or pull requests
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",
},
});
});
});
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?
The text was updated successfully, but these errors were encountered: