-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error id adjustments for intake v2 (#217)
- add transactionId to TraceContext - Error.id is required in intake v2 - Error.id is 128 bits long closes #213
- Loading branch information
1 parent
e760b93
commit d8b583f
Showing
16 changed files
with
361 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
apm-agent-core/src/main/java/co/elastic/apm/impl/transaction/Id.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/*- | ||
* #%L | ||
* Elastic APM Java agent | ||
* %% | ||
* Copyright (C) 2018 Elastic and contributors | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
package co.elastic.apm.impl.transaction; | ||
|
||
import co.elastic.apm.objectpool.Recyclable; | ||
import co.elastic.apm.util.HexUtils; | ||
import com.dslplatform.json.JsonWriter; | ||
|
||
import javax.annotation.Nullable; | ||
import java.nio.ByteBuffer; | ||
import java.util.Arrays; | ||
import java.util.Random; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
/** | ||
* A 128 bit globally unique ID of the whole trace forest | ||
*/ | ||
public class Id implements Recyclable { | ||
|
||
private final byte[] data; | ||
private boolean empty = true; | ||
@Nullable | ||
private String cachedStringRepresentation; | ||
|
||
public static Id new128BitId() { | ||
return new Id(16); | ||
} | ||
|
||
public static Id new64BitId() { | ||
return new Id(8); | ||
} | ||
|
||
private Id(int idLengthBytes) { | ||
data = new byte[idLengthBytes]; | ||
} | ||
|
||
public void setToRandomValue() { | ||
setToRandomValue(ThreadLocalRandom.current()); | ||
} | ||
|
||
public void setToRandomValue(Random random) { | ||
random.nextBytes(data); | ||
onMutation(false); | ||
} | ||
|
||
public void fromHexString(String hexEncodedString, int offset) { | ||
HexUtils.nextBytes(hexEncodedString, offset, data); | ||
onMutation(); | ||
} | ||
|
||
public void fromLongs(long... values) { | ||
if (values.length * Long.BYTES != data.length) { | ||
throw new IllegalArgumentException("Invalid number of long values"); | ||
} | ||
final ByteBuffer buffer = ByteBuffer.wrap(data); | ||
for (long value : values) { | ||
buffer.putLong(value); | ||
} | ||
onMutation(); | ||
} | ||
|
||
@Override | ||
public void resetState() { | ||
for (int i = 0; i < data.length; i++) { | ||
data[i] = 0; | ||
} | ||
onMutation(true); | ||
} | ||
|
||
public void copyFrom(Id other) { | ||
System.arraycopy(other.data, 0, data, 0, data.length); | ||
onMutation(other.empty); | ||
} | ||
|
||
private void onMutation() { | ||
onMutation(isAllZeros(data)); | ||
} | ||
|
||
private void onMutation(boolean empty) { | ||
cachedStringRepresentation = null; | ||
this.empty = empty; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Id that = (Id) o; | ||
return Arrays.equals(data, that.data); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Arrays.hashCode(data); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
String s = cachedStringRepresentation; | ||
if (s == null) { | ||
s = cachedStringRepresentation = HexUtils.bytesToHex(data); | ||
} | ||
return s; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return empty; | ||
} | ||
|
||
private static boolean isAllZeros(byte[] bytes) { | ||
for (byte b : bytes) { | ||
if (b != 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public void writeAsHex(JsonWriter jw) { | ||
HexUtils.writeBytesAsHex(data, jw); | ||
} | ||
|
||
public void writeAsHex(StringBuilder sb) { | ||
HexUtils.writeBytesAsHex(data, sb); | ||
} | ||
|
||
/** | ||
* Returns the last 8 bytes of this id as a {@code long}. | ||
* <p> | ||
* The least significant bits (the right part) of an id is preferred to be used for making random sampling decisions. | ||
* </p> | ||
* <p> | ||
* "There are systems that make random sampling decisions based on the value of trace-id. | ||
* So to increase interoperability it is recommended to keep the random part on the right side of trace-id value." | ||
* </p> | ||
* @see <a href="https://github.com/w3c/distributed-tracing/blob/master/trace_context/HTTP_HEADER_FORMAT.md#trace-id">W3C trace context spec</a> | ||
* @return the last 8 bytes of this id as a {@code long} | ||
*/ | ||
public long getLeastSignificantBits() { | ||
return readLong(data.length - 8); | ||
} | ||
|
||
/** | ||
* Converts the next 8 bytes, starting from the offset, to a {@code long} | ||
*/ | ||
public long readLong(int offset) { | ||
long lsb = 0; | ||
for (int i = offset; i < offset + 8; i++) { | ||
lsb = (lsb << 8) | (data[i] & 0xff); | ||
} | ||
return lsb; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.