APM Server 二进制文件
本指南将解释如何设置和配置 APM Server 二进制文件。
首先,请参阅 Elastic 支持矩阵,了解有关受支持的操作系统和产品兼容性的信息。
您需要
- Elasticsearch,用于存储和索引数据。
- Kibana,用于通过应用程序 UI 进行可视化。
建议使用相同版本的 Elasticsearch、Kibana 和 APM Server。有关安装这些产品的更多信息,请参阅部署。
开始之前:如果您尚未安装 Elastic Stack,请立即安装。请参阅部署。
要下载并安装 APM Server,请使用适合您系统的以下命令。如果您使用 apt 或 yum,可以从我们的存储库安装 APM Server,以便更轻松地更新到最新版本。
curl -L -O https://artifacts.elastic.co/downloads/apm-server/apm-server-9.4.4-amd64.deb
sudo dpkg -i apm-server-9.4.4-amd64.deb
curl -L -O https://artifacts.elastic.co/downloads/apm-server/apm-server-9.4.4-x86_64.rpm
sudo rpm -vi apm-server-9.4.4-x86_64.rpm
curl -L -O https://artifacts.elastic.co/downloads/apm-server/apm-server-9.4.4-linux-x86_64.tar.gz
tar xzvf apm-server-9.4.4-linux-x86_64.tar.gz
有关更多信息,请参阅修改 nofile ulimit。
curl -L -O https://artifacts.elastic.co/downloads/apm-server/apm-server-9.4.4-darwin-x86_64.tar.gz
tar xzvf apm-server-9.4.4-darwin-x86_64.tar.gz
从下载页面下载 APM Server Windows zip 文件。
将 zip 文件的内容解压到
C:\Program Files中。将
apm-server-<version>-windows目录重命名为APM-Server。以管理员身份打开 PowerShell 提示符(右键单击 PowerShell 图标并选择以管理员身份运行)。如果您使用的是 Windows XP,可能需要下载并安装 PowerShell。
在 PowerShell 提示符中,运行以下命令将 APM Server 作为 Windows 服务安装
有关部署 Docker 容器的信息,请参阅在 Docker 上运行。
通过编辑 apm-server.yml 配置文件来配置 APM。该文件的位置因平台而异——如需查找帮助,请参阅安装布局。
最小配置文件可能如下所示
apm-server:
host: "localhost:8200"
output.elasticsearch:
hosts: ["localhost:9200"]
username: "elastic"
password: "changeme"
- APM Server 监听的
host:port。 - 要连接的 Elasticsearch
host:port。 - 本示例使用基本身份验证。此处提供的用户需要将事件发布到 Elasticsearch 所需的权限。要为此角色创建专用用户,请参阅创建 writer 角色。
所有可用的配置选项已在配置 APM Server中概述。
在生产环境中,您会将 APM Server 部署在独立的机器上,这与运行 Elasticsearch 类似。您可以将其与 Elasticsearch 运行在同一台机器上,但并不推荐这样做,因为这些进程会互相竞争资源。
要启动 APM Server,请运行
./apm-server -e
-e 全局标志启用将日志输出到 stderr 并禁用 syslog/文件输出。如果您已在配置文件中启用了日志记录,请移除此标志。对于 Linux 系统,请参阅 APM Server 状态和日志。
您应该会看到 APM Server 启动。它将尝试连接到本地主机端口 9200 上的 Elasticsearch,并在端口 8200 上向 Agent 暴露 API。您可以在 apm-server.yml 中更改默认值,或在命令行中指定不同的地址
./apm-server -e -E output.elasticsearch.hosts=ElasticsearchAddress:9200 -E apm-server.host=localhost:8200
对于 Debian 软件包和 RPM 安装,我们建议 apm-server 进程作为非 root 用户运行。因此,这些安装方法会创建一个 apm-server 用户,您可以使用该用户来启动进程。此外,仅当配置文件属于运行该进程的用户时,APM Server 才会启动。
在此情况下,要启动 APM Server,请运行
sudo -u apm-server apm-server [<argument...>]
默认情况下,APM Server 从 /etc/apm-server/apm-server.yml 加载其配置文件。有关完整的目录布局,请参阅 deb 和 rpm 默认路径。
1. 将 Agent 添加到您的项目中
首先,如下所示将 Elastic APM Agent 插件添加到应用程序的 build.gradle 文件中
// Android app's build.gradle file
plugins {
id "com.android.application"
id "co.elastic.apm.android" version "[latest_version]"
}
- Elastic 插件声明必须添加在 Android 应用插件声明(
com.android.application)下方,以及 Kotlin 插件声明(如果使用)下方。
2. 配置 Agent
添加 Agent 插件后,进行配置。最小配置将设置 Elastic APM 集成终节点,如下所示
// Android app's build.gradle file
plugins {
//...
id "co.elastic.apm.android" version "[latest_version]"
}
elasticApm {
// Minimal configuration
serverUrl = "https://your.elastic.server"
// Optional
serviceName = "your app name"
serviceVersion = "0.0.0"
apiKey = "your server api key"
secretToken = "your server auth token"
}
- 您可以在 Gradle 插件门户中找到最新版本。
- 默认为您的
android.defaultConfig.applicationId值。 - 默认为您的
android.defaultConfig.versionName值。 - 默认为 null。此处有关于 API Key 的更多信息。
- 默认为 null。
同时提供 secretToken 和 apiKey 时,apiKey 具有更高优先级,secretToken 将被忽略。
3. 初始化 Agent
在使用上面的 Gradle 更改同步项目后,需要在 Application 类中初始化 Elastic APM Agent。此示例展示了配置 Agent 的最简单方法
// Your Application class
class MyApp extends android.app.Application {
@Override
public void onCreate() {
super.onCreate();
ElasticApmAgent.initialize(this);
}
}
- 初始化一次 Elastic APM Agent。
剩下的就是编译并运行您的应用程序。大功告成!
在 Agent 参考中了解更多信息
在 APM Android Agent 参考文档中阅读更多内容。
1. 安装 Agent
使用 go get 安装 Elastic APM Go Agent 软件包
go get -u go.elastic.co/apm/v2
2. 配置 Agent
为了简化开发和测试,Agent 默认将数据发送到位于 https://:8200 的 Elastic APM 集成。要将数据发送到其他位置,您必须配置 ELASTIC_APM_SERVER_URL。
# The APM integration host and port
export ELASTIC_APM_SERVER_URL=
# If you do not specify `ELASTIC_APM_SERVICE_NAME`, the Go agent will use the
# executable name. For example, if your executable is called "my-app.exe", then your
# service will be identified as "my-app".
export ELASTIC_APM_SERVICE_NAME=
# Secret tokens are used to authorize requests to the APM integration
export ELASTIC_APM_SECRET_TOKEN=
3. 为您的应用程序添加埋点
埋点(Instrumentation)是扩展应用程序代码以向 Elastic APM 报告追踪数据的过程。Go 应用程序必须在源代码级别手动进行埋点。要为您的应用程序添加埋点,请使用以下方法之一
在 Agent 参考中了解更多信息
1. 将 Agent 依赖项添加到您的项目中
将 Elastic APM iOS Agent 添加到您的 Xcode 项目或 Package.swift 中。
以下是将包依赖项添加到标准 Xcode 项目的说明。
有关向 Package.swift 添加依赖项的详细信息,请参阅添加对另一个 Swift 包的依赖。以下是一个实用的代码片段
Package(
dependencies:[
.package(name: "apm-agent-ios", url: "https://github.com/elastic/apm-agent-ios.git", from: "1.0.0"),
],
targets:[
.target(
name: "MyApp",
dependencies: [
.product(name: "ElasticApm", package: "apm-agent-ios")
]
),
])
2. 初始化 Agent
如果您使用 SwiftUI 构建应用程序,请将以下内容添加到您的 App.swift
import SwiftUI
import ElasticApm
class AppDelegate : NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
var config = AgentConfigBuilder()
.withServerUrl(URL(string:"http://127.0.0.1:8200"))
.withSecretToken("<SecretToken>")
.build()
ElasticApmAgent.start(with: config)
return true
}
}
@main
struct MyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
init() {
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
- APM 集成主机和端口
- 用于 APM 集成连接的 Secret Token
如果您未使用 SwiftUI,也可以将相同的内容添加到 AppDelegate.swift 文件中
import UIKit
import ElasticApm
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
var config = AgentConfigBuilder()
.withServerUrl(URL(string:"http://127.0.0.1:8200"))
.withSecretToken("<SecretToken>")
.build()
ElasticApmAgent.start(with: config)
return true
}
}
- APM 集成主机和端口
- 用于 APM 集成连接的 Secret Token
在 Agent 参考中了解更多信息
在 APM iOS Agent 参考文档中阅读更多内容。
使用 -javaagent JVM 选项手动设置和配置 Agent。无需更改应用程序代码,但这需要重启应用程序。有关此设置方法的更多信息,请参见下文。
1. 下载 APM Agent
开始使用 Elastic APM Java Agent 的第一步是获取 Agent JAR 文件副本。Java Agent 版本会发布到 Maven 中央仓库。为了获取副本,您可以
使用
curl下载curl -o 'elastic-apm-agent.jar' -L 'https://oss.sonatype.org/service/local/artifact/maven/redirect?r=releases&g=co.elastic.apm&a=elastic-apm-agent&v=LATEST'
2. 添加 -javaagent 标志
启动应用程序时,添加 JVM 标志 -javaagent:/path/to/elastic-apm-agent-<version>.jar
3. 配置
不同的应用服务器具有不同的设置 -javaagent 标志和系统属性的方法。启动您的应用程序(例如 Spring Boot 应用程序或其他嵌入式服务器)并添加 -javaagent JVM 标志。使用 -D 前缀以利用系统属性配置 Agent
java -javaagent:/path/to/elastic-apm-agent-<version>.jar -Delastic.apm.service_name=my-cool-service -Delastic.apm.application_packages=org.example,org.another.example -Delastic.apm.server_url=http://127.0.0.1:8200 -jar my-application.jar
请参阅使用 -javaagent 标志进行手动设置以了解更多信息。
其他设置方法
- 使用
apm-agent-attach-cli.jar进行自动设置
自动设置 Agent,无需修改 JVM 或应用服务器的配置。该方法无需更改应用程序代码或 JVM 选项,并允许附加到正在运行的 JVM。有关此设置方法的更多信息,请参阅 Java Agent 文档。 - 通过程序化 API 自挂载设置
通过一行代码更改和额外的apm-agent-attach依赖项设置 Agent。该方法无需更改 JVM 选项,且 Agent 工件嵌入在打包的应用程序二进制文件中。有关此设置方法的更多信息,请参阅 Java Agent 文档。
设置 APM Agent
可以通过几种不同的方式将 .NET Agent 添加到应用程序中
- Profiler 运行时埋点:Agent 支持无代码更改且无需重新编译项目的自动埋点。请参阅 Profiler 自动埋点。
- NuGet 软件包:Agent 以一组可在 nuget.org 上获取的 NuGet 软件包的形式提供。您可以通过引用其中一个或多个软件包并遵循软件包文档,将 Agent 和特定埋点添加到 .NET 应用程序中。
- Host 启动钩子:在 .NET Core 3.0+ 或 .NET 5+ 上,Agent 支持无代码更改且无需重新编译项目的自动埋点。有关详细信息,请参阅 .NET Core 上的零代码更改设置。
在 Agent 参考中了解更多信息
1. 安装 APM Agent
将 Node.js APM Agent 作为依赖项安装到您的应用程序中。
npm install elastic-apm-node --save
2. 初始化
务必在引入 Node.js 应用程序中的任何其他模块之前启动代理——即在 http 以及您的路由器等之前。
这意味着您应该在应用程序的主文件(通常是 index.js、server.js 或 app.js)中引入并启动代理。
以下是如何常规引入和启动 Elastic APM 的简单示例
// Add this to the VERY top of the first file loaded in your app
var apm = require('elastic-apm-node').start({
// Override service name from package.json
// Allowed characters: a-z, A-Z, 0-9, -, _, and space
serviceName: '',
// Use if APM integration requires a token
secretToken: '',
// Use if APM integration uses API keys for authentication
apiKey: '',
// Set custom APM integration host and port (default: http://127.0.0.1:8200)
serverUrl: '',
})
代理现在将监视应用程序的性能并记录任何未捕获的异常。
在 Agent 参考中了解更多信息
1. 安装 Agent
使用支持平台的软件包之一安装代理。
使用 RPM 软件包 (RHEL/CentOS 和 Fedora)
rpm -ivh <package-file>.rpm
使用 DEB 软件包 (Debian 和 Ubuntu)
dpkg -i <package-file>.deb
使用 APK 软件包 (Alpine)
apk add --allow-untrusted <package-file>.apk
如果找不到您的发行版,可以通过从源码构建来安装代理。以下说明将使用 Elastic 用于构建官方软件包的相同 Docker 环境来构建 APM 代理。
该代理目前仅适用于 Linux 操作系统。
- 从 https://github.com/elastic/apm-agent-php/ 下载代理源码。
- 执行以下命令以构建并安装代理
cd apm-agent-php
# for linux glibc - libc distributions (Ubuntu, Redhat, etc)
export BUILD_ARCHITECTURE=linux-x86-64
# for linux with musl - libc distributions (Alpine)
export BUILD_ARCHITECTURE=linuxmusl-x86-64
# provide a path to php-config tool
export PHP_CONFIG=php-config
# build extensions
make -f .ci/Makefile build
# run extension tests
PHP_VERSION=`$PHP_CONFIG --version | cut -d'.' -f 1,2` make -f .ci/Makefile run-phpt-tests
# install agent extensions
sudo cp agent/native/_build/${BUILD_ARCHITECTURE}-release/ext/elastic_apm-*.so `$PHP_CONFIG --extension-dir`
# install automatic loader
sudo cp agent/native/_build/${BUILD_ARCHITECTURE}-release/loader/code/elastic_apm_loader.so `$PHP_CONFIG --extension-dir`
2. 启用并配置 APM Agent
在 php.ini 文件内部启用并配置您的 Agent
extension=elastic_apm_loader.so
elastic_apm.bootstrap_php_part_file=<repo root>/agent/php/bootstrap_php_part.php
在 Agent 参考中了解更多信息
- Django
- 1. 安装 APM Agent
将 Python APM Agent 作为依赖项安装。
$ pip install elastic-apm
2. 配置 Agent
Agent 是在应用程序进程内部运行的库。APM 服务是根据 SERVICE_NAME 以编程方式创建的。
# Add the agent to the installed apps
INSTALLED_APPS = (
'elasticapm.contrib.django',
# ...
)
ELASTIC_APM = {
# Set required service name. Allowed characters:
# a-z, A-Z, 0-9, -, _, and space
'SERVICE_NAME': '',
# Use if APM integration requires a token
'SECRET_TOKEN': '',
# Set custom APM integration host and port (default: https://:8200)
'SERVER_URL': '',
}
# To send performance metrics, add our tracing middleware:
MIDDLEWARE = (
'elasticapm.contrib.django.middleware.TracingMiddleware',
#...
)
- Flask
- 1. 安装 APM Agent
将 Python APM Agent 作为依赖项安装。
$ pip install elastic-apm[flask]
2. 配置 Agent
Agent 是在应用程序进程内部运行的库。APM 服务是根据 SERVICE_NAME 以编程方式创建的。
# initialize using environment variables
from elasticapm.contrib.flask import ElasticAPM
app = Flask(__name__)
apm = ElasticAPM(app)
# or configure to use ELASTIC_APM in your application settings
from elasticapm.contrib.flask import ElasticAPM
app.config['ELASTIC_APM'] = {
# Set required service name. Allowed characters:
# a-z, A-Z, 0-9, -, _, and space
'SERVICE_NAME': '',
# Use if APM integration requires a token
'SECRET_TOKEN': '',
# Set custom APM integration host and port (default: https://:8200)
'SERVER_URL': '',
}
apm = ElasticAPM(app)
在 Agent 参考中了解更多信息
1. 安装 APM Agent
将 Agent 添加到您的 Gemfile。
gem 'elastic-apm'
2. 配置 Agent
- Ruby on Rails
- 当应用引导启动时,APM 会自动启动。通过创建配置文件
config/elastic_apm.yml来配置 Agent
# config/elastic_apm.yml:
# Set service name - allowed characters: a-z, A-Z, 0-9, -, _ and space
# Defaults to the name of your Rails app
service_name: 'my-service'
# Use if APM integration requires a token
secret_token: ''
# Set custom APM integration host and port (default: https://:8200)
server_url: 'https://:8200'
- Rack
- 对于 Rack 或兼容框架(如 Sinatra),请在应用中包含中间件并启动 Agent。
# config.ru
app = lambda do |env|
[200, {'Content-Type' => 'text/plain'}, ['ok']]
end
# Wraps all requests in transactions and reports exceptions
use ElasticAPM::Middleware
# Start an instance of the Agent
ElasticAPM.start(service_name: 'NothingButRack')
run app
# Gracefully stop the agent when process exits.
# Makes sure any pending transactions are sent.
at_exit { ElasticAPM.stop }
创建配置文件
创建配置文件 config/elastic_apm.yml
# config/elastic_apm.yml:
# Set service name - allowed characters: a-z, A-Z, 0-9, -, _ and space
# Defaults to the name of your Rack app's class.
service_name: 'my-service'
# Use if APM integration requires a token
secret_token: ''
# Set custom APM integration host and port (default: https://:8200)
server_url: 'https://:8200'
在 Agent 参考中了解更多信息
1. 启用真实用户监控 (RUM)
RUM 默认禁用。通过将 Enable RUM 设置为 true 来启用它。
2. 设置 Agent
使用 <script> 标签或通过使用打包工具来设置 Agent。
同步 / 阻塞模式
添加一个 <script> 标签来加载打包文件,并使用 elasticApm 全局对象来初始化 Agent
<script src="<YOUR_URL>/path/to/elastic-apm-rum.umd.min-<VERSION>.js" crossorigin></script>
<script>
elasticApm.init({
serviceName: '<instrumented-app>',
serverUrl: '<apm-server-url>',
})
</script>
异步 / 非阻塞模式
异步加载脚本可以确保 Agent 脚本不会阻塞页面上的其他资源,但是它仍然会阻塞浏览器的 onload 事件。
<script>
;(function(d, s, c) {
var j = d.createElement(s),
t = d.getElementsByTagName(s)[0]
j.src = '<YOUR_URL>/path/to/elastic-apm-rum.umd.min-<VERSION>.js'
j.onload = function() {elasticApm.init(c)}
t.parentNode.insertBefore(j, t)
})(document, 'script', {serviceName: '<INSTRUMENTED_APP>', serverUrl: '<APM_SERVER_URL>'})
</script>
使用打包工具
将真实用户监控(RUM)APM Agent 作为依赖项安装到您的应用程序中
npm install @elastic/apm-rum --save
配置代理
import { init as initApm } from '@elastic/apm-rum'
const apm = initApm({
// Set required service name (allowed characters: a-z, A-Z, 0-9, -, _, and space)
serviceName: '',
// Set custom APM integration host and port (default: https://:8200)
serverUrl: 'https://:8200',
// Set service version (required for sourcemap feature)
serviceVersion: ''
})
在 Agent 参考中了解更多信息
Elastic 与 OpenTelemetry 集成,允许您重用现有的埋点,轻松将可观测性数据发送到 Elastic Stack。
有关如何结合使用 Elastic 和 OpenTelemetry 的更多信息,请参阅OpenTelemetry 集成。
当您至少有一个 APM Agent 向 APM Server 发送数据后,就可以开始在 Kibana 应用程序 UI 中可视化您的数据了。
我们为基于 APT 和 YUM 的发行版提供软件仓库。请注意,我们只提供二进制包,不提供源码包。
我们使用 PGP 密钥 D88E42B4(Elasticsearch 签名密钥),其指纹为
4609 5ACC 8548 582C 1A26 99A9 D27D 666C D88E 42B4
来对我们所有的软件包进行签名。该密钥可从 https://pgp.mit.edu 获取。
要为 APT 添加 apm-server 存储库
下载并安装公共签名密钥
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -在继续操作之前,您可能需要在 Debian 上安装
apt-transport-https软件包sudo apt-get install apt-transport-https将存储库定义保存到
/etc/apt/sources.list.d/elastic-9.0.0.listecho "deb https://artifacts.elastic.co/packages/9.0.0-prerelease/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-9.0.0-prerelease.list警告要添加 Elastic 软件仓库,请确保使用示例中显示的
echo方法。不要使用add-apt-repository,因为它会添加一个deb-src条目,而我们不提供源码包。如果你不小心添加了
deb-src条目,将会看到类似以下的错误Unable to find expected entry 'main/source/Sources' in Release file (Wrong sources.list entry or malformed file)运行
apt-get update,存储库即可使用。例如,您可以通过运行以下命令来安装 APM Serversudo apt-get update && sudo apt-get install apm-server要配置 APM Server 在开机时自动启动,请运行
sudo systemctl enable apm-server
要为 YUM 添加 apm-server 存储库
下载并安装公共签名密钥
sudo rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch在 /etc/yum.repos.d/ 目录中创建一个带有 .repo 扩展名的文件(例如 elastic.repo),并添加以下行
[elastic-9.0.0] name=Elastic repository for 9.0.0 packages baseurl=https://artifacts.elastic.co/packages/9.0.0/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md您的存储库已准备就绪。例如,您可以通过运行以下命令来安装 APM Server
sudo yum install apm-server要配置 APM Server 在开机时自动启动,请运行
sudo systemctl enable apm-server
APM Server 的 Docker 镜像可从 Elastic Docker 注册表中获取。基础镜像是 Red Hat Universal Base Images (UBI),如果您使用经过加固的 Docker 镜像,则为 Wolfi。
所有已发布的 Docker 镜像及标签列表可在 www.docker.elastic.co 查看。
根据 Elastic 许可证,这些镜像是可以免费使用的。它们包含开源和免费的商业功能,以及访问付费商业功能的权限。开启 30 天试用以体验所有付费商业功能。有关 Elastic 许可证级别的详细信息,请参阅订阅页面。
获取 Docker 版 APM Server 非常简单,只需对 Elastic Docker 注册表执行 docker pull 命令,然后(可选)验证镜像。
拉取 Docker 镜像
docker pull docker.elastic.co/apm/apm-server:9.0.0或者,您可以使用经过加固的 Wolfi 镜像
docker pull docker.elastic.co/apm/apm-server-wolfi:9.0.0验证 Docker 镜像
wget https://artifacts.elastic.co/cosign.pub cosign verify --key cosign.pub docker.elastic.co/apm/apm-server:9.0.0cosign命令以 JSON 格式打印检查结果和签名有效负载Verification for docker.elastic.co/apm/apm-server:9.0.0 -- The following checks were performed on each of these signatures: - The cosign claims were validated - Existence of the claims in the transparency log was verified offline - The signatures were verified against the specified public key
Docker 镜像提供了几种配置 APM Server 的方法。传统做法是通过卷挂载提供配置文件,但也可以创建一个包含您的配置的自定义镜像。
下载此示例配置文件作为起点
curl -L -O https://raw.githubusercontent.com/elastic/apm-server/master/apm-server.docker.yml
在 Docker 上配置 APM Server 的一种方法是通过卷挂载提供 apm-server.docker.yml。在 docker run 命令中,卷挂载可以像这样指定。
docker run -d \
-p 8200:8200 \
--name=apm-server \
--user=apm-server \
--volume="$(pwd)/apm-server.docker.yml:/usr/share/apm-server/apm-server.yml:ro" \
docker.elastic.co/apm/apm-server:9.0.0 \
--strict.perms=false -e \
-E output.elasticsearch.hosts=["elasticsearch:9200"] <1>
- 替换您的 Elasticsearch 主机与端口。
- 如果您使用的是 Elastic Cloud Hosted,请使用前面显示的语法将
-E output.elasticsearch.hosts行替换为 Cloud ID 和 elastic 密码。
之前下载的 apm-server.docker.yml 应当根据您的环境进行自定义。更多详情请参阅配置 APM Server。编辑配置文件并根据您的环境自定义,然后重新部署 APM Server 容器。
也可以将 APM Server 配置嵌入到自定义镜像中。以下是实现此目的的示例 Dockerfile
FROM docker.elastic.co/apm/apm-server:9.0.0
COPY --chmod=0644 --chown=1000:1000 apm-server.yml /usr/share/apm-server/apm-server.yml
您可以在命令行中使用 --ulimit=soft:hard 来设置 nofile ulimit。有关详细信息,请参阅 Docker 文档中的在容器中设置 ulimit (--ulimit)。
docker run -d \
-p 8200:8200 \
--name=apm-server \
--user=apm-server \
--volume="$(pwd)/apm-server.docker.yml:/usr/share/apm-server/apm-server.yml:ro" \
docker.elastic.co/apm/apm-server:9.0.0 \
--strict.perms=false -e \
--ulimit=524287:524287 \
-E output.elasticsearch.hosts=["elasticsearch:9200"] <1>
- 替换为您的 Elasticsearch 主机和端口。
- 如果您使用的是 Elastic Cloud 托管服务,请如上例所示,将
-E output.elasticsearch.hosts行替换为 Cloud ID 和 Elastic 密码。
当作为独立二进制文件运行 APM Server 时,它会从运行该进程的用户处继承 nofile 限制。在大多数系统上,此限制设置为 1024,这对于高吞吐量场景或使用基于尾部的采样时来说太低了。
要选择合适的限制,请考虑以下事项
- 1024 的限制通常对于低吞吐量用例来说足够了。
- 不设置限制不会带来性能方面的负面影响。
- 打开文件的主要来源是传入连接的数量。
- 基于尾部的采样是基于文件的,并且会随着吞吐量和采样策略按比例增加打开文件的数量。
要为您的用户配置限制
- 确定运行 APM Server 进程的用户
whoami
- 以 root 权限编辑
/etc/security/limits.conf
sudo nano /etc/security/limits.conf
添加以下行以设置您的用户的软限制和硬限制
apm-server soft nofile 524287
apm-server hard nofile 524287
- 将
apm-server替换为您用来运行 APM Server 进程的用户名。
要修改正在运行的 APM Server 进程的 nofile 限制
- 获取进程 ID (PID)
pgrep -f apm-server
- 应用新限制
prlimit --pid PID --nofile=524287:524287
- 将
PID替换为您的 APM Server 进程 ID。