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

Added aditional params option #98

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
22 changes: 15 additions & 7 deletions src/main/java/jenkins/plugins/logstash/LogstashWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

package jenkins.plugins.logstash;


import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.model.Run;
Expand All @@ -43,6 +42,7 @@
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
Expand All @@ -69,10 +69,14 @@ public class LogstashWriter implements Serializable {
private final String agentName;

public LogstashWriter(Run<?, ?> run, OutputStream error, TaskListener listener, Charset charset) {
this(run, error, listener, charset, null, null);
this(run, error, listener, charset, null, null, null);
}

public LogstashWriter(Run<?, ?> run, OutputStream error, TaskListener listener, Charset charset, String stageName, String agentName) {
public LogstashWriter(Run<?, ?> run, OutputStream error, TaskListener listener, Charset charset, HashMap<String, String> additionalParams) {
this(run, error, listener, charset, null, null, additionalParams);
}

public LogstashWriter(Run<?, ?> run, OutputStream error, TaskListener listener, Charset charset, String stageName, String agentName, HashMap<String, String> additionalParams) {
this.errorStream = error != null ? error : System.err;
this.stageName = stageName;
this.agentName = agentName;
Expand All @@ -85,10 +89,14 @@ public LogstashWriter(Run<?, ?> run, OutputStream error, TaskListener listener,
this.buildData = null;
} else {
this.jenkinsUrl = getJenkinsUrl();
this.buildData = getBuildData();
this.buildData = getBuildData(additionalParams);
}
}

public LogstashWriter(Run<?, ?> run, OutputStream error, TaskListener listener, Charset charset, String stageName, String agentName) {
this(run, error, listener, charset, stageName, agentName, null);
}

/**
* Gets the charset that Jenkins is using during this build.
*
Expand Down Expand Up @@ -162,11 +170,11 @@ LogstashIndexerDao getIndexerDao() {
return LogstashConfiguration.getInstance().getIndexerInstance();
}

BuildData getBuildData() {
BuildData getBuildData(HashMap<String, String> additionalParams) {
if (build instanceof AbstractBuild) {
return new BuildData((AbstractBuild<?, ?>) build, new Date(), listener);
return new BuildData((AbstractBuild<?, ?>) build, new Date(), listener, additionalParams);
} else {
return new BuildData(build, new Date(), listener, stageName, agentName);
return new BuildData(build, new Date(), listener, stageName, agentName, additionalParams);
}
}

Expand Down
87 changes: 45 additions & 42 deletions src/main/java/jenkins/plugins/logstash/persistence/BuildData.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,31 @@
* @author Rusty Gerard
* @since 1.0.0
*/
@SuppressFBWarnings(value="SE_NO_SERIALVERSIONID")
@SuppressFBWarnings(value = "SE_NO_SERIALVERSIONID")
public class BuildData implements Serializable {

// ISO 8601 date format
private final static Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getCanonicalName());

public static class TestData implements Serializable {
private final int totalCount, skipCount, failCount, passCount;
private final List<FailedTest> failedTestsWithErrorDetail;
private final List<String> failedTests;

public static class FailedTest implements Serializable {
private final String fullName, errorDetails;

public FailedTest(String fullName, String errorDetails) {
super();
this.fullName = fullName;
this.errorDetails = errorDetails;
}

public String getFullName()
{
public String getFullName() {
return fullName;
}

public String getErrorDetails()
{
public String getErrorDetails() {
return errorDetails;
}
}
Expand Down Expand Up @@ -119,39 +119,33 @@ public TestData(Action action) {
failedTests = new ArrayList<>();
failedTestsWithErrorDetail = new ArrayList<>();
for (TestResult result : testResultAction.getFailedTests()) {
failedTests.add(result.getFullName());
failedTestsWithErrorDetail.add(new FailedTest(result.getFullName(),result.getErrorDetails()));
failedTests.add(result.getFullName());
failedTestsWithErrorDetail.add(new FailedTest(result.getFullName(), result.getErrorDetails()));
}
}

public int getTotalCount()
{
return totalCount;
public int getTotalCount() {
return totalCount;
}

public int getSkipCount()
{
return skipCount;
public int getSkipCount() {
return skipCount;
}

public int getFailCount()
{
return failCount;
public int getFailCount() {
return failCount;
}

public int getPassCount()
{
return passCount;
public int getPassCount() {
return passCount;
}

public List<FailedTest> getFailedTestsWithErrorDetail()
{
return failedTestsWithErrorDetail;
public List<FailedTest> getFailedTestsWithErrorDetail() {
return failedTestsWithErrorDetail;
}

public List<String> getFailedTests()
{
return failedTests;
public List<String> getFailedTests() {
return failedTests;
}
}

Expand Down Expand Up @@ -179,8 +173,11 @@ public List<String> getFailedTests()
private Set<String> sensitiveBuildVariables;
private TestData testResults = null;

// Freestyle project build
public BuildData(AbstractBuild<?, ?> build, Date currentTime, TaskListener listener) {
this(build, currentTime, listener, null);
}
// Freestyle project build
public BuildData(AbstractBuild<?, ?> build, Date currentTime, TaskListener listener, HashMap<String, String> additionalParams) {
initData(build, currentTime);

// build.getDuration() is always 0 in Notifiers
Expand Down Expand Up @@ -209,17 +206,23 @@ public BuildData(AbstractBuild<?, ?> build, Date currentTime, TaskListener liste
}
try {
buildVariables.putAll(build.getEnvironment(listener));
buildVariables.putAll(additionalParams);
} catch (Exception e) {
// no base build env vars to merge
LOGGER.log(WARNING,"Unable update logstash buildVariables with EnvVars from " + build.getDisplayName(),e);
LOGGER.log(WARNING, "Unable update logstash buildVariables with EnvVars from " + build.getDisplayName(), e);
}
for (String key : sensitiveBuildVariables) {
buildVariables.remove(key);
}
}

public BuildData(Run<?, ?> build, Date currentTime, TaskListener listener, String stageName, String agentName){
this(build, currentTime, listener, stageName, agentName, null);
}

// Pipeline project build
public BuildData(Run<?, ?> build, Date currentTime, TaskListener listener, String stageName, String agentName) {
public BuildData(Run<?, ?> build, Date currentTime, TaskListener listener, String stageName, String agentName,
HashMap<String, String> additionalParams) {
initData(build, currentTime);

this.agentName = agentName;
Expand All @@ -230,30 +233,31 @@ public BuildData(Run<?, ?> build, Date currentTime, TaskListener listener, Strin
rootBuildNum = buildNum;

try {
// TODO: sensitive variables are not filtered, c.f. https://stackoverflow.com/questions/30916085
// TODO: sensitive variables are not filtered, c.f.
// https://stackoverflow.com/questions/30916085
buildVariables = build.getEnvironment(listener);
buildVariables.putAll(additionalParams);
} catch (IOException | InterruptedException e) {
LOGGER.log(WARNING,"Unable to get environment for " + build.getDisplayName(),e);
LOGGER.log(WARNING, "Unable to get environment for " + build.getDisplayName(), e);
buildVariables = new HashMap<>();
}
}

private void initData(Run<?, ?> build, Date currentTime) {

this.build = build;
Executor executor = build.getExecutor();
if (executor == null) {
buildHost = "master";
buildLabel = "master";
} else {
Node node = executor.getOwner().getNode();
if (node == null) {
buildHost = "master";
buildLabel = "master";
} else {
Node node = executor.getOwner().getNode();
if (node == null) {
buildHost = "master";
buildLabel = "master";
} else {
buildHost = StringUtils.isBlank(node.getDisplayName()) ? "master" : node.getDisplayName();
buildLabel = StringUtils.isBlank(node.getLabelString()) ? "master" : node.getLabelString();
}
} else {
buildHost = StringUtils.isBlank(node.getDisplayName()) ? "master" : node.getDisplayName();
buildLabel = StringUtils.isBlank(node.getLabelString()) ? "master" : node.getLabelString();
}
}

id = build.getId();
Expand All @@ -269,8 +273,7 @@ private void initData(Run<?, ?> build, Date currentTime) {
updateResult();
}

public void updateResult()
{
public void updateResult() {
if (build != null) {
if (result == null && build.getResult() != null) {
Result result = build.getResult();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package jenkins.plugins.logstash.pipeline;

import java.io.PrintStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -19,64 +21,66 @@
import jenkins.plugins.logstash.Messages;

/**
* Sends the tail of the log in a single event to a logstash indexer.
* Pipeline counterpart of the LogstashNotifier.
* Sends the tail of the log in a single event to a logstash indexer. Pipeline
* counterpart of the LogstashNotifier.
*/
public class LogstashSendStep extends Step
{
public class LogstashSendStep extends Step {

private int maxLines;
private boolean failBuild;

@DataBoundConstructor
public LogstashSendStep(int maxLines, boolean failBuild)
{
public LogstashSendStep(int maxLines, boolean failBuild) {
this.maxLines = maxLines;
this.failBuild = failBuild;
}

public int getMaxLines()
{
public int getMaxLines() {
return maxLines;
}

public boolean isFailBuild()
{
public boolean isFailBuild() {
return failBuild;
}

@Override
public StepExecution start(StepContext context) throws Exception
{
public StepExecution start(StepContext context) throws Exception {
return new Execution(context, maxLines, failBuild);
}

@SuppressFBWarnings(value="SE_TRANSIENT_FIELD_NOT_RESTORED", justification="Only used when starting.")
private static class Execution extends SynchronousNonBlockingStepExecution<Void>
{
@SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED", justification = "Only used when starting.")
private static class Execution extends SynchronousNonBlockingStepExecution<Void> {

private static final long serialVersionUID = 1L;

private transient final int maxLines;
private transient final boolean failBuild;
private transient final HashMap<String, String> additionalParams;

Execution(StepContext context, int maxLines, boolean failBuild)
{
super(context);
this.maxLines = maxLines;
this.failBuild = failBuild;
this.additionalParams = new HashMap<String,String>();
}

Execution(StepContext context, int maxLines, boolean failBuild, HashMap<String, String> additionalParams)
{
super(context);
this.maxLines = maxLines;
this.failBuild = failBuild;
this.additionalParams = additionalParams;
}

@Override
protected Void run() throws Exception
{
protected Void run() throws Exception {
Run<?, ?> run = getContext().get(Run.class);
TaskListener listener = getContext().get(TaskListener.class);
PrintStream errorStream = listener.getLogger();
LogstashWriter logstash = new LogstashWriter(run, errorStream, listener, run.getCharset());
LogstashWriter logstash = new LogstashWriter(run, errorStream, listener, run.getCharset(), this.additionalParams);
logstash.writeBuildLog(maxLines);
if (failBuild && logstash.isConnectionBroken())
{
if (failBuild && logstash.isConnectionBroken()) {
throw new Exception("Failed to send data to Indexer");
}
return null;
Expand All @@ -85,25 +89,21 @@ protected Void run() throws Exception
}

@Extension
public static class DescriptorImpl extends StepDescriptor
{
public static class DescriptorImpl extends StepDescriptor {

/** {@inheritDoc} */
@Override
public String getDisplayName()
{
public String getDisplayName() {
return Messages.DisplayName();
}

@Override
public String getFunctionName()
{
public String getFunctionName() {
return "logstashSend";
}

@Override
public Set<? extends Class<?>> getRequiredContext()
{
public Set<? extends Class<?>> getRequiredContext() {
Set<Class<?>> contexts = new HashSet<>();
contexts.add(TaskListener.class);
contexts.add(Run.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;

import org.junit.After;
Expand Down Expand Up @@ -65,12 +66,12 @@ LogstashIndexerDao getIndexerDao() {
}

@Override
BuildData getBuildData() {
BuildData getBuildData(HashMap<String, String> additionalParams) {
assertNotNull("BuildData should never be requested for missing dao.", this.getDao());

// For testing, providing null data means use the actual method
if (data == null) {
return super.getBuildData();
return super.getBuildData(additionalParams);
} else {
return data;
}
Expand Down