Spring Boot와 함께 Azure Monitor Application Insights 사용

참고 항목

‘Spring Boot 네이티브 이미지 애플리케이션’을 통해 이 프로젝트를 사용할 수 있습니다.

Spring Boot에서 Application Insights Java를 사용하는 방법으로는 JVM(Java Virtual Machine)인수와 프로그래밍 방식, 두 가지 옵션이 있습니다.

JVM 인수를 통해 사용

-jar 앞 어딘가에 JVM 인수 -javaagent:"path/to/applicationinsights-agent-3.5.2.jar"을 추가합니다. 예를 들면 다음과 같습니다.

java -javaagent:"path/to/applicationinsights-agent-3.5.2.jar" -jar <myapp.jar>

Docker 진입점을 통한 Spring Boot

컨테이너 관련 설명서를 참조하세요.

구성

구성 옵션을 참조하세요.

프로그래밍 방식으로 활성화

Application Insights Java를 프로그래밍 방식으로 사용하도록 설정하려면 다음 종속성을 추가해야 합니다.

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>applicationinsights-runtime-attach</artifactId>
    <version>3.5.2</version>
</dependency>

또한 main() 메서드의 시작 줄에 있는 com.microsoft.applicationinsights.attach.ApplicationInsights 클래스의 attach() 메서드를 호출합니다.

Warning

이 호출은 main 메서드의 시작 부분에서 요청해야 합니다.

Warning

JRE는 지원되지 않습니다.

Warning

운영 체제의 임시 디렉터리를 쓸 수 있어야 합니다.

예시:

@SpringBootApplication
public class SpringBootApp {

  public static void main(String[] args) {
    ApplicationInsights.attach();
    SpringApplication.run(SpringBootApp.class, args);
  }
}

구성

프로그래밍 방식을 사용할 때는 JVM 인수를 사용할 때와 동일한 모든 구성 옵션을 지원하며, 차이점은 다음 섹션에서 설명합니다.

구성 파일 위치

기본적으로 Application Insights Java를 프로그래밍 방식으로 사용하는 경우 구성 파일 applicationinsights.json을 클래스 경로(src/main/resources, src/test/resources)에서 읽습니다.

3.4.3부터 applicationinsights.runtime-attach.configuration.classpath.file 시스템 속성을 사용하여 클래스 경로에서 JSON 파일의 이름을 구성할 수 있습니다. 예를 들어 -Dapplicationinsights.runtime-attach.configuration.classpath.file=applicationinsights-dev.json을 사용하는 경우 Application Insights는 구성에 대해 applicationinsights-dev.json 파일을 사용합니다. Classpath에서 다른 파일을 프로그래밍 방식으로 구성하려면 다음을 수행합니다.

public static void main(String[] args) {
    System.setProperty("applicationinsights.runtime-attach.configuration.classpath.file", "applicationinsights-dev.json");
    ApplicationInsights.attach();
    SpringApplication.run(PetClinicApplication.class, args);
}

참고 항목

Spring의 application.properties 또는 application.yaml 파일은 Application Insights Java 구성의 원본으로 지원되지 않습니다.

파일 위치를 클래스 경로 외부로 변경하려면 구성 파일 경로 구성 옵션을 참조하세요.

Classpath에서 파일을 프로그래밍 방식으로 구성하려면 다음을 수행합니다.

public static void main(String[] args) {
    System.setProperty("applicationinsights.configuration.file", "{path}/applicationinsights-dev.json");
    ApplicationInsights.attach();
    SpringApplication.run(PetClinicApplication.class, args);
}

프로그래밍 방식으로 연결 문자열 구성

먼저 applicationinsights-core 종속성을 추가합니다.

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>applicationinsights-core</artifactId>
    <version>3.5.2</version>
</dependency>

그런 다음, ConnectionString.configure 메서드를 ApplicationInsights.attach() 후에 호출합니다.

public static void main(String[] args) {
    System.setProperty("applicationinsights.configuration.file", "{path}/applicationinsights-dev.json");
    ApplicationInsights.attach();
    SpringApplication.run(PetClinicApplication.class, args);
}

또는 Spring 구성 요소에서 ConnectionString.configure 메서드를 호출합니다.

런타임에 구성된 연결 문자열 사용:

{
  "connectionStringConfiguredAtRuntime": true
}

자체 진단 로그 파일 위치

기본적으로 Application Insights Java를 프로그래밍 방식으로 사용하는 경우 JVM이 시작되는 디렉터리(사용자 디렉터리)에 에이전트 로그가 포함된 applicationinsights.log 파일이 배치됩니다.

이 위치를 변경하는 방법을 알아보려면 자체 진단 구성 옵션을 참조하세요.