Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement automatic Test Visibility instrumentation for .NET projects #400

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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ pipeline {
datadog(testVisibility: [
enabled: true,
serviceName: "my-service", // the name of service or library being tested
languages: ["JAVA"], // languages that should be instrumented (available options are "JAVA", "JAVASCRIPT", "PYTHON")
languages: ["JAVA"], // languages that should be instrumented (available options are "JAVA", "JAVASCRIPT", "PYTHON", "DOTNET")
additionalVariables: ["my-var": "value"] // additional tracer configuration settings (optional)
])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class DatadogTracerConfigurator {

public DatadogTracerConfigurator() {
configurators = new EnumMap<>(TracerLanguage.class);
configurators.put(TracerLanguage.DOTNET, new DotnetConfigurator());
configurators.put(TracerLanguage.JAVA, new JavaConfigurator());
configurators.put(TracerLanguage.JAVASCRIPT, new JavascriptConfigurator());
configurators.put(TracerLanguage.PYTHON, new PythonConfigurator());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.datadog.jenkins.plugins.datadog.apm;

import hudson.FilePath;
import hudson.model.Node;
import hudson.model.TaskListener;
import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class DotnetConfigurator implements TracerConfigurator {
private static final int GET_DOTNET_VERSION_TIMEOUT_MILLIS = 30_000;
private static final int INSTALL_TRACER_TIMEOUT_MILLIS = 300_000;
private static final int SHOW_TRACER_VARS_TIMEOUT_MILLIS = 30_000;

@Override
public Map<String, String> configure(DatadogTracerJobProperty<?> tracerConfig, Node node, FilePath workspacePath, Map<String, String> envs, TaskListener listener) throws Exception {
String dotnetVersion = workspacePath.act(new ShellCommandCallable(Collections.emptyMap(), GET_DOTNET_VERSION_TIMEOUT_MILLIS, "dotnet", "--version"));
listener.getLogger().println("[datadog] Configuring DD .NET tracer: got .NET version " + dotnetVersion + " from " + workspacePath + " on " + node);

String installTracerOutput = workspacePath.act(new ShellCommandCallable(Collections.emptyMap(), INSTALL_TRACER_TIMEOUT_MILLIS, "dotnet", "tool", "update", "--tool-path", workspacePath.getRemote(), "dd-trace"));
listener.getLogger().println("[datadog] Configuring DD .NET tracer: tracer installed in " + workspacePath + " on " + node + "; output: " + installTracerOutput);

String tracerVarOutput = workspacePath.act(new ShellCommandCallable(Collections.emptyMap(), SHOW_TRACER_VARS_TIMEOUT_MILLIS, workspacePath.getRemote() + File.separator + "dd-trace", "ci", "configure", "jenkins"));

Map<String, String> variables = new HashMap<>();
for (String line : tracerVarOutput.split("\n")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can tracerVarOutput be null?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'd be an exceptional situation, so a NullPointerException would be appropriate (the job itself won't fail anyway, it's just that we'll see the error in the logs)

if (!line.contains("=")) {
continue;
}
String[] tokens = line.split("=");
variables.put(tokens[0], tokens.length == 2 ? tokens[1] : "");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the variables.get(key) returns empty string. Is this expected from the DotNet configuration perspective?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, some of the variables can (maybe even should) have an empty value, checked this with an actual .NET project

}
return variables;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Map<String, String> configure(DatadogTracerJobProperty<?> tracerConfig, N

Map<String, String> variables = new HashMap<>();
variables.put("DD_TRACE_PATH", tracerPath.toString());
variables.put("NODE_OPTIONS", PropertyUtils.prepend(envs, "NODE_OPTIONS", "-r $DD_TRACE_PATH/ci/init"));
variables.put("NODE_OPTIONS", PropertyUtils.prepend(envs, "NODE_OPTIONS", String.format("-r %s/ci/init", tracerPath)));
return variables;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.datadog.jenkins.plugins.datadog.apm;

public enum TracerLanguage {
DOTNET(".NET"),
JAVA("Java"),
JAVASCRIPT("JS"),
PYTHON("Python");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@
If these variables are overridden inside the job (with their existing value being discarded rather than preserved),
the injection will not happen.
</p>

<strong>.NET</strong>
<p>
The plugin assumes that <code>dotnet</code> and <code>dotnet tool</code> are available on the node that executes the job.
</p>
</div>
Loading