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);
}
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);
}
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();
}
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));
}
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);
}
Aggregations