|
| 1 | +package com.datadog.profiling.ddprof; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | +import static org.mockito.Mockito.when; |
| 6 | + |
| 7 | +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
| 8 | +import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext; |
| 9 | +import datadog.trace.bootstrap.instrumentation.api.ProfilerContext; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +/** |
| 13 | + * Tests for {@link DatadogProfilingIntegration#onSpanFinished(AgentSpan)}. |
| 14 | + * |
| 15 | + * <p>Because {@link DatadogProfiler} wraps a native library, we verify the filtering logic and |
| 16 | + * dispatch path without asserting on the native event itself. Native calls simply must not throw |
| 17 | + * (the {@code if (profiler != null)} guard inside {@link DatadogProfiler} protects them on systems |
| 18 | + * where the native library is unavailable). |
| 19 | + */ |
| 20 | +class DatadogProfilerSpanNodeTest { |
| 21 | + |
| 22 | + /** |
| 23 | + * When the span's context does NOT implement {@link ProfilerContext}, {@code onSpanFinished} |
| 24 | + * should be a no-op and must not throw. |
| 25 | + */ |
| 26 | + @Test |
| 27 | + void onSpanFinished_nonProfilerContext_isNoOp() { |
| 28 | + DatadogProfilingIntegration integration = new DatadogProfilingIntegration(); |
| 29 | + AgentSpan span = mock(AgentSpan.class); |
| 30 | + AgentSpanContext ctx = mock(AgentSpanContext.class); // plain context, NOT a ProfilerContext |
| 31 | + when(span.context()).thenReturn(ctx); |
| 32 | + |
| 33 | + assertDoesNotThrow(() -> integration.onSpanFinished(span)); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * When the span's context DOES implement {@link ProfilerContext}, {@code onSpanFinished} extracts |
| 38 | + * fields and attempts to emit a SpanNode event. Must not throw regardless of whether the native |
| 39 | + * profiler is loaded. |
| 40 | + */ |
| 41 | + @Test |
| 42 | + void onSpanFinished_profilerContext_doesNotThrow() { |
| 43 | + DatadogProfilingIntegration integration = new DatadogProfilingIntegration(); |
| 44 | + |
| 45 | + // Mockito can create a mock that implements multiple interfaces |
| 46 | + AgentSpanContext ctx = mock(AgentSpanContext.class, org.mockito.Answers.RETURNS_DEFAULTS); |
| 47 | + ProfilerContext profilerCtx = mock(ProfilerContext.class); |
| 48 | + |
| 49 | + // We need a single object that satisfies both instanceof checks. |
| 50 | + // Use a hand-rolled stub instead. |
| 51 | + TestContext combinedCtx = new TestContext(42L, 7L, 1L, 3, 5); |
| 52 | + |
| 53 | + AgentSpan span = mock(AgentSpan.class); |
| 54 | + when(span.context()).thenReturn(combinedCtx); |
| 55 | + when(span.getStartTime()).thenReturn(1_700_000_000_000_000_000L); |
| 56 | + when(span.getDurationNano()).thenReturn(1_000_000L); |
| 57 | + |
| 58 | + assertDoesNotThrow(() -> integration.onSpanFinished(span)); |
| 59 | + } |
| 60 | + |
| 61 | + /** Null span must not throw (guard at top of onSpanFinished). */ |
| 62 | + @Test |
| 63 | + void onSpanFinished_nullSpan_doesNotThrow() { |
| 64 | + DatadogProfilingIntegration integration = new DatadogProfilingIntegration(); |
| 65 | + assertDoesNotThrow(() -> integration.onSpanFinished(null)); |
| 66 | + } |
| 67 | + |
| 68 | + // --------------------------------------------------------------------------- |
| 69 | + // Stub: a single object that satisfies both AgentSpanContext and ProfilerContext |
| 70 | + // --------------------------------------------------------------------------- |
| 71 | + |
| 72 | + private static final class TestContext implements AgentSpanContext, ProfilerContext { |
| 73 | + |
| 74 | + private final long spanId; |
| 75 | + private final long parentSpanId; |
| 76 | + private final long rootSpanId; |
| 77 | + private final int encodedOp; |
| 78 | + private final int encodedResource; |
| 79 | + |
| 80 | + TestContext( |
| 81 | + long spanId, long parentSpanId, long rootSpanId, int encodedOp, int encodedResource) { |
| 82 | + this.spanId = spanId; |
| 83 | + this.parentSpanId = parentSpanId; |
| 84 | + this.rootSpanId = rootSpanId; |
| 85 | + this.encodedOp = encodedOp; |
| 86 | + this.encodedResource = encodedResource; |
| 87 | + } |
| 88 | + |
| 89 | + // ProfilerContext |
| 90 | + @Override |
| 91 | + public long getSpanId() { |
| 92 | + return spanId; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public long getParentSpanId() { |
| 97 | + return parentSpanId; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public long getRootSpanId() { |
| 102 | + return rootSpanId; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public int getEncodedOperationName() { |
| 107 | + return encodedOp; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public CharSequence getOperationName() { |
| 112 | + return "test-op"; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public int getEncodedResourceName() { |
| 117 | + return encodedResource; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public CharSequence getResourceName() { |
| 122 | + return "test-resource"; |
| 123 | + } |
| 124 | + |
| 125 | + // AgentSpanContext |
| 126 | + @Override |
| 127 | + public datadog.trace.api.DDTraceId getTraceId() { |
| 128 | + return datadog.trace.api.DDTraceId.ZERO; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public datadog.trace.bootstrap.instrumentation.api.AgentTraceCollector getTraceCollector() { |
| 133 | + return datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopAgentTraceCollector |
| 134 | + .INSTANCE; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public int getSamplingPriority() { |
| 139 | + return datadog.trace.api.sampling.PrioritySampling.UNSET; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public Iterable<java.util.Map.Entry<String, String>> baggageItems() { |
| 144 | + return java.util.Collections.emptyList(); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public datadog.trace.api.datastreams.PathwayContext getPathwayContext() { |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public boolean isRemote() { |
| 154 | + return false; |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments