You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否通过Web服务器转发Application Insights客户端遥测数据?

Absolutely, this is totally feasible and a go-to pattern for scenarios where client devices don't have direct internet access—perfect fit for your setup! Let’s walk through how to make this work step by step:

Step 1: Configure the Client SDK to Send Data to Your Web Server

By default, the Application Insights client SDK sends telemetry directly to Azure's endpoints. You’ll need to override this behavior to point it to your web server’s custom forwarding endpoint instead.

Here’s an example for the JavaScript client SDK:

var appInsights = window.appInsights || function(config){
    function s(config){ t[config] = function(){ var i = arguments; t.queue.push(function(){ t[config].apply(t, i)})} }var t = { config:config}, r = document, f = window, e = "script", o = r.createElement(e), i, u; for(o.src=config.url||"https://az416426.vo.msecnd.net/scripts/b/ai.2.min.js", r.getElementsByTagName(e)[0].parentNode.appendChild(o), t.cookie = r.cookie, t.queue = [], i = ["Event","Exception","Metric","PageView","Trace"];i.length;)s("track"+i.pop());return config.disableExceptionTracking||(i="onerror",s("_"+i),u=f[i],f[i]=function(config, r, f, e, o){var s = u&&u(config, r, f, e, o);return s!==!0&&t["_"+i](config, r, f, e, o),s}),t
}({
    instrumentationKey: "YOUR_INSTRUMENTATION_KEY",
    endpointUrl: "https://your-web-server.com/api/forward-telemetry" // Replace with your server's endpoint
});

window.appInsights = appInsights;
appInsights.trackPageView();

For other client SDKs (like .NET, Java), look for configuration options to set a custom ingestion endpoint—most SDKs support this via a setting similar to EndpointUri.

Step 2: Build the Forwarding Endpoint on Your Web Server

Next, create an API endpoint on your web server that receives telemetry from clients, then forwards it to Azure’s Application Insights ingestion endpoint.

Here’s an example using ASP.NET Core:

[ApiController]
[Route("api/[controller]")]
public class ForwardTelemetryController : ControllerBase
{
    private readonly HttpClient _httpClient;
    // Use the correct Azure ingestion endpoint for your region (this is the global default)
    private const string AppInsightsIngestionEndpoint = "https://dc.services.visualstudio.com/v2/track";

    public ForwardTelemetryController(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }

    [HttpPost]
    public async Task<IActionResult> Post()
    {
        // Optional: Add validation here to ensure requests come from trusted clients
        // Read the raw telemetry data from the client request
        using var requestStream = new MemoryStream();
        await Request.Body.CopyToAsync(requestStream);
        requestStream.Position = 0;

        // Forward the data to Application Insights
        var forwardResponse = await _httpClient.PostAsync(
            AppInsightsIngestionEndpoint, 
            new StreamContent(requestStream)
        );

        // Pass the response from Application Insights back to the client
        return new HttpResponseMessageResult(forwardResponse);
    }
}

// Helper class to convert HttpResponseMessage to an IActionResult
public class HttpResponseMessageResult : IActionResult
{
    private readonly HttpResponseMessage _response;

    public HttpResponseMessageResult(HttpResponseMessage response)
    {
        _response = response;
    }

    public async Task ExecuteResultAsync(ActionContext context)
    {
        var httpResponse = context.HttpContext.Response;
        httpResponse.StatusCode = (int)_response.StatusCode;

        foreach (var header in _response.Headers)
        {
            httpResponse.Headers.TryAdd(header.Key, new Microsoft.Extensions.Primitives.StringValues(header.Value.ToArray()));
        }

        foreach (var header in _response.Content.Headers)
        {
            httpResponse.Headers.TryAdd(header.Key, new Microsoft.Extensions.Primitives.StringValues(header.Value.ToArray()));
        }

        await _response.Content.CopyToAsync(httpResponse.Body);
    }
}

Key Considerations

  • Network Access: Ensure your web server has outbound access to Azure’s Application Insights ingestion endpoint (adjust the endpoint URL if you’re using a regional Azure cloud).
  • Validation & Security: Add checks in your forwarding endpoint to verify requests come from legitimate clients (e.g., API keys, IP whitelisting) to prevent abuse.
  • Reliability: Implement retry logic for failed forwarding requests, or use a queue to buffer telemetry data if Azure is temporarily unavailable—this prevents data loss.
  • Telemetry Enrichment: You can add server-side context to the telemetry (e.g., server ID, network details) before forwarding it to Azure, giving you richer insights.

内容的提问来源于stack exchange,提问作者LuqJensen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:06:01