我正在尝试在古巴平台中使用 Azure Application Insight:https://learn.microsoft.com/en-us/azure/application-insights/app-insights-java-get-started
我设法将库添加到 Cuba Platform,特别是 Web 模块,但我无法以任何方式设置“仪器密钥”。
有以下三种方法:
1- 将ApplicationInsights.xml放入“资源”文件夹中
我找不到放置 TelemetryConfigurationFactory 类要读取的文件的位置。在内部,我看到它使用 getResource() 来扫描各种“合理”的地方。 我尝试了 WEB-INF、META-INF、Tomcat 中的 conf 目录、conf/app、java 包的根目录、Tomcat 中的 work/app 以及可能更多的内容,但没有结果。
2- 系统属性:-DAPPLICATION_INSIGHTS_IKEY=your_ikey
3- 环境变量:APPLICATION_INSIGHTS_IKEY
在 docker 容器中尝试了这两种方法,在本地尝试了最后一种方法:没有结果。特别是,即使在本地手动导出变量后,System.getEnv 也会返回 null,所以这可能是我这边的一些错误
欢迎任何见解:D
我尝试从Application Insights源代码中搜索Instrumentation key的配置。
我可以在TelemetryConfigurationFactory Class中看到如下代码片段。
/**
* Setting an instrumentation key:
* First we try the system property '-DAPPLICATION_INSIGHTS_IKEY=i_key' or '-DAPPINSIGHTS_INSTRUMENTATIONKEY=i_key'
* Next we will try the environment variable 'APPLICATION_INSIGHTS_IKEY' or 'APPINSIGHTS_INSTRUMENTATIONKEY'
* Next we will try to fetch the i-key from the ApplicationInsights.xml
* @param userConfiguration The configuration that was represents the user's configuration in ApplicationInsights.xml.
* @param configuration The configuration class.
*/
private void setInstrumentationKey(ApplicationInsightsXmlConfiguration userConfiguration, TelemetryConfiguration configuration) {
try {
String ikey;
// First, check whether an i-key was provided as a java system property i.e. '-DAPPLICATION_INSIGHTS_IKEY=i_key', or '-DAPPINSIGHTS_INSTRUMENTATIONKEY=i_key'
ikey = System.getProperty(EXTERNAL_PROPERTY_IKEY_NAME);
if (!Strings.isNullOrEmpty(ikey)) {
configuration.setInstrumentationKey(ikey);
return;
}
ikey = System.getProperty(EXTERNAL_PROPERTY_IKEY_NAME_SECONDARY);
if (!Strings.isNullOrEmpty(ikey)) {
configuration.setInstrumentationKey(ikey);
return;
}
// Second, try to find the i-key as an environment variable 'APPLICATION_INSIGHTS_IKEY' or 'APPINSIGHTS_INSTRUMENTATIONKEY'
ikey = System.getenv(EXTERNAL_PROPERTY_IKEY_NAME);
if (!Strings.isNullOrEmpty(ikey)) {
configuration.setInstrumentationKey(ikey);
return;
}
ikey = System.getenv(EXTERNAL_PROPERTY_IKEY_NAME_SECONDARY);
if (!Strings.isNullOrEmpty(ikey)) {
configuration.setInstrumentationKey(ikey);
return;
}
// Else, try to find the i-key in ApplicationInsights.xml
if (userConfiguration != null) {
ikey = userConfiguration.getInstrumentationKey();
if (ikey == null) {
return;
}
ikey = ikey.trim();
if (ikey.length() == 0) {
return;
}
configuration.setInstrumentationKey(ikey);
}
} catch (Exception e) {
InternalLogger.INSTANCE.error("Failed to set instrumentation key: '%s'", e.getMessage());
}
}
仪表键似乎可以配置为
java system property
或 environment variable
或 ApplicationInsights.xml
。
根据您的描述,您代码中的System.getEnv("XXX")返回null。 您可以检查以下可能的原因:
使用“我的电脑”>“高级”>“环境变量”使变量对所有新进程可见。
2.尝试读取变量的进程已经在运行。重新启动。
3.Tomcat的启动脚本在调用java.exe之前取消设置变量
4.Tomcat 取消设置其 Java 代码中的变量。
另外,你可以参考这个帖子: 当环境变量存在时,System.getenv() returns null.
希望对您有帮助。