当 Java APM 代理在 Docker 中运行时,如何将 Java APM 代理连接到 APM 服务器?

Posted

技术标签:

【中文标题】当 Java APM 代理在 Docker 中运行时,如何将 Java APM 代理连接到 APM 服务器?【英文标题】:How do I connect a Java APM aget to a APM server when the Java APM agent is running in Docker? 【发布时间】:2021-10-23 14:54:38 【问题描述】:

我想在 Docker 中运行 Java APM 代理 在 docker 组件中,我收到以下错误消息:

2021-08-23 08:13:52,481 [elastic-apm-server-healthcheck] WARN  co.elastic.apm.agent.report.ApmServerHealthChecker - Elastic APM server https://i-o-optimized-deployment-912238.es.us-west1.gcp.cloud.es.io:9243/ is not available (Server returned status 401)
2021-08-23 08:13:52,779 [elastic-apm-remote-config-poller] ERROR co.elastic.apm.agent.configuration.ApmServerConfigurationSource - Unexpected status 401 while fetching configuration

Dockerfile 看起来像这样

FROM gradle as gradleimage
COPY ./app /home/gradle/source/app
COPY settings.gradle /home/gradle/source/settings.gradle
WORKDIR /home/gradle/source
RUN gradle assemble
EXPOSE 5000
FROM openjdk
COPY --from=gradleimage /home/gradle/source/app/build/libs/app.jar /app/
COPY ./apmplugin/elastic-apm-agent-1.25.0.jar /app/
WORKDIR /app
ENTRYPOINT ["java",  "-javaagent:elastic-apm-agent-1.25.0.jar", "-Delastic.apm.service_name=helloworld","-Delastic.apm.server_urls=https://i-o-optimized-deployment-912238.es.us-west1.gcp.cloud.es.io:9243", "-Delastic.apm.api_key= xxxxxxx" ,"-Delastic.apm.application_packages=org.steinko.helloworld",  "-jar", "app.jar"]

docker run 命令返回这个

steinko@Steins-MacBook-Pro ci-cd-template % docker run -t -p 5000:5000  steinko/helloworld 
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance.
2021-08-23 08:13:50,953 [main] INFO  co.elastic.apm.agent.util.JmxUtils - Found JVM-specific OperatingSystemMXBean interface: com.sun.management.OperatingSystemMXBean
2021-08-23 08:13:50,972 [main] INFO  co.elastic.apm.agent.configuration.StartupInfo - Starting Elastic APM 1.25.0 as helloworld on Java 16.0.2 Runtime version: 16.0.2+7-67 VM version: 16.0.2+7-67 (Oracle Corporation) Linux 5.10.25-linuxkit
2021-08-23 08:13:50,973 [main] INFO  co.elastic.apm.agent.configuration.StartupInfo - service_name: 'helloworld' (source: Java System Properties)
2021-08-23 08:13:50,973 [main] INFO  co.elastic.apm.agent.configuration.StartupInfo - api_key: 'XXXX' (source: Java System Properties)
2021-08-23 08:13:50,973 [main] INFO  co.elastic.apm.agent.configuration.StartupInfo - server_urls: 'https://i-o-optimized-deployment-912238.es.us-west1.gcp.cloud.es.io:9243' (source: Java System Properties)
2021-08-23 08:13:50,973 [main] INFO  co.elastic.apm.agent.configuration.StartupInfo - application_packages: 'org.steinko.helloworld' (source: Java System Properties)
2021-08-23 08:13:51,389 [main] INFO  co.elastic.apm.agent.impl.ElasticApmTracer - Tracer switched to RUNNING state

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.5)

