|
| 1 | +package com.vladsch.flexmark.core.test.util.parser; |
| 2 | + |
| 3 | +import com.vladsch.flexmark.ast.Text; |
| 4 | +import com.vladsch.flexmark.parser.Parser; |
| 5 | +import com.vladsch.flexmark.parser.block.DocumentPostProcessor; |
| 6 | +import com.vladsch.flexmark.parser.block.DocumentPostProcessorFactory; |
| 7 | +import com.vladsch.flexmark.parser.block.NodePostProcessor; |
| 8 | +import com.vladsch.flexmark.parser.block.NodePostProcessorFactory; |
| 9 | +import com.vladsch.flexmark.util.ast.Document; |
| 10 | +import com.vladsch.flexmark.util.ast.Node; |
| 11 | +import com.vladsch.flexmark.util.ast.NodeTracker; |
| 12 | +import io.opentelemetry.api.GlobalOpenTelemetry; |
| 13 | +import io.opentelemetry.api.common.AttributeKey; |
| 14 | +import io.opentelemetry.api.trace.Span; |
| 15 | +import io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule; |
| 16 | +import io.opentelemetry.sdk.trace.data.SpanData; |
| 17 | +import org.jetbrains.annotations.NotNull; |
| 18 | +import org.junit.After; |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Rule; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import static org.junit.Assert.assertEquals; |
| 26 | +import static org.junit.Assert.assertNotNull; |
| 27 | +import static org.junit.Assert.assertTrue; |
| 28 | + |
| 29 | +/** |
| 30 | + * Verifies that the per-processor span wraps {@code PostProcessorFactory.apply(...)}, so time spent |
| 31 | + * constructing a post processor is attributed to that processor's span. |
| 32 | + */ |
| 33 | +final public class PostProcessorManagerSpanTest { |
| 34 | + private static final AttributeKey<String> CLASS_ATTRIBUTE = AttributeKey.stringKey("markdown.post_processor.class"); |
| 35 | + |
| 36 | + @Rule |
| 37 | + public final OpenTelemetryRule openTelemetry = OpenTelemetryRule.create(); |
| 38 | + |
| 39 | + @Before |
| 40 | + public void installAsGlobal() { |
| 41 | + // PostProcessorManager obtains its tracer from GlobalOpenTelemetry, which the rule does not register. |
| 42 | + GlobalOpenTelemetry.resetForTest(); |
| 43 | + GlobalOpenTelemetry.set(openTelemetry.getOpenTelemetry()); |
| 44 | + } |
| 45 | + |
| 46 | + @After |
| 47 | + public void resetGlobal() { |
| 48 | + GlobalOpenTelemetry.resetForTest(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void nodePostProcessorFactoryIsAppliedInsideItsSpan() { |
| 53 | + RecordingNodePostProcessorFactory factory = new RecordingNodePostProcessorFactory(); |
| 54 | + Parser.builder().postProcessorFactory(factory).build().parse("some text"); |
| 55 | + |
| 56 | + assertFactoryAppliedInsideSpan(factory.spanCurrentDuringApply, RecordingNodePostProcessor.class); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void documentPostProcessorFactoryIsAppliedInsideItsSpan() { |
| 61 | + RecordingDocumentPostProcessorFactory factory = new RecordingDocumentPostProcessorFactory(); |
| 62 | + Parser.builder().postProcessorFactory(factory).build().parse("some text"); |
| 63 | + |
| 64 | + assertFactoryAppliedInsideSpan(factory.spanCurrentDuringApply, RecordingDocumentPostProcessor.class); |
| 65 | + } |
| 66 | + |
| 67 | + private void assertFactoryAppliedInsideSpan(Span spanCurrentDuringApply, Class<?> expectedProcessorClass) { |
| 68 | + assertNotNull("factory was not applied", spanCurrentDuringApply); |
| 69 | + List<SpanData> spans = openTelemetry.getSpans(); |
| 70 | + assertEquals(1, spans.size()); |
| 71 | + SpanData span = spans.get(0); |
| 72 | + assertEquals("markdown.parse.post_processor", span.getName()); |
| 73 | + assertEquals("factory was applied outside the post_processor span", span.getSpanId(), spanCurrentDuringApply.getSpanContext().getSpanId()); |
| 74 | + assertEquals(expectedProcessorClass.getName(), span.getAttributes().get(CLASS_ATTRIBUTE)); |
| 75 | + assertTrue(span.hasEnded()); |
| 76 | + } |
| 77 | + |
| 78 | + static class RecordingNodePostProcessorFactory extends NodePostProcessorFactory { |
| 79 | + Span spanCurrentDuringApply; |
| 80 | + |
| 81 | + RecordingNodePostProcessorFactory() { |
| 82 | + super(false); |
| 83 | + addNodes(Text.class); |
| 84 | + } |
| 85 | + |
| 86 | + @NotNull |
| 87 | + @Override |
| 88 | + public NodePostProcessor apply(@NotNull Document document) { |
| 89 | + spanCurrentDuringApply = Span.current(); |
| 90 | + return new RecordingNodePostProcessor(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + static class RecordingNodePostProcessor extends NodePostProcessor { |
| 95 | + @Override |
| 96 | + public void process(@NotNull NodeTracker state, @NotNull Node node) { |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + static class RecordingDocumentPostProcessorFactory extends DocumentPostProcessorFactory { |
| 101 | + Span spanCurrentDuringApply; |
| 102 | + |
| 103 | + @NotNull |
| 104 | + @Override |
| 105 | + public DocumentPostProcessor apply(@NotNull Document document) { |
| 106 | + spanCurrentDuringApply = Span.current(); |
| 107 | + return new RecordingDocumentPostProcessor(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + static class RecordingDocumentPostProcessor extends DocumentPostProcessor { |
| 112 | + @NotNull |
| 113 | + @Override |
| 114 | + public Document processDocument(@NotNull Document document) { |
| 115 | + return document; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments