Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ jobs:
run: |
./gradlew :netty-test:run
env:
SW_APM_SERVICE_KEY: ${{ secrets.SW_APM_SERVICE_KEY }}:smoke-test-linux
SW_APM_SERVICE_KEY: ${{ secrets.SW_APM_SERVICE_KEY }}
OTEL_SERVICE_NAME: smoke-test-linux

smoke-test-no-agent:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -249,7 +250,8 @@ jobs:
run: |
.\gradlew.bat :netty-test:run
env:
SW_APM_SERVICE_KEY: ${{ secrets.SW_APM_SERVICE_KEY }}:smoke-test-windows
SW_APM_SERVICE_KEY: ${{ secrets.SW_APM_SERVICE_KEY }}
OTEL_SERVICE_NAME: smoke-test-windows

release-test:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies {
implementation("com.diffplug.spotless:spotless-plugin-gradle:8.10.4")
implementation("io.freefair.gradle:lombok-plugin:9.5.0")

implementation("io.opentelemetry.instrumentation:gradle-plugins:2.33.1-alpha")
implementation("io.opentelemetry.instrumentation:gradle-plugins:2.34.0-alpha")
implementation("com.gradleup.shadow:shadow-gradle-plugin:9.6.1")
implementation("com.github.gmazzo.buildconfig:com.github.gmazzo.buildconfig.gradle.plugin:6.1.2")
implementation("com.github.spotbugs:com.github.spotbugs.gradle.plugin:6.4.8")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,25 @@
import com.solarwinds.joboe.config.ConfigManager;
import com.solarwinds.joboe.config.ConfigProperty;
import com.solarwinds.joboe.config.JavaRuntimeVersionChecker;
import com.solarwinds.joboe.config.ServiceKeyUtils;
import com.solarwinds.joboe.core.ReporterFactory;
import com.solarwinds.joboe.core.profiler.Profiler;
import com.solarwinds.joboe.core.profiler.ProfilerSetting;
import com.solarwinds.joboe.core.rpc.ClientManagerProvider;
import com.solarwinds.joboe.core.rpc.RpcClientManager;
import com.solarwinds.joboe.core.util.DaemonThreadFactory;
import com.solarwinds.joboe.core.util.HostInfoUtils;
import com.solarwinds.joboe.logging.LogSanitizer;
import com.solarwinds.joboe.logging.Logger;
import com.solarwinds.joboe.logging.LoggerFactory;
import com.solarwinds.joboe.sampling.SettingsManager;
import com.solarwinds.opentelemetry.core.AgentState;
import com.solarwinds.opentelemetry.extensions.config.SdkResourceServiceName;
import io.opentelemetry.javaagent.extension.AgentListener;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.trace.internal.SdkTracerProviderUtil;
import io.opentelemetry.semconv.ServiceAttributes;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
Expand All @@ -52,14 +58,43 @@ public class SolarwindsAgentListener implements AgentListener {
@Override
public void afterAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) {
if (isAgentEnabled()) {
executeStartupTasks();
String serviceName = resolveServiceName(autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk());
logger.info("Resolved service name: " + LogSanitizer.sanitize(serviceName));
SdkResourceServiceName.set(serviceName);

executeStartupTasks(serviceName);
registerShutdownTasks();
logger.info(
"Successfully submitted SolarwindsAPM OpenTelemetry extensions initialization tasks");
}
}

private void executeStartupTasks() {
/**
* Reads the final {@code service.name} from the built SDK's resource. Works for both declarative
* and system config since the tracer provider always carries the merged resource.
*/
static String resolveServiceName(OpenTelemetrySdk sdk) {
if (sdk == null) {
return null;
}

try {
return SdkTracerProviderUtil.getSdkResourceProvider(sdk.getSdkTracerProvider())
.getResource()
.getAttribute(ServiceAttributes.SERVICE_NAME);
} catch (RuntimeException e) {
logger.warn("Unable to read the service name from the SDK resource", e);
return null;
}
}

/** The service key for the profiling client, carrying the final, normalized service name. */
static String profilingServiceKey(String serviceName) {
return ServiceKeyUtils.withServiceName(
ConfigManager.getConfigOptional(ConfigProperty.AGENT_SERVICE_KEY, ""), serviceName);
}

private void executeStartupTasks(String serviceName) {
ExecutorService service =
Executors.newSingleThreadExecutor(DaemonThreadFactory.newInstance("post-startup-tasks"));

Expand Down Expand Up @@ -99,7 +134,8 @@ private void executeStartupTasks() {
ReporterFactory.getInstance()
.createQueuingEventReporter(
RpcClientManager.getClient(
RpcClientManager.OperationType.PROFILING)));
RpcClientManager.OperationType.PROFILING,
profilingServiceKey(serviceName))));
} else {
logger.info("Profiler is disabled, local settings : " + profilerSetting);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ public static void processConfigs(ConfigContainer configs) throws InvalidConfigE
&& !((String) configs.get(ConfigProperty.AGENT_SERVICE_KEY)).isEmpty()) {
// Customer access key (UUID)
String rawServiceKey = (String) configs.get(ConfigProperty.AGENT_SERVICE_KEY);
ServiceKeyUtils.warnIfServiceNameInServiceKey(rawServiceKey);
String serviceKey = ServiceKeyUtils.transformServiceKey(rawServiceKey);

if (!serviceKey.equalsIgnoreCase(rawServiceKey)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,36 @@ public class HttpSettingsReader implements SettingsReader {

private static final String DEFAULT_COLLECTOR = "apm.collector.na-01.cloud.solarwinds.com";

private static final String DEFAULT_SERVICE_KEY = ":unknown-java";

private final HttpSettingsReaderDelegate delegate;

private final Supplier<String> collector;

private final Supplier<String> serviceKey;

public static HttpSettingsReader forSystemConfig(HttpSettingsReaderDelegate delegate) {
return forSystemConfig(delegate, SdkResourceServiceName::get);
}

static HttpSettingsReader forSystemConfig(
HttpSettingsReaderDelegate delegate, Supplier<String> serviceName) {
return new HttpSettingsReader(
delegate,
() -> ConfigManager.getConfigOptional(ConfigProperty.AGENT_COLLECTOR, DEFAULT_COLLECTOR),
() ->
ConfigManager.getConfigOptional(ConfigProperty.AGENT_SERVICE_KEY, DEFAULT_SERVICE_KEY));
ServiceKeyUtils.withServiceName(
ConfigManager.getConfigOptional(ConfigProperty.AGENT_SERVICE_KEY, ""),
serviceName.get()));
}

public static HttpSettingsReader forFileConfig(
HttpSettingsReaderDelegate delegate, String collector, String serviceKey) {
return forFileConfig(delegate, collector, () -> serviceKey);
}

public static HttpSettingsReader forFileConfig(
HttpSettingsReaderDelegate delegate, String collector, Supplier<String> serviceKey) {
return new HttpSettingsReader(
delegate, () -> collector != null ? collector : DEFAULT_COLLECTOR, () -> serviceKey);
delegate, () -> collector != null ? collector : DEFAULT_COLLECTOR, serviceKey);
}

private HttpSettingsReader(
Expand All @@ -72,7 +82,8 @@ private HttpSettingsReader(
public Settings getSettings() {
String collectorUrl = constructSettingsEndpoint(collector.get());
String serviceKey = this.serviceKey.get();
String serviceName = ServiceKeyUtils.getServiceName(serviceKey);
String serviceName =
ServiceKeyUtils.serviceNameOrDefault(ServiceKeyUtils.getServiceName(serviceKey));

String apiToken = ServiceKeyUtils.getApiKey(serviceKey);
String settingsUrl =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* © SolarWinds Worldwide, LLC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.solarwinds.opentelemetry.extensions.config;

import com.solarwinds.joboe.logging.LoggerFactory;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
* Holds the final {@code service.name} of the built SDK's resource, published by the agent listener
* once the SDK is built. The settings pollers are created while the SDK is being built, so they
* wait for the name to be published.
*/
public final class SdkResourceServiceName {
private static final long DEFAULT_WAIT_SECONDS = 1;

private static volatile CompletableFuture<String> serviceName = new CompletableFuture<>();

private static volatile boolean timedOut;

private SdkResourceServiceName() {}

public static void set(String name) {
serviceName.complete(name);
}

/** Returns the published service name, waiting up to 1 second on the first call. */
public static String get() {
return get(DEFAULT_WAIT_SECONDS, TimeUnit.SECONDS);
}

/**
* Returns the published service name, waiting up to the timeout on the first call. Once a wait
* has timed out, later calls return immediately.
*
* @return the service name, or null if it's not available
*/
public static String get(long timeout, TimeUnit unit) {
CompletableFuture<String> current = serviceName;
if (timedOut) {
return current.getNow(null);
}

try {
return current.get(timeout, unit);
} catch (TimeoutException e) {
timedOut = true;
LoggerFactory.getLogger()
.warn(
"Timed out waiting for the SDK resource service name, falling back to the service"
+ " key or default service name");
return null;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return null;
} catch (Exception e) {
return null;
}
}

/** For testing only. */
public static void reset() {
serviceName = new CompletableFuture<>();
timedOut = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
import com.solarwinds.opentelemetry.extensions.config.HttpSettingsReader;
import com.solarwinds.opentelemetry.extensions.config.HttpSettingsReaderDelegate;
import com.solarwinds.opentelemetry.extensions.config.ProxyHelper;
import com.solarwinds.opentelemetry.extensions.config.SdkResourceServiceName;
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
import java.util.function.Function;
import java.util.function.Supplier;

@AutoService(ComponentProvider.class)
public class HttpSamplerComponentProvider extends AbstractSamplerComponentProvider {
Expand All @@ -49,12 +51,17 @@ public class HttpSamplerComponentProvider extends AbstractSamplerComponentProvid

private final Function<ProxyConfig, HttpSettingsReaderDelegate> delegateFactory;

private final Supplier<String> serviceNameSupplier;

public HttpSamplerComponentProvider() {
this(HttpSettingsReaderDelegate::new);
this(HttpSettingsReaderDelegate::new, SdkResourceServiceName::get);
}

HttpSamplerComponentProvider(Function<ProxyConfig, HttpSettingsReaderDelegate> delegateFactory) {
HttpSamplerComponentProvider(
Function<ProxyConfig, HttpSettingsReaderDelegate> delegateFactory,
Supplier<String> serviceNameSupplier) {
this.delegateFactory = delegateFactory;
this.serviceNameSupplier = serviceNameSupplier;
}

@Override
Expand Down Expand Up @@ -94,10 +101,13 @@ protected SettingsFetcher createSettingsFetcher(DeclarativeConfigProperties prop
throw new InvalidConfigException("Missing required sampler " + SERVICE_KEY + " config");
}

serviceKey = ServiceKeyUtils.transformServiceKey(serviceKey);
ServiceKeyUtils.warnIfServiceNameInServiceKey(serviceKey);
// Resolved on the polling thread: the SDK resource is only available after the SDK is built.
return new HttpSettingsFetcher(
HttpSettingsReader.forFileConfig(
delegateFactory.apply(ProxyHelper.toProxyConfig(properties)), collector, serviceKey),
delegateFactory.apply(ProxyHelper.toProxyConfig(properties)),
collector,
() -> ServiceKeyUtils.withServiceName(serviceKey, serviceNameSupplier.get())),
POLLING_INTERVAL_SECONDS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ ProfilerSetting convert(DeclarativeConfigProperties profilerProperties)
"Invalid profiler " + SERVICE_KEY + " config, value cannot be empty");
}

ServiceKeyUtils.warnIfServiceNameInServiceKey(serviceKey);
ConfigManager.setConfig(
ConfigProperty.AGENT_SERVICE_KEY, ServiceKeyUtils.transformServiceKey(serviceKey));

Expand Down
Loading
Loading