Setting environment variables of Sentry dynamically for logback.xml Sentry appender

寵の児 提交于 2020-08-08 05:10:51

问题


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>

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.


回答1:


We use environment (shell) variables that are imported automatically by the sentry logback integration.

SENTRY_DSN=https://xxxxxxxx@sentry.io/xxxxxx
SENTRY_TAGS=app:MyApplication,host:$HOSTNAME,project:${CI_PROJECT_NAME},branch:${CI_COMMIT_REF_NAME}
SENTRY_ENVIRONMENT=${CI_COMMIT_REF_NAME}

However there is a big caveat, they are not interpolated.

This happens even if these variables have a value, it will just not get replaced.

Neither $HOSTNAME nor ${CI_COMMIT_REF_NAME} will be resolved. It will result in the string just added verbatim and sent to sentry, as can be seen in the screenshot:



来源:https://stackoverflow.com/questions/59543580/setting-environment-variables-of-sentry-dynamically-for-logback-xml-sentry-appen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!