Search in sources :

Example 1 with SpanLimits

use of io.opentelemetry.sdk.trace.SpanLimits in project opentelemetry-java by open-telemetry.

the class TraceConfigzZPageHandler method applyTraceConfig.

/**
 * Apply updated trace configuration through the tracerProvider based on query parameters.
 *
 * @param queryMap the map containing URL query parameters.
 * @throws NumberFormatException if one of the {@code double}/{@code integer} valued query string
 *     does not contain a parsable {@code double}/{@code integer}.
 */
private void applyTraceConfig(Map<String, String> queryMap) {
    String action = queryMap.get(QUERY_STRING_ACTION);
    if (action == null) {
        return;
    }
    if (action.equals(QUERY_STRING_ACTION_CHANGE)) {
        SpanLimitsBuilder newConfigBuilder = configSupplier.get().toBuilder();
        String samplingProbabilityStr = queryMap.get(QUERY_STRING_SAMPLING_PROBABILITY);
        if (samplingProbabilityStr != null) {
            try {
                double samplingProbability = Double.parseDouble(samplingProbabilityStr);
                configSupplier.setSampler(Sampler.traceIdRatioBased(samplingProbability));
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("SamplingProbability must be of the type double", e);
            }
        }
        String maxNumOfAttributesStr = queryMap.get(QUERY_STRING_MAX_NUM_OF_ATTRIBUTES);
        if (maxNumOfAttributesStr != null) {
            try {
                int maxNumOfAttributes = Integer.parseInt(maxNumOfAttributesStr);
                newConfigBuilder.setMaxNumberOfAttributes(maxNumOfAttributes);
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("MaxNumOfAttributes must be of the type integer", e);
            }
        }
        String maxNumOfEventsStr = queryMap.get(QUERY_STRING_MAX_NUM_OF_EVENTS);
        if (maxNumOfEventsStr != null) {
            try {
                int maxNumOfEvents = Integer.parseInt(maxNumOfEventsStr);
                newConfigBuilder.setMaxNumberOfEvents(maxNumOfEvents);
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("MaxNumOfEvents must be of the type integer", e);
            }
        }
        String maxNumOfLinksStr = queryMap.get(QUERY_STRING_MAX_NUM_OF_LINKS);
        if (maxNumOfLinksStr != null) {
            try {
                int maxNumOfLinks = Integer.parseInt(maxNumOfLinksStr);
                newConfigBuilder.setMaxNumberOfLinks(maxNumOfLinks);
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("MaxNumOfLinks must be of the type integer", e);
            }
        }
        String maxNumOfAttributesPerEventStr = queryMap.get(QUERY_STRING_MAX_NUM_OF_ATTRIBUTES_PER_EVENT);
        if (maxNumOfAttributesPerEventStr != null) {
            try {
                int maxNumOfAttributesPerEvent = Integer.parseInt(maxNumOfAttributesPerEventStr);
                newConfigBuilder.setMaxNumberOfAttributesPerEvent(maxNumOfAttributesPerEvent);
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("MaxNumOfAttributesPerEvent must be of the type integer", e);
            }
        }
        String maxNumOfAttributesPerLinkStr = queryMap.get(QUERY_STRING_MAX_NUM_OF_ATTRIBUTES_PER_LINK);
        if (maxNumOfAttributesPerLinkStr != null) {
            try {
                int maxNumOfAttributesPerLink = Integer.parseInt(maxNumOfAttributesPerLinkStr);
                newConfigBuilder.setMaxNumberOfAttributesPerLink(maxNumOfAttributesPerLink);
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("MaxNumOfAttributesPerLink must be of the type integer", e);
            }
        }
        configSupplier.setActiveTraceConfig(newConfigBuilder.build());
    } else if (action.equals(QUERY_STRING_ACTION_DEFAULT)) {
        SpanLimits defaultConfig = SpanLimits.builder().build();
        configSupplier.setActiveTraceConfig(defaultConfig);
    }
}
Also used : SpanLimitsBuilder(io.opentelemetry.sdk.trace.SpanLimitsBuilder) SpanLimits(io.opentelemetry.sdk.trace.SpanLimits)

Example 2 with SpanLimits

use of io.opentelemetry.sdk.trace.SpanLimits in project opentelemetry-java by open-telemetry.

the class TracerProviderConfigurationTest method configureTraceConfig_full.

@Test
void configureTraceConfig_full() {
    Map<String, String> properties = new HashMap<>();
    properties.put("otel.traces.sampler", "always_off");
    properties.put("otel.span.attribute.value.length.limit", "100");
    properties.put("otel.span.attribute.count.limit", "5");
    properties.put("otel.span.event.count.limit", "4");
    properties.put("otel.span.link.count.limit", "3");
    SpanLimits config = TracerProviderConfiguration.configureSpanLimits(DefaultConfigProperties.createForTest(properties));
    assertThat(config.getMaxAttributeValueLength()).isEqualTo(100);
    assertThat(config.getMaxNumberOfAttributes()).isEqualTo(5);
    assertThat(config.getMaxNumberOfEvents()).isEqualTo(4);
    assertThat(config.getMaxNumberOfLinks()).isEqualTo(3);
}
Also used : HashMap(java.util.HashMap) SpanLimits(io.opentelemetry.sdk.trace.SpanLimits) Test(org.junit.jupiter.api.Test)

Example 3 with SpanLimits

