Skip to content

Commit 7ae5ade

Browse files
authored
Merge pull request #7 from nulab/fix/opentelemetry-tracing
Add @WithSpan annotations for OpenTelemetry phase tracing
2 parents ddf6438 + f7234e3 commit 7ae5ade

6 files changed

Lines changed: 36 additions & 6 deletions

File tree

‎flexmark/pom.xml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@
6262
<groupId>com.vladsch.flexmark</groupId>
6363
<artifactId>flexmark-util-visitor</artifactId>
6464
</dependency>
65+
<dependency>
66+
<groupId>io.opentelemetry.instrumentation</groupId>
67+
<artifactId>opentelemetry-instrumentation-annotations</artifactId>
68+
</dependency>
6569
<dependency>
6670
<groupId>org.openjdk.jmh</groupId>
6771
<artifactId>jmh-core</artifactId>

‎flexmark/src/main/java/com/vladsch/flexmark/html/HtmlRenderer.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.vladsch.flexmark.util.sequence.Escaping;
1919
import com.vladsch.flexmark.util.sequence.LineAppendable;
2020
import com.vladsch.flexmark.util.sequence.TagRange;
21+
import io.opentelemetry.instrumentation.annotations.WithSpan;
2122
import org.jetbrains.annotations.NotNull;
2223
import org.jetbrains.annotations.Nullable;
2324

