Search in sources :

Example 96 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class SdkSpanBuilder method startSpan.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Span startSpan() {
    Context parentContext = parent == null ? Context.current() : parent;
    Span parentSpan = Span.fromContext(parentContext);
    SpanContext parentSpanContext = parentSpan.getSpanContext();
    String traceId;
    IdGenerator idGenerator = tracerSharedState.getIdGenerator();
    String spanId = idGenerator.generateSpanId();
    if (!parentSpanContext.isValid()) {
        // New root span.
        traceId = idGenerator.generateTraceId();
    } else {
        // New child span.
        traceId = parentSpanContext.getTraceId();
    }
    List<LinkData> immutableLinks = links == null ? Collections.emptyList() : Collections.unmodifiableList(links);
    // Avoid any possibility to modify the links list by adding links to the Builder after the
    // startSpan is called. If that happens all the links will be added in a new list.
    links = null;
    Attributes immutableAttributes = attributes == null ? Attributes.empty() : attributes;
    SamplingResult samplingResult = tracerSharedState.getSampler().shouldSample(parentContext, traceId, spanName, spanKind, immutableAttributes, immutableLinks);
    SamplingDecision samplingDecision = samplingResult.getDecision();
    TraceState samplingResultTraceState = samplingResult.getUpdatedTraceState(parentSpanContext.getTraceState());
    SpanContext spanContext = ImmutableSpanContext.create(traceId, spanId, isSampled(samplingDecision) ? TraceFlags.getSampled() : TraceFlags.getDefault(), samplingResultTraceState, /* remote= */
    false, tracerSharedState.isIdGeneratorSafeToSkipIdValidation());
    if (!isRecording(samplingDecision)) {
        return Span.wrap(spanContext);
    }
    Attributes samplingAttributes = samplingResult.getAttributes();
    if (!samplingAttributes.isEmpty()) {
        samplingAttributes.forEach((key, value) -> attributes().put((AttributeKey) key, value));
    }
    // Avoid any possibility to modify the attributes by adding attributes to the Builder after the
    // startSpan is called. If that happens all the attributes will be added in a new map.
    AttributesMap recordedAttributes = attributes;
    attributes = null;
    return SdkSpan.startSpan(spanContext, spanName, instrumentationLibraryInfo, spanKind, parentSpan, parentContext, spanLimits, tracerSharedState.getActiveSpanProcessor(), tracerSharedState.getClock(), tracerSharedState.getResource(), recordedAttributes, immutableLinks, totalNumberOfLinksAdded, startEpochNanos);
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) ImmutableSpanContext(io.opentelemetry.api.internal.ImmutableSpanContext) TraceState(io.opentelemetry.api.trace.TraceState) SpanContext(io.opentelemetry.api.trace.SpanContext) ImmutableSpanContext(io.opentelemetry.api.internal.ImmutableSpanContext) LinkData(io.opentelemetry.sdk.trace.data.LinkData) SamplingResult(io.opentelemetry.sdk.trace.samplers.SamplingResult) Attributes(io.opentelemetry.api.common.Attributes) Span(io.opentelemetry.api.trace.Span) AttributeKey(io.opentelemetry.api.common.AttributeKey) SamplingDecision(io.opentelemetry.sdk.trace.samplers.SamplingDecision)

Example 97 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class SamplingResultTest method hasAttributes.

@Test
void hasAttributes() {
    Attributes attrs = Attributes.of(longKey("foo"), 42L, stringKey("bar"), "baz");
    SamplingResult sampledSamplingResult = SamplingResult.create(SamplingDecision.RECORD_AND_SAMPLE, attrs);
    assertThat(sampledSamplingResult.getDecision()).isEqualTo(SamplingDecision.RECORD_AND_SAMPLE);
    assertThat(sampledSamplingResult.getAttributes()).isEqualTo(attrs);
    SamplingResult notSampledSamplingResult = SamplingResult.create(SamplingDecision.DROP, attrs);
    assertThat(notSampledSamplingResult.getDecision()).isEqualTo(SamplingDecision.DROP);
    assertThat(notSampledSamplingResult.getAttributes()).isEqualTo(attrs);
}
Also used : Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Example 98 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class HostResourceTest method shouldCreateRuntimeAttributes.

