Search in sources :

Example 6 with Scope

use of org.apache.ignite.spi.tracing.Scope in project ignite by apache.

the class OpenCensusTxTracingConfigurationTest method testThatScopeSpecificConfigurationIsUsedIfLabelSpecificNotFound.

/**
 * Ensure that scope specific configuration is used if corresponding label specific not found.
 *
 * @throws Exception If Failed.
 */
@Test
public void testThatScopeSpecificConfigurationIsUsedIfLabelSpecificNotFound() throws Exception {
    IgniteEx client = startGrid("client");
    client.tracingConfiguration().set(new TracingConfigurationCoordinates.Builder(TX).build(), new TracingConfigurationParameters.Builder().withSamplingRate(SAMPLING_RATE_ALWAYS).build());
    client.transactions().withLabel("label1").txStart(PESSIMISTIC, SERIALIZABLE).commit();
    handler().flush();
    java.util.List<SpanData> gotSpans = handler().allSpans().filter(span -> SpanType.TX.spanName().equals(span.getName())).collect(Collectors.toList());
    assertEquals(1, gotSpans.size());
}
Also used : Arrays(java.util.Arrays) SERIALIZABLE(org.apache.ignite.transactions.TransactionIsolation.SERIALIZABLE) SpanType(org.apache.ignite.internal.processors.tracing.SpanType) Transaction(org.apache.ignite.transactions.Transaction) Set(java.util.Set) IgniteEx(org.apache.ignite.internal.IgniteEx) Scope(org.apache.ignite.spi.tracing.Scope) Test(org.junit.Test) Collectors(java.util.stream.Collectors) SpanData(io.opencensus.trace.export.SpanData) OpenCensusTracingSpi(org.apache.ignite.spi.tracing.opencensus.OpenCensusTracingSpi) TX(org.apache.ignite.spi.tracing.Scope.TX) SAMPLING_RATE_ALWAYS(org.apache.ignite.spi.tracing.TracingConfigurationParameters.SAMPLING_RATE_ALWAYS) TracingConfigurationParameters(org.apache.ignite.spi.tracing.TracingConfigurationParameters) TracingSpi(org.apache.ignite.spi.tracing.TracingSpi) SpanId(io.opencensus.trace.SpanId) SAMPLING_RATE_NEVER(org.apache.ignite.spi.tracing.TracingConfigurationParameters.SAMPLING_RATE_NEVER) PESSIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC) Collections(java.util.Collections) TracingConfigurationCoordinates(org.apache.ignite.spi.tracing.TracingConfigurationCoordinates) TracingConfigurationParameters(org.apache.ignite.spi.tracing.TracingConfigurationParameters) SpanData(io.opencensus.trace.export.SpanData) IgniteEx(org.apache.ignite.internal.IgniteEx) Test(org.junit.Test)

Example 7 with Scope

use of org.apache.ignite.spi.tracing.Scope in project ignite by apache.

the class TracingConfigurationCommand method parseArguments.

/**
 * {@inheritDoc}
 */
