Skip to content

Commit 0cde273

Browse files
authored
Fix redirection logic for calls to quickpulse (Azure#44211)
* fix * fix style * using url syntax instead of substring * pr comments
1 parent 14f41b8 commit 0cde273

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

sdk/monitor/azure-monitor-opentelemetry-autoconfigure/src/main/java/com/azure/monitor/opentelemetry/autoconfigure/implementation/quickpulse/QuickPulse.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse;
55

66
import com.azure.core.http.HttpPipeline;
7+
import com.azure.core.util.logging.ClientLogger;
8+
import com.azure.core.util.logging.LogLevel;
79
import com.azure.monitor.opentelemetry.autoconfigure.implementation.models.TelemetryItem;
810
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.filtering.FilteringConfiguration;
911
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.LiveMetricsRestAPIsForClientSDKs;
@@ -28,6 +30,8 @@ public class QuickPulse {
2830

2931
private volatile QuickPulseDataCollector collector;
3032

33+
private static final ClientLogger LOGGER = new ClientLogger(QuickPulse.class);
34+
3135
public static QuickPulse create(HttpPipeline httpPipeline, Supplier<URL> endpointUrl,
3236
Supplier<String> instrumentationKey, @Nullable String roleName, @Nullable String roleInstance,
3337
String sdkVersion) {
@@ -69,6 +73,11 @@ public void add(TelemetryItem telemetryItem) {
6973

7074
private void initialize(HttpPipeline httpPipeline, Supplier<URL> endpointUrl, Supplier<String> instrumentationKey,
7175
@Nullable String roleName, @Nullable String roleInstance, String sdkVersion) {
76+
if (LOGGER.canLogAtLevel(LogLevel.VERBOSE)) {
77+
LOGGER.verbose(
78+
"Initializing QuickPulse with instrumentation key: {} , URL {}, rolename {}, role instance {}, sdk version {}",
79+
maskIkey(instrumentationKey.get()), endpointUrl.get().toString(), roleName, roleInstance, sdkVersion);
80+
}
7281

7382
String quickPulseId = UUID.randomUUID().toString().replace("-", "");
7483
ArrayBlockingQueue<MonitoringDataPoint> sendQueue = new ArrayBlockingQueue<>(256, true);
@@ -121,4 +130,8 @@ private void initialize(HttpPipeline httpPipeline, Supplier<URL> endpointUrl, Su
121130

122131
this.collector = collector;
123132
}
133+
134+
private String maskIkey(String ikey) {
135+
return "*" + ikey.substring(ikey.length() - 13);
136+
}
124137
}

sdk/monitor/azure-monitor-opentelemetry-autoconfigure/src/main/java/com/azure/monitor/opentelemetry/autoconfigure/implementation/quickpulse/QuickPulseCoordinator.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.slf4j.MDC;
1010
import reactor.util.annotation.Nullable;
1111

12+
import java.net.MalformedURLException;
13+
import java.net.URL;
1214
import java.util.concurrent.TimeUnit;
1315

1416
import static com.azure.monitor.opentelemetry.autoconfigure.implementation.utils.AzureMonitorMsgId.QUICK_PULSE_PING_ERROR;
@@ -70,7 +72,6 @@ public void run() {
7072

7173
@SuppressWarnings("try")
7274
private long sendData() {
73-
dataSender.setRedirectEndpointPrefix(qpsServiceRedirectedEndpoint);
7475
dataFetcher.prepareQuickPulseDataForSend();
7576

7677
QuickPulseStatus qpStatus = dataSender.getQuickPulseStatus();
@@ -152,7 +153,16 @@ private long ping() {
152153
private QuickPulseStatus handleReceivedPingHeaders(IsSubscribedHeaders pingHeaders) {
153154
String redirectLink = pingHeaders.getXMsQpsServiceEndpointRedirectV2();
154155
if (!Strings.isNullOrEmpty(redirectLink)) {
155-
qpsServiceRedirectedEndpoint = redirectLink;
156+
try {
157+
URL redirectUrl = new URL(redirectLink);
158+
// Taking the QuickPulseService.svc part out if present because the swagger will add that on.
159+
qpsServiceRedirectedEndpoint = redirectUrl.getProtocol() + "://" + redirectUrl.getHost() + "/";
160+
logger.verbose("Handling ping header to redirect to {}", qpsServiceRedirectedEndpoint);
161+
dataSender.setRedirectEndpointPrefix(qpsServiceRedirectedEndpoint);
162+
} catch (MalformedURLException e) {
163+
logger.error("The service returned a malformed URL in the redirect header: {}. Exception message: {}",
164+
redirectLink, e.getMessage());
165+
}
156166
}
157167

158168
String pollingIntervalHeader = pingHeaders.getXMsQpsServicePollingIntervalHint();

sdk/monitor/azure-monitor-opentelemetry-autoconfigure/src/main/java/com/azure/monitor/opentelemetry/autoconfigure/implementation/quickpulse/QuickPulseDataSender.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class QuickPulseDataSender implements Runnable {
5454
this.qpStatus = QuickPulseStatus.QP_IS_OFF;
5555
this.instrumentationKey = instrumentationKey;
5656
this.configuration = configuration;
57+
logger.verbose("QuickPulseDataSender initialized with endpointUrl: {}, instrumentationKey: {}",
58+
endpointUrl.get().toString(), instrumentationKey.get());
5759
}
5860

5961
@Override
@@ -73,6 +75,7 @@ public void run() {
7375
}
7476

7577
long sendTime = System.nanoTime();
78+
// should not include "QuickPulseService.svc/"
7679
String endpointPrefix
7780
= Strings.isNullOrEmpty(redirectEndpointPrefix) ? getQuickPulseEndpoint() : redirectEndpointPrefix;
7881
// TODO (harskaur): for a future PR revisit caching & retry mechanism for failed post requests (shouldn't retry), send "cached" data points in the next post
@@ -88,6 +91,8 @@ public void run() {
8891
}
8992

9093
try {
94+
// the swagger will add on the QuickPulseService.svc/ when creating the request.
95+
logger.verbose("About to publish to quickpulse with the endpoint prefix: {}", endpointPrefix);
9196
Response<CollectionConfigurationInfo> responseMono = liveMetricsRestAPIsForClientSDKs
9297
.publishNoCustomHeadersWithResponseAsync(endpointPrefix, instrumentationKey.get(), etag,
9398
transmissionTimeInTicks, dataPointList)
@@ -122,8 +127,7 @@ public void run() {
122127

123128
} catch (RuntimeException e) { // this includes ServiceErrorException & RuntimeException thrown from quickpulse post api
124129
onPostError(sendTime);
125-
logger.error(
126-
"QuickPulseDataSender received a service error while attempting to send data to quickpulse {}",
130+
logger.error("QuickPulseDataSender received an error while attempting to send data to quickpulse {}",
127131
e.getMessage());
128132
}
129133

sdk/monitor/azure-monitor-opentelemetry-autoconfigure/src/main/java/com/azure/monitor/opentelemetry/autoconfigure/implementation/quickpulse/QuickPulsePingSender.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,15 @@ IsSubscribedHeaders ping(String redirectedEndpoint) {
7878

7979
Date currentDate = new Date();
8080
long transmissionTimeInTicks = currentDate.getTime() * 10000 + TICKS_AT_EPOCH;
81+
// should not include "QuickPulseService.svc/"
8182
String endpointPrefix
8283
= Strings.isNullOrEmpty(redirectedEndpoint) ? getQuickPulseEndpoint() : redirectedEndpoint;
84+
logger.verbose("About to ping quickpulse with the endpoint prefix: {}", endpointPrefix);
8385

8486
long sendTime = System.nanoTime();
8587

8688
try {
89+
// The swagger api appends QuickPulseService.svc/ when creating the request.
8790
Response<CollectionConfigurationInfo> responseMono
8891
= liveMetricsRestAPIsForClientSDKs
8992
.isSubscribedNoCustomHeadersWithResponseAsync(endpointPrefix, instrumentationKey,
@@ -118,9 +121,9 @@ IsSubscribedHeaders ping(String redirectedEndpoint) {
118121
} catch (RuntimeException e) {
119122
// 404 landed here
120123
Throwable t = e.getCause();
121-
if (!NetworkFriendlyExceptions.logSpecialOneTimeFriendlyException(t, getQuickPulseEndpoint(),
124+
if (!NetworkFriendlyExceptions.logSpecialOneTimeFriendlyException(t, endpointPrefix,
122125
friendlyExceptionThrown, logger)) {
123-
operationLogger.recordFailure(t.getMessage() + " (" + endpointPrefix + ")", t, QUICK_PULSE_PING_ERROR);
126+
operationLogger.recordFailure(t.getMessage(), t, QUICK_PULSE_PING_ERROR);
124127
}
125128
}
126129
return onPingError(sendTime);

0 commit comments

Comments
 (0)