Skip to content

Support log analytics disabled flag #4133

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.mockserver.model.HttpRequest.request;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

import com.microsoft.applicationinsights.smoketest.schemav2.Data;
import com.microsoft.applicationinsights.smoketest.schemav2.Envelope;
Expand All @@ -19,13 +18,9 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockserver.model.HttpRequest;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(
classes = {OtlpApplication.class},
webEnvironment = RANDOM_PORT)
@UseAgent
abstract class OtlpTest {
abstract class OtlpMetricsTest {

@RegisterExtension
static final SmokeTestExtension testing = SmokeTestExtension.builder().useOtlpEndpoint().build();
Expand All @@ -41,14 +36,14 @@ public void testOtlpTelemetry() throws Exception {

// verify custom histogram metric sent to Application Insights endpoint
List<Envelope> metricList =
testing.mockedIngestion.waitForItems("MetricData", OtlpTest::isHistogramMetric, 1);
testing.mockedIngestion.waitForItems("MetricData", OtlpMetricsTest::isHistogramMetric, 1);
Envelope metricEnvelope = metricList.get(0);
MetricData metricData = (MetricData) ((Data<?>) metricEnvelope.getData()).getBaseData();
assertThat(metricData.getMetrics().get(0).getName()).isEqualTo("histogram-test-otlp-exporter");

// verify stable otel metric sent to Application Insights endpoint
List<Envelope> stableOtelMetrics =
testing.mockedIngestion.waitForItems("MetricData", OtlpTest::isStableOtelMetric, 1);
testing.mockedIngestion.waitForItems("MetricData", OtlpMetricsTest::isStableOtelMetric, 1);
Envelope stableOtelMetricEnvelope = stableOtelMetrics.get(0);
assertThat(
((MetricData) ((Data<?>) stableOtelMetricEnvelope.getData()).getBaseData())
Expand Down Expand Up @@ -117,7 +112,7 @@ private static boolean isStableOtelMetric(Envelope envelope) {

private void verifyStatsbeatSentToBreezeEndpoint() throws Exception {
List<Envelope> statsbeatMetricList =
testing.mockedIngestion.waitForItems("MetricData", OtlpTest::isAttachStatsbeat, 1);
testing.mockedIngestion.waitForItems("MetricData", OtlpMetricsTest::isAttachStatsbeat, 1);
Envelope statsbeatEnvelope = statsbeatMetricList.get(0);
MetricData statsbeatMetricData =
(MetricData) ((Data<?>) statsbeatEnvelope.getData()).getBaseData();
Expand All @@ -126,21 +121,22 @@ private void verifyStatsbeatSentToBreezeEndpoint() throws Exception {
assertThat(statsbeatMetricData.getProperties().get("attach")).isEqualTo("StandaloneAuto");

List<Envelope> features =
testing.mockedIngestion.waitForItems("MetricData", OtlpTest::isFeatureStatsbeat, 2);
testing.mockedIngestion.waitForItems("MetricData", OtlpMetricsTest::isFeatureStatsbeat, 2);
Envelope featureEnvelope = features.get(0);
MetricData featureMetricData = (MetricData) ((Data<?>) featureEnvelope.getData()).getBaseData();
assertThat(featureMetricData.getMetrics().get(0).getName()).isEqualTo("Feature");
assertThat(featureMetricData.getProperties().get("type")).isNotEmpty();

List<Envelope> requestSuccessCounts =
testing.mockedIngestion.waitForItems("MetricData", OtlpTest::isRequestSuccessCount, 1);
testing.mockedIngestion.waitForItems(
"MetricData", OtlpMetricsTest::isRequestSuccessCount, 1);
Envelope rscEnvelope = requestSuccessCounts.get(0);
MetricData rscMetricData = (MetricData) ((Data<?>) rscEnvelope.getData()).getBaseData();
assertThat(rscMetricData.getMetrics().get(0).getName()).isEqualTo("Request_Success_Count");
assertThat(rscMetricData.getProperties().get("endpoint")).isEqualTo("breeze");

List<Envelope> requestDurations =
testing.mockedIngestion.waitForItems("MetricData", OtlpTest::isRequestDuration, 1);
testing.mockedIngestion.waitForItems("MetricData", OtlpMetricsTest::isRequestDuration, 1);
Envelope rdEnvelope = requestDurations.get(0);
MetricData rdMetricData = (MetricData) ((Data<?>) rdEnvelope.getData()).getBaseData();
assertThat(rdMetricData.getMetrics().get(0).getName()).isEqualTo("Request_Duration");
Expand Down Expand Up @@ -180,5 +176,5 @@ private static boolean isRequestDuration(Envelope envelope) {
}

@Environment(TOMCAT_8_JAVA_8)
static class Tomcat8Java8Test extends OtlpTest {}
static class Tomcat8Java8Test extends OtlpMetricsTest {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.applicationinsights.smoketest;

import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.mockserver.model.HttpRequest.request;

import com.microsoft.applicationinsights.smoketest.schemav2.Data;
import com.microsoft.applicationinsights.smoketest.schemav2.Envelope;
import com.microsoft.applicationinsights.smoketest.schemav2.MetricData;
import com.microsoft.applicationinsights.smoketest.schemav2.RequestData;
import io.opentelemetry.proto.metrics.v1.Metric;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockserver.model.HttpRequest;

@UseAgent
abstract class OtlpMetricsWithMetricsToLogAnalyticsDisabledTest {

@RegisterExtension
static final SmokeTestExtension testing =
SmokeTestExtension.builder()
.useOtlpEndpoint()
.setEnvVar("APPLICATIONINSIGHTS_METRICS_TO_LOGANALYTICS_ENABLED", "false")
.build();

@Test
@TargetUri("/ping")
public void testOtlpTelemetry() throws Exception {
// verify request sent to breeze endpoint
List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1);
Envelope rdEnvelope = rdList.get(0);
RequestData rd = (RequestData) ((Data<?>) rdEnvelope.getData()).getBaseData();
assertThat(rd.getName()).isEqualTo("GET /OtlpMetrics/ping");

Thread.sleep(5000);

// verify custom histogram metric are not sent to Application Insights endpoint
assertThat(testing.mockedIngestion.getItemsEnvelopeDataType("MetricData"))
.noneMatch(OtlpMetricsWithMetricsToLogAnalyticsDisabledTest::isHistogramMetric);

// verify stable otel metric are not sent to Application Insights endpoint
assertThat(testing.mockedIngestion.getItemsEnvelopeDataType("MetricData"))
.noneMatch(OtlpMetricsWithMetricsToLogAnalyticsDisabledTest::isStableOtelMetric);

// verify pre-aggregated standard metric sent to Application Insights endpoint
List<Envelope> standardMetrics =
testing.mockedIngestion.waitForStandardMetricItems("requests/duration", 1);
Envelope standardMetricEnvelope = standardMetrics.get(0);
MetricData standardMetricData =
(MetricData) ((Data<?>) standardMetricEnvelope.getData()).getBaseData();
assertThat(standardMetricData.getMetrics().get(0).getName())
.isEqualTo("http.server.request.duration");
assertThat(standardMetricData.getProperties().get("_MS.IsAutocollected")).isEqualTo("True");

// verify Statsbeat sent to the breeze endpoint
verifyStatsbeatSentToBreezeEndpoint();

// verify custom histogram metric 'histogram-test-otlp-exporter' and otel metric
// 'http.server.request.duration' sent to OTLP endpoint
// verify Statsbeat doesn't get sent to OTLP endpoint
verifyMetricsSentToOtlpEndpoint();
}

@SuppressWarnings("PreferJavaTimeOverload")
private void verifyMetricsSentToOtlpEndpoint() {
await()
.atMost(60, SECONDS)
.untilAsserted(
() -> {
HttpRequest[] requests =
testing
.mockedOtlpIngestion
.getCollectorServer()
.retrieveRecordedRequests(request());

// verify metrics
List<Metric> metrics =
testing.mockedOtlpIngestion.extractMetricsFromRequests(requests);
assertThat(metrics)
.extracting(Metric::getName)
.contains("histogram-test-otlp-exporter", "http.server.request.duration")
.doesNotContain("Attach", "Feature"); // statsbeat
});
}

private static boolean isHistogramMetric(Envelope envelope) {
if (envelope.getData().getBaseType().equals("MetricData")) {
MetricData data = (MetricData) ((Data<?>) envelope.getData()).getBaseData();
return data.getMetrics().get(0).getName().equals("histogram-test-otlp-exporter");
}
return false;
}

private static boolean isStableOtelMetric(Envelope envelope) {
if (envelope.getData().getBaseType().equals("MetricData")) {
MetricData data = (MetricData) ((Data<?>) envelope.getData()).getBaseData();
return data.getMetrics().get(0).getName().equals("http.server.request.duration")
&& data.getProperties().get("http.response.status_code") != null;
}
return false;
}

private void verifyStatsbeatSentToBreezeEndpoint() throws Exception {
List<Envelope> statsbeatMetricList =
testing.mockedIngestion.waitForItems(
"MetricData", OtlpMetricsWithMetricsToLogAnalyticsDisabledTest::isAttachStatsbeat, 1);
Envelope statsbeatEnvelope = statsbeatMetricList.get(0);
MetricData statsbeatMetricData =
(MetricData) ((Data<?>) statsbeatEnvelope.getData()).getBaseData();
assertThat(statsbeatMetricData.getMetrics().get(0).getName()).isEqualTo("Attach");
assertThat(statsbeatMetricData.getProperties().get("rp")).isNotNull();
assertThat(statsbeatMetricData.getProperties().get("attach")).isEqualTo("StandaloneAuto");

List<Envelope> features =
testing.mockedIngestion.waitForItems(
"MetricData", OtlpMetricsWithMetricsToLogAnalyticsDisabledTest::isFeatureStatsbeat, 2);
Envelope featureEnvelope = features.get(0);
MetricData featureMetricData = (MetricData) ((Data<?>) featureEnvelope.getData()).getBaseData();
assertThat(featureMetricData.getMetrics().get(0).getName()).isEqualTo("Feature");
assertThat(featureMetricData.getProperties().get("type")).isNotEmpty();

List<Envelope> requestSuccessCounts =
testing.mockedIngestion.waitForItems(
"MetricData",
OtlpMetricsWithMetricsToLogAnalyticsDisabledTest::isRequestSuccessCount,
1);
Envelope rscEnvelope = requestSuccessCounts.get(0);
MetricData rscMetricData = (MetricData) ((Data<?>) rscEnvelope.getData()).getBaseData();
assertThat(rscMetricData.getMetrics().get(0).getName()).isEqualTo("Request_Success_Count");
assertThat(rscMetricData.getProperties().get("endpoint")).isEqualTo("breeze");

List<Envelope> requestDurations =
testing.mockedIngestion.waitForItems(
"MetricData", OtlpMetricsWithMetricsToLogAnalyticsDisabledTest::isRequestDuration, 1);
Envelope rdEnvelope = requestDurations.get(0);
MetricData rdMetricData = (MetricData) ((Data<?>) rdEnvelope.getData()).getBaseData();
assertThat(rdMetricData.getMetrics().get(0).getName()).isEqualTo("Request_Duration");
assertThat(rdMetricData.getProperties().get("endpoint")).isEqualTo("breeze");
}

private static boolean isAttachStatsbeat(Envelope envelope) {
if (envelope.getData().getBaseType().equals("MetricData")) {
MetricData data = (MetricData) ((Data<?>) envelope.getData()).getBaseData();
return data.getMetrics().get(0).getName().equals("Attach");
}
return false;
}

private static boolean isFeatureStatsbeat(Envelope envelope) {
if (envelope.getData().getBaseType().equals("MetricData")) {
MetricData data = (MetricData) ((Data<?>) envelope.getData()).getBaseData();
return data.getMetrics().get(0).getName().equals("Feature");
}
return false;
}

private static boolean isRequestSuccessCount(Envelope envelope) {
if (envelope.getData().getBaseType().equals("MetricData")) {
MetricData data = (MetricData) ((Data<?>) envelope.getData()).getBaseData();
return data.getMetrics().get(0).getName().equals("Request_Success_Count");
}
return false;
}

private static boolean isRequestDuration(Envelope envelope) {
if (envelope.getData().getBaseType().equals("MetricData")) {
MetricData data = (MetricData) ((Data<?>) envelope.getData()).getBaseData();
return data.getMetrics().get(0).getName().equals("Request_Duration");
}
return false;
}

@Environment(TOMCAT_8_JAVA_8)
static class Tomcat8Java8Test extends OtlpMetricsWithMetricsToLogAnalyticsDisabledTest {}
}
Loading