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 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: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ timestamps {
}
```

You can also send extra parameters to the indexer by:
```Groovy
logstashSend failBuild: true, maxLines: 1000, additionalParams: [param1: "value1", param2: "value2"]
```
Note: this feature won't work in freestyle builds

License
=======

Expand Down
20 changes: 14 additions & 6 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 @@ -44,6 +43,7 @@
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* A writer that wraps all Logstash DAOs. Handles error reporting and per build connection state.
Expand All @@ -69,10 +69,17 @@ 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, Map<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) {
this(run, error, listener, charset, stageName, agentName, null);
}

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


/**
* 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(Map<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
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,20 @@ public List<String> getFailedTests()
private String rootProjectDisplayName;
private int rootBuildNum;
private Map<String, String> buildVariables;
private Map<String, String> additionalParams;
private Set<String> sensitiveBuildVariables;
private TestData testResults = null;

// Freestyle project build

public BuildData(AbstractBuild<?, ?> build, Date currentTime, TaskListener listener) {
this(build, currentTime, listener, Collections.emptyMap());
}
// Freestyle project build
public BuildData(AbstractBuild<?, ?> build, Date currentTime, TaskListener listener, Map<String, String> additionalParams) {

Choose a reason for hiding this comment

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

it seems there is no way to use this additional params in freestile builds, right?

Copy link
Author

Choose a reason for hiding this comment

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

why not?

Choose a reason for hiding this comment

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

I don't see any stapler bindings updated, how would it be configured?

Copy link
Author

Choose a reason for hiding this comment

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

For now it won't be implemented in freestyle builds

Choose a reason for hiding this comment

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

OK, can you please remove those extra constructors?
Otherwise LGTM

initData(build, currentTime);

// build.getDuration() is always 0 in Notifiers
rootProjectName = build.getRootBuild().getProject().getName();
this.additionalParams = additionalParams;
rootFullProjectName = build.getRootBuild().getProject().getFullName();
rootProjectDisplayName = build.getRootBuild().getDisplayName();
rootBuildNum = build.getRootBuild().getNumber();
Expand All @@ -211,30 +216,37 @@ public BuildData(AbstractBuild<?, ?> build, Date currentTime, TaskListener liste
buildVariables.putAll(build.getEnvironment(listener));
} 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, Collections.emptyMap());
}

// 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,
Map<String, String> additionalParams) {
initData(build, currentTime);

this.additionalParams = additionalParams;
this.agentName = agentName;
this.stageName = stageName;
rootProjectName = projectName;
rootFullProjectName = fullProjectName;
rootProjectDisplayName = displayName;
rootBuildNum = buildNum;
this.buildVariables = new HashMap<String, String>();

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

Expand Down Expand Up @@ -438,6 +450,14 @@ public void setBuildVariables(Map<String, String> buildVariables) {
this.buildVariables = buildVariables;
}

public Map<String, String> getAdditionalParams() {
return additionalParams;
}

public void setAdditionalParams(Map<String, String> additionalParams) {
this.additionalParams = additionalParams;
}

public Set<String> getSensitiveBuildVariables() {
return sensitiveBuildVariables;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package jenkins.plugins.logstash.pipeline;

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

import org.jenkinsci.plugins.workflow.steps.Step;
Expand All @@ -27,12 +29,14 @@ public class LogstashSendStep extends Step

private int maxLines;
private boolean failBuild;
private Map<String,String> additionalParams;

@DataBoundConstructor
public LogstashSendStep(int maxLines, boolean failBuild)
public LogstashSendStep(int maxLines, boolean failBuild, Map<String,String> additionalParams)
{
this.maxLines = maxLines;
this.failBuild = failBuild;
this.additionalParams = additionalParams;
}

public int getMaxLines()
Expand All @@ -48,7 +52,7 @@ public boolean isFailBuild()
@Override
public StepExecution start(StepContext context) throws Exception
{
return new Execution(context, maxLines, failBuild);
return new Execution(context, maxLines, failBuild, additionalParams);
}

@SuppressFBWarnings(value="SE_TRANSIENT_FIELD_NOT_RESTORED", justification="Only used when starting.")
Expand All @@ -59,29 +63,36 @@ private static class Execution extends SynchronousNonBlockingStepExecution<Void>

private transient final int maxLines;
private transient final boolean failBuild;
private transient final Map<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, Map<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;
}

}

@Extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -58,19 +60,19 @@ static LogstashWriter createLogstashWriter(final AbstractBuild<?, ?> testBuild,
final String url,
final LogstashIndexerDao indexer,
final BuildData data) {
return new LogstashWriter(testBuild, error, null, testBuild.getCharset()) {
return new LogstashWriter(testBuild, error, null, testBuild.getCharset(), Collections.emptyMap()) {
@Override
LogstashIndexerDao getIndexerDao() {
return indexer;
}

@Override
BuildData getBuildData() {
BuildData getBuildData(Map<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
27 changes: 26 additions & 1 deletion src/test/java/jenkins/plugins/logstash/PipelineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
Expand Down Expand Up @@ -128,4 +130,27 @@ public void logstashSend() throws Exception
JSONObject data = firstLine.getJSONObject("data");
assertThat(data.getString("result"),equalTo("SUCCESS"));
}
}

@Test
public void logstashSendWithAdditionalParams() throws Exception
{
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
HashMap<String, String> someMap = new HashMap<String,String>();
someMap.put("param1", "value1");
someMap.put("param2", "value2");
p.setDefinition(new CpsFlowDefinition(
"echo 'Message'\n" +
"currentBuild.result = 'SUCCESS'\n" +
"def someMap = [param1:'value1', param2:'value2']\n" +
"logstashSend failBuild: true, maxLines: 5, additionalParams: someMap"
, true));
j.assertBuildStatusSuccess(p.scheduleBuild2(0).get());
List<JSONObject> dataLines = memoryDao.getOutput();
assertThat(dataLines.size(), equalTo(1));
JSONObject firstLine = dataLines.get(0);
JSONObject data = firstLine.getJSONObject("data");
assertThat(data.getJSONObject("additionalParams").getString("param1"), equalTo("value1"));
assertThat(data.getJSONObject("additionalParams").getString("param2"), equalTo("value2"));
assertThat(data.getString("result"),equalTo("SUCCESS"));
This conversation was marked as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand Down Expand Up @@ -51,13 +52,12 @@
@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.crypto.*", "javax.xml.*", "org.xml.*"})
@PrepareForTest(LogstashConfiguration.class)
public class BuildDataTest {

static final String FULL_STRING = "{\"id\":\"TEST_JOB_123\",\"result\":\"SUCCESS\",\"fullProjectName\":\"parent/BuildDataTest\","
+ "\"projectName\":\"BuildDataTest\",\"displayName\":\"BuildData Test\",\"fullDisplayName\":\"BuildData Test #123456\","
+ "\"description\":\"Mock project for testing BuildData\",\"url\":\"http://localhost:8080/jenkins/jobs/PROJECT_NAME/123\","
+ "\"buildHost\":\"master\",\"buildLabel\":\"master\",\"buildNum\":123456,\"buildDuration\":60,"
+ "\"rootProjectName\":\"RootBuildDataTest\",\"rootFullProjectName\":\"parent/RootBuildDataTest\","
+ "\"rootProjectDisplayName\":\"Root BuildData Test\",\"rootBuildNum\":456,\"buildVariables\":{},"
+ "\"rootProjectDisplayName\":\"Root BuildData Test\",\"rootBuildNum\":456,\"buildVariables\":{}, \"additionalParams\":{},"
+ "\"sensitiveBuildVariables\":[],\"testResults\":{\"totalCount\":0,\"skipCount\":0,\"failCount\":0, \"passCount\":0,"
+ "\"failedTests\":[], \"failedTestsWithErrorDetail\":[]}}";

Expand Down Expand Up @@ -373,7 +373,7 @@ public void toJsonSuccess() throws Exception
// Verify results
JSONAssert.assertEquals("Results don't match", JSONObject.fromObject(FULL_STRING), result);

verifyMocks();
verifyMocks();
verifyTestResultActions();
}

Expand Down Expand Up @@ -404,4 +404,30 @@ public void rootProjectFullName() throws Exception
verifyMocks();
verifyTestResultActions();
}

@Test
public void additionalParamsAreAddedCorrectly() throws Exception
{
when(mockBuild.getEnvironments()).thenReturn(new EnvironmentList(Arrays.asList(mockEnvironment)));
when(mockBuild.getBuildVariables()).thenReturn(new HashMap<String, String>());
when(mockComputer.getNode()).thenReturn(mockNode);
final String buildVarKey = "BuildVarKey";
final String buildVarVal = "BuildVarVal";
when(mockBuild.getEnvironment(mockListener)).thenReturn(new EnvVars(buildVarKey, buildVarVal));
when(mockBuild.getSensitiveBuildVariables()).thenReturn(new HashSet<>());
doNothing().when(mockEnvironment).buildEnvVars(any());

HashMap<String,String> additionalParams = new HashMap<String, String>();
additionalParams.put("param1", "value1");
additionalParams.put("param2", "value2");

BuildData buildData = new BuildData(mockBuild, mockDate, mockListener, additionalParams);

Assert.assertEquals("Missing additionalParam: param1", "value1", buildData.getAdditionalParams().get("param1"));
Assert.assertEquals("Missing additionalParam: param1", "value2", buildData.getAdditionalParams().get("param2"));

verify(mockEnvironment).buildEnvVars(any());
verifyMocks();
verifyTestResultActions();
}
}