Skip to content

Commit

Permalink
Do not persiste entire Node instance to disk with the tracer config a…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
nikita-tkachenko-datadog committed Feb 1, 2024
1 parent d2b216e commit 40c5864
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,42 @@ public static boolean isMainNode(String nodeName) {
return "master".equalsIgnoreCase(nodeName) || "built-in".equalsIgnoreCase(nodeName);
}

public static String getNodeHostname(@Nullable EnvVars envVars, @Nullable Computer computer) {
if (envVars != null) {
String ddHostname = envVars.get(DatadogGlobalConfiguration.DD_CI_HOSTNAME);
if (DatadogUtilities.isValidHostname(ddHostname)) {
return ddHostname;
}
String hostname = envVars.get("HOSTNAME");
if (DatadogUtilities.isValidHostname(hostname)) {
return hostname;
}
}

try {
if(computer != null) {
String computerNodeName = DatadogUtilities.getNodeName(computer);
if (DatadogUtilities.isMainNode(computerNodeName)) {
String masterHostname = DatadogUtilities.getHostname(null);
if (DatadogUtilities.isValidHostname(masterHostname)) {
return masterHostname;
}
}

String computerHostName = computer.getHostName();
if (DatadogUtilities.isValidHostname(computerHostName)) {
return computerHostName;
}
}
} catch (InterruptedException e){
Thread.currentThread().interrupt();
logger.fine("Interrupted while trying to extract hostname from StepContext.");

} catch (IOException e){
logger.fine("Unable to extract hostname from StepContext.");
}
return null;
}

@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
public static Set<String> getNodeLabels(Computer computer) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.datadog.jenkins.plugins.datadog.apm;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.InvisibleAction;
import hudson.model.Computer;
import hudson.model.Job;
import hudson.model.Node;
import hudson.model.Run;
Expand All @@ -19,10 +20,12 @@
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.datadog.jenkins.plugins.datadog.DatadogClient;
import org.datadog.jenkins.plugins.datadog.DatadogGlobalConfiguration;
import org.datadog.jenkins.plugins.datadog.DatadogUtilities;
import org.datadog.jenkins.plugins.datadog.model.DatadogPluginAction;

