Skip to content

Commit 51204fc

Browse files
authored
fix universal profiling in java8 again (#4394)
* fix universal profiling java 8 compatibility again * remove another potential issue * update changelog
1 parent d4cc377 commit 51204fc

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

‎CHANGELOG.next-release.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This file contains all changes which are not released yet.
1010
# Fixes
1111
<!--FIXES-START-->
1212

13+
* fix universal profiling integration compatibility with java8 - [#4394](https://github.com/elastic/apm-agent-java/pull/4394)
14+
1315
<!--FIXES-END-->
1416
# Features and enhancements
1517
<!--ENHANCEMENTS-START-->

‎apm-agent-core/src/main/java/co/elastic/apm/agent/universalprofiling/ProfilerSharedMemoryWriter.java‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ public class ProfilerSharedMemoryWriter {
4848
static ByteBuffer generateProcessCorrelationStorage(String serviceName, @Nullable String environment, String socketFilePath) {
4949
ByteBuffer buffer = ByteBuffer.allocateDirect(4096);
5050
buffer.order(ByteOrder.nativeOrder());
51-
//To make it java 8 compatible regardless of how it's compiled, need to explicitly use Buffer.position(int)
52-
((Buffer) buffer).position(0);
51+
setBufferPosition(buffer, 0);
5352

5453
buffer.putChar((char) 1); // layout-minor-version
5554
writeUtf8Str(buffer, serviceName);
@@ -64,6 +63,19 @@ private static void writeUtf8Str(ByteBuffer buffer, String str) {
6463
buffer.put(utf8);
6564
}
6665

66+
/**
67+
* Set {@link ByteBuffer} position in a way that is compatible with Java8.
68+
*
69+
* @param buffer byte buffer
70+
* @param position new position to set
71+
*/
72+
private static void setBufferPosition(ByteBuffer buffer, int position) {
73+
//To make it java 8 compatible regardless of how it's compiled, need to explicitly use Buffer.position(int)
74+
// a covariant return type override was introduced in ByteBuffer.position(int) in Java 9 which makes it break
75+
// binary compatibility with Java 8.
76+
((Buffer) buffer).position(position);
77+
}
78+
6779
/**
6880
* This method ensures that all writes which happened prior to this method call are not moved
6981
* after the method call due to reordering.
@@ -96,11 +108,11 @@ static void updateThreadCorrelationStorage(@Nullable AbstractSpanImpl<?> newSpan
96108
TransactionImpl tx = newSpan.getParentTransaction();
97109
tls.put(TLS_TRACE_PRESENT_OFFSET, (byte) 1);
98110
tls.put(TLS_TRACE_FLAGS_OFFSET, newSpan.getTraceContext().getFlags());
99-
tls.position(TLS_TRACE_ID_OFFSET);
111+
setBufferPosition(tls, TLS_TRACE_ID_OFFSET);
100112
newSpan.getTraceContext().getTraceId().writeToBuffer(tls);
101-
tls.position(TLS_SPAN_ID_OFFSET);
113+
setBufferPosition(tls,TLS_SPAN_ID_OFFSET);
102114
newSpan.getTraceContext().getId().writeToBuffer(tls);
103-
tls.position(TLS_LOCAL_ROOT_SPAN_ID_OFFSET);
115+
setBufferPosition(tls, TLS_LOCAL_ROOT_SPAN_ID_OFFSET);
104116
tx.getTraceContext().getId().writeToBuffer(tls);
105117
} else {
106118
tls.put(TLS_TRACE_PRESENT_OFFSET, (byte) 0);

‎apm-agent-plugin-sdk/src/main/java/co/elastic/apm/agent/sdk/internal/util/IOUtils.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public static byte[] copyToByteArray(List<ByteBuffer> buffers) {
285285
int off = 0;
286286
for (ByteBuffer buff : buffers) {
287287
int len = buff.position();
288-
buff.position(0);
288+
((Buffer) buff).position(0);
289289
buff.get(data, off, len);
290290
off += len;
291291
}

0 commit comments

Comments
 (0)