@Test
void shouldCreateRuntimeAttributes() {
    // when
    Resource resource = HostResource.buildResource();
    Attributes attributes = resource.getAttributes();
    // then
    assertThat(resource.getSchemaUrl()).isEqualTo(ResourceAttributes.SCHEMA_URL);
    assertThat(attributes.get(ResourceAttributes.HOST_NAME)).isNotBlank();
    assertThat(attributes.get(ResourceAttributes.HOST_ARCH)).isNotBlank();
}
Also used : Resource(io.opentelemetry.sdk.resources.Resource) Attributes(io.opentelemetry.api.common.Attributes) ResourceAttributes(io.opentelemetry.semconv.resource.attributes.ResourceAttributes) Test(org.junit.jupiter.api.Test)

Example 99 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class ProcessResourceTest method notWindows.

@Test
@SetSystemProperty(key = "os.name", value = "Linux 4.12")
void notWindows() {
    Resource resource = ProcessResource.buildResource();
    assertThat(resource.getSchemaUrl()).isEqualTo(ResourceAttributes.SCHEMA_URL);
    Attributes attributes = resource.getAttributes();
    assertThat(attributes.get(ResourceAttributes.PROCESS_PID)).isGreaterThan(1);
    assertThat(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH)).contains("java").doesNotEndWith(".exe");
    assertThat(attributes.get(ResourceAttributes.PROCESS_COMMAND_LINE)).contains(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH));
}
Also used : Resource(io.opentelemetry.sdk.resources.Resource) Attributes(io.opentelemetry.api.common.Attributes) ResourceAttributes(io.opentelemetry.semconv.resource.attributes.ResourceAttributes) SetSystemProperty(org.junitpioneer.jupiter.SetSystemProperty) Test(org.junit.jupiter.api.Test)

Example 100 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class AdapterTest method getTimedEvent.

private static EventData getTimedEvent(int totalAttributeCount) {
    long epochNanos = MILLISECONDS.toNanos(System.currentTimeMillis());
    Attributes attributes = Attributes.of(stringKey("foo"), "bar");
    if (totalAttributeCount <= 0) {
        totalAttributeCount = attributes.size();
    }
    return EventData.create(epochNanos, "the log message", attributes, totalAttributeCount);
}
Also used : Attributes(io.opentelemetry.api.common.Attributes)

Aggregations

Attributes (io.opentelemetry.api.common.Attributes)175 Test (org.junit.jupiter.api.Test)128 ResourceAttributes (io.opentelemetry.semconv.resource.attributes.ResourceAttributes)45 Resource (io.opentelemetry.sdk.resources.Resource)33 InstrumentationLibraryInfo (io.opentelemetry.sdk.common.InstrumentationLibraryInfo)27 Context (io.opentelemetry.context.Context)25 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)24 SpanContext (io.opentelemetry.api.trace.SpanContext)22 MetricAssertions.assertThat (io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat)22 SpanData (io.opentelemetry.sdk.trace.data.SpanData)22 HashMap (java.util.HashMap)21 Duration (java.time.Duration)20 ExemplarData (io.opentelemetry.sdk.metrics.data.ExemplarData)18 InMemoryMetricReader (io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader)18 TestClock (io.opentelemetry.sdk.testing.time.TestClock)18 AttributeKey (io.opentelemetry.api.common.AttributeKey)17 Span (io.opentelemetry.api.trace.Span)17 DoubleExemplarData (io.opentelemetry.sdk.metrics.data.DoubleExemplarData)17 AttributeKey.stringKey (io.opentelemetry.api.common.AttributeKey.stringKey)16 Map (java.util.Map)16