public class DatadogTracerConfigurator {

Expand All @@ -37,16 +40,17 @@ public DatadogTracerConfigurator() {
configurators.put(TracerLanguage.PYTHON, new PythonConfigurator());
}

public Map<String, String> configure(Run<?, ?> run, Node node, Map<String, String> envs, TaskListener listener) {
public Map<String, String> configure(Run<?, ?> run, Computer computer, Node node, EnvVars envs, TaskListener listener) {
Job<?, ?> job = run.getParent();
DatadogTracerJobProperty<?> tracerConfig = job.getProperty(DatadogTracerJobProperty.class);
if (tracerConfig == null || !tracerConfig.isOn()) {
return Collections.emptyMap();
}

String nodeHostname = DatadogUtilities.getNodeHostname(envs, computer);
Collection<TracerLanguage> languages = tracerConfig.getLanguages();
for (ConfigureTracerAction action : run.getActions(ConfigureTracerAction.class)) {
if (action.node == node && languages.equals(action.languages)) {
if (nodeHostname != null && nodeHostname.equals(action.nodeHostname) && action.languages.containsAll(languages)) {
return action.variables;
}
}
Expand Down Expand Up @@ -80,7 +84,7 @@ public Map<String, String> configure(Run<?, ?> run, Node node, Map<String, Strin
return Collections.emptyMap();
}
}
run.addAction(new ConfigureTracerAction(node, languages, variables));
run.addAction(new ConfigureTracerAction(nodeHostname, languages, variables));
return variables;
}

Expand Down Expand Up @@ -159,13 +163,13 @@ private static String getAgentPort(Integer traceCollectionPort) {
}
}

private static final class ConfigureTracerAction extends InvisibleAction {
private final Node node;
private static final class ConfigureTracerAction extends DatadogPluginAction {
private final String nodeHostname;
private final Collection<TracerLanguage> languages;
private final Map<String, String> variables;

private ConfigureTracerAction(Node node, Collection<TracerLanguage> languages, Map<String, String> variables) {
this.node = node;
private ConfigureTracerAction(String nodeHostname, @Nonnull Collection<TracerLanguage> languages, Map<String, String> variables) {
this.nodeHostname = nodeHostname;
this.languages = languages;
this.variables = variables;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.EnvVars;
import hudson.Extension;
import hudson.model.Computer;
import hudson.model.EnvironmentContributor;
import hudson.model.Executor;
import hudson.model.Node;
Expand All @@ -26,8 +27,9 @@ public void buildEnvironmentFor(@NonNull Run run, @NonNull EnvVars envs, @NonNul
return;
}

Node node = executor.getOwner().getNode();
Map<String, String> additionalEnvVars = DatadogTracerConfigurator.INSTANCE.configure(run, node, envs, listener);
Computer computer = executor.getOwner();
Node node = computer.getNode();
Map<String, String> additionalEnvVars = DatadogTracerConfigurator.INSTANCE.configure(run, computer, node, envs, listener);
envs.putAll(additionalEnvVars);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nonnull;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
Expand All @@ -27,7 +28,7 @@ public class DatadogTracerJobProperty<T extends Job<?, ?>> extends JobProperty<T
private final Collection<TracerLanguage> languages;
private final Map<String, String> additionalVariables;

public DatadogTracerJobProperty(boolean on, String serviceName, Collection<TracerLanguage> languages, Map<String, String> additionalVariables) {
public DatadogTracerJobProperty(boolean on, String serviceName, @Nonnull Collection<TracerLanguage> languages, Map<String, String> additionalVariables) {
this.on = on;
this.serviceName = serviceName;
this.languages = languages;
Expand All @@ -42,6 +43,7 @@ public String getServiceName() {
return serviceName;
}

@Nonnull
public Collection<TracerLanguage> getLanguages() {
return languages;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.EnvVars;
import hudson.Extension;
import hudson.model.Computer;
import hudson.model.Node;
import hudson.model.Run;
import hudson.model.TaskListener;
Expand All @@ -17,9 +18,10 @@ public class DatadogTracerStepEnvironmentContributor extends StepEnvironmentCont
@Override
public void buildEnvironmentFor(StepContext stepContext, @NonNull EnvVars envs, @NonNull TaskListener listener) throws IOException, InterruptedException {
Run<?, ?> run = stepContext.get(Run.class);
Computer computer = stepContext.get(Computer.class);
Node node = stepContext.get(Node.class);
if (run != null && node != null) {
Map<String, String> additionalEnvVars = DatadogTracerConfigurator.INSTANCE.configure(run, node, envs, listener);
Map<String, String> additionalEnvVars = DatadogTracerConfigurator.INSTANCE.configure(run, computer, node, envs, listener);
envs.putAll(additionalEnvVars);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.datadog.jenkins.plugins.datadog.DatadogGlobalConfiguration;
import javax.annotation.Nullable;
import org.datadog.jenkins.plugins.datadog.DatadogUtilities;
import org.datadog.jenkins.plugins.datadog.audit.DatadogAudit;
import org.datadog.jenkins.plugins.datadog.model.GitCommitAction;
Expand Down Expand Up @@ -136,43 +136,19 @@ private static String getNodeName(StepContext stepContext) {
*/
@SuppressFBWarnings("REC_CATCH_EXCEPTION")
private static String getNodeHostname(final StepContext stepContext) {
try {
EnvVars envVars = stepContext.get(EnvVars.class);
if (envVars != null) {
String ddHostname = envVars.get(DatadogGlobalConfiguration.DD_CI_HOSTNAME);
if (DatadogUtilities.isValidHostname(ddHostname)) {
return ddHostname;
}
String hostname = envVars.get("HOSTNAME");
if (DatadogUtilities.isValidHostname(hostname)) {
return hostname;
}
}
} catch (Exception e){
logger.fine("Unable to extract environment variables from StepContext.");
}
return DatadogUtilities.getNodeHostname(getSafely(stepContext, EnvVars.class), getSafely(stepContext, Computer.class));
}

@Nullable
private static <T> T getSafely(final StepContext stepContext, final Class<T> type) {
try {
Computer computer = stepContext.get(Computer.class);
if(computer != null) {
String computerNodeName = DatadogUtilities.getNodeName(computer);
if (DatadogUtilities.isMainNode(computerNodeName)) {
String masterHostname = DatadogUtilities.getHostname(null);
if (DatadogUtilities.isValidHostname(masterHostname)) {
return masterHostname;
}
}

String computerHostName = computer.getHostName();
if (DatadogUtilities.isValidHostname(computerHostName)) {
return computerHostName;
}
}
} catch (Exception e){
logger.fine("Unable to extract hostname from StepContext.");
return stepContext.get(type);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return null;
} catch (Exception e) {
return null;
}

return null;
}

/**
Expand Down

0 comments on commit 40c5864

Please sign in to comment.