简体   繁体   English

如何使用 bootBuildImage 将 NewRelic 等 APM 代理链接到 Spring 引导应用程序?

[英]How to link an APM agent like NewRelic to a Spring Boot application with bootBuildImage?

I have a gradle based Spring Boot 3 application.我有一个基于 gradle 的 Spring Boot 3 应用程序。 I use the bootBuildImage gradle task in circleci to build a docker image of this application.我在 circleci 中使用 bootBuildImage gradle 任务来构建此应用程序的 docker 映像。

Now, I want to add NewRelic to this application.现在,我想将 NewRelic 添加到此应用程序。 I know I can do it by writing my own Dockerfile but I want to do it by configuring the bootBuildImage gradle task.我知道我可以通过编写自己的 Dockerfile 来完成,但我想通过配置 bootBuildImage gradle 任务来完成。

I saw that I can add buildPacks like this:我看到我可以像这样添加 buildPacks:

tasks.named("bootBuildImage") {
    buildpacks = [...]
}

And it appears that NewRelic has a buildpack here . NewRelic 似乎在这里有一个构建包。

How can I generate the docker image with NewRelic integration?如何使用 NewRelic 集成生成 docker 图像?

Bonus: I need to inject environment variable as NEW_RELIC_ENABLE_AGENT=true|false.奖励:我需要将环境变量注入为 NEW_RELIC_ENABLE_AGENT=true|false。 How can I do it?我该怎么做?

You're on the right track.你在正确的轨道上。 You want to use the New Relic Buildpack that you found.您想要使用找到的New Relic Buildpack

High-level instructions for that buildpack can be found here .可以在此处找到该构建包的高级说明。 It essentially works by taking in bindings (the secret config data) and the buildpack securely maps those values to the standard New Relic agent configuration properties (through env variables).它本质上是通过接受绑定(秘密配置数据)来工作的,buildpack 将这些值安全地映射到标准的 New Relic 代理配置属性(通过环境变量)。

An example of an APM tool configured through bindings can be found here . 可以在此处找到通过绑定配置的 APM 工具示例 The specific example is using a different APM tool, but the same steps will work with any APM tool configured through bindings, like New Relic.具体示例使用不同的 APM 工具,但相同的步骤适用于通过绑定配置的任何 APM 工具,例如 New Relic。

