Skip to content

Commit

Permalink
Propagate trace context in plugin code that executes in Jenkins agents
Browse files Browse the repository at this point in the history
Signed-off-by: Cyrille Le Clerc <cyrille.leclerc@grafana.com>
  • Loading branch information
cyrille-leclerc committed Jul 31, 2024
1 parent 5161102 commit 7560c32
Showing 1 changed file with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@
import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Predicate;
import java.util.logging.Level;
Expand All @@ -54,13 +59,21 @@ public class OpenTelemetryConfigurerComputerListener extends ComputerListener im
JenkinsOpenTelemetryPluginConfiguration jenkinsOpenTelemetryPluginConfiguration;

@Override
public void preOnline(Computer computer, Channel channel, FilePath root, TaskListener listener) throws InterruptedException {
public void preOnline(Computer computer, Channel channel, FilePath root, TaskListener listener) {
if (buildAgentsInstrumentationEnabled.get()) {

Check warning on line 63 in src/main/java/io/jenkins/plugins/opentelemetry/jenkins/OpenTelemetryConfigurerComputerListener.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 63 is only partially covered, one branch is missing
OpenTelemetryConfiguration openTelemetryConfiguration = jenkinsOpenTelemetryPluginConfiguration.toOpenTelemetryConfiguration();
Map<String, String> otelSdkProperties = openTelemetryConfiguration.toOpenTelemetryProperties();
Map<String, String> otelSdkResourceProperties = openTelemetryConfiguration.toOpenTelemetryResourceAsMap();

Future<Object> result = configureOpenTelemetrySdkOnComputer(computer, channel, otelSdkProperties, otelSdkResourceProperties);
try {
Object result = configureOpenTelemetrySdkOnComputer(computer, channel, otelSdkProperties, otelSdkResourceProperties).get();
logger.log(Level.FINE, () -> "Updated OpenTelemetry configuration for computer/build-agent '" + computer.getName() + "' with result: " + result);
} catch (InterruptedException e) {
logger.log(Level.INFO, e, () -> "Failure to update OpenTelemetry configuration for computer/build-agent '" + computer.getName() + "'");
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
logger.log(Level.INFO, e, () -> "Failure to update OpenTelemetry configuration for computer/build-agent '" + computer.getName() + "'");
}

Check warning on line 76 in src/main/java/io/jenkins/plugins/opentelemetry/jenkins/OpenTelemetryConfigurerComputerListener.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 64-76 are not covered by tests
}
}

Expand All @@ -86,7 +99,9 @@ public void afterConfiguration(ConfigProperties configProperties) {
// build agent instrumentation remains disabled, don't do anything
logger.log(Level.FINE, () -> "Build agent instrumentation remains disabled, no need to update OpenTelemetry configuration on jenkins build agents");
} else {
Arrays.stream(Jenkins.get().getComputers()).forEach(computer -> {
Computer[] computers = Jenkins.get().getComputers();
List<Future<Object>> configureAgentResults = new ArrayList<>(computers.length);
Arrays.stream(computers).forEach(computer -> {
Node node = computer.getNode();
VirtualChannel channel = computer.getChannel();

Expand All @@ -98,12 +113,20 @@ public void afterConfiguration(ConfigProperties configProperties) {
// skip Jenkins controller
} else if (channel == null) {
logger.log(Level.FINE, () -> "Failure to update OpenTelemetry configuration for computer/build-agent '" + computer.getName() + "' as its channel is null, probably offline");

} else {
Future<Object> result = configureOpenTelemetrySdkOnComputer(computer, channel, otelSdkProperties, otelSdkResourceProperties);
configureAgentResults.add(configureOpenTelemetrySdkOnComputer(computer, channel, otelSdkProperties, otelSdkResourceProperties));
}
});
configureAgentResults.forEach(result -> {
try {
result.get(10, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
logger.log(Level.WARNING, e, () -> "Failure to update OpenTelemetry configuration for computer/build-agent");
}
}

Check warning on line 126 in src/main/java/io/jenkins/plugins/opentelemetry/jenkins/OpenTelemetryConfigurerComputerListener.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 102-126 are not covered by tests
);
}

}

/**
Expand Down

0 comments on commit 7560c32

Please sign in to comment.