Search in sources :

Example 1 with RecordedEvent

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);
            }
        }
    });
}
Also used : EventType(jdk.jfr.EventType) RecordedEvent(jdk.jfr.consumer.RecordedEvent) RecordingFile(jdk.jfr.consumer.RecordingFile) IOException(java.io.IOException) Properties(java.util.Properties) RecordingFile(jdk.jfr.consumer.RecordingFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.jupiter.api.Test)

Example 2 with RecordedEvent

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;
}
Also used : RecordedEvent(jdk.jfr.consumer.RecordedEvent) RecordingFile(jdk.jfr.consumer.RecordingFile)

Example 3 with RecordedEvent

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);
}
Also used : EventPeriods(com.splunk.opentelemetry.profiler.events.EventPeriods) EventType(jdk.jfr.EventType) Attributes(io.opentelemetry.api.common.Attributes) RecordedEvent(jdk.jfr.consumer.RecordedEvent) Test(org.junit.jupiter.api.Test)

Example 4 with RecordedEvent

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());
}
Also used : Clock(io.opentelemetry.sdk.common.Clock) TraceFlags(io.opentelemetry.api.trace.TraceFlags) LogProcessor(io.opentelemetry.sdk.logs.LogProcessor) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) SpanId(io.opentelemetry.api.trace.SpanId) Resource(io.opentelemetry.sdk.resources.Resource) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) SOURCE_EVENT_NAME(com.splunk.opentelemetry.profiler.ProfilingSemanticAttributes.SOURCE_EVENT_NAME) SpanLinkage(com.splunk.opentelemetry.profiler.context.SpanLinkage) EventPeriods(com.splunk.opentelemetry.profiler.events.EventPeriods) AtomicReference(java.util.concurrent.atomic.AtomicReference) Answer(org.mockito.stubbing.Answer) TraceState(io.opentelemetry.api.trace.TraceState) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ALLOCATION_SIZE_KEY(com.splunk.opentelemetry.profiler.TLABProcessor.ALLOCATION_SIZE_KEY) CONFIG_KEY_MEMORY_SAMPLER_INTERVAL(com.splunk.opentelemetry.profiler.Configuration.CONFIG_KEY_MEMORY_SAMPLER_INTERVAL) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EventType(jdk.jfr.EventType) LogData(io.opentelemetry.sdk.logs.data.LogData) CONFIG_KEY_TLAB_ENABLED(com.splunk.opentelemetry.profiler.Configuration.CONFIG_KEY_TLAB_ENABLED) SpanContext(io.opentelemetry.api.trace.SpanContext) DEFAULT_MEMORY_ENABLED(com.splunk.opentelemetry.profiler.Configuration.DEFAULT_MEMORY_ENABLED) Mockito.when(org.mockito.Mockito.when) DEFAULT_MEMORY_SAMPLING_INTERVAL(com.splunk.opentelemetry.profiler.Configuration.DEFAULT_MEMORY_SAMPLING_INTERVAL) Instant(java.time.Instant) RecordedStackTrace(jdk.jfr.consumer.RecordedStackTrace) SpanContextualizer(com.splunk.opentelemetry.profiler.context.SpanContextualizer) Config(io.opentelemetry.instrumentation.api.config.Config) TimeUnit(java.util.concurrent.TimeUnit) RecordedEvent(jdk.jfr.consumer.RecordedEvent) Test(org.junit.jupiter.api.Test) SOURCE_TYPE(com.splunk.opentelemetry.profiler.ProfilingSemanticAttributes.SOURCE_TYPE) RecordedThread(jdk.jfr.consumer.RecordedThread) StackSerializer(com.splunk.opentelemetry.profiler.util.StackSerializer) AttributeKey(io.opentelemetry.api.common.AttributeKey) TraceId(io.opentelemetry.api.trace.TraceId) Mockito.mock(org.mockito.Mockito.mock) LogData(io.opentelemetry.sdk.logs.data.LogData) EventPeriods(com.splunk.opentelemetry.profiler.events.EventPeriods) SpanContext(io.opentelemetry.api.trace.SpanContext) StackSerializer(com.splunk.opentelemetry.profiler.util.StackSerializer) Config(io.opentelemetry.instrumentation.api.config.Config) SpanLinkage(com.splunk.opentelemetry.profiler.context.SpanLinkage) Instant(java.time.Instant) RecordedEvent(jdk.jfr.consumer.RecordedEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) LogProcessor(io.opentelemetry.sdk.logs.LogProcessor) Clock(io.opentelemetry.sdk.common.Clock) SpanContextualizer(com.splunk.opentelemetry.profiler.context.SpanContextualizer)

Example 5 with RecordedEvent

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;
}
Also used : EventType(jdk.jfr.EventType) RecordedEvent(jdk.jfr.consumer.RecordedEvent) RecordedStackTrace(jdk.jfr.consumer.RecordedStackTrace) RecordedThread(jdk.jfr.consumer.RecordedThread)

Aggregations

RecordedEvent (jdk.jfr.consumer.RecordedEvent)22 Test (org.junit.jupiter.api.Test)13 EventType (jdk.jfr.EventType)11 RecordingFile (jdk.jfr.consumer.RecordingFile)6 Instant (java.time.Instant)5 RecordedThread (jdk.jfr.consumer.RecordedThread)5 Config (io.opentelemetry.instrumentation.api.config.Config)4 EventPeriods (com.splunk.opentelemetry.profiler.events.EventPeriods)3 TimeUnit (java.util.concurrent.TimeUnit)3 RecordedStackTrace (jdk.jfr.consumer.RecordedStackTrace)3 CONFIG_KEY_MEMORY_SAMPLER_INTERVAL (com.splunk.opentelemetry.profiler.Configuration.CONFIG_KEY_MEMORY_SAMPLER_INTERVAL)2 CONFIG_KEY_TLAB_ENABLED (com.splunk.opentelemetry.profiler.Configuration.CONFIG_KEY_TLAB_ENABLED)2 DEFAULT_MEMORY_ENABLED (com.splunk.opentelemetry.profiler.Configuration.DEFAULT_MEMORY_ENABLED)2 DEFAULT_MEMORY_SAMPLING_INTERVAL (com.splunk.opentelemetry.profiler.Configuration.DEFAULT_MEMORY_SAMPLING_INTERVAL)2 SOURCE_EVENT_NAME (com.splunk.opentelemetry.profiler.ProfilingSemanticAttributes.SOURCE_EVENT_NAME)2 SOURCE_TYPE (com.splunk.opentelemetry.profiler.ProfilingSemanticAttributes.SOURCE_TYPE)2 ALLOCATION_SIZE_KEY (com.splunk.opentelemetry.profiler.TLABProcessor.ALLOCATION_SIZE_KEY)2 SpanContextualizer (com.splunk.opentelemetry.profiler.context.SpanContextualizer)2 SpanLinkage (com.splunk.opentelemetry.profiler.context.SpanLinkage)2 StackSerializer (com.splunk.opentelemetry.profiler.util.StackSerializer)2