Skip to content

Commit c539ed4

Browse files
Copilottrask
authored andcommitted
Fix Java warnings by suppressing deprecation warnings for backward compatibility
1 parent bda633d commit c539ed4

File tree

10 files changed

+97
-20
lines changed

10 files changed

+97
-20
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/configuration/ConfigurationBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public class ConfigurationBuilder {
104104
private static final String APPLICATIONINSIGHTS_PREVIEW_PROFILER_ENABLEDIAGNOSTICS =
105105
"APPLICATIONINSIGHTS_PREVIEW_PROFILER_ENABLEDIAGNOSTICS";
106106

107-
@Deprecated
107+
// supported for backwards compatibility
108108
private static final String APPLICATIONINSIGHTS_PREVIEW_METRIC_INTERVAL_SECONDS =
109109
"APPLICATIONINSIGHTS_PREVIEW_METRIC_INTERVAL_SECONDS";
110110

@@ -141,6 +141,8 @@ public static Configuration create(
141141
return config;
142142
}
143143

144+
@SuppressWarnings(
145+
"deprecation") // logging warnings for usages of deprecated configuration options
144146
private static void logConfigurationWarnings(Configuration config) {
145147
if (config.instrumentation.micrometer.reportingIntervalSeconds != 60) {
146148
configurationLogger.warn(
@@ -354,6 +356,8 @@ private static void supportTelemetryProcessorsOldSemConv(Configuration config) {
354356
}
355357
}
356358

359+
@SuppressWarnings(
360+
"deprecation") // support deprecated semconv attributes for backwards compatibility
357361
private static String mapAttributeKey(String oldAttributeKey) {
358362
String result = null;
359363
// Common attributes across HTTP client and server spans
@@ -479,6 +483,8 @@ private static boolean isOpenJ9Jvm() {
479483
return jvmName != null && jvmName.contains("OpenJ9");
480484
}
481485

486+
@SuppressWarnings(
487+
"deprecation") // support deprecated configuration options for backwards compatibility
482488
private static void overlayAadEnvVars(
483489
Configuration config, Function<String, String> envVarsFunction) {
484490
String aadAuthString = getEnvVar(APPLICATIONINSIGHTS_AUTHENTICATION_STRING, envVarsFunction);

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/httpclient/LazyHttpClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ public Mono<HttpResponse> send(HttpRequest request, Context context) {
150150
return getDelegate().send(request, context);
151151
}
152152

153+
@SuppressWarnings(
154+
"deprecation") // support deprecated configuration options for backwards compatibility
153155
private static HttpPipelinePolicy getAuthenticationPolicy(
154156
Configuration.AadAuthentication configuration, String aadAudienceWithScope) {
155157
switch (configuration.type) {
@@ -174,6 +176,7 @@ private static HttpPipelinePolicy getAuthenticationPolicyWithUami(
174176
managedIdentityCredential.build(), aadAudienceWithScope);
175177
}
176178

179+
@Deprecated
177180
private static HttpPipelinePolicy getAuthenticationPolicyWithClientSecret(
178181
Configuration.AadAuthentication configuration, String aadAudienceWithScope) {
179182
ClientSecretCredentialBuilder credential =

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/AiContextCustomizer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public AiContextCustomizer(
3434
}
3535

3636
@Override
37+
@SuppressWarnings(
38+
"deprecation") // support deprecated semconv attributes for backwards compatibility
3739
public Context onStart(Context context, R request, Attributes startAttributes) {
3840

3941
// TODO (trask) ideally would also check parentSpanContext !isValid || isRemote

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/DelegatingLogData.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static java.util.Objects.requireNonNull;
77

88
import io.opentelemetry.api.common.Attributes;
9+
import io.opentelemetry.api.common.Value;
910
import io.opentelemetry.api.logs.Severity;
1011
import io.opentelemetry.api.trace.SpanContext;
1112
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
@@ -59,10 +60,16 @@ public String getSeverityText() {
5960
}
6061

6162
@Override
63+
@SuppressWarnings("deprecation") // Implementation of deprecated method
6264
public Body getBody() {
6365
return delegate.getBody();
6466
}
6567

68+
@Override
69+
public Value<?> getBodyValue() {
70+
return delegate.getBodyValue();
71+
}
72+
6673
@Override
6774
public Attributes getAttributes() {
6875
return delegate.getAttributes();
@@ -100,8 +107,8 @@ public String toString() {
100107
+ "severityText="
101108
+ getSeverityText()
102109
+ ", "
103-
+ "body="
104-
+ getBody()
110+
+ "bodyValue="
111+
+ getBodyValue()
105112
+ "}";
106113
}
107114
}

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessor.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ProcessorConfig;
77
import com.microsoft.applicationinsights.agent.internal.processors.AgentProcessor.IncludeExclude;
8+
import io.opentelemetry.api.common.Value;
9+
import io.opentelemetry.api.common.ValueType;
810
import io.opentelemetry.sdk.common.CompletableResultCode;
911
import io.opentelemetry.sdk.logs.data.LogRecordData;
1012
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
@@ -36,12 +38,22 @@ public CompletableResultCode export(Collection<LogRecordData> logs) {
3638

3739
private LogRecordData process(LogRecordData log) {
3840
IncludeExclude include = logProcessor.getInclude();
39-
if (include != null && !include.isMatch(log.getAttributes(), log.getBody().asString())) {
41+
Value<?> bodyValue = log.getBodyValue();
42+
String body;
43+
if (bodyValue == null) {
44+
body = "";
45+
} else if (bodyValue.getType() == ValueType.STRING) {
46+
body = bodyValue.asString();
47+
} else {
48+
// TODO (trask) support complex log bodies
49+
body = "";
50+
}
51+
if (include != null && !include.isMatch(log.getAttributes(), body)) {
4052
// If Not included we can skip further processing
4153
return log;
4254
}
4355
IncludeExclude exclude = logProcessor.getExclude();
44-
if (exclude != null && exclude.isMatch(log.getAttributes(), log.getBody().asString())) {
56+
if (exclude != null && exclude.isMatch(log.getAttributes(), body)) {
4557
return log;
4658
}
4759

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/LogProcessor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import io.opentelemetry.api.common.AttributeKey;
1111
import io.opentelemetry.api.common.Attributes;
1212
import io.opentelemetry.api.common.AttributesBuilder;
13-
import io.opentelemetry.sdk.logs.data.Body;
1413
import io.opentelemetry.sdk.logs.data.LogRecordData;
1514
import java.util.ArrayList;
1615
import java.util.List;
@@ -84,7 +83,7 @@ public LogRecordData processFromAttributes(LogRecordData log) {
8483
updatedLogBuffer.setLength(updatedLogBuffer.length() - separator.length());
8584
}
8685

87-
return new MyLogData(log, existingLogAttributes, Body.string(updatedLogBuffer.toString()));
86+
return new MyLogData(log, existingLogAttributes, updatedLogBuffer.toString());
8887
}
8988

9089
return log;
@@ -107,7 +106,7 @@ public LogRecordData processToAttributes(LogRecordData log) {
107106
applyRule(groupNames.get(i), toAttributeRulePatterns.get(i), bodyAsString, builder);
108107
}
109108

110-
return new MyLogData(log, builder.build(), Body.string(bodyAsString));
109+
return new MyLogData(log, builder.build(), bodyAsString);
111110
}
112111

113112
public static boolean logHasAllFromAttributeKeys(

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/MyLogData.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,40 @@
44
package com.microsoft.applicationinsights.agent.internal.processors;
55

66
import io.opentelemetry.api.common.Attributes;
7+
import io.opentelemetry.api.common.Value;
78
import io.opentelemetry.sdk.logs.data.Body;
89
import io.opentelemetry.sdk.logs.data.LogRecordData;
910

11+
// backwards compatibility is just in case any extensions out there are using custom log processors
12+
@SuppressWarnings("deprecation") // using deprecated Body for backwards compatibility
1013
public class MyLogData extends DelegatingLogData {
1114

1215
private final Attributes attributes;
1316
private final Body body;
17+
private final Value<?> bodyValue;
1418

1519
public MyLogData(LogRecordData delegate, Attributes attributes) {
16-
this(delegate, attributes, delegate.getBody());
20+
this(delegate, attributes, delegate.getBodyValue(), delegate.getBody());
1721
}
1822

19-
public MyLogData(LogRecordData delegate, Attributes attributes, Body body) {
23+
public MyLogData(LogRecordData delegate, Attributes attributes, String body) {
24+
this(delegate, attributes, Value.of(body), Body.string(body));
25+
}
26+
27+
private MyLogData(LogRecordData delegate, Attributes attributes, Value<?> bodyValue, Body body) {
2028
super(delegate);
2129
this.attributes = attributes;
2230
this.body = body;
31+
this.bodyValue = bodyValue;
32+
}
33+
34+
@Override
35+
public Value<?> getBodyValue() {
36+
return bodyValue;
2337
}
2438

2539
@Override
40+
@Deprecated
2641
public Body getBody() {
2742
return body;
2843
}

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/profiler/service/ServiceProfilerClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.microsoft.applicationinsights.agent.internal.profiler.service;
55

66
import com.azure.core.exception.HttpResponseException;
7+
import com.azure.core.http.HttpHeaderName;
78
import com.azure.core.http.HttpMethod;
89
import com.azure.core.http.HttpPipeline;
910
import com.azure.core.http.HttpRequest;
@@ -69,7 +70,7 @@ private static BlobAccessPass getUploadAccess(HttpResponse response) {
6970
if (response.getStatusCode() >= 300) {
7071
throw new HttpResponseException(response);
7172
}
72-
String location = response.getHeaderValue("Location");
73+
String location = response.getHeaderValue(HttpHeaderName.LOCATION);
7374
if (location == null || location.isEmpty()) {
7475
throw new AssertionError("response did not have a location");
7576
}
@@ -86,7 +87,7 @@ public Mono<HttpResponse> executePostWithRedirect(URL requestUrl) {
8687

8788
HttpRequest request = new HttpRequest(HttpMethod.POST, requestUrl);
8889
if (userAgent != null) {
89-
request.setHeader("User-Agent", userAgent);
90+
request.setHeader(HttpHeaderName.USER_AGENT, userAgent);
9091
}
9192
return httpPipeline.send(request);
9293
}

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/SamplingOverrides.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ private StrictMatcher(String key, String value) {
122122
}
123123

124124
@Override
125+
@SuppressWarnings(
126+
"deprecation") // support deprecated semconv attributes for backwards compatibility
125127
public boolean test(
126128
Attributes attributes, LazyHttpUrl lazyHttpUrl, LazyHttpTarget lazyHttpTarget) {
127129
String val = MatcherGroup.getValueIncludingThreadName(attributes, key);
@@ -213,6 +215,8 @@ private KeyOnlyMatcher(String key) {
213215
}
214216

215217
@Override
218+
@SuppressWarnings(
219+
"deprecation") // support deprecated semconv attributes for backwards compatibility
216220
public boolean test(
217221
Attributes attributes,
218222
@Nullable LazyHttpUrl lazyHttpUrl,
@@ -247,6 +251,8 @@ private String get() {
247251
}
248252
}
249253

254+
@SuppressWarnings(
255+
"deprecation") // support deprecated semconv attributes for backwards compatibility
250256
private static boolean getHttpUrlKeyOldOrStableSemconv(AttributeKey<String> key) {
251257
String keyString = key.getKey();
252258
return keyString.equals(HttpIncubatingAttributes.HTTP_URL.getKey())

agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ToAttributeConfig;
1414
import io.opentelemetry.api.common.AttributeKey;
1515
import io.opentelemetry.api.common.Attributes;
16+
import io.opentelemetry.api.common.Value;
17+
import io.opentelemetry.api.common.ValueType;
1618
import io.opentelemetry.sdk.logs.data.LogRecordData;
1719
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
1820
import io.opentelemetry.sdk.testing.logs.TestLogRecordData;
@@ -85,7 +87,10 @@ void simpleRenameLogMessageTest() {
8587
// verify that resulting logs are filtered in the way we want
8688
List<LogRecordData> result = mockExporter.getLogs();
8789
LogRecordData resultLog = result.get(0);
88-
assertThat(resultLog.getBody().asString()).isEqualTo("locationget1234");
90+
Value<?> bodyValue = resultLog.getBodyValue();
91+
assertThat(bodyValue).isNotNull();
92+
assertThat(bodyValue.getType()).isEqualTo(ValueType.STRING);
93+
assertThat(bodyValue.asString()).isEqualTo("locationget1234");
8994
}
9095

9196
@Test
@@ -104,7 +109,10 @@ void simpleRenameLogWithSeparatorTest() {
104109
// verify that resulting logs are filtered in the way we want
105110
List<LogRecordData> result = mockExporter.getLogs();
106111
LogRecordData resultLog = result.get(0);
107-
assertThat(resultLog.getBody().asString()).isEqualTo("location::get::1234");
112+
Value<?> bodyValue = resultLog.getBodyValue();
113+
assertThat(bodyValue).isNotNull();
114+
assertThat(bodyValue.getType()).isEqualTo(ValueType.STRING);
115+
assertThat(bodyValue.asString()).isEqualTo("location::get::1234");
108116
}
109117

110118
@Test
@@ -124,7 +132,10 @@ void simpleRenameLogWithMissingKeysTest() {
124132
// verify that resulting logs are filtered in the way we want
125133
List<LogRecordData> result = mockExporter.getLogs();
126134
LogRecordData resultLog = result.get(0);
127-
assertThat(resultLog.getBody().asString()).isEqualTo("location::get::1234");
135+
Value<?> bodyValue = resultLog.getBodyValue();
136+
assertThat(bodyValue).isNotNull();
137+
assertThat(bodyValue.getType()).isEqualTo(ValueType.STRING);
138+
assertThat(bodyValue.asString()).isEqualTo("location::get::1234");
128139
}
129140

130141
@Test
@@ -169,7 +180,10 @@ void simpleToAttributesTest() {
169180
Objects.requireNonNull(
170181
resultLog.getAttributes().get(AttributeKey.stringKey("documentId"))))
171182
.isEqualTo("12345678");
172-
assertThat(resultLog.getBody().asString()).isEqualTo("/api/v1/document/{documentId}/update");
183+
Value<?> bodyValue = resultLog.getBodyValue();
184+
assertThat(bodyValue).isNotNull();
185+
assertThat(bodyValue.getType()).isEqualTo(ValueType.STRING);
186+
assertThat(bodyValue.asString()).isEqualTo("/api/v1/document/{documentId}/update");
173187
}
174188

175189
@Test
@@ -228,7 +242,10 @@ void multiRuleToAttributesTest() {
228242
Objects.requireNonNull(
229243
resultA.getAttributes().get(AttributeKey.stringKey("password2"))))
230244
.isEqualTo("555"); // The first match is taken to populate the attribute
231-
assertThat(resultA.getBody().asString())
245+
Value<?> bodyValueA = resultA.getBodyValue();
246+
assertThat(bodyValueA).isNotNull();
247+
assertThat(bodyValueA.getType()).isEqualTo(ValueType.STRING);
248+
assertThat(bodyValueA.asString())
232249
.isEqualTo("yyyPassword={password1} aba Pass={password2} xyx Pass={password2} zzz");
233250
assertThat(
234251
Objects.requireNonNull(
@@ -238,7 +255,10 @@ void multiRuleToAttributesTest() {
238255
Objects.requireNonNull(
239256
resultB.getAttributes().get(AttributeKey.stringKey("password1"))))
240257
.isEqualTo("****");
241-
assertThat(resultB.getBody().asString()).isEqualTo("yyyPassword={password1} aba");
258+
Value<?> bodyValueB = resultB.getBodyValue();
259+
assertThat(bodyValueB).isNotNull();
260+
assertThat(bodyValueB.getType()).isEqualTo(ValueType.STRING);
261+
assertThat(bodyValueB.asString()).isEqualTo("yyyPassword={password1} aba");
242262
}
243263

244264
@Test
@@ -264,7 +284,10 @@ void multiMatch() {
264284
List<LogRecordData> result = mockExporter.getLogs();
265285
LogRecordData resultA = result.get(0);
266286

267-
assertThat(resultA.getBody().asString()).isEqualTo("yyyPassword={x} aba Password={x} xyx");
287+
Value<?> bodyValueA = resultA.getBodyValue();
288+
assertThat(bodyValueA).isNotNull();
289+
assertThat(bodyValueA.getType()).isEqualTo(ValueType.STRING);
290+
assertThat(bodyValueA.asString()).isEqualTo("yyyPassword={x} aba Password={x} xyx");
268291
}
269292

270293
@Test
@@ -292,6 +315,9 @@ void simpleRenameLogTestWithLogProcessor() {
292315
// verify that resulting logs are not modified
293316
List<LogRecordData> result = mockExporter.getLogs();
294317
LogRecordData resultLog = result.get(0);
295-
assertThat(resultLog.getBody().asString()).isEqualTo("locationget1234");
318+
Value<?> bodyValue = resultLog.getBodyValue();
319+
assertThat(bodyValue).isNotNull();
320+
assertThat(bodyValue.getType()).isEqualTo(ValueType.STRING);
321+
assertThat(bodyValue.asString()).isEqualTo("locationget1234");
296322
}
297323
}

0 commit comments

Comments
 (0)