Skip to content

Commit

Permalink
Update vm prefix (#36059)
Browse files Browse the repository at this point in the history
* Update vm prefix when metadata service call is successful

* Add copyright

* Don't skip spotbug in spring-native-test

* Fix ci

* Fix

* Revert spotbugs plugin

* Fix ci

* Fix Javadoc

* Feedback

* Add param description

* Fix lint

* Fix lint - extra space

* Fix codesnipet and jacoco plugins
  • Loading branch information
heyams authored Jul 26, 2023
1 parent db9b5d4 commit 6621f61
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

class AzureMetadataService implements Runnable {

Expand All @@ -46,14 +47,16 @@ class AzureMetadataService implements Runnable {
private final AttachStatsbeat attachStatsbeat;
private final CustomDimensions customDimensions;
private final HttpPipeline httpPipeline;
private final Consumer<MetadataInstanceResponse> vmMetadataServiceCallback;

AzureMetadataService(AttachStatsbeat attachStatsbeat, CustomDimensions customDimensions) {
AzureMetadataService(AttachStatsbeat attachStatsbeat, CustomDimensions customDimensions, Consumer<MetadataInstanceResponse> vmMetadataServiceCallback) {
this.attachStatsbeat = attachStatsbeat;
this.customDimensions = customDimensions;
this.httpPipeline =
new HttpPipelineBuilder()
.policies(new UserAgentPolicy(), new RetryPolicy(), new CookiePolicy())
.build();
this.vmMetadataServiceCallback = vmMetadataServiceCallback;
}

void scheduleWithFixedDelay(long interval) {
Expand All @@ -75,6 +78,7 @@ void updateMetadata(String response) throws IOException {

// visible for testing
private void updateMetadata(MetadataInstanceResponse metadataInstanceResponse) {
vmMetadataServiceCallback.accept(metadataInstanceResponse);
attachStatsbeat.updateMetadataInstance(metadataInstanceResponse);
customDimensions.setResourceProvider(ResourceProvider.RP_VM);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.monitor.opentelemetry.exporter.implementation.statsbeat;

public enum RpAttachType {
AUTO,
MANUAL;

private static volatile RpAttachType attachType;

public static void setRpAttachType(RpAttachType type) {
attachType = type;
}

public static RpAttachType getRpAttachType() {
return attachType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
import org.slf4j.MDC;

import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import static com.azure.monitor.opentelemetry.exporter.implementation.utils.AzureMonitorMsgId.FAIL_TO_SEND_STATSBEAT_ERROR;
Expand All @@ -39,14 +42,14 @@ public class StatsbeatModule {

private final AtomicBoolean shutdown = new AtomicBoolean();

public StatsbeatModule() {
public StatsbeatModule(Consumer<MetadataInstanceResponse> vmMetadataServiceCallback) {
customDimensions = new CustomDimensions();
networkStatsbeat = new NetworkStatsbeat(customDimensions);
attachStatsbeat = new AttachStatsbeat(customDimensions);
featureStatsbeat = new FeatureStatsbeat(customDimensions, FeatureType.FEATURE);
instrumentationStatsbeat = new FeatureStatsbeat(customDimensions, FeatureType.INSTRUMENTATION);
nonessentialStatsbeat = new NonessentialStatsbeat(customDimensions);
azureMetadataService = new AzureMetadataService(attachStatsbeat, customDimensions);
azureMetadataService = new AzureMetadataService(attachStatsbeat, customDimensions, vmMetadataServiceCallback);
}

public void start(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void testVirtualMachineResourceProviderId() throws IOException {
source.close();
CustomDimensions customDimensions = new CustomDimensions();
AzureMetadataService azureMetadataService =
new AzureMetadataService(attachStatsbeat, customDimensions);
new AzureMetadataService(attachStatsbeat, customDimensions, (response) -> {});
azureMetadataService.updateMetadata(result);
assertThat("2a1216c3-a2a0-4fc5-a941-b1f5acde7051/65b2f83e-7bf1-4be3-bafc-3a4163265a52")
.isEqualTo(attachStatsbeat.getResourceProviderId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void testParseJsonResponseLinux() throws IOException {

AttachStatsbeat attachStatsbeat = new AttachStatsbeat(new CustomDimensions());
AzureMetadataService azureMetadataService =
new AzureMetadataService(attachStatsbeat, new CustomDimensions());
new AzureMetadataService(attachStatsbeat, new CustomDimensions(), response -> {});
azureMetadataService.updateMetadata(result);

MetadataInstanceResponse response = attachStatsbeat.getMetadataInstanceResponse();
Expand All @@ -52,7 +52,7 @@ public void testParseJsonResponseWindows() throws IOException {

AttachStatsbeat attachStatsbeat = new AttachStatsbeat(new CustomDimensions());
AzureMetadataService azureMetadataService =
new AzureMetadataService(attachStatsbeat, new CustomDimensions());
new AzureMetadataService(attachStatsbeat, new CustomDimensions(), (response) -> {});
azureMetadataService.updateMetadata(result);

MetadataInstanceResponse response = attachStatsbeat.getMetadataInstanceResponse();
Expand Down
103 changes: 62 additions & 41 deletions sdk/monitor/azure-monitor-spring-native-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spotbugs.skip>true</spotbugs.skip>
<codesnippet.skip>true</codesnippet.skip>
</properties>

<dependencies>
<dependency>
<groupId>com.azure.spring</groupId>
Expand All @@ -48,6 +50,65 @@
</dependency>
</dependencies>

<!-- Apply checkstyle, spotbugs, and revapi for all java versions -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version> <!-- {x-version-update;org.apache.maven.plugins:maven-checkstyle-plugin;external_dependency} -->
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.2.2</version> <!-- {x-version-update;com.github.spotbugs:spotbugs-maven-plugin;external_dependency} -->
</plugin>
<plugin>
<groupId>org.revapi</groupId>
<artifactId>revapi-maven-plugin</artifactId>
<version>0.14.6</version> <!-- {x-version-update;org.revapi:revapi-maven-plugin;external_dependency} -->
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>com.azure.tools</groupId>
<artifactId>codesnippet-maven-plugin</artifactId>
<version>1.0.0-beta.8</version> <!-- {x-version-update;com.azure.tools:codesnippet-maven-plugin;external_dependency} -->
<configuration>
<codesnippetRootDirectory>${project.basedir}/azure-resourcemanager/src/samples/java</codesnippetRootDirectory>
<includeSource>false</includeSource>
<skip>${codesnippet.skip}</skip>
</configuration>
<executions>
<execution>
<id>update-codesnippets</id>
<goals>
<goal>update-codesnippet</goal>
</goals>
</execution>
<execution>
<id>verify-codesnippets</id>
<goals>
<goal>verify-codesnippet</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.9</version> <!-- {x-version-update;org.jacoco:jacoco-maven-plugin;external_dependency} -->
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<!-- Library cannot build for Java 17 and below -->
<profile>
Expand All @@ -57,15 +118,6 @@
</activation>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.9</version> <!-- {x-version-update;org.jacoco:jacoco-maven-plugin;external_dependency} -->
<configuration>
<skip>true</skip>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand All @@ -75,7 +127,6 @@
<skip>true</skip>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand All @@ -84,7 +135,6 @@
<skip>true</skip>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
Expand All @@ -93,7 +143,6 @@
<skip>true</skip>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
Expand All @@ -102,36 +151,8 @@
<skip>true</skip>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version> <!-- {x-version-update;org.apache.maven.plugins:maven-checkstyle-plugin;external_dependency} -->
<configuration>
<skip>true</skip>
</configuration>
</plugin>

<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.2.2</version> <!-- {x-version-update;com.github.spotbugs:spotbugs-maven-plugin;external_dependency} -->
<configuration>
<skip>true</skip>
</configuration>
</plugin>

<plugin>
<groupId>org.revapi</groupId>
<artifactId>revapi-maven-plugin</artifactId>
<version>0.14.6</version> <!-- {x-version-update;org.revapi:revapi-maven-plugin;external_dependency} -->
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@

package com.azure.monitor.applicationinsights.spring;

/**
* Azure telemetry activation
*/
public final class AzureTelemetryActivation {

/** a flag to indicate if AzureTelemetry is activated or not. */
private final boolean activated;

/**
* Creates an instance of {@link AzureTelemetryActivation}.
*/
public AzureTelemetryActivation() {
this.activated = true; // We leave the AzureTelemetryActivation class because it could be used to provide the ability
// to disable the starter features
}

/**
* @return true if it's activated.
*/
public boolean isTrue() {
return activated;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Config for AzureTelemetryActivation
*/
@Configuration(proxyBeanMethods = false)
public class AzureTelemetryActivationConfig {

/**
* Declare an AzureTelemetryActivation bean
* @return AzureTelemetryActivation
*/
@Bean
public AzureTelemetryActivation azureTelemetryActivation() {
return new AzureTelemetryActivation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
* Auto config for Azure Telemetry
*/
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore(OpenTelemetryAutoConfiguration.class)
@Import({AzureTelemetryConfig.class, AzureTelemetryActivationConfig.class})
public class AzureTelemetryAutoConfig {


}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import java.util.Optional;

/**
* Config for Azure Telemetry
*/
@Configuration(proxyBeanMethods = false)
public class AzureTelemetryConfig {

Expand All @@ -28,6 +31,12 @@ public class AzureTelemetryConfig {

private final Optional<AzureMonitorExporterBuilder> azureMonitorExporterBuilderOpt;

/**
* Create an instance of AzureTelemetryConfig
* @param connectionStringSysProp connection string system property
* @param azureTelemetryActivation a instance of AzureTelemetryActivation
* @param httpPipeline an instance of HttpPipeline
*/
public AzureTelemetryConfig(@Value("${applicationinsights.connection.string:}") String connectionStringSysProp, AzureTelemetryActivation azureTelemetryActivation, ObjectProvider<HttpPipeline> httpPipeline) {
if (azureTelemetryActivation.isTrue()) {
this.azureMonitorExporterBuilderOpt = createAzureMonitorExporterBuilder(connectionStringSysProp, httpPipeline);
Expand Down Expand Up @@ -67,6 +76,10 @@ private Optional<AzureMonitorExporterBuilder> createAzureMonitorExporterBuilder(
return Optional.empty();
}

/**
* Declare a MetricExporter bean
* @return MetricExporter
*/
@Bean
public MetricExporter metricExporter() {
if (!azureMonitorExporterBuilderOpt.isPresent()) {
Expand All @@ -75,6 +88,10 @@ public MetricExporter metricExporter() {
return azureMonitorExporterBuilderOpt.get().buildMetricExporter();
}

/**
* Declare a SpanExporter bean
* @return SpanExporter
*/
@Bean
public SpanExporter spanExporter() {
if (!azureMonitorExporterBuilderOpt.isPresent()) {
Expand All @@ -83,6 +100,10 @@ public SpanExporter spanExporter() {
return azureMonitorExporterBuilderOpt.get().buildTraceExporter();
}

/**
* Declare a LogRecordExporter bean
* @return LogRecordExporter
*/
@Bean
public LogRecordExporter logRecordExporter() {
if (!azureMonitorExporterBuilderOpt.isPresent()) {
Expand Down
Loading

0 comments on commit 6621f61

Please sign in to comment.