Skip to content

Support attaching tags to telemetry logs #8921

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

Open
wants to merge 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nullable;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

Expand All @@ -34,13 +35,27 @@ private LogCollector() {
this.rawLogMessages = new ConcurrentHashMap<>(maxCapacity);
}

public void addLogMessage(String logLevel, String message, Throwable throwable) {
public void addLogMessage(String logLevel, String message, @Nullable Throwable throwable) {
addLogMessage(logLevel, message, throwable, null);
}

/**
* Queue a log message to be sent on next telemetry flush.
*
* @param logLevel Log level (ERROR, WARN, DEBUG). Unknown log levels will be ignored.
* @param message Log message.
* @param throwable Optional throwable to attach a stacktrace.
* @param tags Optional tags to attach to the log. These are a comma-separated list, e.g.
* tag1:value1,tag2:value2
*/
public void addLogMessage(
String logLevel, String message, @Nullable Throwable throwable, @Nullable String tags) {
if (rawLogMessages.size() >= maxCapacity) {
// TODO: We could emit a metric for dropped logs.
return;
}
RawLogMessage rawLogMessage =
new RawLogMessage(logLevel, message, throwable, System.currentTimeMillis() / 1000);
new RawLogMessage(logLevel, message, throwable, tags, System.currentTimeMillis() / 1000);
AtomicInteger count = rawLogMessages.computeIfAbsent(rawLogMessage, k -> new AtomicInteger());
count.incrementAndGet();
}
Expand Down Expand Up @@ -73,13 +88,16 @@ public static class RawLogMessage {
public final String message;
public final String logLevel;
public final Throwable throwable;
public final String tags;
public final long timestamp;
public int count;

public RawLogMessage(String logLevel, String message, Throwable throwable, long timestamp) {
public RawLogMessage(
String logLevel, String message, Throwable throwable, String tags, long timestamp) {
this.logLevel = logLevel;
this.message = message;
this.throwable = throwable;
this.tags = tags;
this.timestamp = timestamp;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void doIteration(TelemetryService service) {
new LogMessage()
.message(rawLogMsg.message)
.tracerTime(rawLogMsg.timestamp)
.tags(rawLogMsg.tags)
.count(rawLogMsg.count);

if (rawLogMsg.logLevel != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ class LogPeriodicActionTest extends DDSpecification {
LogCollector.get().drain()
}

void 'log with tags'() {
LogMessage logMessage

when:
LogCollector.get().addLogMessage('ERROR', "test", null, 'tag1:value1,tag2:value2')
periodicAction.doIteration(telemetryService)

then:
1 * telemetryService.addLogMessage(_) >> { args -> logMessage = args[0] }
0 * _
logMessage.getLevel() == LogMessageLevel.ERROR
logMessage.getMessage() == 'test'
logMessage.getTags() == 'tag1:value1,tag2:value2'
}

void 'log with datadog throwable'() {
LogMessage logMessage

Expand Down