@Override
public void parseArguments(CommandArgIterator argIter) {
    // If there is no arguments, use list command.
    if (!argIter.hasNextSubArg()) {
        args = new TracingConfigurationArguments.Builder(TracingConfigurationSubcommand.GET_ALL).build();
        return;
    }
    TracingConfigurationSubcommand cmd = of(argIter.nextArg("Expected tracing configuration action."));
    if (cmd == null)
        throw new IllegalArgumentException("Expected correct tracing configuration action.");
    TracingConfigurationArguments.Builder tracingConfigurationArgs = new TracingConfigurationArguments.Builder(cmd);
    Scope scope = null;
    String lb = null;
    double samplingRate = SAMPLING_RATE_NEVER;
    Set<Scope> includedScopes = new HashSet<>();
    while (argIter.hasNextSubArg()) {
        TracingConfigurationCommandArg arg = CommandArgUtils.of(argIter.nextArg(""), TracingConfigurationCommandArg.class);
        String strVal;
        assert arg != null;
        switch(arg) {
            case SCOPE:
                {
                    String peekedNextArg = argIter.peekNextArg();
                    if (!TracingConfigurationCommandArg.args().contains(peekedNextArg)) {
                        strVal = argIter.nextArg("The scope should be specified. The following " + "values can be used: " + Arrays.toString(Scope.values()) + '.');
                        try {
                            scope = Scope.valueOf(strVal);
                        } catch (IllegalArgumentException e) {
                            throw new IllegalArgumentException("Invalid scope '" + strVal + "'. The following " + "values can be used: " + Arrays.toString(Scope.values()) + '.');
                        }
                    }
                    break;
                }
            case LABEL:
                {
                    lb = argIter.nextArg("The label should be specified.");
                    break;
                }
            case SAMPLING_RATE:
                {
                    strVal = argIter.nextArg("The sampling rate should be specified. Decimal value between 0 and 1 should be used.");
                    try {
                        samplingRate = Double.parseDouble(strVal);
                    } catch (IllegalArgumentException e) {
                        throw new IllegalArgumentException("Invalid sampling-rate '" + strVal + "'. Decimal value between 0 and 1 should be used.");
                    }
                    if (samplingRate < SAMPLING_RATE_NEVER || samplingRate > SAMPLING_RATE_ALWAYS)
                        throw new IllegalArgumentException("Invalid sampling-rate '" + strVal + "'. Decimal value between 0 and 1 should be used.");
                    break;
                }
            case INCLUDED_SCOPES:
                {
                    Set<String> setStrVals = argIter.nextStringSet("At least one supported scope should be specified.");
                    for (String scopeStrVal : setStrVals) {
                        try {
                            includedScopes.add(Scope.valueOf(scopeStrVal));
                        } catch (IllegalArgumentException e) {
                            throw new IllegalArgumentException("Invalid supported scope '" + scopeStrVal + "'. The following " + "values can be used: " + Arrays.toString(Scope.values()) + '.');
                        }
                    }
                    break;
                }
        }
    }
    // Scope is a mandatory attribute for all sub-commands except get_all and reset_all.
    if ((cmd != GET_ALL && cmd != RESET_ALL) && scope == null) {
        throw new IllegalArgumentException("Scope attribute is missing. Following values can be used: " + Arrays.toString(Scope.values()) + '.');
    }
    switch(cmd) {
        case GET_ALL:
        case RESET_ALL:
            {
                tracingConfigurationArgs.withScope(scope);
                break;
            }
        case RESET:
        case GET:
            {
                tracingConfigurationArgs.withScope(scope).withLabel(lb);
                break;
            }
        case SET:
            {
                tracingConfigurationArgs.withScope(scope).withLabel(lb).withSamplingRate(samplingRate).withIncludedScopes(includedScopes);
                break;
            }
        default:
            {
                // We should never get here.
                assert false : "Unexpected tracing configuration argument [arg= " + cmd + ']';
            }
    }
    args = tracingConfigurationArgs.build();
}
Also used : TracingConfigurationCommandArg(org.apache.ignite.internal.commandline.tracing.configuration.TracingConfigurationCommandArg) HashSet(java.util.HashSet) Set(java.util.Set) Scope(org.apache.ignite.spi.tracing.Scope) TracingConfigurationArguments(org.apache.ignite.internal.commandline.tracing.configuration.TracingConfigurationArguments) TracingConfigurationSubcommand(org.apache.ignite.internal.commandline.tracing.configuration.TracingConfigurationSubcommand) HashSet(java.util.HashSet)

Example 8 with Scope

use of org.apache.ignite.spi.tracing.Scope 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);
}
Also used : DeferredSpan(org.apache.ignite.internal.processors.tracing.DeferredSpan) SpanType(org.apache.ignite.internal.processors.tracing.SpanType) Scope(org.apache.ignite.spi.tracing.Scope) SpanImpl(org.apache.ignite.internal.processors.tracing.SpanImpl) TracingConfigurationCoordinates(org.apache.ignite.spi.tracing.TracingConfigurationCoordinates) Span(org.apache.ignite.internal.processors.tracing.Span) NoopSpan(org.apache.ignite.internal.processors.tracing.NoopSpan) SpiSpecificSpan(org.apache.ignite.spi.tracing.SpiSpecificSpan) DeferredSpan(org.apache.ignite.internal.processors.tracing.DeferredSpan) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) HashSet(java.util.HashSet)

Example 9 with Scope

use of org.apache.ignite.spi.tracing.Scope 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;
        }
    }
}
Also used : DeferredSpan(org.apache.ignite.internal.processors.tracing.DeferredSpan) TracingConfigurationParameters(org.apache.ignite.spi.tracing.TracingConfigurationParameters) Scope(org.apache.ignite.spi.tracing.Scope) SpanImpl(org.apache.ignite.internal.processors.tracing.SpanImpl) TracingConfigurationCoordinates(org.apache.ignite.spi.tracing.TracingConfigurationCoordinates) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with Scope

use of org.apache.ignite.spi.tracing.Scope in project ignite by apache.

