Skip to content

Commit

Permalink
Refactoring (#2091)
Browse files Browse the repository at this point in the history
* Don't modify swagger generated files

* Updates

* MORE

* MORE

* more

* more

* more

* more

* Rename to builder

* Cleanup

* Cleanup

* Cleanup

* Rename package

* Moves

* Fix

* Oops

* errorprone

* Sync

* spotless

* empty

* Simplify TelemetryClient

* And network statsbeat

* Remove shared ikey redirect cache

* Simplify public surface of local storage components

* Network friendly exceptions

* Pass real url to listener

* Better name

* Start breaking dependencies on apache commons

* Spotless

* errorprone

* Removing apache commons

* renames

* feedback

* Remove "channel" name
  • Loading branch information
trask authored Feb 8, 2022
1 parent 327b970 commit 7ddb5c5
Show file tree
Hide file tree
Showing 52 changed files with 1,311 additions and 1,009 deletions.
2 changes: 0 additions & 2 deletions agent/agent-tooling/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ dependencies {
implementation("net.bytebuddy:byte-buddy")

implementation("commons-codec:commons-codec")
implementation("org.apache.commons:commons-lang3")
implementation("commons-io:commons-io")
implementation("org.apache.commons:commons-text")
// TODO (trask) this is probably still needed for above apache commons projects
implementation("org.slf4j:jcl-over-slf4j")
Expand Down
1 change: 0 additions & 1 deletion agent/agent-tooling/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ com.squareup.moshi:moshi:1.11.0=runtimeClasspath
com.squareup.okhttp3:okhttp:4.9.3=runtimeClasspath
com.squareup.okio:okio:2.8.0=runtimeClasspath
commons-codec:commons-codec:1.15=runtimeClasspath
commons-io:commons-io:2.7=runtimeClasspath
io.netty:netty-bom:4.1.72.Final=runtimeClasspath
io.netty:netty-buffer:4.1.72.Final=runtimeClasspath
io.netty:netty-codec-dns:4.1.72.Final=runtimeClasspath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
package com.microsoft.applicationinsights.agent.internal.common;

import java.lang.management.ManagementFactory;
import org.apache.commons.lang3.SystemUtils;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -32,18 +32,28 @@ public class SystemInformation {

private static final String DEFAULT_PROCESS_NAME = "Java_Process";

private static final boolean WINDOWS;
private static final boolean LINUX;

static {
String osName = System.getProperty("os.name");
String osNameLower = osName == null ? null : osName.toLowerCase(Locale.ENGLISH);
WINDOWS = osNameLower != null && osNameLower.startsWith("windows");
LINUX = osNameLower != null && osNameLower.startsWith("linux");
}

private static final String processId = initializeProcessId();

public static String getProcessId() {
return processId;
}

public static boolean isWindows() {
return SystemUtils.IS_OS_WINDOWS;
return WINDOWS;
}

public static boolean isUnix() {
return SystemUtils.IS_OS_UNIX;
public static boolean isLinux() {
return LINUX;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -107,24 +106,19 @@ public void initialize(TelemetryClient telemetryClient) {
public boolean addHeartBeatProperty(
String propertyName, String propertyValue, boolean isHealthy) {

boolean isAdded = false;
if (!StringUtils.isEmpty(propertyName)) {
if (!heartbeatProperties.containsKey(propertyName)) {
HeartBeatPropertyPayload payload = new HeartBeatPropertyPayload();
payload.setHealthy(isHealthy);
payload.setPayloadValue(propertyValue);
heartbeatProperties.put(propertyName, payload);
isAdded = true;
logger.trace("added heartbeat property {} - {}", propertyName, propertyValue);
} else {
logger.trace(
"heartbeat property {} cannot be added twice. Please use setHeartBeatProperty instead to modify the value",
propertyName);
}
} else {
logger.warn("cannot add property without property name");
if (heartbeatProperties.containsKey(propertyName)) {
logger.trace(
"heartbeat property {} cannot be added twice. Please use setHeartBeatProperty instead to modify the value",
propertyName);
return false;
}
return isAdded;

HeartBeatPropertyPayload payload = new HeartBeatPropertyPayload();
payload.setHealthy(isHealthy);
payload.setPayloadValue(propertyValue);
heartbeatProperties.put(propertyName, payload);
logger.trace("added heartbeat property {} - {}", propertyName, propertyValue);
return true;
}

public long getHeartBeatInterval() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

package com.microsoft.applicationinsights.agent.internal.httpclient;

import static java.util.Arrays.asList;

import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpPipelineBuilder;
Expand All @@ -29,17 +31,18 @@
import com.azure.core.http.ProxyOptions;
import com.azure.core.http.netty.NettyAsyncHttpClientBuilder;
import com.azure.core.http.policy.BearerTokenAuthenticationPolicy;
import com.azure.core.http.policy.DefaultRedirectStrategy;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.HttpLoggingPolicy;
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.core.http.policy.RedirectPolicy;
import com.azure.core.util.Context;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.azure.identity.ManagedIdentityCredential;
import com.azure.identity.ManagedIdentityCredentialBuilder;
import com.azure.identity.VisualStudioCodeCredential;
import com.azure.identity.VisualStudioCodeCredentialBuilder;
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration;
import io.opentelemetry.instrumentation.api.cache.Cache;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -125,16 +128,19 @@ private static HttpClient init() {
.build();
}

// pass non-null ikeyRedirectCache if you want to use ikey-specific redirect policy
public static HttpPipeline newHttpPipeLineWithDefaultRedirect(
@Nullable Configuration.AadAuthentication aadConfiguration) {
return newHttpPipeLine(aadConfiguration, new RedirectPolicy(new DefaultRedirectStrategy()));
}

public static HttpPipeline newHttpPipeLine(
@Nullable Configuration.AadAuthentication aadConfiguration,
@Nullable Cache<String, String> ikeyRedirectCache) {
HttpPipelinePolicy... additionalPolicies) {
List<HttpPipelinePolicy> policies = new ArrayList<>();
// Redirect policy to handle v2.1/track redirects (and other redirects too, e.g. profiler)
policies.add(new RedirectPolicy(ikeyRedirectCache));
if (aadConfiguration != null && aadConfiguration.enabled) {
policies.add(getAuthenticationPolicy(aadConfiguration));
}
policies.addAll(asList(additionalPolicies));
// Add Logging Policy. Can be enabled using AZURE_LOG_LEVEL.
// TODO set the logging level based on self diagnostic log level set by user
policies.add(new HttpLoggingPolicy(new HttpLogOptions()));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import com.microsoft.applicationinsights.profiler.config.ServiceProfilerServiceConfig;
import io.opentelemetry.instrumentation.api.aisdk.AiAppId;
import io.opentelemetry.instrumentation.api.aisdk.AiLazyConfiguration;
import io.opentelemetry.instrumentation.api.cache.Cache;
import io.opentelemetry.sdk.common.CompletableResultCode;
import java.io.File;
import java.lang.instrument.Instrumentation;
Expand Down Expand Up @@ -164,13 +163,11 @@ private static AppIdSupplier start(Instrumentation instrumentation) {
.map(MetricFilter::new)
.collect(Collectors.toList());

Cache<String, String> ikeyEndpointMap = Cache.bounded(100);
StatsbeatModule statsbeatModule = new StatsbeatModule(ikeyEndpointMap);
StatsbeatModule statsbeatModule = new StatsbeatModule();
TelemetryClient telemetryClient =
TelemetryClient.builder()
.setCustomDimensions(config.customDimensions)
.setMetricFilters(metricFilters)
.setIkeyEndpointMap(ikeyEndpointMap)
.setStatsbeatModule(statsbeatModule)
.setReadOnlyFileSystem(readOnlyFileSystem)
.setGeneralExportQueueSize(config.preview.generalExportQueueCapacity)
Expand Down Expand Up @@ -283,7 +280,7 @@ private static String getCodelessSdkNamePrefix() {
sdkNamePrefix.append(DiagnosticsHelper.rpIntegrationChar());
if (SystemInformation.isWindows()) {
sdkNamePrefix.append("w");
} else if (SystemInformation.isUnix()) {
} else if (SystemInformation.isLinux()) {
sdkNamePrefix.append("l");
} else {
startupLogger.warn("could not detect os: {}", System.getProperty("os.name"));
Expand Down Expand Up @@ -311,7 +308,7 @@ public void run() {
CompletableResultCode result = new CompletableResultCode();
otelFlush.whenComplete(
() -> {
CompletableResultCode batchingClientFlush = telemetryClient.flushChannelBatcher();
CompletableResultCode batchingClientFlush = telemetryClient.forceFlush();
batchingClientFlush.whenComplete(
() -> {
if (otelFlush.isSuccess() && batchingClientFlush.isSuccess()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ private static SeverityLevel getSeverityLevel(int value) {
public void flush() {
// this is not null because sdk instrumentation is not added until TelemetryClient.setActive()
// is called
TelemetryClient.getActive().flushChannelBatcher().join(10, SECONDS);
TelemetryClient.getActive().forceFlush().join(10, SECONDS);
}

@Override
Expand Down
Loading

0 comments on commit 7ddb5c5

Please sign in to comment.