Application Insights支持java的多环境

Application Insights支持java的多环境,java,azure,azure-application-insights,Java,Azure,Azure Application Insights,我们开发了一个web应用程序,使用Java作为NetBeans的后端。我希望从环境(如Dev、Prod、Stage)而不是从XML文件中读取InstrumentationKey。我读了,但还是不知道从哪里开始。任何人都可以帮助我吗?请参阅中com.microsoft.applicationinsights.TelemetryClient和com.microsoft.applicationinsights.telemetry.TelemetryContext类的链接和Javadoc。我建议您参考本

我们开发了一个web应用程序,使用Java作为NetBeans的后端。我希望从环境(如Dev、Prod、Stage)而不是从XML文件中读取InstrumentationKey。我读了,但还是不知道从哪里开始。任何人都可以帮助我吗?

请参阅中com.microsoft.applicationinsights.TelemetryClient和com.microsoft.applicationinsights.telemetry.TelemetryContext类的链接和Javadoc。我建议您参考本office文档,了解如何使用Java使用application insight:

为了避免混淆开发、测试和生产环境中的遥测,您可以根据环境更改仪器键。 您可以在代码中设置插入密钥,而不是从配置XML文件中获取插入密钥。在初始化方法中设置密钥

Java示例代码:

import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.applicationinsights.telemetry.TelemetryContext;

TelemetryClient client = new TelemetryClient();
TelemetryContext context = client.getContext();

// Change instrumentationKey
// Step #1: Get OS Environment Variable

String env = System.getenv("APP_INSIGHTS_ENV");

// Step #2: Get Key from Properties file
Properties props = new Properties();
props.load(...);
String myKey = props.getProperty(env);
Context.setInstrumentationKey(myKey);
此外,您还可以使用C#参考类似的场景:


问候。

您在哪里设置了此代码?我使用的是spring boot,我在主类中设置了密钥,不起作用。@RonaldRandon您能为您的问题和代码创建一个新的SO线程吗?