Skip to content

Commit db16d34

Browse files
authored
Mark Spans With Snapshot Attribute (#2197)
* Add attribute to all entry spans of traces that are registered for snapshotting. * Apply spotless code formatting. * Update licenses. * Rename test. * Remove unecessary OpenTelemetry context management from tests. * Extract attribute key to ProfilingSemanticAttributes.
1 parent 66aac94 commit db16d34

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

profiler/src/main/java/com/splunk/opentelemetry/profiler/ProfilingSemanticAttributes.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class ProfilingSemanticAttributes {
4141
public static final AttributeKey<Long> FRAME_COUNT = longKey("profiling.data.total.frame.count");
4242
public static final AttributeKey<String> INSTRUMENTATION_SOURCE =
4343
stringKey("profiling.instrumentation.source");
44+
public static final AttributeKey<Boolean> SNAPSHOT_PROFILING =
45+
AttributeKey.booleanKey("splunk.snapshot.profiling");
4446

4547
public static final AttributeKey<Long> THREAD_ID = longKey("thread.id");
4648
public static final AttributeKey<String> THREAD_NAME = stringKey("thread.name");

profiler/src/main/java/com/splunk/opentelemetry/profiler/snapshot/SnapshotProfilingSpanProcessor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.splunk.opentelemetry.profiler.snapshot;
1818

19+
import static com.splunk.opentelemetry.profiler.ProfilingSemanticAttributes.SNAPSHOT_PROFILING;
20+
1921
import io.opentelemetry.api.trace.SpanKind;
2022
import io.opentelemetry.context.Context;
2123
import io.opentelemetry.sdk.trace.ReadWriteSpan;
@@ -37,6 +39,10 @@ public void onStart(Context context, ReadWriteSpan span) {
3739
registry.register(span.getSpanContext());
3840
}
3941
}
42+
43+
if (isEntry(span) && registry.isRegistered(span.getSpanContext())) {
44+
span.setAttribute(SNAPSHOT_PROFILING, true);
45+
}
4046
}
4147

4248
@Override
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright Splunk Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.splunk.opentelemetry.profiler.snapshot;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import com.splunk.opentelemetry.profiler.snapshot.SnapshotSpanAttributeTest.TogglableTraceRegistry.State;
22+
import io.opentelemetry.api.common.AttributeKey;
23+
import io.opentelemetry.api.trace.SpanContext;
24+
import io.opentelemetry.api.trace.SpanKind;
25+
import io.opentelemetry.api.trace.Tracer;
26+
import io.opentelemetry.sdk.trace.ReadWriteSpan;
27+
import org.junit.jupiter.api.extension.RegisterExtension;
28+
import org.junit.jupiter.params.ParameterizedTest;
29+
30+
class SnapshotSpanAttributeTest {
31+
private final TogglableTraceRegistry registry = new TogglableTraceRegistry();
32+
private final SnapshotProfilingSdkCustomizer customizer =
33+
new SnapshotProfilingSdkCustomizer(registry);
34+
35+
@RegisterExtension
36+
public final OpenTelemetrySdkExtension s =
37+
OpenTelemetrySdkExtension.builder()
38+
.with(customizer)
39+
.withProperty("splunk.snapshot.profiler.enabled", "true")
40+
.build();
41+
42+
@ParameterizedTest
43+
@SpanKinds.Entry
44+
void addSnapshotSpanAttributeToEntrySpans(SpanKind kind, Tracer tracer) {
45+
var span = (ReadWriteSpan) tracer.spanBuilder("root").setSpanKind(kind).startSpan();
46+
var attribute = span.getAttribute(AttributeKey.booleanKey("splunk.snapshot.profiling"));
47+
assertThat(attribute).isTrue();
48+
}
49+
50+
@ParameterizedTest
51+
@SpanKinds.NonEntry
52+
void onlyRegisterTraceForProfilingWhenRootSpanIsEntrySpan(SpanKind kind, Tracer tracer) {
53+
var span = (ReadWriteSpan) tracer.spanBuilder("root").setSpanKind(kind).startSpan();
54+
var attribute = span.getAttribute(AttributeKey.booleanKey("splunk.snapshot.profiling"));
55+
assertThat(attribute).isNull();
56+
}
57+
58+
@ParameterizedTest
59+
@SpanKinds.Entry
60+
void addSnapshotSpanAttributeToAllEntrySpans(SpanKind kind, Tracer tracer) {
61+
try (var root = tracer.spanBuilder("root").setSpanKind(kind).startSpan().makeCurrent()) {
62+
try (var client =
63+
tracer.spanBuilder("client").setSpanKind(SpanKind.CLIENT).startSpan().makeCurrent()) {
64+
var span = (ReadWriteSpan) tracer.spanBuilder("root").setSpanKind(kind).startSpan();
65+
try (var ignored = span.makeCurrent()) {
66+
var attribute = span.getAttribute(AttributeKey.booleanKey("splunk.snapshot.profiling"));
67+
assertThat(attribute).isTrue();
68+
}
69+
}
70+
}
71+
}
72+
73+
@ParameterizedTest
74+
@SpanKinds.Entry
75+
void doNotAddSnapshotSpanAttributeWhenTraceIsNotRegisteredForSnapshotting(
76+
SpanKind kind, Tracer tracer) {
77+
registry.toggle(State.OFF);
78+
79+
var span = (ReadWriteSpan) tracer.spanBuilder("root").setSpanKind(kind).startSpan();
80+
var attribute = span.getAttribute(AttributeKey.booleanKey("splunk.snapshot.profiling"));
81+
assertThat(attribute).isNull();
82+
}
83+
84+
static class TogglableTraceRegistry extends TraceRegistry {
85+
enum State {
86+
ON,
87+
OFF
88+
}
89+
90+
private State state = State.ON;
91+
92+
@Override
93+
public void register(SpanContext spanContext) {
94+
if (state == State.ON) {
95+
super.register(spanContext);
96+
}
97+
}
98+
99+
public void toggle(State state) {
100+
this.state = state;
101+
}
102+
103+
@Override
104+
public boolean isRegistered(SpanContext spanContext) {
105+
return super.isRegistered(spanContext);
106+
}
107+
108+
@Override
109+
public void unregister(SpanContext spanContext) {
110+
super.unregister(spanContext);
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)