Skip to content
New issue

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

ApplicationInsights setAutoCollectRequests not logging #1417

Open
sajidhashmi opened this issue Mar 3, 2025 · 1 comment
Open

ApplicationInsights setAutoCollectRequests not logging #1417

sajidhashmi opened this issue Mar 3, 2025 · 1 comment

Comments

@sajidhashmi
Copy link

sajidhashmi commented Mar 3, 2025

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?

@JacksonWeber
Copy link
Contributor

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants