|
| 1 | +package io.rsocket.metadata; |
| 2 | + |
| 3 | +import io.netty.buffer.ByteBuf; |
| 4 | +import io.netty.buffer.ByteBufAllocator; |
| 5 | + |
| 6 | +public class TracingMetadataCodec { |
| 7 | + |
| 8 | + static final int FLAG_EXTENDED_TRACE_ID_SIZE = 0b0000_1000; |
| 9 | + static final int FLAG_INCLUDE_PARENT_ID = 0b0000_0100; |
| 10 | + static final int FLAG_NOT_SAMPLED = 0b0001_0000; |
| 11 | + static final int FLAG_SAMPLED = 0b0010_0000; |
| 12 | + static final int FLAG_DEBUG = 0b0100_0000; |
| 13 | + static final int FLAG_IDS_SET = 0b1000_0000; |
| 14 | + |
| 15 | + public static ByteBuf encodeEmpty(ByteBufAllocator allocator, Flags flag) { |
| 16 | + |
| 17 | + return encode(allocator, true, 0, 0, false, 0, 0, false, flag); |
| 18 | + } |
| 19 | + |
| 20 | + public static ByteBuf encode128( |
| 21 | + ByteBufAllocator allocator, |
| 22 | + long traceIdHigh, |
| 23 | + long traceId, |
| 24 | + long spanId, |
| 25 | + long parentId, |
| 26 | + Flags flag) { |
| 27 | + |
| 28 | + return encode(allocator, false, traceIdHigh, traceId, true, spanId, parentId, true, flag); |
| 29 | + } |
| 30 | + |
| 31 | + public static ByteBuf encode128( |
| 32 | + ByteBufAllocator allocator, long traceIdHigh, long traceId, long spanId, Flags flag) { |
| 33 | + |
| 34 | + return encode(allocator, false, traceIdHigh, traceId, true, spanId, 0, false, flag); |
| 35 | + } |
| 36 | + |
| 37 | + public static ByteBuf encode64( |
| 38 | + ByteBufAllocator allocator, long traceId, long spanId, long parentId, Flags flag) { |
| 39 | + |
| 40 | + return encode(allocator, false, 0, traceId, false, spanId, parentId, true, flag); |
| 41 | + } |
| 42 | + |
| 43 | + public static ByteBuf encode64( |
| 44 | + ByteBufAllocator allocator, long traceId, long spanId, Flags flag) { |
| 45 | + return encode(allocator, false, 0, traceId, false, spanId, 0, false, flag); |
| 46 | + } |
| 47 | + |
| 48 | + static ByteBuf encode( |
| 49 | + ByteBufAllocator allocator, |
| 50 | + boolean isEmpty, |
| 51 | + long traceIdHigh, |
| 52 | + long traceId, |
| 53 | + boolean extendedTraceId, |
| 54 | + long spanId, |
| 55 | + long parentId, |
| 56 | + boolean includesParent, |
| 57 | + Flags flag) { |
| 58 | + int size = |
| 59 | + 1 |
| 60 | + + (isEmpty |
| 61 | + ? 0 |
| 62 | + : (Long.BYTES |
| 63 | + + Long.BYTES |
| 64 | + + (extendedTraceId ? Long.BYTES : 0) |
| 65 | + + (includesParent ? Long.BYTES : 0))); |
| 66 | + final ByteBuf buffer = allocator.buffer(size); |
| 67 | + |
| 68 | + int byteFlags = 0; |
| 69 | + switch (flag) { |
| 70 | + case NOT_SAMPLE: |
| 71 | + byteFlags |= FLAG_NOT_SAMPLED; |
| 72 | + break; |
| 73 | + case SAMPLE: |
| 74 | + byteFlags |= FLAG_SAMPLED; |
| 75 | + break; |
| 76 | + case DEBUG: |
| 77 | + byteFlags |= FLAG_DEBUG; |
| 78 | + break; |
| 79 | + } |
| 80 | + |
| 81 | + if (isEmpty) { |
| 82 | + return buffer.writeByte(byteFlags); |
| 83 | + } |
| 84 | + |
| 85 | + byteFlags |= FLAG_IDS_SET; |
| 86 | + |
| 87 | + if (extendedTraceId) { |
| 88 | + byteFlags |= FLAG_EXTENDED_TRACE_ID_SIZE; |
| 89 | + } |
| 90 | + |
| 91 | + if (includesParent) { |
| 92 | + byteFlags |= FLAG_INCLUDE_PARENT_ID; |
| 93 | + } |
| 94 | + |
| 95 | + buffer.writeByte(byteFlags); |
| 96 | + |
| 97 | + if (extendedTraceId) { |
| 98 | + buffer.writeLong(traceIdHigh); |
| 99 | + } |
| 100 | + |
| 101 | + buffer.writeLong(traceId).writeLong(spanId); |
| 102 | + |
| 103 | + if (includesParent) { |
| 104 | + buffer.writeLong(parentId); |
| 105 | + } |
| 106 | + |
| 107 | + return buffer; |
| 108 | + } |
| 109 | + |
| 110 | + public static TracingMetadata decode(ByteBuf byteBuf) { |
| 111 | + byteBuf.markReaderIndex(); |
| 112 | + try { |
| 113 | + byte flags = byteBuf.readByte(); |
| 114 | + boolean isNotSampled = (flags & FLAG_NOT_SAMPLED) == FLAG_NOT_SAMPLED; |
| 115 | + boolean isSampled = (flags & FLAG_SAMPLED) == FLAG_SAMPLED; |
| 116 | + boolean isDebug = (flags & FLAG_DEBUG) == FLAG_DEBUG; |
| 117 | + boolean isIDSet = (flags & FLAG_IDS_SET) == FLAG_IDS_SET; |
| 118 | + |
| 119 | + if (!isIDSet) { |
| 120 | + return new TracingMetadata(0, 0, 0, false, 0, true, isNotSampled, isSampled, isDebug); |
| 121 | + } |
| 122 | + |
| 123 | + boolean extendedTraceId = |
| 124 | + (flags & FLAG_EXTENDED_TRACE_ID_SIZE) == FLAG_EXTENDED_TRACE_ID_SIZE; |
| 125 | + |
| 126 | + long traceIdHigh; |
| 127 | + if (extendedTraceId) { |
| 128 | + traceIdHigh = byteBuf.readLong(); |
| 129 | + } else { |
| 130 | + traceIdHigh = 0; |
| 131 | + } |
| 132 | + |
| 133 | + long traceId = byteBuf.readLong(); |
| 134 | + long spanId = byteBuf.readLong(); |
| 135 | + |
| 136 | + boolean includesParent = (flags & FLAG_INCLUDE_PARENT_ID) == FLAG_INCLUDE_PARENT_ID; |
| 137 | + |
| 138 | + long parentId; |
| 139 | + if (includesParent) { |
| 140 | + parentId = byteBuf.readLong(); |
| 141 | + } else { |
| 142 | + parentId = 0; |
| 143 | + } |
| 144 | + |
| 145 | + return new TracingMetadata( |
| 146 | + traceIdHigh, |
| 147 | + traceId, |
| 148 | + spanId, |
| 149 | + includesParent, |
| 150 | + parentId, |
| 151 | + false, |
| 152 | + isNotSampled, |
| 153 | + isSampled, |
| 154 | + isDebug); |
| 155 | + } finally { |
| 156 | + byteBuf.resetReaderIndex(); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + public enum Flags { |
| 161 | + UNDECIDED, |
| 162 | + NOT_SAMPLE, |
| 163 | + SAMPLE, |
| 164 | + DEBUG |
| 165 | + } |
| 166 | +} |
0 commit comments