Skip to content

Commit

Permalink
rename sendSplunk to sendSplunkScript, add new step sendSplunkConsoleLog
Browse files Browse the repository at this point in the history
  • Loading branch information
Ted Xiao committed Nov 25, 2016
1 parent 6aa702c commit c0a3888
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.splunk.splunkins</groupId>
<artifactId>pom</artifactId>
<version>1.3.2-SNAPSHOT</version>
<version>1.4-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Splunk Plugin Main Module</name>
Expand Down
20 changes: 16 additions & 4 deletions splunk-devops-extend/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.splunk.splunkins</groupId>
<artifactId>pom</artifactId>
<version>1.3.2-SNAPSHOT</version>
<version>1.4-SNAPSHOT</version>
</parent>

<artifactId>splunk-devops-extend</artifactId>
Expand Down Expand Up @@ -58,12 +59,23 @@
<version>2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>junit</artifactId>
<version>1.18</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<compatibleSinceVersion>1.4</compatibleSinceVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.splunk.splunkjenkins;

import hudson.Extension;
import hudson.console.ConsoleLogFilter;
import hudson.model.Run;
import org.jenkinsci.plugins.workflow.steps.*;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.Nonnull;

public class SplunkConsoleLogStep extends AbstractStepImpl {
@DataBoundConstructor
public SplunkConsoleLogStep() {
}

@Extension(optional = true)
public static class DescriptorImpl extends AbstractStepDescriptorImpl {
public DescriptorImpl() {
super(ExecutionImpl.class);
}

/**
* {@inheritDoc}
*/
@Override
public String getFunctionName() {
return "sendSplunkConsoleLog";
}

/**
* {@inheritDoc}
*/
@Nonnull
@Override
public String getDisplayName() {
return "Send console log Splunk";
}

/**
* {@inheritDoc}
*/
@Override
public boolean takesImplicitBlockArgument() {
return true;
}
}


public static class ExecutionImpl extends AbstractStepExecutionImpl {
/**
* {@inheritDoc}
*/
@Override
public boolean start() throws Exception {
//refer to WithContextStep implementation
StepContext context = getContext();
Run run = context.get(Run.class);
ConsoleLogFilter filter = BodyInvoker.mergeConsoleLogFilters(context.get(ConsoleLogFilter.class), new TeeConsoleLogFilter(run));
context.newBodyInvoker().withContext(filter).withCallback(BodyExecutionCallback.wrap(context)).start();
return false;
}

/**
* {@inheritDoc}
*/
@Override
public void stop(@Nonnull Throwable cause) throws Exception {
getContext().onFailure(cause);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public String getFunctionName() {
@Nonnull
@Override
public String getDisplayName() {
return "Send log files to Splunk";
return "Forwarding pipeline console log to Splunk";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public DescriptorImpl() {

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

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core">
Forwarding pipeline console log to Splunk
</j:jelly>
2 changes: 1 addition & 1 deletion splunk-devops/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.splunk.splunkins</groupId>
<artifactId>pom</artifactId>
<version>1.3.2-SNAPSHOT</version>
<version>1.4-SNAPSHOT</version>
</parent>

<artifactId>splunk-devops</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.logging.Logger;

import static com.splunk.splunkjenkins.Constants.LOG_TIME_FORMAT;
import static com.splunk.splunkjenkins.SplunkJenkinsInstallation.MIN_BUFFER_SIZE;
import static com.splunk.splunkjenkins.model.EventType.CONSOLE_LOG;
import static com.splunk.splunkjenkins.utils.LogEventHelper.decodeConsoleBase64Text;
import static java.nio.charset.StandardCharsets.UTF_8;
Expand All @@ -37,22 +36,46 @@
public class TeeConsoleLogFilter extends ConsoleLogFilter implements Serializable {
private static final Logger LOG = Logger.getLogger(TeeConsoleLogFilter.class.getName());
private static final long serialVersionUID = 1091734060617902662L;
private static final String SUFFIX = "console";
private String source;

public TeeConsoleLogFilter(String source) {
this.source = source;
}

public TeeConsoleLogFilter(Run run) {
if (run != null) {
this.source = run.getUrl() + SUFFIX;
}
}

public TeeConsoleLogFilter() {
this.source = "Jobconsole";
}

//backwards compatibility
@Override
public OutputStream decorateLogger(AbstractBuild build, OutputStream output) throws IOException, InterruptedException {
return teeOutput(output, build.getUrl() + "console", true);
return decorateLogger((Run) build, output);
}

//introduced in jenkins 1.632
public OutputStream decorateLogger(Run build, OutputStream output) throws IOException, InterruptedException {
return teeOutput(output, build.getUrl() + "console", true);
String logSource = this.source;
if (build != null) {
logSource = build.getUrl() + SUFFIX;
}
return teeOutput(output, logSource, true);

}

//introduced in jenkins 1.632
public OutputStream decorateLogger(Computer computer, OutputStream logger) throws IOException, InterruptedException {
return teeOutput(logger, computer.getUrl() + "log", false);
String logSource = this.source;
if (computer != null) {
logSource = computer.getUrl() + SUFFIX;
}
return teeOutput(logger, logSource, false);
}

private OutputStream teeOutput(OutputStream output, String source, boolean addLineNumber) {
Expand All @@ -70,7 +93,7 @@ public static class TeeOutputStream extends FilterOutputStream {
//holds data received, will be cleared when \n received
private ByteArrayOutputStream2 branch = new ByteArrayOutputStream2(512);
//holds decoded text with timestamp and line number, will be cleared when job is finished or batch size is reached
private ByteArrayOutputStream2 logText = new ByteArrayOutputStream2(MIN_BUFFER_SIZE);
private ByteArrayOutputStream2 logText = new ByteArrayOutputStream2(SplunkJenkinsInstallation.MIN_BUFFER_SIZE);
SimpleDateFormat sdf = new SimpleDateFormat(LOG_TIME_FORMAT, Locale.US);

public TeeOutputStream(OutputStream out, String sourceName) {
Expand Down

0 comments on commit c0a3888

Please sign in to comment.