我目前正在使用 Springboot Java 来 POC Azure Application Insight。我能够将请求和跟踪发送到 Azure App Insight,但无法附加自定义属性来丰富请求详细信息
我提取记录的操作如下:
现在我需要的是丰富现有的 REQUEST 记录,我可以添加新的 REQUEST 记录,但它会重复自动添加的记录
我尝试使用不同的方法,例如
没用,属性里没有添加。
如果您在自动跟踪的遥测中没有看到属性,请检查
TelemetryInitializer
目标 RequestTelemetry
。
RequestTelemetryInitializer,java:
import com.microsoft.applicationinsights.telemetry.RequestTelemetry;
import com.microsoft.applicationinsights.telemetry.Telemetry;
import com.microsoft.applicationinsights.extensibility.TelemetryInitializer;
public class RequestTelemetryInitializer implements TelemetryInitializer {
@Override
public void initialize(Telemetry telemetry) {
if (telemetry instanceof RequestTelemetry) {
RequestTelemetry requestTelemetry = (RequestTelemetry) telemetry;
requestTelemetry.getProperties().put("CustomProperty1", "CustomValue1");
requestTelemetry.getProperties().put("CustomProperty2", "CustomValue2");
}
}
}
TelemetryInitializer
已注册遥测配置。
ApplicationInsightsConfig.java:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.microsoft.applicationinsights.TelemetryConfiguration;
@Configuration
public class ApplicationInsightsConfig {
@Bean
public TelemetryInitializer requestTelemetryInitializer() {
return new RequestTelemetryInitializer();
}
@Bean
public TelemetryConfiguration telemetryConfiguration(TelemetryInitializer initializer) {
TelemetryConfiguration configuration = TelemetryConfiguration.getActive();
configuration.getTelemetryInitializers().add(initializer);
return configuration;
}
}
TelemetryInitializer
处于活动状态,自定义属性应出现在 Azure Application Insights 中的请求遥测下。Pom.xml:
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Actuator for metrics -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Azure Application Insights -->
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>applicationinsights-spring-boot-starter</artifactId>
<version>2.6.4</version>
</dependency>
</dependencies>
上面的屏幕截图显示,值为
customPropertyKey
的自定义属性 customPropertyValue
已成功添加到 Azure Application Insights 中的 Request 遥测。
这证实了自定义遥测丰富功能正常工作。