use of org.apache.ignite.internal.processors.tracing.DeferredSpan in project ignite by apache.
the class GridTracingManager method serialize.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public byte[] serialize(@NotNull Span span) {
// Optimization for noop spi.
if (noop)
return NoopTracing.NOOP_SERIALIZED_SPAN;
// Optimization for NoopSpan.
if (span == NoopSpan.INSTANCE)
return NoopTracing.NOOP_SERIALIZED_SPAN;
if (span instanceof DeferredSpan)
return ((DeferredSpan) span).serializedSpan();
// Spi specific serialized span.
byte[] spiSpecificSerializedSpan = getSpi().serialize(((SpanImpl) span).spiSpecificSpan());
int serializedSpanLen = SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + PARENT_SPAN_TYPE_BYTES_LENGTH + INCLUDED_SCOPES_SIZE_BYTE_LENGTH + spiSpecificSerializedSpan.length + SCOPE_INDEX_BYTE_LENGTH * span.includedScopes().size();
byte[] serializedSpanBytes = new byte[serializedSpanLen];
// Skip special flags bytes.
// Spi type idx.
serializedSpanBytes[SPI_TYPE_OFF] = getSpi().type();
// Major protocol version;
serializedSpanBytes[MAJOR_PROTOCOL_VERSION_OFF] = MAJOR_PROTOCOL_VERSION;
// Minor protocol version;
serializedSpanBytes[MINOR_PROTOCOL_VERSION_OFF] = MINOR_PROTOCOL_VERSION;
// Spi specific serialized span length.
System.arraycopy(intToBytes(spiSpecificSerializedSpan.length), 0, serializedSpanBytes, SPI_SPECIFIC_SERIALIZED_SPAN_BYTES_LENGTH_OFF, SPI_SPECIFIC_SERIALIZED_SPAN_BYTES_LENGTH);
// Spi specific span.
System.arraycopy(spiSpecificSerializedSpan, 0, serializedSpanBytes, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF, spiSpecificSerializedSpan.length);
// Span type.
System.arraycopy(intToBytes(span.type().index()), 0, serializedSpanBytes, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + spiSpecificSerializedSpan.length, PARENT_SPAN_TYPE_BYTES_LENGTH);
assert span.includedScopes() != null;
// Included scope size
System.arraycopy(intToBytes(span.includedScopes().size()), 0, serializedSpanBytes, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + PARENT_SPAN_TYPE_BYTES_LENGTH + spiSpecificSerializedSpan.length, INCLUDED_SCOPES_SIZE_BYTE_LENGTH);
int includedScopesCnt = 0;
if (!span.includedScopes().isEmpty()) {
for (Scope includedScope : span.includedScopes()) {
System.arraycopy(shortToBytes(includedScope.idx()), 0, serializedSpanBytes, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + PARENT_SPAN_TYPE_BYTES_LENGTH + INCLUDED_SCOPES_SIZE_BYTE_LENGTH + spiSpecificSerializedSpan.length + SCOPE_INDEX_BYTE_LENGTH * includedScopesCnt++, SCOPE_INDEX_BYTE_LENGTH);
}
}
return serializedSpanBytes;
}
use of org.apache.ignite.internal.processors.tracing.DeferredSpan in project ignite by apache.
the class GridTracingManager method create.
/**
* {@inheritDoc}
*/
@Override
public Span create(@NotNull SpanType spanType, @Nullable byte[] serializedParentSpan) {
// Optimization for noop spi.
if (noop)
return NoopSpan.INSTANCE;
// Optimization for zero sampling rate == 0.
if ((serializedParentSpan.length == 0 || serializedParentSpan == null) && tracingConfiguration.get(new TracingConfigurationCoordinates.Builder(spanType.scope()).build()).samplingRate() == SAMPLING_RATE_NEVER)
return NoopSpan.INSTANCE;
// 1 byte: special flags;
// 1 bytes: spi type;
// 2 bytes: major protocol version;
// 2 bytes: minor protocol version;
// 4 bytes: spi specific serialized span length;
// n bytes: spi specific serialized span body;
// 4 bytes: span type
// 4 bytes included scopes size;
// 2 * included scopes size: included scopes items one by one;
Span span;
try {
if (serializedParentSpan == null || serializedParentSpan.length == 0)
return create(spanType, NoopSpan.INSTANCE);
// propagate serializedSpan as DeferredSpan.
if (serializedParentSpan[SPI_TYPE_OFF] != getSpi().type())
return new DeferredSpan(serializedParentSpan);
// propagate serializedSpan as DeferredSpan.
if (serializedParentSpan[MAJOR_PROTOCOL_VERSION_OFF] != MAJOR_PROTOCOL_VERSION)
return new DeferredSpan(serializedParentSpan);
// Deserialize and check minor protocol version.
// within the scope of the same major protocol version, protocol should be backwards compatible
byte minProtoVer = serializedParentSpan[MINOR_PROTOCOL_VERSION_OFF];
// Deserialize spi specific span size.
int spiSpecificSpanSize = bytesToInt(Arrays.copyOfRange(serializedParentSpan, SPI_SPECIFIC_SERIALIZED_SPAN_BYTES_LENGTH_OFF, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF), 0);
SpanType parentSpanType = null;
Set<Scope> includedScopes = new HashSet<>();
// Fall through.
switch(minProtoVer) {
case 0:
{
// Deserialize parent span type.
parentSpanType = SpanType.fromIndex(bytesToInt(Arrays.copyOfRange(serializedParentSpan, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + spiSpecificSpanSize, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + PARENT_SPAN_TYPE_BYTES_LENGTH + spiSpecificSpanSize), 0));
// Deserialize included scopes size.
int includedScopesSize = bytesToInt(Arrays.copyOfRange(serializedParentSpan, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + PARENT_SPAN_TYPE_BYTES_LENGTH + spiSpecificSpanSize, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + PARENT_SPAN_TYPE_BYTES_LENGTH + INCLUDED_SCOPES_SIZE_BYTE_LENGTH + spiSpecificSpanSize), 0);
// Deserialize included scopes one by one.
for (int i = 0; i < includedScopesSize; i++) {
includedScopes.add(Scope.fromIndex(bytesToShort(Arrays.copyOfRange(serializedParentSpan, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + PARENT_SPAN_TYPE_BYTES_LENGTH + INCLUDED_SCOPES_SIZE_BYTE_LENGTH + spiSpecificSpanSize + i * SCOPE_INDEX_BYTE_LENGTH, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + PARENT_SPAN_TYPE_BYTES_LENGTH + INCLUDED_SCOPES_SIZE_BYTE_LENGTH + SCOPE_INDEX_BYTE_LENGTH + spiSpecificSpanSize + i * SCOPE_INDEX_BYTE_LENGTH), 0)));
}
}
}
assert parentSpanType != null;
// If there's is parent span and parent span supports given scope then...
if (parentSpanType.scope() == spanType.scope() || includedScopes.contains(spanType.scope())) {
// create new span as child span for parent span, using parents span included scopes.
Set<Scope> mergedIncludedScopes = new HashSet<>(includedScopes);
mergedIncludedScopes.add(parentSpanType.scope());
mergedIncludedScopes.remove(spanType.scope());
span = new SpanImpl(getSpi().create(spanType.spanName(), Arrays.copyOfRange(serializedParentSpan, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + spiSpecificSpanSize)), spanType, mergedIncludedScopes);
} else {
// do nothing;
return new DeferredSpan(serializedParentSpan);
// "suppress" parent span for a while, create new span as separate one.
// return spi.create(trace, null, includedScopes);
}
} catch (Exception e) {
LT.warn(log, "Failed to create span from serialized value " + "[serializedValue=" + Arrays.toString(serializedParentSpan) + "]");
span = NoopSpan.INSTANCE;
}
return enrichWithLocalNodeParameters(span);
}
use of org.apache.ignite.internal.processors.tracing.DeferredSpan in project ignite by apache.
the class GridTracingManager method generateSpan.
/**
* Generates child span if it's possible due to parent/child included scopes, otherwise returns patent span as is.
* @param parentSpan Parent span.
* @param spanTypeToCreate Span type to create.
* @param lb Label.
*/
@SuppressWarnings("unchecked")
@NotNull
private Span generateSpan(@Nullable Span parentSpan, @NotNull SpanType spanTypeToCreate, @Nullable String lb) {
if (parentSpan instanceof DeferredSpan)
return create(spanTypeToCreate, ((DeferredSpan) parentSpan).serializedSpan());
if (parentSpan == NoopSpan.INSTANCE || parentSpan == null) {
if (spanTypeToCreate.rootSpan()) {
// Get tracing configuration.
TracingConfigurationParameters tracingConfigurationParameters = tracingConfiguration.get(new TracingConfigurationCoordinates.Builder(spanTypeToCreate.scope()).withLabel(lb).build());
return shouldSample(tracingConfigurationParameters.samplingRate()) ? new SpanImpl(getSpi().create(spanTypeToCreate.spanName(), (SpiSpecificSpan) null), spanTypeToCreate, tracingConfigurationParameters.includedScopes()) : NoopSpan.INSTANCE;
} else
return NoopSpan.INSTANCE;
} else {
// If there's is parent span and parent span supports given scope then...
if (parentSpan.isChainable(spanTypeToCreate.scope())) {
// create new span as child span for parent span, using parents span included scopes.
Set<Scope> mergedIncludedScopes = new HashSet<>(parentSpan.includedScopes());
mergedIncludedScopes.add(parentSpan.type().scope());
mergedIncludedScopes.remove(spanTypeToCreate.scope());
return new SpanImpl(getSpi().create(spanTypeToCreate.spanName(), ((SpanImpl) parentSpan).spiSpecificSpan()), spanTypeToCreate, mergedIncludedScopes);
} else {
// do nothing;
return NoopSpan.INSTANCE;
}
}
}
Aggregations