-
Notifications
You must be signed in to change notification settings - Fork 312
Update file probe format #9047
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
Merged
Merged
Update file probe format #9047
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
145 changes: 145 additions & 0 deletions
145
...gent/agent-debugger/src/main/java/com/datadog/debugger/agent/ConfigurationFileLoader.java
This file contains hidden or 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,145 @@ | ||
package com.datadog.debugger.agent; | ||
|
||
import com.datadog.debugger.probe.LogProbe; | ||
import com.datadog.debugger.probe.MetricProbe; | ||
import com.datadog.debugger.probe.ProbeDefinition; | ||
import com.datadog.debugger.probe.SpanDecorationProbe; | ||
import com.datadog.debugger.probe.SpanProbe; | ||
import com.datadog.debugger.probe.TriggerProbe; | ||
import com.datadog.debugger.util.MoshiHelper; | ||
import com.squareup.moshi.JsonAdapter; | ||
import com.squareup.moshi.JsonReader; | ||
import com.squareup.moshi.JsonWriter; | ||
import com.squareup.moshi.Moshi; | ||
import com.squareup.moshi.Types; | ||
import datadog.trace.util.SizeCheckedInputStream; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
import okio.Okio; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ConfigurationFileLoader { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationFileLoader.class); | ||
|
||
public static Configuration from(Path probeFilePath, long maxPayloadSize) { | ||
LOGGER.debug("try to load from file..."); | ||
try (InputStream inputStream = | ||
new SizeCheckedInputStream(new FileInputStream(probeFilePath.toFile()), maxPayloadSize)) { | ||
byte[] buffer = new byte[4096]; | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(4096); | ||
int bytesRead; | ||
do { | ||
bytesRead = inputStream.read(buffer); | ||
if (bytesRead > -1) { | ||
outputStream.write(buffer, 0, bytesRead); | ||
} | ||
} while (bytesRead > -1); | ||
byte[] configContent = outputStream.toByteArray(); | ||
Moshi moshi = MoshiHelper.createMoshiConfigBuilder().add(new ProbeFileFactory()).build(); | ||
ParameterizedType type = Types.newParameterizedType(List.class, ProbeDefinition.class); | ||
JsonAdapter<List<ProbeDefinition>> adapter = moshi.adapter(type); | ||
List<ProbeDefinition> probeDefinitions = | ||
adapter.fromJson( | ||
JsonReader.of(Okio.buffer(Okio.source(new ByteArrayInputStream(configContent))))); | ||
return new Configuration(null, probeDefinitions); | ||
} catch (IOException ex) { | ||
LOGGER.error("Unable to load config file {}: {}", probeFilePath, ex); | ||
return null; | ||
} | ||
} | ||
|
||
private static class ProbeFileFactory implements JsonAdapter.Factory { | ||
@Override | ||
public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) { | ||
if (Types.equals(type, Types.newParameterizedType(List.class, ProbeDefinition.class))) { | ||
return new ProbeFileAdapter( | ||
moshi.adapter(LogProbe.class), | ||
moshi.adapter(MetricProbe.class), | ||
moshi.adapter(SpanProbe.class), | ||
moshi.adapter(SpanDecorationProbe.class), | ||
moshi.adapter(TriggerProbe.class)); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
private static class ProbeFileAdapter extends JsonAdapter<List<ProbeDefinition>> { | ||
private final JsonAdapter<LogProbe> logProbeAdapter; | ||
private final JsonAdapter<MetricProbe> metricProbeAdapter; | ||
private final JsonAdapter<SpanProbe> spanProbeAdapter; | ||
private final JsonAdapter<SpanDecorationProbe> spanDecorationProbeAdapter; | ||
private final JsonAdapter<TriggerProbe> triggerProbeAdapter; | ||
|
||
public ProbeFileAdapter( | ||
JsonAdapter<LogProbe> logProbeAdapter, | ||
JsonAdapter<MetricProbe> metricProbeAdapter, | ||
JsonAdapter<SpanProbe> spanProbeAdapter, | ||
JsonAdapter<SpanDecorationProbe> spanDecorationProbeAdapter, | ||
JsonAdapter<TriggerProbe> triggerProbeAdapter) { | ||
this.logProbeAdapter = logProbeAdapter; | ||
this.metricProbeAdapter = metricProbeAdapter; | ||
this.spanProbeAdapter = spanProbeAdapter; | ||
this.spanDecorationProbeAdapter = spanDecorationProbeAdapter; | ||
this.triggerProbeAdapter = triggerProbeAdapter; | ||
} | ||
|
||
@Override | ||
public List<ProbeDefinition> fromJson(JsonReader reader) throws IOException { | ||
List<ProbeDefinition> probeDefinitions = new ArrayList<>(); | ||
reader.beginArray(); | ||
while (reader.hasNext()) { | ||
if (reader.peek() == JsonReader.Token.END_ARRAY) { | ||
reader.endArray(); | ||
break; | ||
} | ||
JsonReader jsonPeekReader = reader.peekJson(); | ||
jsonPeekReader.beginObject(); | ||
while (jsonPeekReader.hasNext()) { | ||
if (jsonPeekReader.selectName(JsonReader.Options.of("type")) == 0) { | ||
String type = jsonPeekReader.nextString(); | ||
switch (type) { | ||
case "LOG_PROBE": | ||
probeDefinitions.add(logProbeAdapter.fromJson(reader)); | ||
break; | ||
case "METRIC_PROBE": | ||
probeDefinitions.add(metricProbeAdapter.fromJson(reader)); | ||
break; | ||
case "SPAN_PROBE": | ||
probeDefinitions.add(spanProbeAdapter.fromJson(reader)); | ||
break; | ||
case "SPAN_DECORATION_PROBE": | ||
probeDefinitions.add(spanDecorationProbeAdapter.fromJson(reader)); | ||
break; | ||
case "TRIGGER_PROBE": | ||
probeDefinitions.add(triggerProbeAdapter.fromJson(reader)); | ||
break; | ||
default: | ||
throw new RuntimeException("Unknown type: " + type); | ||
} | ||
break; | ||
} else { | ||
jsonPeekReader.skipName(); | ||
jsonPeekReader.skipValue(); | ||
} | ||
} | ||
} | ||
return probeDefinitions; | ||
} | ||
|
||
@Override | ||
public void toJson(JsonWriter writer, List<ProbeDefinition> value) throws IOException { | ||
// Implement the logic to write the list of ProbeDefinition to JSON | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
31 changes: 31 additions & 0 deletions
31
.../agent-debugger/src/test/java/com/datadog/debugger/agent/ConfigurationFileLoaderTest.java
This file contains hidden or 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,31 @@ | ||
package com.datadog.debugger.agent; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import com.datadog.debugger.probe.LogProbe; | ||
import com.datadog.debugger.probe.MetricProbe; | ||
import com.datadog.debugger.probe.ProbeDefinition; | ||
import com.datadog.debugger.probe.SpanDecorationProbe; | ||
import com.datadog.debugger.probe.SpanProbe; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ConfigurationFileLoaderTest { | ||
|
||
@Test | ||
public void load() throws Exception { | ||
Path probeFilePath = | ||
Paths.get(ConfigurationFileLoaderTest.class.getResource("/test_probe_file.json").toURI()); | ||
Configuration configuration = ConfigurationFileLoader.from(probeFilePath, 1024 * 1024); | ||
assertNotNull(configuration); | ||
List<ProbeDefinition> definitions = configuration.getDefinitions(); | ||
assertEquals(5, definitions.size()); | ||
assertInstanceOf(MetricProbe.class, definitions.get(0)); | ||
assertInstanceOf(LogProbe.class, definitions.get(1)); | ||
assertInstanceOf(LogProbe.class, definitions.get(2)); | ||
assertInstanceOf(SpanProbe.class, definitions.get(3)); | ||
assertInstanceOf(SpanDecorationProbe.class, definitions.get(4)); | ||
} | ||
} |
This file contains hidden or 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
91 changes: 91 additions & 0 deletions
91
dd-java-agent/agent-debugger/src/test/resources/test_probe_file.json
This file contains hidden or 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,91 @@ | ||
[ | ||
{ | ||
"id": "123356536", | ||
"version": 0, | ||
"language": "java", | ||
"type": "LOG_PROBE", | ||
"where": { | ||
"typeName": "java.lang.Object", | ||
"methodName": "toString", | ||
"signature": "java.lang.String ()" | ||
} | ||
}, | ||
{ | ||
"id": "100c9a5c-45ad-49dc-818b-c570d31e11d1", | ||
"version": 0, | ||
"type": "LOG_PROBE", | ||
"where": { | ||
"sourceFile": "index.js", | ||
"lines": ["25"] | ||
}, | ||
"template": "Hello World", | ||
"segments": [{ | ||
"str": "Hello World" | ||
}], | ||
"captureSnapshot": true, | ||
"capture": { "maxReferenceDepth": 3 }, | ||
"sampling": { "snapshotsPerSecond": 100 } | ||
}, | ||
{ | ||
"id": "123356537", | ||
"language": "java", | ||
"type": "METRIC_PROBE", | ||
"where": { | ||
"typeName": "VetController", | ||
"methodName": "showVetList" | ||
}, | ||
"tags": ["version:v123", "env:staging"], | ||
"kind": "COUNT", | ||
"metricName": "datadog.debugger.showVetList.calls", | ||
"value": { | ||
"dsl": "42", | ||
"json": 42 | ||
} | ||
}, | ||
{ | ||
"id": "ad4cba6f-d476-4554-b5ed-80dd941a40d8", | ||
"version": 0, | ||
"type": "SPAN_DECORATION_PROBE", | ||
"language": "java", | ||
"where": { | ||
"typeName": "com.datadog.debugger.demomonitor.DemoRequestScheduledJob", | ||
"methodName": "fireRequests", | ||
"signature": "()" | ||
}, | ||
"tags": [], | ||
"evaluateAt": "EXIT", | ||
"targetSpan": "ROOT", | ||
"decorations": [ | ||
{ | ||
"tags": [ | ||
{ | ||
"name": "client", | ||
"value": { | ||
"segments": [ | ||
{ | ||
"dsl": "client", | ||
"json": { | ||
"ref": "client" | ||
} | ||
} | ||
], | ||
"template": "{client}" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"id": "70b55d06-f9fa-403b-a329-4f2f960aed01", | ||
"version": 0, | ||
"type": "SPAN_PROBE", | ||
"language": "java", | ||
"where": { | ||
"typeName": "MetadataClientUtils", | ||
"methodName": "listTableWithContinuation" | ||
}, | ||
"tags": [], | ||
"evaluateAt": "EXIT" | ||
} | ||
] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems a random, unrelated change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not random, I want a builder to add option for tests, while most of the time i need just a
Moshi
instance