the class OpenCensusTxTracingConfigurationTest method testTxConfigurationSamplingRateHalfSamplesSomethingAboutHalfTransactions.

/**
 * Ensure that specifying 0 < sapling rate < 1 within TX scope will trace some but not all transactions.
 * Cause of probability nature of sampling, it's not possible to check that 0.5 sampling rate
 * will result in exactly half of the transactions being traced.
 *
 * @throws Exception If Failed.
 */
@Test
public void testTxConfigurationSamplingRateHalfSamplesSomethingAboutHalfTransactions() throws Exception {
    IgniteEx client = startGrid("client");
    client.tracingConfiguration().set(new TracingConfigurationCoordinates.Builder(TX).build(), new TracingConfigurationParameters.Builder().withSamplingRate(0.5).build());
    final int txAmount = 100;
    for (int i = 0; i < txAmount; i++) client.transactions().txStart(PESSIMISTIC, SERIALIZABLE).commit();
    handler().flush();
    java.util.List<SpanData> gotSpans = handler().allSpans().filter(span -> SpanType.TX.spanName().equals(span.getName())).collect(Collectors.toList());
    // Cause of probability nature of sampling, it's not possible to check that 0.5 sampling rate will end with
    // 5 sampling transactions out of {@code txAmount},
    // so we just check that some and not all transactions were traced.
    assertTrue(!gotSpans.isEmpty() && gotSpans.size() < txAmount);
}
Also used : Arrays(java.util.Arrays) SERIALIZABLE(org.apache.ignite.transactions.TransactionIsolation.SERIALIZABLE) SpanType(org.apache.ignite.internal.processors.tracing.SpanType) Transaction(org.apache.ignite.transactions.Transaction) Set(java.util.Set) IgniteEx(org.apache.ignite.internal.IgniteEx) Scope(org.apache.ignite.spi.tracing.Scope) Test(org.junit.Test) Collectors(java.util.stream.Collectors) SpanData(io.opencensus.trace.export.SpanData) OpenCensusTracingSpi(org.apache.ignite.spi.tracing.opencensus.OpenCensusTracingSpi) TX(org.apache.ignite.spi.tracing.Scope.TX) SAMPLING_RATE_ALWAYS(org.apache.ignite.spi.tracing.TracingConfigurationParameters.SAMPLING_RATE_ALWAYS) TracingConfigurationParameters(org.apache.ignite.spi.tracing.TracingConfigurationParameters) TracingSpi(org.apache.ignite.spi.tracing.TracingSpi) SpanId(io.opencensus.trace.SpanId) SAMPLING_RATE_NEVER(org.apache.ignite.spi.tracing.TracingConfigurationParameters.SAMPLING_RATE_NEVER) PESSIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC) Collections(java.util.Collections) TracingConfigurationCoordinates(org.apache.ignite.spi.tracing.TracingConfigurationCoordinates) TracingConfigurationParameters(org.apache.ignite.spi.tracing.TracingConfigurationParameters) SpanData(io.opencensus.trace.export.SpanData) IgniteEx(org.apache.ignite.internal.IgniteEx) Test(org.junit.Test)

Aggregations

Scope (org.apache.ignite.spi.tracing.Scope)10 TracingConfigurationCoordinates (org.apache.ignite.spi.tracing.TracingConfigurationCoordinates)8 TracingConfigurationParameters (org.apache.ignite.spi.tracing.TracingConfigurationParameters)7 Collections (java.util.Collections)6 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6 SpanType (org.apache.ignite.internal.processors.tracing.SpanType)6 SpanId (io.opencensus.trace.SpanId)5 SpanData (io.opencensus.trace.export.SpanData)5 Arrays (java.util.Arrays)5 IgniteEx (org.apache.ignite.internal.IgniteEx)5 TX (org.apache.ignite.spi.tracing.Scope.TX)5 SAMPLING_RATE_ALWAYS (org.apache.ignite.spi.tracing.TracingConfigurationParameters.SAMPLING_RATE_ALWAYS)5 SAMPLING_RATE_NEVER (org.apache.ignite.spi.tracing.TracingConfigurationParameters.SAMPLING_RATE_NEVER)5 TracingSpi (org.apache.ignite.spi.tracing.TracingSpi)5 OpenCensusTracingSpi (org.apache.ignite.spi.tracing.opencensus.OpenCensusTracingSpi)5 Transaction (org.apache.ignite.transactions.Transaction)5 PESSIMISTIC (org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC)5 SERIALIZABLE (org.apache.ignite.transactions.TransactionIsolation.SERIALIZABLE)5 Test (org.junit.Test)5