Skip to content

Commit d152a4f

Browse files
committed
Add support for JFR contextual information
Fixes #2057
1 parent 42fea9d commit d152a4f

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

jfr-events/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Create JFR events that can be recorded and viewed in Java Mission Control (JMC).
55
* Creates Open Telemetry Tracing/Span events for spans
66
* The thread and stacktrace will be of the thead ending the span which might be different from the thread creating the span.
77
* Has the fields
8-
* Operation Name
8+
* Operation Name (`@Contextual`)
99
* Trace ID
1010
* Parent Span ID
1111
* Span ID
@@ -14,7 +14,7 @@ Create JFR events that can be recorded and viewed in Java Mission Control (JMC).
1414
* Multiple scopes might be collected for a single span
1515
* Has the fields
1616
* Trace ID
17-
* Span ID
17+
* Span ID (`@Contextual`)
1818
* Supports the Open Source version of JFR in Java 11.
1919
* Might support back port to OpenJDK 8, but not tested and classes are built with JDK 11 bytecode.
2020

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.contrib.jfrevent;
7+
8+
import java.lang.annotation.ElementType;
9+
import java.lang.annotation.Retention;
10+
import java.lang.annotation.RetentionPolicy;
11+
import java.lang.annotation.Target;
12+
import jdk.jfr.Label;
13+
import jdk.jfr.MetadataDefinition;
14+
import jdk.jfr.Name;
15+
16+
/**
17+
* Meta-annotation to support @Contextual without compiling against JDK 25.
18+
*
19+
* @see <a href="https://bugs.openjdk.org/browse/JDK-8356699">JDK-8356699<</a>
20+
*/
21+
@MetadataDefinition
22+
@Label("Context")
23+
@Retention(RetentionPolicy.RUNTIME)
24+
@Target({ElementType.FIELD})
25+
@Name("jdk.jfr.Contextual")
26+
@interface Contextual {}

jfr-events/src/main/java/io/opentelemetry/contrib/jfrevent/ScopeEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
+ "in scope/active on this thread.")
2121
class ScopeEvent extends Event {
2222

23-
private final String traceId;
23+
@Contextual private final String traceId;
2424
private final String spanId;
2525

2626
ScopeEvent(SpanContext spanContext) {

jfr-events/src/main/java/io/opentelemetry/contrib/jfrevent/SpanEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
@Description("Open Telemetry trace event corresponding to a span.")
1919
class SpanEvent extends Event {
2020

21-
private final String operationName;
21+
@Contextual private final String operationName;
2222
private final String traceId;
2323
private final String spanId;
2424
private final String parentId;

jfr-events/src/test/java/io/opentelemetry/contrib/jfrevent/JfrSpanProcessorTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.nio.file.Files;
1717
import java.nio.file.Path;
1818
import java.util.List;
19+
import java.util.stream.Collectors;
1920
import jdk.jfr.Recording;
2021
import jdk.jfr.consumer.RecordedEvent;
2122
import jdk.jfr.consumer.RecordingFile;
@@ -77,6 +78,14 @@ void basicSpan() throws IOException {
7778
.extracting(e -> e.getValue("spanId"))
7879
.isEqualTo(span.getSpanContext().getSpanId());
7980
assertThat(events).extracting(e -> e.getValue("operationName")).isEqualTo(OPERATION_NAME);
81+
assertThat(events)
82+
.extracting(
83+
e ->
84+
e.getFields().stream()
85+
.filter(f -> f.getName().equals("operationName"))
86+
.map(d -> d.getAnnotation(Contextual.class))
87+
.collect(Collectors.toList()))
88+
.isNotNull();
8089
} finally {
8190
Files.delete(output);
8291
}
@@ -119,6 +128,15 @@ void basicSpanWithScope() throws IOException, InterruptedException {
119128
.filteredOn(e -> "Span".equals(e.getEventType().getLabel()))
120129
.extracting(e -> e.getValue("operationName"))
121130
.isEqualTo(OPERATION_NAME);
131+
assertThat(events)
132+
.filteredOn(e -> "Scope".equals(e.getEventType().getLabel()))
133+
.extracting(
134+
e ->
135+
e.getFields().stream()
136+
.filter(f -> f.getName().equals("traceId"))
137+
.map(d -> d.getAnnotation(Contextual.class))
138+
.collect(Collectors.toList()))
139+
.isNotNull();
122140

123141
} finally {
124142
Files.delete(output);

0 commit comments

Comments
 (0)