Skip to content

Commit

Permalink
Do not activate logs writer if logs are not enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-tkachenko-datadog committed Sep 20, 2024
1 parent aaa14ef commit 1d545fb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,6 @@ of this software and associated documentation files (the "Software"), to deal
import com.timgroup.statsd.NonBlockingStatsDClient;
import com.timgroup.statsd.ServiceCheck;
import com.timgroup.statsd.StatsDClient;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.logging.Handler;
import java.util.logging.Logger;
import java.util.logging.SocketHandler;
import javax.annotation.concurrent.GuardedBy;
import org.datadog.jenkins.plugins.datadog.DatadogClient;
import org.datadog.jenkins.plugins.datadog.DatadogEvent;
import org.datadog.jenkins.plugins.datadog.DatadogGlobalConfiguration;
Expand Down Expand Up @@ -305,6 +288,10 @@ private void stopStatsDClient() {

@Override
public LogWriteStrategy createLogWriteStrategy() {
if (logCollectionPort == null) {
logger.severe("Datadog Log Collection Port is not set properly, logs will not be written to Datadog");
return LogWriteStrategy.NO_OP;
}
return new AgentLogWriteStrategy(hostname, logCollectionPort);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ private void postApi(final JSONObject payload, final String type) throws IOExcep

@Override
public LogWriteStrategy createLogWriteStrategy() {
if (logIntakeUrl == null || logIntakeUrl.isEmpty()) {
logger.severe("Datadog Log Intake URL is not set properly, logs will not be written to Datadog");
return LogWriteStrategy.NO_OP;
}
return new ApiLogWriteStrategy(logIntakeUrl, apiKey, httpClient);
}

Expand Down Expand Up @@ -350,11 +354,6 @@ public void send(List<String> logs) {
}

private void doSend(List<String> payloads) throws Exception {
if (logIntakeUrl == null || logIntakeUrl.isEmpty()) {
logger.severe("Datadog Log Intake URL is not set properly");
return;
}

Map<String, String> headers = new HashMap<>();
headers.put("DD-API-KEY", Secret.toString(apiKey));
headers.put("Content-Encoding", "gzip");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
import java.util.List;

public interface LogWriteStrategy {

LogWriteStrategy NO_OP = new LogWriteStrategy() {
@Override
public void send(List<String> logs) {
// no op
}

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

void send(List<String> logs);
void close();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.datadog.jenkins.plugins.datadog.logs;

import hudson.init.Terminator;
import org.datadog.jenkins.plugins.datadog.DatadogGlobalConfiguration;
import org.datadog.jenkins.plugins.datadog.util.AsyncWriter;
import org.datadog.jenkins.plugins.datadog.DatadogClient;
import org.datadog.jenkins.plugins.datadog.DatadogUtilities;
Expand Down Expand Up @@ -32,6 +33,12 @@ public static synchronized void onDatadogClientUpdate(@Nullable DatadogClient cl
LOG_WRITER.stopAsynchronously();
}

DatadogGlobalConfiguration globalConfiguration = DatadogUtilities.getDatadogGlobalDescriptor();
if (globalConfiguration == null || !globalConfiguration.isCollectBuildLogs()) {
// skip activating the writer if logs should not be sent
return;
}

LogWriteStrategy logWriteStrategy = client.createLogWriteStrategy();
LOG_WRITER = new AsyncWriter<>("DD-Log-Writer",
logWriteStrategy::send,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import hudson.init.Terminator;
import org.datadog.jenkins.plugins.datadog.DatadogClient;
import org.datadog.jenkins.plugins.datadog.DatadogGlobalConfiguration;
import org.datadog.jenkins.plugins.datadog.DatadogUtilities;

import javax.annotation.Nullable;

Expand All @@ -18,6 +20,12 @@ public static synchronized void onDatadogClientUpdate(@Nullable DatadogClient cl
TRACE_WRITER.stopAsynchronously();
}

DatadogGlobalConfiguration globalConfiguration = DatadogUtilities.getDatadogGlobalDescriptor();
if (globalConfiguration == null || !globalConfiguration.getEnableCiVisibility()) {
// skip activating the writer if traces should not be sent
return;
}

TRACE_WRITER = new TraceWriter(client);
TRACE_WRITER.start();
}
Expand Down

0 comments on commit 1d545fb

Please sign in to comment.