Skip to content

Commit

Permalink
Merge branch 'master' into nikita-tkachenko/tracer-autoconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-tkachenko-datadog committed Sep 7, 2023
2 parents b38ccfb + d7ef20e commit 437749d
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ of this software and associated documentation files (the "Software"), to deal
import com.timgroup.statsd.ServiceCheck;
import hudson.model.Run;
import hudson.util.Secret;
import org.datadog.jenkins.plugins.datadog.model.BuildData;
import org.jenkinsci.plugins.workflow.graph.FlowNode;

import java.util.Map;
import java.util.Set;
import org.datadog.jenkins.plugins.datadog.clients.Metrics;
import org.datadog.jenkins.plugins.datadog.model.BuildData;
import org.jenkinsci.plugins.workflow.graph.FlowNode;

public interface DatadogClient {

Expand Down Expand Up @@ -114,16 +114,7 @@ public ServiceCheck.Status toServiceCheckStatus(){
*/
public void flushCounters();

/**
* Sends a metric to the Datadog API, including the gauge name, and value.
*
* @param name - A String with the name of the metric to record.
* @param value - A long containing the value to submit.
* @param hostname - A String with the hostname to submit.
* @param tags - A Map containing the tags to submit.
* @return a boolean to signify the success or failure of the HTTP POST request.
*/
public boolean gauge(String name, long value, String hostname, Map<String, Set<String>> tags);
public Metrics metrics();

/**
* Sends a service check to the Datadog API, including the check name, and status.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ of this software and associated documentation files (the "Software"), to deal
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.datadog.jenkins.plugins.datadog.clients.HttpClient;
import org.datadog.jenkins.plugins.datadog.model.CIGlobalTagsAction;
import org.datadog.jenkins.plugins.datadog.model.GitCommitAction;
Expand Down Expand Up @@ -862,18 +863,14 @@ public static String statusFromResult(String result) {
}

@SuppressFBWarnings("NP_NULL_ON_SOME_PATH")
public static void severe(Logger logger, Throwable e, String message) {
if (message == null) {
message = e != null ? "An unexpected error occurred" : "";
public static void severe(Logger logger, Throwable e, String message){
if (e != null) {
String stackTrace = ExceptionUtils.getStackTrace(e);
message = (message != null ? message : "An unexpected error occurred: ") + stackTrace;
}
if (!message.isEmpty()) {
if (StringUtils.isNotEmpty(message)) {
logger.severe(message);
}
if (e != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.info(message + ": " + sw.toString());
}
}

public static int toInt(boolean b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,22 @@ public static DatadogClient getClient() {
// Only used for tests
return testClient;
}

DatadogGlobalConfiguration descriptor = DatadogUtilities.getDatadogGlobalDescriptor();
String reportWith = null;
String targetApiURL = null;
String targetLogIntakeURL = null;
String targetWebhookIntakeURL = null;
Secret targetApiKey = null;
String targetHost = null;
Integer targetPort = null;
Integer targetLogCollectionPort = null;
Integer targetTraceCollectionPort = null;
String ciInstanceName = null;
if(descriptor != null){
reportWith = descriptor.getReportWith();
targetApiURL = descriptor.getTargetApiURL();
targetLogIntakeURL = descriptor.getTargetLogIntakeURL();
targetWebhookIntakeURL = descriptor.getTargetWebhookIntakeURL();
targetApiKey = descriptor.getUsedApiKey();
targetHost = descriptor.getTargetHost();
targetPort = descriptor.getTargetPort();
targetLogCollectionPort = descriptor.getTargetLogCollectionPort();
targetTraceCollectionPort = descriptor.getTargetTraceCollectionPort();
ciInstanceName = descriptor.getCiInstanceName();
if (descriptor == null) {
return null;
}

String reportWith = descriptor.getReportWith();
String targetApiURL = descriptor.getTargetApiURL();
String targetLogIntakeURL = descriptor.getTargetLogIntakeURL();
String targetWebhookIntakeURL = descriptor.getTargetWebhookIntakeURL();
Secret targetApiKey = descriptor.getUsedApiKey();
String targetHost = descriptor.getTargetHost();
Integer targetPort = descriptor.getTargetPort();
Integer targetLogCollectionPort = descriptor.getTargetLogCollectionPort();
Integer targetTraceCollectionPort = descriptor.getTargetTraceCollectionPort();
String ciInstanceName = descriptor.getCiInstanceName();
return ClientFactory.getClient(DatadogClient.ClientType.valueOf(reportWith), targetApiURL, targetLogIntakeURL, targetWebhookIntakeURL,
targetApiKey, targetHost, targetPort, targetLogCollectionPort, targetTraceCollectionPort, ciInstanceName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ of this software and associated documentation files (the "Software"), to deal
import com.timgroup.statsd.StatsDClient;
import hudson.model.Run;
import hudson.util.Secret;
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.Socket;
Expand Down Expand Up @@ -287,6 +286,8 @@ private boolean reinitializeLogger(boolean force) {
}
try {
logger.info("Re/Initialize Datadog-Plugin Logger: hostname = " + this.hostname + ", logCollectionPort = " + this.logCollectionPort);
// need to close existing logger since it has a socket opened - not closing it leads to a file descriptor leak
close(this.ddLogger);
this.ddLogger = Logger.getLogger("Datadog-Plugin Logger");
this.ddLogger.setUseParentHandlers(false);
//Remove all existing Handlers
Expand Down Expand Up @@ -315,6 +316,24 @@ private boolean reinitializeLogger(boolean force) {
return true;
}


private void close(Logger logger) {
if (logger == null) {
return;
}
Handler[] handlers = logger.getHandlers();
if (handlers == null) {
return;
}
for (Handler handler : handlers) {
try {
handler.close();
} catch (Exception e) {
// ignore
}
}
}

/**
* Fetches the supported endpoints from the Trace Agent /info API
*
Expand Down Expand Up @@ -609,19 +628,29 @@ public void flushCounters() {
}

@Override
public boolean gauge(String name, long value, String hostname, Map<String, Set<String>> tags) {
try {
boolean status = reinitializeStatsDClient(false);
if(!status){
return false;
public Metrics metrics() {
return new AgentMetrics();
}

private final class AgentMetrics implements Metrics {
@Override
public void gauge(String name, long value, String hostname, Map<String, Set<String>> tags) {
try {
boolean status = reinitializeStatsDClient(false);
if (!status) {
return;
}
logger.fine("Submit gauge with dogStatD client");
statsd.gauge(name, value, TagsUtil.convertTagsToArray(tags));
} catch(Exception e){
DatadogUtilities.severe(logger, e, "Failed to send gauge metric payload to DogStatsD");
reinitializeStatsDClient(true);
}
logger.fine("Submit gauge with dogStatD client");
this.statsd.gauge(name, value, TagsUtil.convertTagsToArray(tags));
return true;
} catch(Exception e){
DatadogUtilities.severe(logger, e, "Failed to send gauge metric payload to DogStatsD");
reinitializeStatsDClient(true);
return false;
}

@Override
public void close() throws Exception {
// no op
}
}

Expand Down
Loading

0 comments on commit 437749d

Please sign in to comment.