如何在Java Function App的Application Insights中跟踪出站REST调用
Java Azure Function Application Insights 出站HTTP请求采集与链路关联方案
默认绑定Application Insights的Java HTTP触发型Function App,仅会自动采集入站HTTP请求遥测,代码主动发起的出站HTTP调用不会自动落库,也不会默认和上层调用做链路关联,以下是针对Java栈的可落地操作指引:
1 基础配置校验(必须先做)
- 检查项目
pom.xml中引入的applicationinsights-agent-azure-function依赖版本不低于3.4.0,低版本对Java HTTP客户端的自动插桩支持存在缺失。 - 在Function App的配置页确认两个关键配置:
APPLICATIONINSIGHTS_CONNECTION_STRING填写为目标Application Insights资源的连接字符串,不要使用已废弃的instrumentation key单独配置APPLICATIONINSIGHTS_ENABLE_AGENT值设为true,这是Java版自动插桩的总开关,漏开的话所有自动采集逻辑都不会生效
- 配置修改后重启Function App生效。
2 无代码侵入自动采集方案(优先推荐)
只要完成上述基础配置,使用以下常用HTTP客户端发起的出站调用,会自动被识别为HTTP类型依赖项上报,且自动和当前触发函数的入站请求做trace链路关联,不需要写任何埋点代码:
- JDK原生
java.net.HttpURLConnection - Apache HttpClient 4.x、5.x版本
- OkHttp 3.x、4.x版本
- Java 11+ 原生
java.net.http.HttpClient - Spring WebClient(基于Reactor Netty实现的异步客户端)
注意:如果业务代码中手动清理了线程上下文MDC变量,或者使用自定义线程池异步发起调用时没做上下文传播,会导致关联失效。这种场景下自定义线程池需要用Agent提供的线程上下文包装类做一层包装,即可自动透传trace信息。
3 自定义HTTP客户端手动埋点方案
如果使用的HTTP客户端不在上述自动支持列表中,可以通过Application Insights Java SDK手动上报依赖遥测,步骤如下:
- 在
pom.xml中引入核心SDK依赖:
<dependency> <groupId>com.microsoft.azure</groupId> <artifactId>applicationinsights-core</artifactId> <version>3.4.19</version> </dependency>
- 代码中全局初始化一次
TelemetryClient实例(不要每次请求都新建,避免资源浪费),在发起HTTP调用的逻辑块中上报依赖遥测,示例代码如下:
import com.microsoft.applicationinsights.TelemetryClient; import com.microsoft.applicationinsights.telemetry.DependencyTelemetry; import com.microsoft.azure.functions.ExecutionContext; import com.microsoft.azure.functions.HttpMethod; import com.microsoft.azure.functions.HttpRequestMessage; import com.microsoft.azure.functions.HttpResponseMessage; import com.microsoft.azure.functions.HttpStatus; import com.microsoft.azure.functions.annotation.AuthorizationLevel; import com.microsoft.azure.functions.annotation.FunctionName; import com.microsoft.azure.functions.annotation.HttpTrigger; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.time.Duration; import java.time.Instant; import java.util.Optional; public class MyHttpTrigger { // 全局初始化单例 private static final TelemetryClient TELEMETRY_CLIENT = new TelemetryClient(); @FunctionName("thirdPartyCallFunction") public HttpResponseMessage run( @HttpTrigger(name = "req", methods = {HttpMethod.GET}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, final ExecutionContext context) { String thirdPartyApi = "https://api.third-party.com/business/data"; HttpClient httpClient = HttpClient.newHttpClient(); HttpRequest outRequest = HttpRequest.newBuilder() .uri(URI.create(thirdPartyApi)) .GET() .build(); DependencyTelemetry dependency = new DependencyTelemetry(); dependency.setType("HTTP"); dependency.setTarget("api.third-party.com"); dependency.setName("GET /business/data"); dependency.setData(thirdPartyApi); dependency.setTimestamp(Instant.now()); long startTime = System.currentTimeMillis(); boolean callSuccess = false; try { HttpResponse<String> response = httpClient.send(outRequest, HttpResponse.BodyHandlers.ofString()); callSuccess = response.statusCode() < 400; dependency.setResultCode(String.valueOf(response.statusCode())); // 此处写自身业务逻辑 return request.createResponseBuilder(HttpStatus.OK).body("call success").build(); } catch (Exception e) { dependency.setSuccess(false); dependency.setResultCode("500"); throw new RuntimeException("third party call failed", e); } finally { dependency.setDuration(Duration.ofMillis(System.currentTimeMillis() - startTime)); dependency.setSuccess(callSuccess); // 自动继承当前函数上下文的traceId、parentId,无需手动赋值,自动完成链路关联 TELEMETRY_CLIENT.trackDependency(dependency); TELEMETRY_CLIENT.flush(); } } }
- 该方案不需要手动设置trace相关ID,SDK会自动从当前函数执行上下文中提取关联信息,上报的依赖项会自动绑定到对应入站请求的调用链下,在Application Insights的端到端事务视图中可以直接看到父子调用层级。
4 常见问题排查
- 不要在代码中手动新建
TelemetryConfiguration实例,使用默认初始化的配置即可,手动新建配置会丢失上下文关联能力。 - 异步发起出站调用时,必须做线程上下文传播,否则会出现trace断链。
- 验证是否关联成功可以进入Application Insights的事务搜索页,筛选
依赖项类型,找到对应出站调用记录后查看端到端事务详情,能看到入站函数请求作为父节点、出站调用作为子节点即为配置正确。
内容的提问来源于stack exchange,提问作者Sajith Dilshan Jamal
相关产品推荐
相关产品推荐

