Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow connectionString to be overridden multiple times without restarting the AppInsights Agent #3473

Open
slavik112211 opened this issue Dec 23, 2023 · 5 comments
Assignees

Comments

@slavik112211
Copy link

Is your feature request related to a problem? Please describe.
As per your documentation, it's possible to override connection strings per each httpPathPrefix:
https://learn.microsoft.com/en-us/azure/azure-monitor/app/java-standalone-config#connection-string-overrides-preview

{
  "preview": {
    "connectionStringOverrides": [
      {
        "httpPathPrefix": "/myapp1",
        "connectionString": "..."
      },
      {
        "httpPathPrefix": "/myapp2",
        "connectionString": "..."
      }
    ]
  }
}

This config is being processed in the following class of AppInsights Agent: InheritedConnectionStringSpanProcessor.java:L58-L62

I understand that this feature is geared towards a use-case where an app being instrumented with AppInsights is an MVC app, and a connectionStringOverride is applied at a Controller method level, where each Controller method has a different PathPrefix. I.e. this use-case is described in OpenTelemetryAPI documentation as creating a custom span per controller method:
https://opentelemetry.io/docs/instrumentation/java/manual/#create-spans

Describe the solution you would like
We are looking for a way to change connectionString multiple times throughout application lifecycle, not per httpPathPrefix

Ideally we would be able to run ConnectionString.configure() and change to a desired connectionString, and ApplicationInsight agent taking that into account, and posting metrics to a newly specified location.

com.microsoft.applicationinsights.connectionstring.ConnectionString.configure(CONNECTION_STRING)

Currently ConnectionString.configure(CONNECTION_STRING) can be run only once to set ConnectionString:
https://github.com/microsoft/ApplicationInsights-Java/blob/main/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/classicsdk/BytecodeUtilImpl.java#L70-L73

We need a way to determine custom OpenTelemetry spans, and have a different ConnectionString per each of these spans.

Describe alternatives you have considered
Describe any alternative solutions or features you've considered.

Additional context
Our app is not Spring MVC app that has different httpPathPrefix, but is rather a SpringBoot cron-executed org.springframework.boot.CommandLineRunner task, that runs periodically, and needs to send AppInsights telemetry to 3 different AppInsights containers.

@jeanbisutti
Copy link
Member

@slavik112211 You can download here a SNAPSHOT version with the connection string check removed. Could you please try it?

@slavik112211
Copy link
Author

Fantastic, thanks @jeanbisutti.

Will provide feedback early next week.

@slavik112211
Copy link
Author

@jeanbisutti,

confirming that the provided applicationinsights-agent-3.5-SNAPSHOT.jar correctly sets the connectionString multiple times throught the lifecycle of the application using:
com.microsoft.applicationinsights.connectionstring.ConnectionString.configure(connectionString);
Tested using telemetryClient.trackMetric(metric), and confirm that customMetrics end up in different AppInsights endpoints.

Please proceed with merging your change into Production version.

Please note, our app is not using the agent jar directly (i.e. applicationinsights-agent-3.5-SNAPSHOT.jar), but rather uses <artifactId>applicationinsights-runtime-attach</artifactId>, and thus the new agent would need to be incorporated into the new applicationinsights-runtime-attach Maven package.

@jeanbisutti
Copy link
Member

@slavik112211 Thanks for testing. The fix will work both with -javaagent and with the applicationinsights-runtime-attach dependency.

@JRiggenbach
Copy link

@jeanbisutti With a similar need, with the 2.x SDK, we were able to create different TelemetryClient instances that sent data to different application insight resources by setting the respective instrumentation key.

@Configuration
public class TelemetryClientConfiguration {
    @Value("${azure.application-insights.vendorcomm-instrumentation-key}")
    private String vendorCommAppInsightsKey;
  
    @Value("${azure.application-insights.cps-app-insights-instrumentation-key}")
    private String appInsightsKey;

    @Bean("vendorCommTelemetryClient")
    public TelemetryClient vendorCommTelemetryClient() {
        TelemetryClient telemetryClient = new TelemetryClient();
        telemetryClient.getContext().setInstrumentationKey(vendorCommAppInsightsKey);
        return telemetryClient;
    }
  
    @Bean("appInsightsTelemetryClient")
    public TelemetryClient appInsightsTelemetryClient() {
        TelemetryClient telemetryClient = new TelemetryClient();
        telemetryClient.getContext().setInstrumentationKey(appInsightsKey);
        return telemetryClient;
    }
}

However, when moving to the 3.x SDK, it does not appear that we have same capability. I was hoping that something like this would have worked...

@Configuration
public class TelemetryClientConfiguration {
    @Value("${azure.application-insights.connection-string}")
    private String connectionString;

    @Value("${azure.application-insights.vendorcomm-connection-string}")
    private String vendorCommConnectionString;

    @Primary
    @Bean("appInsightsTelemetryClient")
    TelemetryClient telemetryClient() {
        TelemetryClient telemetryClient = new TelemetryClient();
        telemetryClient.getContext().setConnectionString(connectionString);
        return telemetryClient;
    }

    @Bean("vendorCommTelemetryClient")
    TelemetryClient vendorCommTelemetryClient() {
        TelemetryClient telemetryClient = new TelemetryClient();
        telemetryClient.getContext().setConnectionString(vendorCommConnectionString);
        return telemetryClient;
    }
}

...but unfortunately all of the telemetry is being sent to the application insight resource that was set with this line of code...

com.microsoft.applicationinsights.connectionstring.ConnectionString.configure(connectionString);

Is there a way to accomplish what we're attempting to do with having multiple TelemetryClients with different connection strings in the same application?

Thank you for your time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants