Skip to content

Commit 40dbb76

Browse files
committed
[#12936] Support OtelServerTraceId in TraceIndexTable
1 parent 34b4d3e commit 40dbb76

File tree

4 files changed

+131
-9
lines changed

4 files changed

+131
-9
lines changed

commons-server/src/main/java/com/navercorp/pinpoint/common/server/scatter/TraceIndexValue.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.navercorp.pinpoint.common.buffer.AutomaticBuffer;
44
import com.navercorp.pinpoint.common.buffer.Buffer;
55
import com.navercorp.pinpoint.common.buffer.OffsetFixedBuffer;
6+
import com.navercorp.pinpoint.common.server.trace.OtelServerTraceId;
67
import com.navercorp.pinpoint.common.server.trace.PinpointServerTraceId;
78
import com.navercorp.pinpoint.common.server.trace.ServerTraceId;
89
import com.navercorp.pinpoint.common.server.util.TransactionIdParser;
@@ -12,6 +13,9 @@
1213

1314
public class TraceIndexValue {
1415

16+
public static final byte TRACE_TYPE_PINPOINT = (byte) 1;
17+
public static final byte TRACE_TYPE_OTEL = (byte) 2;
18+
1519
public record Index(String agentId, int elapsed, int errorCode) {
1620
public Index(String agentId, int elapsed, int errorCode) {
1721
this.agentId = Objects.requireNonNull(agentId, "agentId");
@@ -36,20 +40,20 @@ public static Index decode(byte[] bytes, int offset, int length) {
3640
}
3741
}
3842

39-
public record Meta(ServerTraceId transactionId, long startTime, String remoteAddr, String endpoint, String agentName) {
40-
public Meta(ServerTraceId transactionId, long startTime, String remoteAddr, String endpoint, String agentName) {
41-
this.transactionId = Objects.requireNonNull(transactionId, "transactionId");
43+
public record Meta(ServerTraceId serverTraceId, long startTime, String remoteAddr, String endpoint,
44+
String agentName) {
45+
public Meta(ServerTraceId serverTraceId, long startTime, String remoteAddr, String endpoint, String agentName) {
46+
this.serverTraceId = Objects.requireNonNull(serverTraceId, "serverTraceId");
4247
this.startTime = startTime;
4348
this.remoteAddr = remoteAddr;
4449
this.endpoint = endpoint;
4550
this.agentName = agentName;
4651
}
4752

48-
public static byte[] encode(ServerTraceId transactionId, long startTime, String remoteAddr, String endpoint, String agentName) {
53+
public static byte[] encode(ServerTraceId serverTraceId, long startTime, String remoteAddr, String endpoint, String agentName) {
4954
final Buffer buffer = new AutomaticBuffer(128);
5055
buffer.putLong(startTime);
51-
buffer.putByte((byte) 1); // version
52-
TransactionIdParser.writeTransactionIdV1(buffer, transactionId);
56+
encodeServerTraceId(buffer, serverTraceId);
5357
buffer.putPrefixedString(remoteAddr);
5458
buffer.putPrefixedString(endpoint);
5559
buffer.putPrefixedString(agentName);
@@ -59,13 +63,36 @@ public static byte[] encode(ServerTraceId transactionId, long startTime, String
5963
public static Meta decode(byte[] bytes, int offset, int length) {
6064
Buffer buffer = new OffsetFixedBuffer(bytes, offset, length);
6165
long startTime = buffer.readLong();
62-
buffer.readByte(); // version
63-
ServerTraceId serverTraceId = PinpointServerTraceId.of(buffer);
66+
ServerTraceId serverTraceId = decodeServerTraceId(buffer);
6467
String remoteAddr = buffer.readPrefixedString();
6568
String endpoint = buffer.readPrefixedString();
6669
String agentName = buffer.readPrefixedString();
6770
return new Meta(serverTraceId, startTime, remoteAddr, endpoint, agentName);
6871
}
72+
73+
74+
private static void encodeServerTraceId(Buffer buffer, ServerTraceId serverTraceId) {
75+
if (serverTraceId instanceof PinpointServerTraceId pinpointServerTraceId) {
76+
buffer.putByte(TRACE_TYPE_PINPOINT);
77+
TransactionIdParser.writeTransactionIdV1(buffer, pinpointServerTraceId);
78+
} else if (serverTraceId instanceof OtelServerTraceId otelServerTraceId) {
79+
buffer.putByte(TRACE_TYPE_OTEL);
80+
buffer.putBytes(otelServerTraceId.getId());
81+
} else {
82+
throw new IllegalArgumentException("Unknown ServerTraceId implementation: " + serverTraceId.getClass());
83+
}
84+
}
85+
86+
private static ServerTraceId decodeServerTraceId(Buffer buffer) {
87+
byte type = buffer.readByte();
88+
if (type == TRACE_TYPE_PINPOINT) {
89+
return PinpointServerTraceId.of(buffer);
90+
} else if (type == TRACE_TYPE_OTEL) {
91+
return OtelServerTraceId.of(buffer);
92+
} else {
93+
throw new IllegalArgumentException("Unknown ServerTraceId type: " + type);
94+
}
95+
}
6996
}
7097

7198
public record MetaRpc(String rpc) {

commons-server/src/main/java/com/navercorp/pinpoint/common/server/trace/OtelServerTraceId.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import com.navercorp.pinpoint.common.PinpointConstants;
5+
import com.navercorp.pinpoint.common.buffer.Buffer;
56
import com.navercorp.pinpoint.common.server.util.Base16Utils;
67

78
import java.util.Arrays;
@@ -12,6 +13,10 @@ public static OtelServerTraceId of(byte[] traceIdBytes, int offset, int length)
1213
return new OtelServerTraceId(Arrays.copyOfRange(traceIdBytes, offset, offset + length));
1314
}
1415

16+
public static OtelServerTraceId of(Buffer buffer) {
17+
return new OtelServerTraceId(buffer.readPadBytes(PinpointConstants.OPENTELEMETRY_TRACE_ID_LEN));
18+
}
19+
1520
public static OtelServerTraceId of(final String transactionId) {
1621
return new OtelServerTraceId(Base16Utils.decodeToBytes(transactionId));
1722
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2026 NAVER Corp.
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.navercorp.pinpoint.common.server.scatter;
18+
19+
import com.navercorp.pinpoint.common.server.trace.OtelServerTraceId;
20+
import com.navercorp.pinpoint.common.server.trace.PinpointServerTraceId;
21+
import com.navercorp.pinpoint.common.server.trace.ServerTraceId;
22+
import org.assertj.core.api.Assertions;
23+
import org.junit.jupiter.api.Test;
24+
25+
import java.util.Arrays;
26+
27+
public class TraceIndexValueTest {
28+
29+
@Test
30+
public void IndexEncodeDecodeTest() {
31+
String agentId = "agentId";
32+
int elapsed = 1234;
33+
int errorCode = 0;
34+
35+
byte[] encoded = TraceIndexValue.Index.encode(agentId, elapsed, errorCode);
36+
TraceIndexValue.Index index = TraceIndexValue.Index.decode(encoded, 0, encoded.length);
37+
38+
Assertions.assertThat(index.agentId()).isEqualTo(agentId);
39+
Assertions.assertThat(index.elapsed()).isEqualTo(elapsed);
40+
Assertions.assertThat(index.errorCode()).isEqualTo(errorCode);
41+
}
42+
43+
@Test
44+
public void MetaEncodeDecodeTest1() {
45+
ServerTraceId serverTraceId = new PinpointServerTraceId("rootAgentId", 123456789L, 1);
46+
long startTime = 123456789L;
47+
String remoteAddr = "1.2.3.4";
48+
String endpoint = "host:8080";
49+
String agentName = "agentName";
50+
51+
byte[] encoded = TraceIndexValue.Meta.encode(serverTraceId, startTime, remoteAddr, endpoint, agentName);
52+
TraceIndexValue.Meta meta = TraceIndexValue.Meta.decode(encoded, 0, encoded.length);
53+
54+
Assertions.assertThat(meta.serverTraceId()).isEqualTo(serverTraceId);
55+
Assertions.assertThat(meta.startTime()).isEqualTo(startTime);
56+
Assertions.assertThat(meta.remoteAddr()).isEqualTo(remoteAddr);
57+
Assertions.assertThat(meta.endpoint()).isEqualTo(endpoint);
58+
Assertions.assertThat(meta.agentName()).isEqualTo(agentName);
59+
}
60+
61+
@Test
62+
public void MetaEncodeDecodeTest2() {
63+
byte[] byte16 = new byte[16];
64+
Arrays.fill(byte16, (byte) 2);
65+
ServerTraceId serverTraceId = new OtelServerTraceId(byte16);
66+
long startTime = 123456789L;
67+
String remoteAddr = "1.2.3.4";
68+
String endpoint = "host:8080";
69+
String agentName = "agentName";
70+
71+
byte[] encoded = TraceIndexValue.Meta.encode(serverTraceId, startTime, remoteAddr, endpoint, agentName);
72+
TraceIndexValue.Meta meta = TraceIndexValue.Meta.decode(encoded, 0, encoded.length);
73+
74+
Assertions.assertThat(meta.serverTraceId()).isEqualTo(serverTraceId);
75+
Assertions.assertThat(meta.startTime()).isEqualTo(startTime);
76+
Assertions.assertThat(meta.remoteAddr()).isEqualTo(remoteAddr);
77+
Assertions.assertThat(meta.endpoint()).isEqualTo(endpoint);
78+
Assertions.assertThat(meta.agentName()).isEqualTo(agentName);
79+
}
80+
81+
@Test
82+
public void MetaRpcEncodeDecodeTest() {
83+
String rpc = "/api/test";
84+
85+
byte[] encoded = TraceIndexValue.MetaRpc.encode(rpc);
86+
TraceIndexValue.MetaRpc metaRpc = TraceIndexValue.MetaRpc.decode(encoded, 0, encoded.length);
87+
88+
Assertions.assertThat(metaRpc.rpc()).isEqualTo(rpc);
89+
}
90+
}

web/src/main/java/com/navercorp/pinpoint/web/scatter/vo/DotMetaData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public void setSpanId(long spanId) {
183183
}
184184

185185
public DotMetaData build() {
186-
Dot dot = new Dot(meta.transactionId(), acceptedTime, index.elapsed(), index.errorCode(), index.agentId());
186+
Dot dot = new Dot(meta.serverTraceId(), acceptedTime, index.elapsed(), index.errorCode(), index.agentId());
187187
String rpc = metaRpc != null ? metaRpc.rpc() : null;
188188
return new DotMetaData(dot, meta.agentName(), meta.remoteAddr(), rpc, meta.endpoint(), spanId, meta.startTime());
189189
}

0 commit comments

Comments
 (0)