@@ -203,6 +204,7 @@ public void render(@NotNull Node node, @NotNull Appendable output) {
203204
* @param node node to render
204205
* @param output appendable to use for the output
205206
*/
207+
@WithSpan("markdown.render")
206208
public void render(@NotNull Node node, @NotNull Appendable output, int maxTrailingBlankLines) {
207209
HtmlWriter htmlWriter = new HtmlWriter(output, htmlOptions.indentSize, htmlOptions.formatFlags, !htmlOptions.htmlBlockOpenTagEol, !htmlOptions.htmlBlockCloseTagEol);
208210
MainNodeRenderer renderer = new MainNodeRenderer(options, htmlWriter, node.getDocument());

‎flexmark/src/main/java/com/vladsch/flexmark/parser/Parser.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.vladsch.flexmark.util.sequence.BasedSequence;
1818
import com.vladsch.flexmark.util.sequence.ReplacedBasedSequence;
1919
import com.vladsch.flexmark.util.sequence.mappers.SpecialLeadInHandler;
20+
import io.opentelemetry.instrumentation.annotations.WithSpan;
2021
import org.jetbrains.annotations.NotNull;
2122
import org.jetbrains.annotations.Nullable;
2223

@@ -353,6 +354,7 @@ public static Builder builder(DataHolder options) {
353354
* @param input the text to parse
354355
* @return the root node
355356
*/
357+
@WithSpan("markdown.parse")
356358
public @NotNull Document parse(@NotNull BasedSequence input) {
357359
// NOTE: parser can only handle contiguous sequences with no out of base characters
358360
if (input instanceof ReplacedBasedSequence) {
@@ -379,6 +381,7 @@ public static Builder builder(DataHolder options) {
379381
* @param input the text to parse
380382
* @return the root node
381383
*/
384+
@WithSpan("markdown.parse")
382385
public @NotNull Document parse(@NotNull String input) {
383386
DocumentParser documentParser = new DocumentParser(options
384387
, blockParserFactories
@@ -398,6 +401,7 @@ public static Builder builder(DataHolder options) {
398401
* @return the root node
399402
* @throws IOException when reading throws an exception
400403
*/
404+
@WithSpan("markdown.parse")
401405
public @NotNull Document parseReader(@NotNull Reader input) throws IOException {
402406
DocumentParser documentParser = new DocumentParser(options
403407
, blockParserFactories

‎flexmark/src/main/java/com/vladsch/flexmark/parser/internal/DocumentParser.java‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.vladsch.flexmark.util.misc.CharPredicate;
1919
import com.vladsch.flexmark.util.sequence.BasedSequence;
2020
import com.vladsch.flexmark.util.sequence.PrefixedSubSequence;
21+
import io.opentelemetry.instrumentation.annotations.WithSpan;
2122
import org.jetbrains.annotations.NotNull;
2223

2324
import java.io.BufferedReader;
@@ -271,16 +272,23 @@ public InlineParser getInlineParser() {
271272
*/
272273
public Document parse(CharSequence source) {
273274
BasedSequence input = BasedSequence.of(source);
274-
int lineStart = 0;
275-
int lineBreak;
276-
int lineEOL;
277-
int lineEnd;
278275
lineNumber = 0;
279276

280277
documentBlockParser.initializeDocument(options, input);
281278
inlineParser.initializeDocument(documentBlockParser.getBlock());
282279

283280
currentPhase = ParserPhase.PARSE_BLOCKS;
281+
parseBlocks(input);
282+
283+
return finalizeAndProcess();
284+
}
285+
286+
@WithSpan("markdown.parse.blocks")
287+
private void parseBlocks(BasedSequence input) {
288+
int lineStart = 0;
289+
int lineBreak;
290+
int lineEOL;
291+
int lineEnd;
284292

285293
while ((lineBreak = Parsing.findLineBreak(input, lineStart)) != -1) {
286294
BasedSequence line = input.subSequence(lineStart, lineBreak);
@@ -308,8 +316,6 @@ public Document parse(CharSequence source) {
308316
incorporateLine(lineWithEOL);
309317
lineNumber++;
310318
}
311-
312-
return finalizeAndProcess();
313319
}
314320

315321
public Document parse(Reader input) throws IOException {
@@ -745,6 +751,7 @@ private void finalize(BlockParser blockParser) {
745751
/**
746752
* Walk through a block & children recursively, parsing string content into inline content where appropriate.
747753
*/
754+
@WithSpan("markdown.parse.inlines")
748755
private void processInlines() {
749756
for (BlockParser blockParser : blockTracker.allBlockParsers()) {
750757
blockParser.parseInlines(inlineParser);
@@ -938,6 +945,7 @@ private void preProcessParagraph(Paragraph block, List<ParagraphPreProcessorFact
938945
}
939946
}
940947

948+
@WithSpan("markdown.parse.pre_process_paragraphs")
941949
private void preProcessParagraphs() {
942950
// here we run preProcessing stages
943951
if (blockTracker.getNodeClassifier().containsCategory(Paragraph.class)) {
@@ -951,6 +959,7 @@ private void preProcessParagraphs() {
951959
}
952960
}
953961

962+
@WithSpan("markdown.parse.pre_process_blocks")
954963
private void preProcessBlocks() {
955964
// here we run preProcessing stages
956965
HashSet<Class<?>> blockTypes = new HashSet<>();

‎flexmark/src/main/java/com/vladsch/flexmark/parser/internal/PostProcessorManager.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.vladsch.flexmark.util.dependency.DependencyResolver;
1313
import com.vladsch.flexmark.util.dependency.DependentItem;
1414
import com.vladsch.flexmark.util.dependency.DependentItemMap;
15+
import io.opentelemetry.instrumentation.annotations.WithSpan;
1516

1617
import java.util.*;
1718

@@ -50,6 +51,7 @@ public static List<PostProcessorDependencyStage> calculatePostProcessors(DataHol
5051
return dependencyStages;
5152
}
5253

54+
@WithSpan("markdown.parse.post_process")
5355
public static Document processDocument(Document document, List<PostProcessorDependencyStage> processorDependencies) {
5456
if (!processorDependencies.isEmpty()) {
5557
PostProcessorManager manager = new PostProcessorManager(processorDependencies);

‎pom.xml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,15 @@
538538
<version>${project.version}</version>
539539
</dependency>
540540

541+
<!-- Annotation-only dependency, spans are created by a Java agent
542+
(dd-java-agent with dd.trace.otel.enabled=true, or opentelemetry-javaagent).
543+
Inert without an agent. -->
544+
<dependency>
545+
<groupId>io.opentelemetry.instrumentation</groupId>
546+
<artifactId>opentelemetry-instrumentation-annotations</artifactId>
547+
<version>2.16.0</version>
548+
</dependency>
549+
541550
<!-- Common test dependencies -->
542551
<dependency>
543552
<groupId>junit</groupId>

0 commit comments

Comments
 (0)