Skip to content

Commit d4136a8

Browse files
traskgithub-actions[bot]
andauthored
Syncing changes back from azure-monitor-opentelemetry-exporter (#2784)
(note for reviewing, the json file diffs are much easier with hiding whitespace) Co-authored-by: github-actions[bot] <github-action[bot]@users.noreply.github.com>
1 parent e5f5182 commit d4136a8

File tree

19 files changed

+1048
-979
lines changed

19 files changed

+1048
-979
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,14 +678,24 @@ private static String getConfigPath() {
678678

679679
private static String getWebsiteSiteNameEnvVar() {
680680
String websiteSiteName = getEnvVar(WEBSITE_SITE_NAME);
681-
// TODO we can update this check after the new functions model is deployed.
682-
if (websiteSiteName != null && "java".equals(getEnvVar("FUNCTIONS_WORKER_RUNTIME"))) {
681+
if (websiteSiteName != null && inAzureFunctionsWorker()) {
683682
// special case for Azure Functions
684683
return websiteSiteName.toLowerCase(Locale.ENGLISH);
685684
}
686685
return websiteSiteName;
687686
}
688687

688+
public static boolean inAzureFunctionsConsumptionWorker() {
689+
// for now its the same, but in future should be different check
690+
return inAzureFunctionsWorker();
691+
}
692+
693+
public static boolean inAzureFunctionsWorker() {
694+
// supporting both Azure Functions RP Integration, as well as bring your own agent deployments
695+
// in Azure Functions
696+
return "java".equals(System.getenv("FUNCTIONS_WORKER_RUNTIME"));
697+
}
698+
689699
public static String overlayWithSysPropEnvVar(
690700
String systemPropertyName, String envVarName, String defaultValue) {
691701
String value = getSystemProperty(systemPropertyName);

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

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

66
import com.google.auto.service.AutoService;
7+
import com.microsoft.applicationinsights.agent.internal.configuration.ConfigurationBuilder;
78
import com.microsoft.applicationinsights.agent.internal.httpclient.LazyHttpClient;
89
import io.opentelemetry.javaagent.extension.AgentListener;
910
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
@@ -23,10 +24,8 @@ public void afterAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetr
2324
// triggers loading of java.util.logging (starting with Java 8u231)
2425
// and JBoss/Wildfly need to install their own JUL manager before JUL is initialized.
2526

26-
// TODO we can update this check after the new functions model is deployed.
27-
if (!"java".equals(System.getenv("FUNCTIONS_WORKER_RUNTIME"))) {
28-
// Delay registering and starting AppId retrieval until the connection string becomes
29-
// available for Linux Consumption Plan.
27+
if (!ConfigurationBuilder.inAzureFunctionsConsumptionWorker()) {
28+
// Delay registering and starting AppId retrieval until the connection string is available
3029
appIdSupplier.updateAppId();
3130
}
3231

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration;
2424
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ProcessorConfig;
2525
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration.SamplingTelemetryType;
26+
import com.microsoft.applicationinsights.agent.internal.configuration.ConfigurationBuilder;
2627
import com.microsoft.applicationinsights.agent.internal.configuration.RpConfiguration;
2728
import com.microsoft.applicationinsights.agent.internal.exporter.AgentLogExporter;
2829
import com.microsoft.applicationinsights.agent.internal.exporter.AgentMetricExporter;
@@ -97,8 +98,7 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
9798

9899
Configuration configuration = FirstEntryPoint.getConfiguration();
99100
if (Strings.isNullOrEmpty(configuration.connectionString)) {
100-
// TODO we can update this check after the new functions model is deployed.
101-
if (!"java".equals(System.getenv("FUNCTIONS_WORKER_RUNTIME"))) {
101+
if (!ConfigurationBuilder.inAzureFunctionsConsumptionWorker()) {
102102
throw new FriendlyException(
103103
"No connection string provided", "Please provide connection string.");
104104
}
@@ -172,9 +172,7 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
172172
}
173173
}
174174

175-
// this is for Azure Function Linux consumption plan support.
176-
// TODO we can update this check after the new functions model is deployed.
177-
if ("java".equals(System.getenv("FUNCTIONS_WORKER_RUNTIME"))) {
175+
if (ConfigurationBuilder.inAzureFunctionsConsumptionWorker()) {
178176
AzureFunctions.setup(
179177
() -> telemetryClient.getConnectionString() != null,
180178
new AzureFunctionsInitializer(
@@ -432,7 +430,7 @@ private static SdkLoggerProviderBuilder configureLogging(
432430

433431
builder.addLogRecordProcessor(new AzureMonitorLogProcessor());
434432

435-
if ("java".equals(System.getenv("FUNCTIONS_WORKER_RUNTIME"))) {
433+
if (ConfigurationBuilder.inAzureFunctionsWorker()) {
436434
builder.addLogRecordProcessor(new AzureFunctionsLogProcessor());
437435
}
438436

@@ -474,6 +472,7 @@ private static LogRecordExporter createLogExporter(
474472
LogDataMapper mapper =
475473
new LogDataMapper(
476474
configuration.preview.captureLoggingLevelAsCustomDimension,
475+
ConfigurationBuilder.inAzureFunctionsWorker(),
477476
telemetryClient::populateDefaults);
478477

479478
List<Configuration.SamplingOverride> logSamplingOverrides =

agent/azure-monitor-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public MetricExporter buildMetricExporter() {
252252
*/
253253
public LogRecordExporter buildLogRecordExporter() {
254254
return new AzureMonitorLogRecordExporter(
255-
new LogDataMapper(true, this::populateDefaults), initExporterBuilder());
255+
new LogDataMapper(true, false, this::populateDefaults), initExporterBuilder());
256256
}
257257

258258
private TelemetryItemExporter initExporterBuilder() {

agent/azure-monitor-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/LogDataMapper.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class LogDataMapper {
2626
private static final ClientLogger logger = new ClientLogger(LogDataMapper.class);
2727

2828
private final boolean captureLoggingLevelAsCustomDimension;
29+
private final boolean captureAzureFunctionsAttributes;
2930
private final BiConsumer<AbstractTelemetryBuilder, Resource> telemetryInitializer;
3031

3132
private static final AttributeKey<String> OTEL_LOG4J_MARKER =
@@ -36,8 +37,11 @@ public class LogDataMapper {
3637

3738
public LogDataMapper(
3839
boolean captureLoggingLevelAsCustomDimension,
40+
boolean captureAzureFunctionsAttributes,
3941
BiConsumer<AbstractTelemetryBuilder, Resource> telemetryInitializer) {
42+
4043
this.captureLoggingLevelAsCustomDimension = captureLoggingLevelAsCustomDimension;
44+
this.captureAzureFunctionsAttributes = captureAzureFunctionsAttributes;
4145
this.telemetryInitializer = telemetryInitializer;
4246
}
4347

@@ -144,9 +148,9 @@ private static void setItemCount(
144148
private static final String LOGBACK_MDC_PREFIX = "logback.mdc.";
145149
private static final String JBOSS_LOGGING_MDC_PREFIX = "jboss-logmanager.mdc.";
146150

147-
private static void setExtraAttributes(
151+
private void setExtraAttributes(
148152
AbstractTelemetryBuilder telemetryBuilder, Attributes attributes) {
149-
if ("java".equals(System.getenv("FUNCTIONS_WORKER_RUNTIME"))) {
153+
if (captureAzureFunctionsAttributes) {
150154
setFunctionExtraAttributes(telemetryBuilder, attributes);
151155
}
152156
attributes.forEach(

agent/azure-monitor-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/SpanDataMapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ static boolean applyCommonTags(
834834
new WarningLogger(
835835
SpanDataMapper.class,
836836
AiSemanticAttributes.DEPRECATED_CONNECTION_STRING.getKey()
837-
+ " is no longer supported starting from Application Insights Java 3.4.0, because it"
837+
+ " is no longer supported because it"
838838
+ " is incompatible with pre-aggregated standard metrics. Please use"
839839
+ " \"connectionStringOverrides\" configuration, or reach out to"
840840
+ " https://github.com/microsoft/ApplicationInsights-Java/issues if you have a"
@@ -843,7 +843,7 @@ static boolean applyCommonTags(
843843
new WarningLogger(
844844
SpanDataMapper.class,
845845
AiSemanticAttributes.DEPRECATED_ROLE_NAME.getKey()
846-
+ " is no longer supported starting from Application Insights Java 3.4.0, because it"
846+
+ " is no longer supported because it"
847847
+ " is incompatible with pre-aggregated standard metrics. Please use"
848848
+ " \"roleNameOverrides\" configuration, or reach out to"
849849
+ " https://github.com/microsoft/ApplicationInsights-Java/issues if you have a"
@@ -852,15 +852,15 @@ static boolean applyCommonTags(
852852
new WarningLogger(
853853
SpanDataMapper.class,
854854
AiSemanticAttributes.DEPRECATED_ROLE_INSTANCE.getKey()
855-
+ " is no longer supported starting from Application Insights Java 3.4.0, because it"
855+
+ " is no longer supported because it"
856856
+ " is incompatible with pre-aggregated standard metrics. Please reach out to"
857857
+ " https://github.com/microsoft/ApplicationInsights-Java/issues if you have a use"
858858
+ " case for this.");
859859
private static final WarningLogger instrumentationKeyAttributeNoLongerSupported =
860860
new WarningLogger(
861861
SpanDataMapper.class,
862862
AiSemanticAttributes.DEPRECATED_INSTRUMENTATION_KEY.getKey()
863-
+ " is no longer supported starting from Application Insights Java 3.4.0, because it"
863+
+ " is no longer supported because it"
864864
+ " is incompatible with pre-aggregated standard metrics. Please use"
865865
+ " \"connectionStringOverrides\" configuration, or reach out to"
866866
+ " https://github.com/microsoft/ApplicationInsights-Java/issues if you have a"

agent/azure-monitor-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/logging/AggregatingLogger.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ class AggregatingLogger {
2222
Executors.newSingleThreadScheduledExecutor(
2323
ThreadPoolUtils.createDaemonThreadFactory(AggregatingLogger.class, "aggregating logger"));
2424

25-
private static final String NEWLINE = System.getProperty("line.separator");
26-
2725
private final Logger logger;
2826
private final String grouping;
2927

@@ -170,7 +168,7 @@ public void run() {
170168
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
171169
.forEach(
172170
entry -> {
173-
message.append(NEWLINE);
171+
message.append(System.lineSeparator());
174172
message.append(" * ");
175173
message.append(entry.getKey());
176174
message.append(" (");

agent/azure-monitor-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/logging/NetworkFriendlyExceptions.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public class NetworkFriendlyExceptions {
2929
private static final List<FriendlyExceptionDetector> DETECTORS;
3030
private static final ClientLogger logger = new ClientLogger(NetworkFriendlyExceptions.class);
3131

32-
private static final String NEWLINE = System.getProperty("line.separator");
33-
3432
static {
3533
DETECTORS = new ArrayList<>();
3634
// Note this order is important to determine the right exception!
@@ -96,7 +94,7 @@ private static boolean hasCausedByOfType(Throwable throwable, Class<?> type) {
9694
}
9795

9896
private static String getFriendlyExceptionBanner(String url) {
99-
return "Application Insights Java Agent failed to connect to " + url;
97+
return "Application Insights Java failed to connect to " + url;
10098
}
10199

102100
private static String populateFriendlyMessage(
@@ -180,17 +178,17 @@ private static String getSslFriendlyExceptionAction(String url) {
180178
return "Please import the ROOT SSL certificate from "
181179
+ getHostOnly(url)
182180
+ ", into your custom java key store located at:"
183-
+ NEWLINE
181+
+ System.lineSeparator()
184182
+ customJavaKeyStorePath
185-
+ NEWLINE
183+
+ System.lineSeparator()
186184
+ "Learn more about importing the certificate here: https://go.microsoft.com/fwlink/?linkid=2151450";
187185
}
188186
return "Please import the ROOT SSL certificate from "
189187
+ getHostOnly(url)
190188
+ ", into the default java key store located at:"
191-
+ NEWLINE
189+
+ System.lineSeparator()
192190
+ getJavaCacertsPath()
193-
+ NEWLINE
191+
+ System.lineSeparator()
194192
+ "Learn more about importing the certificate here: https://go.microsoft.com/fwlink/?linkid=2151450";
195193
}
196194

@@ -226,7 +224,7 @@ public String message(String url) {
226224
private static String getUnknownHostFriendlyExceptionAction(String url) {
227225
return "Please update your network configuration so that the host in this url can be resolved: "
228226
+ url
229-
+ NEWLINE
227+
+ System.lineSeparator()
230228
+ "Learn more about troubleshooting unknown host exception here: https://go.microsoft.com/fwlink/?linkid=2185830";
231229
}
232230
}
@@ -294,31 +292,31 @@ private String getCipherFriendlyExceptionAction() {
294292
.append(
295293
"Investigate why the security providers in your Java distribution's"
296294
+ " java.security configuration file differ from a standard Java distribution.")
297-
.append(NEWLINE)
298-
.append(NEWLINE);
295+
.append(System.lineSeparator())
296+
.append(System.lineSeparator());
299297
for (String missingCipher : EXPECTED_CIPHERS) {
300-
actionBuilder.append(" ").append(missingCipher).append(NEWLINE);
298+
actionBuilder.append(" ").append(missingCipher).append(System.lineSeparator());
301299
}
302300
actionBuilder
303-
.append(NEWLINE)
301+
.append(System.lineSeparator())
304302
.append(
305303
"Here are the cipher suites that the JVM does have, in case this is"
306304
+ " helpful in identifying why the ones above are missing:")
307-
.append(NEWLINE);
305+
.append(System.lineSeparator());
308306
for (String foundCipher : cipherSuitesFromJvm) {
309-
actionBuilder.append(foundCipher).append(NEWLINE);
307+
actionBuilder.append(foundCipher).append(System.lineSeparator());
310308
}
311309
// even though we log this info at startup, this info is particularly important for this error
312310
// so we duplicate it here to make sure we get it as quickly and as easily as possible
313311
return actionBuilder
314-
.append(NEWLINE)
312+
.append(System.lineSeparator())
315313
.append("Java version:")
316314
.append(System.getProperty("java.version"))
317315
.append(", vendor: ")
318316
.append(System.getProperty("java.vendor"))
319317
.append(", home: ")
320318
.append(System.getProperty("java.home"))
321-
.append(NEWLINE)
319+
.append(System.lineSeparator())
322320
.append(
323321
"Learn more about troubleshooting this network issue related to cipher suites here:"
324322
+ " https://go.microsoft.com/fwlink/?linkid=2185426")

agent/azure-monitor-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/pipeline/TelemetryItemExporter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ public class TelemetryItemExporter {
3535

3636
private static final ClientLogger logger = new ClientLogger(TelemetryItemExporter.class);
3737

38-
private static final String NEWLINE = System.getProperty("line.separator");
39-
4038
private static final OperationLogger operationLogger =
4139
new OperationLogger(
4240
TelemetryItemExporter.class,
@@ -135,7 +133,7 @@ List<ByteBuffer> encode(List<TelemetryItem> telemetryItems) throws IOException {
135133
try (JsonGenerator jg = mapper.createGenerator(debug)) {
136134
writeTelemetryItems(jg, telemetryItems);
137135
}
138-
logger.verbose("sending telemetry to ingestion service:{}{}", NEWLINE, debug);
136+
logger.verbose("sending telemetry to ingestion service:{}{}", System.lineSeparator(), debug);
139137
}
140138

141139
ByteBufferOutputStream out = new ByteBufferOutputStream(byteBufferPool);

agent/azure-monitor-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/AppConfigurationExporterIntegrationTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.azure.core.http.HttpPipelineNextPolicy;
1111
import com.azure.core.http.HttpResponse;
1212
import com.azure.core.http.policy.HttpPipelinePolicy;
13-
import com.azure.core.test.TestBase;
1413
import com.azure.core.test.TestMode;
1514
import com.azure.core.util.Context;
1615
import com.azure.core.util.FluxUtil;
@@ -35,7 +34,7 @@
3534
import org.junit.jupiter.api.TestInfo;
3635
import reactor.core.publisher.Mono;
3736

38-
public class AppConfigurationExporterIntegrationTest extends TestBase {
37+
public class AppConfigurationExporterIntegrationTest extends MonitorExporterClientTestBase {
3938

4039
@Override
4140
@BeforeEach
@@ -54,9 +53,9 @@ public void setConfigurationTest() throws InterruptedException {
5453
CountDownLatch appConfigCountDown = new CountDownLatch(1);
5554
CountDownLatch exporterCountDown = new CountDownLatch(1);
5655

56+
ValidationPolicy validationPolicy = new ValidationPolicy(exporterCountDown, "AppConfig.setKey");
5757
OpenTelemetry openTelemetry =
58-
TestUtils.createOpenTelemetrySdk(
59-
new ValidationPolicy(exporterCountDown, "AppConfig.setKey"));
58+
TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy));
6059

6160
Tracer tracer = openTelemetry.getTracer("Sample");
6261

@@ -83,9 +82,10 @@ public void testDisableTracing() throws InterruptedException {
8382
CountDownLatch appConfigCountDown = new CountDownLatch(1);
8483
CountDownLatch exporterCountDown = new CountDownLatch(1);
8584

85+
ValidationPolicy validationPolicy =
86+
new ValidationPolicy(exporterCountDown, "disable-config-exporter-testing");
8687
OpenTelemetry openTelemetry =
87-
TestUtils.createOpenTelemetrySdk(
88-
new ValidationPolicy(exporterCountDown, "disable-config-exporter-testing"));
88+
TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy));
8989

9090
Tracer tracer = openTelemetry.getTracer("Sample");
9191

0 commit comments

Comments
 (0)