问题
I have configured my .net web application with azure portal using application insights. Now what I want is that the details that are shown in the portal should be displayed on my web page of my .net web app. I don't know hoe to do it and want someone to help me in this regard. T am also sharing the snapshot of what actually I want to fetch from my azure portal, to display as a grid report on my application's web page. [
回答1:
There's an API for retrieving data from Application Insights. From the documentation you can "Query and integrate with the performance, availability and usage data collected by Application Insights for your application" and "Access all your app's event and metric data with a powerful and simple REST API".
Some sample requests:
Return the total number of requests in the last day (timespan=P1D):
GET /v1/apps/DEMO_APP/metrics/requests/count?timespan=P1D HTTP/1.1
Average server response time per hour (interval=PT1H) for the last 6 hours:
GET /v1/apps/{app-id}/metrics/requests/duration?timespan=PT6H&interval=PT1H
List the last 5 events:
GET /v1/apps/{app-id}/events/$all?$top=5
List GET requests which failed or took longer than 0.5 seconds:
GET /v1/apps/{app-id}/events/requests?$filter=startswith(request/name, 'GET') and (request/resultCode ne '200' or request/duration gt 500)
More info here:
https://dev.applicationinsights.io/
EDIT:
Below is how you add Application Insights:
- Click on your App Service and then in the Overview panel you should see a link for Application Insights:
- After adding this click on the App service again and select Application Insights in the Settings section, then scroll over and select API Access:
- Click Create API key
EDIT 2:
Ok once you create the API key you can use this along with your application ID to return data from Application Insights. You can do this using the public API which has this format:
https://api.applicationinsights.io/{version}/apps/{app-id}/{operation}/[path]?[parameters] X-API-Key:{key}
You can use this page to query the data or you can use something like Postman or cUrl:
https://dev.applicationinsights.io/apiexplorer/metrics
You'll need to provide your application ID which you'll find in your Azure portal and the API key you generated. Below is an example GET request that gets the total number of requests for the last 30 days:
GET /v1/apps/{yourApplicationId}/metrics/requests/count?timespan=P30D HTTP/1.1
Host: api.applicationinsights.io
x-api-key: {yourAPIKey}
When you have the API call working correctly you can use whatever client you want to retrieve the data e.g. Angular, jQuery, C# HttpClient, etc.
EDIT 3:
Ok so here's a basic but complete html page to retrieve app insight data from your application. All you need to do is replace "{applicationId}" and "{api-key}" with your values. You get these in the azure portal - click on the "Application Insights" and "API Access" sections as shown in the previous screen shots.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>App Insights Sample</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
function getAppInsightsData() {
const userAction = async () => {
const url = 'https://api.applicationinsights.io/v1/apps/{applicationId}/metrics/requests/count?timespan=P30D';
const myHeaders = { headers: new Headers({
'x-api-key': '{api-key}'
})
}
const response = await fetch(url, myHeaders);
const myJson = await response.json();
document.getElementById('p1').innerHTML = 'Date Range: ' + myJson.value.start + ' to ' + myJson.value.end + '. Requests: ' + myJson.value["requests/count"].sum;
}
userAction();
}
</script>
</head>
<body>
<button type="submit" onclick="javascript:getAppInsightsData()">Get data using fetch</button>
<div id='div'>
<p id='p1'></p>
</div>
</body>
</html>
来源:https://stackoverflow.com/questions/51761528/fetching-data-from-application-insights-azure-portal-to-display-it-on-a-web-pa