Skip to content

Commit

Permalink
feat(bootstrap): Migrate to JSON component
Browse files Browse the repository at this point in the history
  • Loading branch information
PerfectSlayer committed Dec 4, 2024
1 parent b9230b5 commit 1776ab7
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 459 deletions.
1 change: 1 addition & 0 deletions dd-java-agent/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ tasks.withType(GenerateMavenPom).configureEach { task ->
}

dependencies {
implementation project(path: ':components:json')
modules {
module("com.squareup.okio:okio") {
replacedBy("com.datadoghq.okio:okio") // embed our patched fork
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package datadog.trace.bootstrap;

import datadog.json.JsonWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/** Thread safe telemetry class used to relay information about tracer activation. */
public abstract class BootstrapInitializationTelemetry {
/** Returns a singleton no op instance of initialization telemetry */
public static final BootstrapInitializationTelemetry noOpInstance() {
public static BootstrapInitializationTelemetry noOpInstance() {
return NoOp.INSTANCE;
}

Expand All @@ -17,8 +20,7 @@ public static final BootstrapInitializationTelemetry noOpInstance() {
*
* @param forwarderPath - a String - path to forwarding executable
*/
public static final BootstrapInitializationTelemetry createFromForwarderPath(
String forwarderPath) {
public static BootstrapInitializationTelemetry createFromForwarderPath(String forwarderPath) {
return new JsonBased(new ForwarderJsonSender(forwarderPath));
}

Expand Down Expand Up @@ -85,27 +87,29 @@ public void finish() {}
public static final class JsonBased extends BootstrapInitializationTelemetry {
private final JsonSender sender;

private JsonBuffer metaBuffer = new JsonBuffer();
private JsonBuffer pointsBuffer = new JsonBuffer();
private final List<String> meta;
private final List<String> points;

// one way false to true
private volatile boolean incomplete = false;

JsonBased(JsonSender sender) {
this.sender = sender;
this.meta = new ArrayList<>();
this.points = new ArrayList<>();
}

@Override
public void initMetaInfo(String attr, String value) {
synchronized (metaBuffer) {
metaBuffer.name(attr).value(value);
synchronized (this.meta) {
this.meta.add(attr);
this.meta.add(value);
}
}

@Override
public void onAbort(String reasonCode) {
onPoint("library_entrypoint.abort", "reason:" + reasonCode);

markIncomplete();
}

Expand All @@ -117,7 +121,6 @@ public void onError(Throwable t) {
@Override
public void onFatalError(Throwable t) {
onError(t);

markIncomplete();
}

Expand All @@ -126,71 +129,57 @@ public void onError(String reasonCode) {
onPoint("library_entrypoint.error", "error_type:" + reasonCode);
}

@Override
public void markIncomplete() {
incomplete = true;
}

void onPoint(String pointName) {
synchronized (pointsBuffer) {
pointsBuffer.beginObject();
pointsBuffer.name("name").value(pointName);
pointsBuffer.endObject();
private void onPoint(String name, String tag) {
synchronized (this.points) {
this.points.add(name);
this.points.add(tag);
}
}

void onPoint(String pointName, String tag) {
synchronized (pointsBuffer) {
pointsBuffer.beginObject();
pointsBuffer.name("name").value(pointName);
pointsBuffer.name("tags").array(tag);
pointsBuffer.endObject();
}
}

void onPoint(String pointName, String[] tags) {
synchronized (pointsBuffer) {
pointsBuffer.beginObject();
pointsBuffer.name("name").value(pointName);
pointsBuffer.name("tags").array(tags);
pointsBuffer.endObject();
}
@Override
public void markIncomplete() {
this.incomplete = true;
}

@Override
public void finish() {
if (!incomplete) {
onPoint("library_entrypoint.complete");
}

JsonBuffer buffer = new JsonBuffer();
buffer.beginObject();

buffer.name("metadata");
synchronized (metaBuffer) {
buffer.object(metaBuffer);
}

buffer.name("points");
synchronized (pointsBuffer) {
buffer.array(pointsBuffer);

pointsBuffer.reset();
}

buffer.endObject();

try {
sender.send(buffer);
try (JsonWriter writer = new JsonWriter()) {
writer.beginObject();
writer.name("metadata").beginObject();
synchronized (this.meta) {
for (int i = 0; i + 1 < this.meta.size(); i = i + 2) {
writer.name(this.meta.get(i));
writer.value(this.meta.get(i + 1));
}
}
writer.endObject();

writer.name("points").beginArray();
synchronized (this.points) {
for (int i = 0; i + 1 < this.points.size(); i = i + 2) {
writer.beginObject();
writer.name("name").value(this.points.get(i));
writer.name("tags").beginArray().value(this.points.get(i + 1)).endArray();
writer.endObject();
}
this.points.clear();
}
if (!this.incomplete) {
writer.beginObject().name("name").value("library_entrypoint.complete").endObject();
}
writer.endArray();
writer.endObject();

this.sender.send(writer.toByteArray());
} catch (Throwable t) {
// Since this is the reporting mechanism, there's little recourse here
// Decided to simply ignore - arguably might want to write to stderr
}
}
}

public static interface JsonSender {
public abstract void send(JsonBuffer buffer) throws IOException;
public interface JsonSender {
void send(byte[] payload) throws IOException;
}

public static final class ForwarderJsonSender implements JsonSender {
Expand All @@ -201,12 +190,12 @@ public static final class ForwarderJsonSender implements JsonSender {
}

@Override
public void send(JsonBuffer buffer) throws IOException {
public void send(byte[] payload) throws IOException {
ProcessBuilder builder = new ProcessBuilder(forwarderPath, "library_entrypoint");

Process process = builder.start();
try (OutputStream out = process.getOutputStream()) {
out.write(buffer.toByteArray());
out.write(payload);
}

try {
Expand Down
Loading

0 comments on commit 1776ab7

Please sign in to comment.