-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad7af64
commit fab4a0b
Showing
17 changed files
with
609 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
109 changes: 109 additions & 0 deletions
109
src/main/java/org/datadog/jenkins/plugins/datadog/DatadogPluginManagement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package org.datadog.jenkins.plugins.datadog; | ||
|
||
import edu.umd.cs.findbugs.annotations.CheckForNull; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import hudson.Extension; | ||
import hudson.ExtensionList; | ||
import hudson.model.ManagementLink; | ||
import hudson.security.Permission; | ||
import jenkins.model.Jenkins; | ||
import org.apache.commons.lang3.exception.ExceptionUtils; | ||
import org.datadog.jenkins.plugins.datadog.flare.FlareContributor; | ||
import org.kohsuke.stapler.StaplerRequest; | ||
import org.kohsuke.stapler.StaplerResponse; | ||
import org.kohsuke.stapler.interceptor.RequirePOST; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipOutputStream; | ||
|
||
@Extension | ||
public class DatadogPluginManagement extends ManagementLink { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(DatadogPluginManagement.class.getName()); | ||
|
||
@CheckForNull | ||
@Override | ||
public String getIconFileName() { | ||
return "/plugin/datadog/icons/dd_icon_rgb.svg"; | ||
} | ||
|
||
@CheckForNull | ||
@Override | ||
public String getDisplayName() { | ||
return "Datadog"; | ||
} | ||
|
||
@CheckForNull | ||
@Override | ||
public String getUrlName() { | ||
return "datadog"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Datadog Plugin Troubleshooting"; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Category getCategory() { | ||
return Category.TROUBLESHOOTING; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Permission getRequiredPermission() { | ||
return Jenkins.MANAGE; | ||
} | ||
|
||
@RequirePOST | ||
public void doDownloadDiagnosticFlare(StaplerRequest request, StaplerResponse response) throws Exception { | ||
if (!Jenkins.get().hasPermission(Jenkins.MANAGE)) { | ||
response.sendError(HttpServletResponse.SC_FORBIDDEN); | ||
return; | ||
} | ||
|
||
try { | ||
LocalDateTime now = LocalDateTime.now(); | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss"); | ||
String formattedTimestamp = now.format(formatter); | ||
|
||
response.setContentType("application/octet-stream"); | ||
response.setHeader("Content-Disposition", String.format("attachment; filename=dd-jenkins-plugin-flare-%s.zip", formattedTimestamp)); | ||
try (OutputStream out = response.getOutputStream()) { | ||
writeDiagnosticFlare(out); | ||
} | ||
|
||
} catch (Exception e) { | ||
LOGGER.log(Level.SEVERE, "Failed to generate Datadog plugin flare", e); | ||
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
||
private void writeDiagnosticFlare(OutputStream out) throws IOException { | ||
ExtensionList<FlareContributor> contributors = ExtensionList.lookup(FlareContributor.class); | ||
try (ZipOutputStream zipOut = new ZipOutputStream(out)) { | ||
for (FlareContributor contributor : contributors) { | ||
zipOut.putNextEntry(new ZipEntry(contributor.getFilename())); | ||
try { | ||
contributor.writeFileContents(zipOut); | ||
} catch (Exception e) { | ||
LOGGER.log(Level.SEVERE, "Datadog plugin flare contributor failed: " + contributor.getClass(), e); | ||
|
||
zipOut.closeEntry(); | ||
zipOut.putNextEntry(new ZipEntry(contributor.getFilename() + ".error")); | ||
zipOut.write(ExceptionUtils.getStackTrace(e).getBytes()); | ||
Check warning on line 102 in src/main/java/org/datadog/jenkins/plugins/datadog/DatadogPluginManagement.java ci.jenkins.io / SpotBugsDM_DEFAULT_ENCODING
Raw output
|
||
} finally { | ||
zipOut.closeEntry(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/org/datadog/jenkins/plugins/datadog/flare/ConnectivityChecksFlare.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.datadog.jenkins.plugins.datadog.flare; | ||
|
||
import hudson.Extension; | ||
import net.sf.json.JSONObject; | ||
import org.apache.commons.io.IOUtils; | ||
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.clients.DatadogApiClient; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
@Extension | ||
public class ConnectivityChecksFlare implements FlareContributor { | ||
|
||
@Override | ||
public String getFilename() { | ||
return "connectivity-checks.json"; | ||
} | ||
|
||
@Override | ||
public void writeFileContents(OutputStream out) throws IOException { | ||
JSONObject payload = new JSONObject(); | ||
|
||
// TODO rework the checks below following configuration refactoring | ||
DatadogGlobalConfiguration globalConfiguration = DatadogUtilities.getDatadogGlobalDescriptor(); | ||
DatadogClient.ClientType clientType = DatadogClient.ClientType.valueOf(globalConfiguration.getReportWith()); | ||
|
||
if (clientType == DatadogClient.ClientType.DSD) { | ||
payload.put("client-type", DatadogClient.ClientType.DSD); | ||
payload.put("logs-connectivity", globalConfiguration.doCheckAgentConnectivityLogs(globalConfiguration.getTargetHost(), String.valueOf(globalConfiguration.getTargetLogCollectionPort())).toString()); | ||
payload.put("traces-connectivity", globalConfiguration.doCheckAgentConnectivityTraces(globalConfiguration.getTargetHost(), String.valueOf(globalConfiguration.getTargetTraceCollectionPort())).toString()); | ||
|
||
} else if (clientType == DatadogClient.ClientType.HTTP) { | ||
payload.put("client-type", DatadogClient.ClientType.HTTP); | ||
payload.put("api-connectivity", DatadogApiClient.validateDefaultIntakeConnection(globalConfiguration.getTargetApiURL(), globalConfiguration.getUsedApiKey())); | ||
payload.put("logs-connectivity", DatadogApiClient.validateLogIntakeConnection(globalConfiguration.getTargetLogIntakeURL(), globalConfiguration.getUsedApiKey())); | ||
payload.put("traces-connectivity", DatadogApiClient.validateWebhookIntakeConnection(globalConfiguration.getTargetWebhookIntakeURL(), globalConfiguration.getUsedApiKey())); | ||
|
||
} else { | ||
throw new IllegalArgumentException("Unsupported client type: " + clientType); | ||
} | ||
|
||
String payloadString = payload.toString(2); | ||
IOUtils.write(payloadString, out, StandardCharsets.UTF_8); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/org/datadog/jenkins/plugins/datadog/flare/DatadogConfigFlare.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.datadog.jenkins.plugins.datadog.flare; | ||
|
||
import hudson.Extension; | ||
import hudson.util.XStream2; | ||
import org.datadog.jenkins.plugins.datadog.DatadogGlobalConfiguration; | ||
import org.datadog.jenkins.plugins.datadog.DatadogUtilities; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
@Extension | ||
public class DatadogConfigFlare implements FlareContributor { | ||
|
||
// TODO use XSTREAM from DatadogGlobalConfiguration following configuration refactor | ||
private static final XStream2 XSTREAM; | ||
|
||
static { | ||
XSTREAM = new XStream2(); | ||
XSTREAM.autodetectAnnotations(true); | ||
} | ||
|
||
@Override | ||
public String getFilename() { | ||
return "DatadogGlobalConfiguration.xml"; | ||
} | ||
|
||
@Override | ||
public void writeFileContents(OutputStream out) throws IOException { | ||
DatadogGlobalConfiguration globalConfiguration = DatadogUtilities.getDatadogGlobalDescriptor(); | ||
XSTREAM.toXMLUTF8(globalConfiguration, out); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/org/datadog/jenkins/plugins/datadog/flare/DatadogEnvVarsFlare.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.datadog.jenkins.plugins.datadog.flare; | ||
|
||
import hudson.Extension; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
@Extension | ||
public class DatadogEnvVarsFlare implements FlareContributor { | ||
|
||
@Override | ||
public String getFilename() { | ||
return "dd-env-vars.properties"; | ||
} | ||
|
||
@Override | ||
public void writeFileContents(OutputStream out) throws IOException { | ||
Properties datadogVariables = new Properties(); | ||
for (Map.Entry<String, String> e : System.getenv().entrySet()) { | ||
String name = e.getKey(); | ||
if (name.startsWith("DD_") || name.startsWith("DATADOG_")) { | ||
if (!name.contains("API_KEY") && !name.contains("APP_KEY")) { | ||
datadogVariables.put(name, e.getValue()); | ||
} | ||
} | ||
} | ||
datadogVariables.store(out, "Environment variables prefixed with DD_ or DATADOG_"); | ||
} | ||
|
||
} |
Oops, something went wrong.