(Update) JavaAgent in SpringBoot Docker image

要把opentelemetry-javaagent加入springboot application的docker image裡,目前看來最簡單的方式是套用paketo-buildpacks/opentelemetry。不過目前仍未列在paketo的release清單上,清單有哪些可以參考Java Buildpack Reference

做法依maven與gradle分別如下

Maven

      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${spring.boot.version}</version>
        <configuration>
          <image>
            <buildpacks>
              <buildpack>paketo-buildpacks/ca-certificates</buildpack>
              <buildpack>paketo-buildpacks/bellsoft-liberica</buildpack>
              <buildpack>paketo-buildpacks/syft</buildpack>
              <buildpack>paketo-buildpacks/executable-jar</buildpack>
              <buildpack>paketo-buildpacks/dist-zip</buildpack>
              <buildpack>paketo-buildpacks/spring-boot</buildpack>
              <buildpack>gcr.io/paketo-buildpacks/opentelemetry</buildpack>
            </buildpacks>
            <env>
              <BP_OPENTELEMETRY_ENABLED>true</BP_OPENTELEMETRY_ENABLED>
            </env>
          </image>
        </configuration>
      </plugin>

Gradle

tasks.getByName<BootBuildImage>("bootBuildImage") {
    verboseLogging.set(true)
    buildpacks.addAll("paketo-buildpacks/ca-certificates",
        "paketo-buildpacks/bellsoft-liberica",
        "paketo-buildpacks/syft",
        "paketo-buildpacks/executable-jar",
        "paketo-buildpacks/dist-zip",
        "paketo-buildpacks/spring-boot",
        "gcr.io/paketo-buildpacks/opentelemetry")
    environment.put("BP_OPENTELEMETRY_ENABLED","true")
}

說明

由於在設定xml上沒有辦法extend再疊加,所以目前僅能將spring會用到的列出後再加入,但也還算方便。

BP_OPENTELEMETRY_ENABLED一定要開啟。

建構出的的image在docker裡的設定要多一個OTEL_JAVAAGENT_ENABLED,未設定的話即為不使用agent。

  spring:
    image: elliot/single/docker:latest
    container_name: spring
    environment:
      - OTEL_JAVAAGENT_ENABLED=true
      - OTEL_EXPORTER_JAEGER_ENDPOINT=http://jaegertracing:14250
      - OTEL_METRICS_EXPORTER=none
      - OTEL_SERVICE_NAME=single-sv
      - OTEL_TRACES_EXPORTER=jaeger
    ports:
      - 8080:8080
    volumes:
      - ./config/spring/application-docker.yml:/workspace/config/application-docker.yml
    depends_on:
      postgresql: 
        condition: service_healthy

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料