【问题标题】:Quarkus: MDC is not exposed in Azure Application InsightsQuarkus:MDC 未在 Azure Application Insights 中公开
【发布时间】:2021-04-09 10:19:56
【问题描述】:

如何让 MDC 信息显示在 Azure Insights 中。 目前我只在跟踪日志中看到它。

我正在使用:

  1. Quarkus 1.13.0.Final
  2. Application Insights 3.0

我已根据这些说明插入 Application Insights => Java codeless application monitoring Azure Monitor Application Insights

源代码

...
import org.slf4j.MDC;


@Provider
@ApplicationScoped
public class DemoRequestFilter implements ContainerRequestFilter {

    private static final String DEMO_TAG = "demoTag";

    @Override
    public void filter(final ContainerRequestContext context) {
        MDC.put(DEMO_TAG, "myDemoTag");
  
    }

}

application.yaml

log:
    console:
      format: "%d{HH:mm:ss} %-5p [%c{2.}] (%t) requestDemoTag=[%X{demoTag}]  %s%e%n"

感谢您的帮助

【问题讨论】:

    标签: azure logback slf4j azure-application-insights quarkus


    【解决方案1】:

    你的意思是你只能在这个地方看到 MDC 信息吗?你的目标是什么?

    ==============================更新================= ==============

    我将在项目中分享我的配置。如果我们需要在我们的程序中添加appinsights java代理,可以关注this tutorial

    首先,下载教程中提到的jar文件你可以保存在你的工作空间中,我存放在路径:D:/applicationinsights-agent-3.0.2.jar

    然后我在与 jar 文件相同的文件夹中创建了一个文件“applicationinsights.json”。在 json 文件中,我设置了 Azure 应用程序洞察实例的连接字符串。 Creating a new application insights 然后我可以在概览页面中获取连接字符串。

    接下来,我需要设置 jvm 参数。我使用sts作为ide,所以菜单栏->窗口->首选项-> Java->已安装的JRE->双击你使用的那个->默认vm参数设置值-Xmx512m -XX:+UseG1GC -javaagent:D:/applicationinsights-agent-3.0.2.jar

    添加MDC代码并启动程序,然后我可以在答案的顶部得到结果。

    如果你需要将你的程序部署到azure app service,official doc说agent appinsights不支持app service,但我发现another answer证明这个功能没问题。那就是

    为应用服务启用应用洞察并指定应用 见解实例并添加此应用程序设置; XDT_MicrosoftApplicationInsights_Java -> 1

    看看我这边的log4j2配置。

    log4j2.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="FATAL">
        <Properties>
            <Property name="pattern">%d{HH:mm:ss} %-5p [%c{2.}] (%t) requestDemoTag=[%X{demoTag}]  %s%e%n</Property>
            <Property name="logPath">logs</Property>
        </Properties>
        <Appenders>
            <Console name="console" target="SYSTEM_OUT">
                <PatternLayout pattern="${pattern}"/>
            </Console>
        </Appenders>
        <Loggers>
            <Root level="INFO">
                <AppenderRef ref="Console"/>
            </Root>
        </Loggers>
    </Configuration>
    

    【讨论】:

    • 我只在 StackTrace 中看到了 MDC 信息。就像您的示例的左侧图片一样。不幸的是,我在 Azure 中看不到 MDC 信息。也不在自定义属性中。我的目标是在自定义属性中播放 MDC 信息。
    • 感谢您的回复先生,我已经更新了我的答案并分享了我的配置。如果您还有其他问题,请随时添加评论。
    • 感谢您的帮助。我已经实现了描述的所有步骤。 Azure Application Insights 中也检测到我的服务。即使我在 applicationsinsights.json 中设置环境变量,此变量也会显示在 CustomProperties 中。只有MDC信息不显示
    • 我已经在log4j2上添加了我的配置,也许你可以和你的比较一下,得到一些发现。
    • 感谢您的 log4j2 设置。通过 SpringBoot 项目,MDC 也显示在 Azure Application Insights 中。但是,不适用于 Quarkus 项目。所以我认为这是 Quarkus、sl4j 和 Azure Application Insights 之间的问题。 MDC 信息在某处丢失
    猜你喜欢
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    相关资源
    最近更新 更多