1717package com .splunk .opentelemetry .profiler .snapshot ;
1818
1919import static com .google .perftools .profiles .ProfileProto .Sample ;
20+ import static com .splunk .opentelemetry .profiler .ProfilingSemanticAttributes .SPAN_ID ;
21+ import static com .splunk .opentelemetry .profiler .ProfilingSemanticAttributes .TRACE_ID ;
2022import static org .awaitility .Awaitility .await ;
2123import static org .junit .jupiter .api .Assertions .assertEquals ;
2224
2325import com .google .perftools .profiles .ProfileProto .Profile ;
2426import com .splunk .opentelemetry .profiler .OtelLoggerFactory ;
25- import com .splunk .opentelemetry .profiler .ProfilingSemanticAttributes ;
2627import com .splunk .opentelemetry .profiler .pprof .PprofUtils ;
28+ import io .opentelemetry .api .common .AttributeKey ;
29+ import io .opentelemetry .api .trace .SpanContext ;
2730import io .opentelemetry .api .trace .SpanKind ;
2831import io .opentelemetry .api .trace .Tracer ;
2932import io .opentelemetry .context .Context ;
4447import org .junit .jupiter .params .ParameterizedTest ;
4548
4649class SnapshotProfilingLogExportingTest {
47- private static final Predicate <Map .Entry <String , Object >> TRACE_ID_LABEL =
48- kv -> ProfilingSemanticAttributes .TRACE_ID .getKey ().equals (kv .getKey ());
49-
5050 @ RegisterExtension private final ResetContextStorage spanTrackingActivator = new ResetContextStorage ();
5151
5252 private final InMemoryLogRecordExporter logExporter = InMemoryLogRecordExporter .create ();
@@ -71,11 +71,11 @@ void tearDown() {
7171 @ ParameterizedTest
7272 @ SpanKinds .Entry
7373 void exportStackTracesForProfiledTraces (SpanKind kind , Tracer tracer ) throws Exception {
74- String traceId ;
75- try (var ignored = Context .root ().with (Volume .HIGHEST ).makeCurrent ()) {
74+ SpanContext spanContext ;
75+ try (var ignoredScope1 = Context .root ().with (Volume .HIGHEST ).makeCurrent ()) {
7676 var span = tracer .spanBuilder ("root" ).setSpanKind (kind ).startSpan ();
77- try (var spanScope = span .makeCurrent ()) {
78- traceId = span .getSpanContext (). getTraceId ();
77+ try (var ignoredScope2 = span .makeCurrent ()) {
78+ spanContext = span .getSpanContext ();
7979 Thread .sleep (250 );
8080 }
8181 span .end ();
@@ -86,19 +86,27 @@ void exportStackTracesForProfiledTraces(SpanKind kind, Tracer tracer) throws Exc
8686 var logRecord = logExporter .getFinishedLogRecordItems ().get (0 );
8787 var profile = Profile .parseFrom (PprofUtils .deserialize (logRecord ));
8888
89- var traceIds =
90- profile .getSampleList ().stream ()
89+ assertEquals (Set .of (spanContext .getTraceId ()), findLabelValues (profile , TRACE_ID ));
90+ assertEquals (Set .of (spanContext .getSpanId ()), findLabelValues (profile , SPAN_ID ));
91+ }
92+
93+ private Set <String > findLabelValues (Profile profile , AttributeKey <String > key ) {
94+ return profile .getSampleList ().stream ()
9195 .flatMap (sampleLabels (profile ))
92- .filter (TRACE_ID_LABEL )
96+ .filter (label ( key ) )
9397 .map (Map .Entry ::getValue )
98+ .map (String ::valueOf )
9499 .collect (Collectors .toSet ());
95- assertEquals (Set .of (traceId ), traceIds );
96100 }
97101
98102 private Function <Sample , Stream <Map .Entry <String , Object >>> sampleLabels (Profile profile ) {
99103 return sample -> PprofUtils .toLabelString (sample , profile ).entrySet ().stream ();
100104 }
101105
106+ private Predicate <Map .Entry <String , Object >> label (AttributeKey <String > key ) {
107+ return kv -> key .getKey ().equals (kv .getKey ());
108+ }
109+
102110 private static class ResetContextStorage implements SpanTrackingActivator , AfterEachCallback {
103111 @ Override
104112 public void activate (TraceRegistry registry ) {
0 commit comments