为logback.xml Sentry附加程序动态设置Sentry的环境变量 [英] Setting environment variables of Sentry dynamically for logback.xml Sentry appender
问题描述
我的应用程序正在使用slf4j,自然地,我有一个logback.xml文件.我尝试将Sentry追加程序添加到此文件.这是我的logback.xml文件.
My app is using slf4j and naturally, I have a logback.xml file. I try to add a Sentry appender to this file. This is my logback.xml file.
<appender name="CONSOLE-INFO" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<target>System.out</target>
<encoder>
<pattern>%d %p %c{1.} %m%n</pattern>
</encoder>
</appender>
<appender name="CONSOLE-ERROR" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<target>System.error</target>
<encoder>
<pattern>%d %p %c{1.} %m%n%ex{100}</pattern>
</encoder>
</appender>
<appender name="SENTRY" class="io.sentry.logback.SentryAppender">
<dsn>
https://...Sentry dsn
</dsn>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<pattern>%d %p %c{1.} %m%n%ex{100}</pattern>
</encoder>
</appender>
<logger name="kafkaOrderLogger" level="info" additivity="false">
<appender-ref ref="CONSOLE-INFO"/>
</logger>
<logger name="kafkaOrderErrorLogger" level="error" additivity="false">
<appender-ref ref="CONSOLE-ERROR"/>
</logger>
<logger>
<appender-ref ref="SENTRY"/>
</logger>
<root level="info">
<appender-ref ref="CONSOLE-INFO"/>
<appender-ref ref="CONSOLE-ERROR"/>
<appender-ref ref="SENTRY"/>
</root>
我的应用在部署之前已进行了泊坞化,并且作为我们ci/cd的一部分,已将其部署在三个不同的环境中,分别进行预生产和生产.问题是我只能通过应用程序根目录下的属性文件来提供变量.该属性文件将使用基于部署环境的值填充.这意味着我无法在自动填充的资源文件夹中拥有sentry.properties.我想要的是设置Sentry环境,最好不要触摸代码,就像slf4j附加程序的全部要点一样.
My app is dockerized before deployment and as part of our ci/cd it is deployed on three different environment, staging pre and production. The problem is I can provide variables only through a property file at the root of my application. This property file will be filled with values based on the deployment environment This means I cannot have a sentry.properties in the resource folder that is filled automatically. What I want is to have Sentry environment set and preferably without touching code as is the whole point of slf4j appenders.
推荐答案
在运行应用程序时(无论是Java,Node,Python还是使用其他任何官方支持的Sentry SDK都无关紧要),环境变量应为由部署提供,而不是由docker映像或打包的应用程序(jar文件,...)提供.
When running an application (and it does not matter whether it is Java, Node, Python or using any other officially supported Sentry SDK), the environment variables should be provided by the deployment, not the docker image or the packaged application (jar file, ...).
确切放置它们的位置取决于您实际上如何运行Docker容器:
Where exactly to put them depends on how you are actually running the Docker containers:
- 在某些主机上
- 普通
docker run
- 作为
docker-compose
设置的一部分 的服务 - 在Kubernetes部署中
- ...
- plain
docker run
on some host - a service as part of a
docker-compose
setup - inside a Kubernetes Deployment
- ...
在这里我仅描述前两种情况,因为其他类型的部署的策略相同,并且应参考相应工具的文档以了解如何将环境变量注入到容器中.
I will just describe the first two cases here, since the strategy is the same for other types of deployments and documentation of the respective tooling should be consulted for how to inject environment variables into containers.
如果您是通过命令行(或通过脚本)运行单个Docker容器,则只需将以下内容添加到 docker run
命令(
If you are running a single Docker container from the command line (or through scripts), then just add the following to the docker run
command (documentation):
docker run ... -e SENTRY_ENVIRONMENT="<your-env>" ...
docker-compose
...或 docker-compose.yml
内(文档):
...
services:
my-service:
...
environment:
SENTRY_ENVIRONMENT: "<your-env>"
docker-compose设置还支持一个名为 .env
的文件,该文件可用于从外部设置变量并使用
A docker-compose setup also supports a file called .env
, which can be used to set variables from the outside and use variable substitution.
因此,您可以在 docker-compose.yml
中使用以下内容:
So you could use the following in your docker-compose.yml
instead:
...
services:
my-service:
...
environment:
SENTRY_ENVIRONMENT: "${MYENV}"
...并在您的 .env
文件中:
...and in your .env
file:
MYENV=development
当然,您还可以在CI/CD链中生成一个包含环境变量的文件,然后将该文件与您的应用程序一起部署,然后使用 docker run --env-file myapp.env ...
或以下 docker-compose
配置:
Of course, you can also generate a file containing environment variables inside your CI/CD chain and deploy that file with your application, for which you then use docker run --env-file myapp.env ...
or the following docker-compose
configuration:
...
services:
my-service:
...
env_file:
- myapp.env
这篇关于为logback.xml Sentry附加程序动态设置Sentry的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!