use of jdk.jfr.consumer.RecordedEvent in project btrace by btraceio.
the class BTraceFunctionalTests method testJfr.
@Test
public void testJfr() throws Exception {
String rtVersion = System.getProperty("java.runtime.version", "");
String testJavaHome = System.getenv().get("TEST_JAVA_HOME");
if (testJavaHome != null) {
Properties releaseProps = new Properties();
releaseProps.load(new FileInputStream(new File(testJavaHome + File.separator + "release")));
rtVersion = releaseProps.getProperty("JAVA_VERSION").replace("\"", "");
}
if (!rtVersion.startsWith("15.")) {
// skip the test for 8.0.* because of missing support
// skip all non-LTS versions (except the last one)
// skip the test for JDK 11 since the latest version 11.0.9 and newer ends in SISGSEGV
System.err.println("Skipping test for JDK " + rtVersion);
return;
}
testWithJfr("resources.Main", "btrace/JfrTest.java", 10, new ResultValidator() {
@Override
public void validate(String stdout, String stderr, int retcode, String jfrFile) {
assertFalse(stdout.contains("FAILED"), "Script should not have failed");
assertTrue(stderr.isEmpty(), "Non-empty stderr");
assertNotNull(jfrFile);
try {
RecordingFile f = new RecordingFile(Paths.get(jfrFile));
boolean hasPeriodicType = false, hasPeriodicValue = false, hasCustomType = false, hasCustomValue = false;
for (EventType et : f.readEventTypes()) {
if (et.getName().equals("periodic")) {
hasPeriodicType = true;
} else if (et.getName().equals("custom")) {
hasCustomType = true;
}
if (hasPeriodicType && hasCustomType) {
while (f.hasMoreEvents()) {
RecordedEvent e = f.readEvent();
if (e.getEventType().getName().equals("periodic")) {
hasPeriodicValue = true;
} else if (e.getEventType().getName().equals("custom")) {
hasCustomValue = true;
}
if (hasPeriodicValue && hasCustomValue) {
return;
}
}
break;
}
}
fail("periodic type ok: " + hasPeriodicType + ", periodic value ok: " + hasPeriodicValue + ", custom type ok: " + hasCustomType + ", custom value ok: " + hasCustomValue);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}
use of jdk.jfr.consumer.RecordedEvent in project opentelemetry-java-instrumentation by open-telemetry.
the class JFRUtils method reduce.
private static <T, V> T reduce(Path jfrFile, String eventName, String valueKey, T initial, BiFunction<T, V, T> reducer) throws IOException {
RecordingFile recordingFile = new RecordingFile(jfrFile);
T result = initial;
while (recordingFile.hasMoreEvents()) {
RecordedEvent recordedEvent = recordingFile.readEvent();
if (eventName.equals(recordedEvent.getEventType().getName())) {
V value = recordedEvent.getValue(valueKey);
result = reducer.apply(result, value);
}
}
return result;
}
use of jdk.jfr.consumer.RecordedEvent in project splunk-otel-java by signalfx.
the class LogDataCommonAttributesTest method testBuild.
@Test
void testBuild() {
String sourceType = "otel.profiling";
String eventName = "core.core.unicorn";
Attributes expected = Attributes.builder().put(SOURCE_TYPE, sourceType).put(SOURCE_EVENT_NAME, eventName).put(SOURCE_EVENT_PERIOD, 999L).build();
EventPeriods periods = mock(EventPeriods.class);
RecordedEvent event = mock(RecordedEvent.class);
EventType eventType = mock(EventType.class);
when(event.getEventType()).thenReturn(eventType);
when(eventType.getName()).thenReturn(eventName);
when(periods.getDuration(eventName)).thenReturn(Duration.ofMillis(999));
LogDataCommonAttributes logDataAttributes = new LogDataCommonAttributes(periods);
Attributes result = logDataAttributes.builder(event.getEventType().getName()).build();
assertEquals(expected, result);
}
use of jdk.jfr.consumer.RecordedEvent in project splunk-otel-java by signalfx.
the class TLABProcessorTest method tlabProcessTest.
private void tlabProcessTest(Long tlabSize) {
Instant now = Instant.now();
AtomicReference<LogData> seenLogData = new AtomicReference<>();
LogProcessor consumer = seenLogData::set;
String stackAsString = "\"mockingbird\" #606\n" + " java.lang.Thread.State: UNKNOWN\n" + "i am a serialized stack believe me";
StackSerializer serializer = mock(StackSerializer.class);
LogDataCommonAttributes commonAttrs = new LogDataCommonAttributes(new EventPeriods(x -> null));
Clock clock = new MockClock(now);
RecordedEvent event = createMockEvent(serializer, now, tlabSize);
Config config = mock(Config.class);
when(config.getBoolean(CONFIG_KEY_TLAB_ENABLED, DEFAULT_MEMORY_ENABLED)).thenReturn(true);
SpanContext spanContext = SpanContext.create(TraceId.fromLongs(123, 456), SpanId.fromLong(123), TraceFlags.getSampled(), TraceState.getDefault());
SpanContextualizer spanContextualizer = mock(SpanContextualizer.class);
when(spanContextualizer.link(THREAD_ID)).thenReturn(new SpanLinkage(spanContext, THREAD_ID));
TLABProcessor processor = TLABProcessor.builder(config).stackSerializer(serializer).logProcessor(consumer).commonAttributes(commonAttrs).resource(Resource.getDefault()).spanContextualizer(spanContextualizer).build();
processor.accept(event);
assertEquals(stackAsString, seenLogData.get().getBody().asString());
assertEquals(TimeUnit.SECONDS.toNanos(now.getEpochSecond()) + clock.nanoTime(), seenLogData.get().getEpochNanos());
assertEquals("otel.profiling", seenLogData.get().getAttributes().get(SOURCE_TYPE));
assertEquals("tee-lab", seenLogData.get().getAttributes().get(SOURCE_EVENT_NAME));
assertEquals(ONE_MB, seenLogData.get().getAttributes().get(ALLOCATION_SIZE_KEY));
assertEquals(spanContext, seenLogData.get().getSpanContext());
}
use of jdk.jfr.consumer.RecordedEvent in project splunk-otel-java by signalfx.
the class TLABProcessorTest method createMockEvent.
private RecordedEvent createMockEvent(StackSerializer serializer, Instant now, Long tlabSize) {
String stackAsString = "i am a serialized stack believe me";
RecordedEvent event = mock(RecordedEvent.class);
RecordedStackTrace stack = mock(RecordedStackTrace.class);
EventType eventType = mock(EventType.class);
RecordedThread mockThread = mock(RecordedThread.class);
when(event.getStartTime()).thenReturn(now);
when(event.getStackTrace()).thenReturn(stack);
when(event.getEventType()).thenReturn(eventType);
when(event.getLong("allocationSize")).thenReturn(ONE_MB);
when(event.getThread()).thenReturn(mockThread);
when(mockThread.getJavaThreadId()).thenReturn(THREAD_ID);
when(mockThread.getJavaName()).thenReturn("mockingbird");
when(event.hasField("tlabSize")).thenReturn(tlabSize != null);
if (tlabSize == null) {
when(event.getLong("tlabSize")).thenThrow(NullPointerException.class);
} else {
when(event.getLong("tlabSize")).thenReturn(tlabSize);
}
when(eventType.getName()).thenReturn("tee-lab");
when(serializer.serialize(stack)).thenReturn(stackAsString);
return event;
}
Aggregations