For your app:对于您的应用:

  1. Create a bindings directory.创建绑定目录。 The root of your project is a reasonable place, but the path doesn't ultimately matter.项目的根是一个合理的位置,但路径最终并不重要。 Don't check in binding files that contain secret data:)不要签入包含秘密数据的绑定文件:)

  2. In the folder, create a subfolder called new-relic .在该文件夹中,创建一个名为new-relic的子文件夹。 Again, the name doesn't really matter.同样,名称并不重要。

  3. In the folder from the previous step, create a file called type .在上一步的文件夹中,创建一个名为type的文件。 The name does matter.名字很重要。 In that file, write NewRelic and that's it.在该文件中,写入NewRelic Save the file.保存文件。 This is how the buildpack identifies the bindings.这就是 buildpack 识别绑定的方式。

  4. In the same folder, you can now add additional files to configure New Relic.在同一文件夹中,您现在可以添加其他文件来配置 New Relic。 The name of the file is the key and the contents of the file are the value .文件名是key ,文件内容是value When your app runs, the buildpack will read the bindings and translate these to New Relic configuration settings in the form NEW_RELIC_<KEY>=<VALUE> .当您的应用程序运行时,buildpack 将读取绑定并将它们转换为NEW_RELIC_<KEY>=<VALUE>形式的 New Relic 配置设置。 Thus if you read the New Relic docs and see a property called foo , you could make a file called foo set the value to bar and at runtime, you'll end up with an env variable NEW_RELIC_foo=bar being set.因此,如果您阅读 New Relic 文档并看到一个名为foo的属性,您可以让一个名为foo的文件将值设置为bar ,并且在运行时,您最终会设置一个环境变量NEW_RELIC_foo=bar The New Relic agent reads environment variables for it's configuration, although sometimes it's not the first way that's mentioned in their docs. New Relic 代理读取其配置的环境变量,尽管有时这不是他们的文档中提到的第一种方式。

  5. Next you need to configure your build.gradle file.接下来您需要配置您的build.gradle文件。 These changes will tell bootBuildImage to add the New Relic buildpack and to pass through your bindings.这些更改将告诉bootBuildImage添加 New Relic buildpack 并传递您的绑定。

    • In the tasks.named("bootBuildImage") block, add buildpacks = ["urn:cnb:builder:paketo-buildpacks/java", "gcr.io/paketo-buildpacks/new-relic"] .tasks.named("bootBuildImage")块中,添加buildpacks = ["urn:cnb:builder:paketo-buildpacks/java", "gcr.io/paketo-buildpacks/new-relic"] This will run the standard Java buildpack and then append New Relic onto the end of that list.这将运行标准的 Java buildpack,然后将 append New Relic 运行到该列表的末尾。 Example . 例子

    • Add a bindings list.添加绑定列表。 In the same tasks.named("bootBuildImage") block add bindings = ["path/to/local/bindings/new-relic:/platform/bindings/new-relic"] .在同一个tasks.named("bootBuildImage")块中添加bindings = ["path/to/local/bindings/new-relic:/platform/bindings/new-relic"] This will mount path/to/local/bindings/new-relic on your host to /platform/bindings/new-relic in the container, which is where the buildpack expects bindings to live.这会将主机上的path/to/local/bindings/new-relic挂载到容器中的/platform/bindings/new-relic ,这是 buildpack 期望绑定存在的地方。 You will need to change the first path to point to the local bindings you created above (you can probably use a Gradle variable to the project to reference them, but I don't know if off the top of my head).您将需要更改第一个路径以指向您在上面创建的本地绑定(您可能可以使用项目的 Gradle 变量来引用它们,但我不知道是否脱离了我的头脑)。 Don't change the path on the container side, that needs to be exactly what I put above.不要更改容器端的路径,这需要正是我在上面放置的路径。

  6. Run your build.运行你的构建。 ./gradlew bootBuildImage . ./gradlew bootBuildImage In the output, you should see the New Relic buildpack pass detection (it passes if it finds the type file with NewRelic as the contents) and it should also run and contribute the New Relic agent as is described in the buildpack README.md.在 output 中,您应该看到 New Relic buildpack 通过检测(如果它找到以NewRelic为内容的type文件,它就会通过)并且它还应该运行并贡献 New Relic 代理,如 buildpack README.md 中所述。

  7. After a successful build, you'll have the image.成功构建后,您将拥有图像。 The key to remember is that bindings are not added to the image.要记住的关键是绑定不会添加到图像中。 This is intentional for security reasons.出于安全原因,这是有意的。 You don't want secret binding info to be included in the image, as that will leak your secrets.您不希望图像中包含秘密绑定信息,因为那样会泄露您的秘密。

    This means that you must also pass the bindings through to your container runtime when you run the image.这意味着您还必须在运行图像时将绑定传递给容器运行时。 If you're using Docker, you can docker run --volume path/to/local/bindings/new-relic:/platform/bindings/new-relic... and use the same paths as build time.如果您使用的是 Docker,则可以docker run --volume path/to/local/bindings/new-relic:/platform/bindings/new-relic...并使用与构建时相同的路径。 If you're deploying to Kube.nets, you'll need to set up Secrets in K8s and mount those secrets as files within the container under the same path as before /platform/bindings/new-relic .如果您要部署到 Kube.nets,则需要在 K8s 中设置 Secrets,并将这些 Secret 作为文件安装在容器中,路径与之前相同/platform/bindings/new-relic So you need to make a type file, /platform/bindings/new-relic/type , and files for each key/value parameter you want to set.所以你需要制作一个type文件, /platform/bindings/new-relic/type ,以及你想要设置的每个键/值参数的文件。

At some point in the future, we're working to have all of the APM buildpacks included in the main Java buildpack by default.在未来的某个时候,我们正在努力将所有 APM 构建包默认包含在主 Java 构建包中。 This would eliminate the first config change in step #5.这将消除第 5 步中的第一个配置更改。

Because managing bindings can be kind of a pain, I also have a project called binding-tool that can help with steps 1-3.因为管理绑定可能有点痛苦,所以我还有一个名为绑定工具的项目可以帮助完成步骤 1-3。 It allows you to easily create the binding files, like bt add -t NewRelic -p key1=val1 -p key2=val2 .它允许您轻松创建绑定文件,如bt add -t NewRelic -p key1=val1 -p key2=val2 It's not doing anything magic, just creates the files for you, but I find it handy.它并没有做任何神奇的事情,只是为你创建文件,但我觉得它很方便。 In the future, I want it to generate the Kube.netes YAML as well.将来,我希望它也能生成 Kube.netes YAML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM