|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.microsoft.applicationinsights.smoketest; |
| 5 | + |
| 6 | +import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8; |
| 7 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | +import static org.awaitility.Awaitility.await; |
| 10 | +import static org.mockserver.model.HttpRequest.request; |
| 11 | + |
| 12 | +import com.microsoft.applicationinsights.smoketest.schemav2.Data; |
| 13 | +import com.microsoft.applicationinsights.smoketest.schemav2.Envelope; |
| 14 | +import com.microsoft.applicationinsights.smoketest.schemav2.MetricData; |
| 15 | +import com.microsoft.applicationinsights.smoketest.schemav2.RequestData; |
| 16 | +import io.opentelemetry.proto.metrics.v1.Metric; |
| 17 | +import java.util.List; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 20 | +import org.mockserver.model.HttpRequest; |
| 21 | + |
| 22 | +@UseAgent |
| 23 | +abstract class OtlpMetricsWithMetricsToLogAnalyticsDisabledTest { |
| 24 | + |
| 25 | + @RegisterExtension |
| 26 | + static final SmokeTestExtension testing = |
| 27 | + SmokeTestExtension.builder() |
| 28 | + .useOtlpEndpoint() |
| 29 | + .setEnvVar("APPLICATIONINSIGHTS_METRICS_TO_LOGANALYTICS_ENABLED", "false") |
| 30 | + .build(); |
| 31 | + |
| 32 | + @Test |
| 33 | + @TargetUri("/ping") |
| 34 | + public void testOtlpTelemetry() throws Exception { |
| 35 | + // verify request sent to breeze endpoint |
| 36 | + List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1); |
| 37 | + Envelope rdEnvelope = rdList.get(0); |
| 38 | + RequestData rd = (RequestData) ((Data<?>) rdEnvelope.getData()).getBaseData(); |
| 39 | + assertThat(rd.getName()).isEqualTo("GET /OtlpMetrics/ping"); |
| 40 | + |
| 41 | + Thread.sleep(5000); |
| 42 | + |
| 43 | + // verify custom histogram metric are not sent to Application Insights endpoint |
| 44 | + assertThat(testing.mockedIngestion.getItemsEnvelopeDataType("MetricData")) |
| 45 | + .noneMatch(OtlpMetricsWithMetricsToLogAnalyticsDisabledTest::isHistogramMetric); |
| 46 | + |
| 47 | + // verify stable otel metric are not sent to Application Insights endpoint |
| 48 | + assertThat(testing.mockedIngestion.getItemsEnvelopeDataType("MetricData")) |
| 49 | + .noneMatch(OtlpMetricsWithMetricsToLogAnalyticsDisabledTest::isStableOtelMetric); |
| 50 | + |
| 51 | + // verify pre-aggregated standard metric sent to Application Insights endpoint |
| 52 | + List<Envelope> standardMetrics = |
| 53 | + testing.mockedIngestion.waitForStandardMetricItems("requests/duration", 1); |
| 54 | + Envelope standardMetricEnvelope = standardMetrics.get(0); |
| 55 | + MetricData standardMetricData = |
| 56 | + (MetricData) ((Data<?>) standardMetricEnvelope.getData()).getBaseData(); |
| 57 | + assertThat(standardMetricData.getMetrics().get(0).getName()) |
| 58 | + .isEqualTo("http.server.request.duration"); |
| 59 | + assertThat(standardMetricData.getProperties().get("_MS.IsAutocollected")).isEqualTo("True"); |
| 60 | + |
| 61 | + // verify Statsbeat sent to the breeze endpoint |
| 62 | + verifyStatsbeatSentToBreezeEndpoint(); |
| 63 | + |
| 64 | + // verify custom histogram metric 'histogram-test-otlp-exporter' and otel metric |
| 65 | + // 'http.server.request.duration' sent to OTLP endpoint |
| 66 | + // verify Statsbeat doesn't get sent to OTLP endpoint |
| 67 | + verifyMetricsSentToOtlpEndpoint(); |
| 68 | + } |
| 69 | + |
| 70 | + @SuppressWarnings("PreferJavaTimeOverload") |
| 71 | + private void verifyMetricsSentToOtlpEndpoint() { |
| 72 | + await() |
| 73 | + .atMost(60, SECONDS) |
| 74 | + .untilAsserted( |
| 75 | + () -> { |
| 76 | + HttpRequest[] requests = |
| 77 | + testing |
| 78 | + .mockedOtlpIngestion |
| 79 | + .getCollectorServer() |
| 80 | + .retrieveRecordedRequests(request()); |
| 81 | + |
| 82 | + // verify metrics |
| 83 | + List<Metric> metrics = |
| 84 | + testing.mockedOtlpIngestion.extractMetricsFromRequests(requests); |
| 85 | + assertThat(metrics) |
| 86 | + .extracting(Metric::getName) |
| 87 | + .contains("histogram-test-otlp-exporter", "http.server.request.duration") |
| 88 | + .doesNotContain("Attach", "Feature"); // statsbeat |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + private static boolean isHistogramMetric(Envelope envelope) { |
| 93 | + if (envelope.getData().getBaseType().equals("MetricData")) { |
| 94 | + MetricData data = (MetricData) ((Data<?>) envelope.getData()).getBaseData(); |
| 95 | + return data.getMetrics().get(0).getName().equals("histogram-test-otlp-exporter"); |
| 96 | + } |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + private static boolean isStableOtelMetric(Envelope envelope) { |
| 101 | + if (envelope.getData().getBaseType().equals("MetricData")) { |
| 102 | + MetricData data = (MetricData) ((Data<?>) envelope.getData()).getBaseData(); |
| 103 | + return data.getMetrics().get(0).getName().equals("http.server.request.duration") |
| 104 | + && data.getProperties().get("http.response.status_code") != null; |
| 105 | + } |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + private void verifyStatsbeatSentToBreezeEndpoint() throws Exception { |
| 110 | + List<Envelope> statsbeatMetricList = |
| 111 | + testing.mockedIngestion.waitForItems( |
| 112 | + "MetricData", OtlpMetricsWithMetricsToLogAnalyticsDisabledTest::isAttachStatsbeat, 1); |
| 113 | + Envelope statsbeatEnvelope = statsbeatMetricList.get(0); |
| 114 | + MetricData statsbeatMetricData = |
| 115 | + (MetricData) ((Data<?>) statsbeatEnvelope.getData()).getBaseData(); |
| 116 | + assertThat(statsbeatMetricData.getMetrics().get(0).getName()).isEqualTo("Attach"); |
| 117 | + assertThat(statsbeatMetricData.getProperties().get("rp")).isNotNull(); |
| 118 | + assertThat(statsbeatMetricData.getProperties().get("attach")).isEqualTo("StandaloneAuto"); |
| 119 | + |
| 120 | + List<Envelope> features = |
| 121 | + testing.mockedIngestion.waitForItems( |
| 122 | + "MetricData", OtlpMetricsWithMetricsToLogAnalyticsDisabledTest::isFeatureStatsbeat, 2); |
| 123 | + Envelope featureEnvelope = features.get(0); |
| 124 | + MetricData featureMetricData = (MetricData) ((Data<?>) featureEnvelope.getData()).getBaseData(); |
| 125 | + assertThat(featureMetricData.getMetrics().get(0).getName()).isEqualTo("Feature"); |
| 126 | + assertThat(featureMetricData.getProperties().get("type")).isNotEmpty(); |
| 127 | + |
| 128 | + List<Envelope> requestSuccessCounts = |
| 129 | + testing.mockedIngestion.waitForItems( |
| 130 | + "MetricData", |
| 131 | + OtlpMetricsWithMetricsToLogAnalyticsDisabledTest::isRequestSuccessCount, |
| 132 | + 1); |
| 133 | + Envelope rscEnvelope = requestSuccessCounts.get(0); |
| 134 | + MetricData rscMetricData = (MetricData) ((Data<?>) rscEnvelope.getData()).getBaseData(); |
| 135 | + assertThat(rscMetricData.getMetrics().get(0).getName()).isEqualTo("Request_Success_Count"); |
| 136 | + assertThat(rscMetricData.getProperties().get("endpoint")).isEqualTo("breeze"); |
| 137 | + |
| 138 | + List<Envelope> requestDurations = |
| 139 | + testing.mockedIngestion.waitForItems( |
| 140 | + "MetricData", OtlpMetricsWithMetricsToLogAnalyticsDisabledTest::isRequestDuration, 1); |
| 141 | + Envelope rdEnvelope = requestDurations.get(0); |
| 142 | + MetricData rdMetricData = (MetricData) ((Data<?>) rdEnvelope.getData()).getBaseData(); |
| 143 | + assertThat(rdMetricData.getMetrics().get(0).getName()).isEqualTo("Request_Duration"); |
| 144 | + assertThat(rdMetricData.getProperties().get("endpoint")).isEqualTo("breeze"); |
| 145 | + } |
| 146 | + |
| 147 | + private static boolean isAttachStatsbeat(Envelope envelope) { |
| 148 | + if (envelope.getData().getBaseType().equals("MetricData")) { |
| 149 | + MetricData data = (MetricData) ((Data<?>) envelope.getData()).getBaseData(); |
| 150 | + return data.getMetrics().get(0).getName().equals("Attach"); |
| 151 | + } |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + private static boolean isFeatureStatsbeat(Envelope envelope) { |
| 156 | + if (envelope.getData().getBaseType().equals("MetricData")) { |
| 157 | + MetricData data = (MetricData) ((Data<?>) envelope.getData()).getBaseData(); |
| 158 | + return data.getMetrics().get(0).getName().equals("Feature"); |
| 159 | + } |
| 160 | + return false; |
| 161 | + } |
| 162 | + |
| 163 | + private static boolean isRequestSuccessCount(Envelope envelope) { |
| 164 | + if (envelope.getData().getBaseType().equals("MetricData")) { |
| 165 | + MetricData data = (MetricData) ((Data<?>) envelope.getData()).getBaseData(); |
| 166 | + return data.getMetrics().get(0).getName().equals("Request_Success_Count"); |
| 167 | + } |
| 168 | + return false; |
| 169 | + } |
| 170 | + |
| 171 | + private static boolean isRequestDuration(Envelope envelope) { |
| 172 | + if (envelope.getData().getBaseType().equals("MetricData")) { |
| 173 | + MetricData data = (MetricData) ((Data<?>) envelope.getData()).getBaseData(); |
| 174 | + return data.getMetrics().get(0).getName().equals("Request_Duration"); |
| 175 | + } |
| 176 | + return false; |
| 177 | + } |
| 178 | + |
| 179 | + @Environment(TOMCAT_8_JAVA_8) |
| 180 | + static class Tomcat8Java8Test extends OtlpMetricsWithMetricsToLogAnalyticsDisabledTest {} |
| 181 | +} |
0 commit comments