2021-08-23 08:13:51.968  INFO 1 --- [           main] org.steinko.helloworld.App               : Starting App using Java 16.0.2 on 0f3c5bf51643 with PID 1 (/app/app.jar started by root in /app)
2021-08-23 08:13:51.973  INFO 1 --- [           main] org.steinko.helloworld.App               : No active profile set, falling back to default profiles: default
2021-08-23 08:13:52,481 [elastic-apm-server-healthcheck] WARN  co.elastic.apm.agent.report.ApmServerHealthChecker - Elastic APM server https://i-o-optimized-deployment-912238.es.us-west1.gcp.cloud.es.io:9243/ is not available (Server returned status 401)
2021-08-23 08:13:52,779 [elastic-apm-remote-config-poller] ERROR co.elastic.apm.agent.configuration.ApmServerConfigurationSource - Unexpected status 401 while fetching configuration
2021-08-23 08:13:52.970  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 5000 (http)
2021-08-23 08:13:52.982  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-08-23 08:13:52.982  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.45]
2021-08-23 08:13:53.019  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-08-23 08:13:53.019  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1016 ms
2021-08-23 08:13:53,048 [main] INFO  co.elastic.apm.agent.micrometer.MicrometerMetricsReporter - Registering Micrometer MeterRegistry: io.micrometer.core.instrument.simple.SimpleMeterRegistry@7593ea79
2021-08-23 08:13:53.451  INFO 1 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 14 endpoint(s) beneath base path '/actuator'
2021-08-23 08:13:53.524  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-08-23 08:13:53.688  INFO 1 --- [           main] .s.s.UserDetailsServiceAutoConfiguration : 

Using generated security password: c9f8d5ae-61d5-4410-b99c-bcecbf3593de

2021-08-23 08:13:53.776  INFO 1 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@6af310c7, org.springframework.security.web.context.SecurityContextPersistenceFilter@291a4791, org.springframework.security.web.header.HeaderWriterFilter@1d289d3f, org.springframework.security.web.csrf.CsrfFilter@34045582, org.springframework.security.web.authentication.logout.LogoutFilter@349c4d1c, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@46394f65, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@340cb97f, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@4a216eb4, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@4aed311e, org.springframework.security.web.session.SessionManagementFilter@3520958b, org.springframework.security.web.access.ExceptionTranslationFilter@44d7e24, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@75eaba95]
2021-08-23 08:13:53.824  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 5000 (http) with context path ''
2021-08-23 08:13:54.034  INFO 1 --- [           main] org.steinko.helloworld.App               : Started App in 2.376 seconds (JVM running for 3.354)
2021-08-23 08:14:32,106 [elastic-apm-server-reporter] INFO  co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Backing off for 0 seconds (+/-10%)
2021-08-23 08:14:32,110 [elastic-apm-server-reporter] ERROR co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Error sending data to APM server: cannot retry due to server authentication, in streaming mode, response code is 401
2021-08-23 08:14:32,111 [elastic-apm-server-reporter] WARN  co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - null

我必须做什么才能连接到 APM 服务器?

【问题讨论】:

Unexpected status 401 通常是安全/登录问题,即您没有在配置中提供 APM 服务器凭据。您确定配置的 API 密钥存在吗? 【参考方案1】:

可能是您尝试向 api_key 配置选项提供秘密令牌。这些不是一回事。尝试使用-Delastic.apm.secret_token=xxxxxxx 而不是-Delastic.apm.api_key=xxxxxxx

有关差异的更多详细信息,请参阅APM Server docs。

【讨论】:

【参考方案2】:

另外,您的 server_urls 似乎指向的是 Elasticsearch URL 而不是 APM URL。

我希望 APM 服务器 URL 的格式类似于 https://i-o-optimized-deployment-912238.apm.us-west1.gcp.cloud.es.io(基于您当前的 Elasticsearch URL https://i-o-optimized-deployment-912238.es.us-west1.gcp.cloud.es.io:9243)。

HTTPs Elasticsearch 将 HTTP 401 错误 w/o 身份验证,但您可以使用 APM URL 进一步设置。

【讨论】:

以上是关于当 Java APM 代理在 Docker 中运行时,如何将 Java APM 代理连接到 APM 服务器?的主要内容,如果未能解决你的问题,请参考以下文章

Observability:设置 Elastic APM Java 代理 - 自动设置

APM 代理环境部署之 Hadoop 篇

仅使用 Nginx 反向代理启用 Docker 端口访问

sh 自托管Kadira APM - Docker Compose - Ubuntu

在 Docker 中安装 GCP Profiler 代理时遇到问题

在 Docker 中运行的自定义 Azure DevOps 构建代理上具有卷装载的测试容器