Application Insights与SpringBoot 2.7.12的集成

hfwmuf9z  于 2024-01-06  发布在  Spring
关注(0)|答案(1)|浏览(140)

我有一个spring Boot 版本,和applicationinsights-runtime-attach依赖项。在main方法之前,ApplicationInsights.attach()被执行,json中的instrument key被选中,但是当它转到main类的run方法时,即runApplication,它从spring-logback.xml初始化aiterder类,其中instrument key为null。
我的任务是将日志发布到来自springboot应用程序的应用程序洞察中,并使用这些应用程序创建一个 Jmeter 板(基本上是性能基准测试)。具体来说,事务所需的时间。为此,我使用自定义注解@Timed注解了方法
我尝试了以下步骤:
添加配置:{spring Boot version - 2.7.12}
a. com.microsoft.azure:applicationsinsights-springboot-starter,2.6.4
B. com.microsoft.azure:applicationsinsights-logging-logback,2.6.4 ApplicationInsightslogder的依赖关系
c. com.microsoft.azure:applicationsinsights-runtime-attach,3.4.14
d. com.microsoft.azure:applicationsinsights-core,3.4.14
yaml文件和json中的连接字符串
Applicationinsights.attach before run主类中的Application方法
spring-logback.xml中的aimplayer
@定时自定义注解。
TimingAspect.java class having @Around(Timed package){ calculating execution time}此方法还使用EventTelemetry发送事件,即TelemetryClient.trackEvent(event),其中事件设置有两个属性;方法名称和执行时间。
使用@Timed注解方法以捕获其持续时间。
请提供适当的步骤来发送这些日志到应用程序

2lpgd968

2lpgd9681#

配置logback调试语句,并在应用程序启动期间将状态消息打印到控制台。

spring-logback.xml:

  1. <configuration>
  2. <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
  3. <include resource="org/springframework/boot/logging/logback/base.xml"/>
  4. <appender name="aiAppender" class="com.microsoft.applicationinsights.logback.ApplicationInsightsAppender">
  5. <instrumentationKey>${applicationinsights.instrumentationKey}</instrumentationKey>
  6. </appender>
  7. <logger name="com.example" level="INFO"/>
  8. <root level="INFO">
  9. <appender-ref ref="aiAppender"/>
  10. </root>
  11. </configuration>

字符串

  • 下面是我在应用程序中使用的依赖项,并创建了一个TimingAspect类来捕获方法执行时间。
  • build.gradle:*
  1. implementation 'com.microsoft.azure:applicationinsights-spring-boot-starter:2.6.4'
  2. implementation 'com.microsoft.azure:applicationinsights-logging-logback:2.6.4'
  3. implementation 'com.microsoft.azure:applicationinsights-runtime-attach:3.4.14'
  4. implementation 'com.microsoft.azure:applicationinsights-core:3.4.14'

TimingAspect:

  1. @Aspect
  2. @Component
  3. public class TimingAspect {
  4. @Around("@annotation(timed)")
  5. public Object logExecutionTime(ProceedingJoinPoint joinPoint, Timed timed) throws Throwable {
  6. long startTime = System.currentTimeMillis();
  7. Object result = joinPoint.proceed();
  8. long endTime = System.currentTimeMillis();
  9. long executionTime = endTime - startTime;
  10. // Log or send telemetry with method name and execution time
  11. logMethodExecutionTime(joinPoint, timed.value(), executionTime);
  12. return result;
  13. }
  14. private void logMethodExecutionTime(ProceedingJoinPoint joinPoint, String customName, long executionTime) {
  15. // Use TelemetryClient to send telemetry data
  16. TelemetryClient telemetryClient = new TelemetryClient();
  17. EventTelemetry event = new EventTelemetry("MethodExecutionTime");
  18. event.getProperties().put("methodName", customName.isEmpty() ? joinPoint.getSignature().getName() : customName);
  19. event.getProperties().put("executionTime", String.valueOf(executionTime));
  20. telemetryClient.trackEvent(event);
  21. }
  22. }

主类:

  1. @SpringBootApplication
  2. public class sampletestapp {
  3. public static void main(String[] args) {
  4. // Attach Application Insights
  5. ApplicationInsights.attach();
  6. // Start OpenTelemetry
  7. startOpenTelemetry();
  8. // Run the Spring Boot application
  9. SpringApplication.run(YourApplication.class, args);
  10. }
  11. private static void startOpenTelemetry() {
  12. ApplicationInsightsSpanExporter applicationInsightsExporter = new ApplicationInsightsSpanExporter();
  13. // Optionally, add other exporters (e.g., LoggingSpanExporter) for debugging purposes
  14. LoggingSpanExporter loggingExporter = new LoggingSpanExporter();
  15. // Create an OpenTelemetry Tracer
  16. Tracer tracer = GlobalOpenTelemetry.getTracer("your-application-name");
  17. // Set up the span processors with the exporters
  18. OpenTelemetrySdk.getTracerProvider()
  19. .addSpanProcessor(SimpleSpanProcessor.create(applicationInsightsExporter))
  20. .addSpanProcessor(SimpleSpanProcessor.create(loggingExporter));
  21. // Optionally, you can also set other OpenTelemetry configurations
  22. // Register the ApplicationInsightsHttpFilter for automatic correlation of HTTP requests
  23. ApplicationInsightsHttpFilter instrumentedFilter = new ApplicationInsightsHttpFilter();
  24. // Optionally, configure the filter if needed
  25. // instrumentedFilter.set...
  26. // Register the filter with the servlet container
  27. GlobalOpenTelemetry.getPropagators().getHttpTextFormat().inject(tracer.getCurrentSpan().getContext(),
  28. instrumentedFilter, ApplicationInsightsHttpFilter.SETTER);
  29. // Optionally, you can register other instrumentation (e.g., JDBC, gRPC, etc.) based on your application needs
  30. }
  31. }


x1c 0d1x的数据

应用洞察:


展开查看全部

相关问题