use of io.opentelemetry.sdk.trace.SpanLimits in project opentelemetry-java by open-telemetry.

the class SpanLimitsTest method updateSpanLimits_All.

@Test
void updateSpanLimits_All() {
    SpanLimits spanLimits = SpanLimits.builder().setMaxNumberOfAttributes(8).setMaxNumberOfEvents(10).setMaxNumberOfLinks(11).setMaxNumberOfAttributesPerEvent(1).setMaxNumberOfAttributesPerLink(2).build();
    assertThat(spanLimits.getMaxNumberOfAttributes()).isEqualTo(8);
    assertThat(spanLimits.getMaxNumberOfEvents()).isEqualTo(10);
    assertThat(spanLimits.getMaxNumberOfLinks()).isEqualTo(11);
    assertThat(spanLimits.getMaxNumberOfAttributesPerEvent()).isEqualTo(1);
    assertThat(spanLimits.getMaxNumberOfAttributesPerLink()).isEqualTo(2);
    // Preserves values
    SpanLimits spanLimitsDupe = spanLimits.toBuilder().build();
    // Use reflective comparison to catch when new fields are added.
    assertThat(spanLimitsDupe).usingRecursiveComparison().isEqualTo(spanLimits);
}
Also used : SpanLimits(io.opentelemetry.sdk.trace.SpanLimits) Test(org.junit.jupiter.api.Test)

Example 4 with SpanLimits

use of io.opentelemetry.sdk.trace.SpanLimits in project opentelemetry-java by open-telemetry.

the class OpenTelemetrySdkTest method fullOpenTelemetrySdkConfigurationDemo.

// This is just a demonstration of maximum that one can do with OpenTelemetry configuration.
// Demonstrates how clear or confusing is SDK configuration
@Test
void fullOpenTelemetrySdkConfigurationDemo() {
    SpanLimits newConfig = SpanLimits.builder().setMaxNumberOfAttributes(512).build();
    OpenTelemetrySdkBuilder sdkBuilder = OpenTelemetrySdk.builder().setTracerProvider(SdkTracerProvider.builder().setSampler(mock(Sampler.class)).addSpanProcessor(SimpleSpanProcessor.create(mock(SpanExporter.class))).addSpanProcessor(SimpleSpanProcessor.create(mock(SpanExporter.class))).setClock(mock(Clock.class)).setIdGenerator(mock(IdGenerator.class)).setResource(Resource.empty()).setSpanLimits(newConfig).build());
    sdkBuilder.build();
}
Also used : SpanExporter(io.opentelemetry.sdk.trace.export.SpanExporter) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) SpanLimits(io.opentelemetry.sdk.trace.SpanLimits) IdGenerator(io.opentelemetry.sdk.trace.IdGenerator) Test(org.junit.jupiter.api.Test)

Example 5 with SpanLimits

use of io.opentelemetry.sdk.trace.SpanLimits in project opentelemetry-java by open-telemetry.

the class OpenTelemetrySdkTest method testConfiguration_tracerSettings.

@Test
void testConfiguration_tracerSettings() {
    Resource resource = Resource.create(Attributes.builder().put("cat", "meow").build());
    IdGenerator idGenerator = mock(IdGenerator.class);
    SpanLimits spanLimits = SpanLimits.getDefault();
    OpenTelemetrySdk openTelemetry = OpenTelemetrySdk.builder().setTracerProvider(SdkTracerProvider.builder().setClock(clock).setResource(resource).setIdGenerator(idGenerator).setSpanLimits(spanLimits).build()).build();
    TracerProvider unobfuscatedTracerProvider = ((OpenTelemetrySdk.ObfuscatedTracerProvider) openTelemetry.getTracerProvider()).unobfuscate();
    assertThat(unobfuscatedTracerProvider).isInstanceOfSatisfying(SdkTracerProvider.class, sdkTracerProvider -> assertThat(sdkTracerProvider.getSpanLimits()).isEqualTo(spanLimits));
    // Since TracerProvider is in a different package, the only alternative to this reflective
    // approach would be to make the fields public for testing which is worse than this.
    assertThat(unobfuscatedTracerProvider).extracting("sharedState").hasFieldOrPropertyWithValue("clock", clock).hasFieldOrPropertyWithValue("resource", resource).hasFieldOrPropertyWithValue("idGenerator", idGenerator);
}
Also used : Resource(io.opentelemetry.sdk.resources.Resource) SpanLimits(io.opentelemetry.sdk.trace.SpanLimits) IdGenerator(io.opentelemetry.sdk.trace.IdGenerator) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) TracerProvider(io.opentelemetry.api.trace.TracerProvider) Test(org.junit.jupiter.api.Test)

Aggregations

SpanLimits (io.opentelemetry.sdk.trace.SpanLimits)5 Test (org.junit.jupiter.api.Test)4 IdGenerator (io.opentelemetry.sdk.trace.IdGenerator)2 TracerProvider (io.opentelemetry.api.trace.TracerProvider)1 Resource (io.opentelemetry.sdk.resources.Resource)1 SdkTracerProvider (io.opentelemetry.sdk.trace.SdkTracerProvider)1 SpanLimitsBuilder (io.opentelemetry.sdk.trace.SpanLimitsBuilder)1 SpanExporter (io.opentelemetry.sdk.trace.export.SpanExporter)1 Sampler (io.opentelemetry.sdk.trace.samplers.Sampler)1 HashMap (java.util.HashMap)1