Skip to content

Commit

Permalink
Ability to disable Spring Monitor (Azure#36912)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanbisutti authored Sep 22, 2023
1 parent 4671680 commit 0e64e66
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 234 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@

<!-- AzureSpringMonitorConfig defines Spring bean used in https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfiguration.java-->
<suppress checks="com.azure.tools.checkstyle.checks.ExternalDependencyExposedCheck"
files="com.azure.monitor.applicationinsights.spring.AzureSpringMonitorConfig.java"/>
files="com.azure.monitor.applicationinsights.spring.AzureSpringMonitorAutoConfig.java"/>
<!-- To be able to return a configured loggger for self-diagnostics and use it as a Spring bean-->
<suppress checks="com.azure.tools.checkstyle.checks.ExternalDependencyExposedCheck"
files="com.azure.monitor.applicationinsights.spring.selfdiagnostics.*Log.*Config.*.java"/>
Expand Down
6 changes: 5 additions & 1 deletion sdk/spring/spring-cloud-azure-starter-monitor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ docker run -e APPLICATIONINSIGHTS_CONNECTION_STRING="{CONNECTION_STRING}" {image
```
where you have to replace `{CONNECTION_STRING}` and `{image-name}` by your connection string and the native image name.

## Debug your Spring native application
### Debug

If something does not work as expected, you can enable self-diagnostics features at DEBUG level to get some insights.

Expand All @@ -193,6 +193,10 @@ docker run -e APPLICATIONINSIGHTS_SELF_DIAGNOSTICS_LEVEL=DEBUG {image-name}

You have to replace `{image-name}` by your docker image name.

### Disable the monitoring

You can disable the monitoring by setting the `otel.sdk.disabled` property or the `OTEL_SDK_DISABLED` environment variable to true.

## Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,119 @@

package com.azure.monitor.applicationinsights.spring;

import com.azure.core.http.HttpPipeline;
import com.azure.core.util.logging.ClientLogger;
import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.Optional;

/**
* Auto config for Azure Spring Monitor
*/
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore(OpenTelemetryAutoConfiguration.class)
@Import({AzureSpringMonitorConfig.class, AzureSpringMonitorActivationConfig.class})
@ConditionalOnProperty(name = "otel.sdk.disabled", havingValue = "false", matchIfMissing = true)
public class AzureSpringMonitorAutoConfig {

private static final ClientLogger LOGGER = new ClientLogger(AzureSpringMonitorAutoConfig.class);

private static final String CONNECTION_STRING_ERROR_MESSAGE = "Unable to find the Application Insights connection string.";

private final Optional<AzureMonitorExporterBuilder> azureMonitorExporterBuilderOpt;

/**
* Create an instance of AzureSpringMonitorConfig
* @param connectionStringSysProp connection string system property
* @param httpPipeline an instance of HttpPipeline
*/
public AzureSpringMonitorAutoConfig(@Value("${applicationinsights.connection.string:}") String connectionStringSysProp, ObjectProvider<HttpPipeline> httpPipeline) {
this.azureMonitorExporterBuilderOpt = createAzureMonitorExporterBuilder(connectionStringSysProp, httpPipeline);
if (!isNativeRuntimeExecution()) {
LOGGER.warning("You are using Application Insights for Spring in a non-native GraalVM runtime environment. We recommend using the Application Insights Java agent.");
}
}

private static boolean isNativeRuntimeExecution() {
String imageCode = System.getProperty("org.graalvm.nativeimage.imagecode");
return imageCode != null;
}

private Optional<AzureMonitorExporterBuilder> createAzureMonitorExporterBuilder(String connectionStringSysProp, ObjectProvider<HttpPipeline> httpPipeline) {
Optional<String> connectionString = ConnectionStringRetriever.retrieveConnectionString(connectionStringSysProp);
if (connectionString.isPresent()) {
try {
AzureMonitorExporterBuilder azureMonitorExporterBuilder = new AzureMonitorExporterBuilder().connectionString(connectionString.get());
HttpPipeline providedHttpPipeline = httpPipeline.getIfAvailable();
if (providedHttpPipeline != null) {
azureMonitorExporterBuilder = azureMonitorExporterBuilder.httpPipeline(providedHttpPipeline);
}
return Optional.of(azureMonitorExporterBuilder);
} catch (IllegalArgumentException illegalArgumentException) {
String errorMessage = illegalArgumentException.getMessage();
if (errorMessage.contains("InstrumentationKey")) {
LOGGER.warning(CONNECTION_STRING_ERROR_MESSAGE + " Please check you have not used an instrumentation key instead of a connection string");
}
}
} else {
LOGGER.warning(CONNECTION_STRING_ERROR_MESSAGE);
}
return Optional.empty();
}

/**
* Declare a MetricExporter bean
* @return MetricExporter
*/
@Bean
public MetricExporter azureSpringMonitorMetricExporter() {
if (!azureMonitorExporterBuilderOpt.isPresent()) {
return null;
}
return azureMonitorExporterBuilderOpt.get().buildMetricExporter();
}

/**
* Declare a SpanExporter bean
* @return SpanExporter
*/
@Bean
public SpanExporter azureSpringMonitorSpanExporter() {
if (!azureMonitorExporterBuilderOpt.isPresent()) {
return null;
}
return azureMonitorExporterBuilderOpt.get().buildTraceExporter();
}

/**
* Declare a LogRecordExporter bean
* @return LogRecordExporter
*/
@Bean
public LogRecordExporter azureSpringMonitorLogRecordExporter() {
if (!azureMonitorExporterBuilderOpt.isPresent()) {
return null;
}
return azureMonitorExporterBuilderOpt.get().buildLogRecordExporter();
}

/**
* Declare OpenTelemetryVersionCheckRunner bean to check the OpenTelemetry version
* @param resource An OpenTelemetry resource
* @return OpenTelemetryVersionCheckRunner
*/
@Bean
public OpenTelemetryVersionCheckRunner openTelemetryVersionCheckRunner(Resource resource) {
return new OpenTelemetryVersionCheckRunner(resource);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
* This component alerts the user to the fact that the OpenTelemetry version used is not compatible
* with the starter. One use case is Spring Boot 3 using OpenTelemetry.
*/
@Component
public class OpenTelemetryVersionCheckRunner implements CommandLineRunner {
private static final Logger LOG = LoggerFactory.getLogger(OpenTelemetryVersionCheckRunner.class);

Expand Down
Loading

0 comments on commit 0e64e66

Please sign in to comment.