Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package com.aliyun.openservices.shade.com.aliyun.openservices.shade.io.opentelemetry.api.trace;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import com.aliyun.openservices.shade.com.aliyun.openservices.shade.io.opentelemetry.api.common.AttributeKey;
import com.aliyun.openservices.shade.com.aliyun.openservices.shade.io.opentelemetry.api.common.Attributes;
import com.aliyun.openservices.shade.com.aliyun.openservices.shade.io.opentelemetry.api.internal.ValidationUtil;
import com.aliyun.openservices.shade.com.aliyun.openservices.shade.io.opentelemetry.context.Context;
import com.aliyun.openservices.shade.com.aliyun.openservices.shade.io.opentelemetry.context.ImplicitContextKeyed;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;
/**
* An interface that represents a span. It has an associated {@link SpanContext}.
*
*
Spans are created by the {@link SpanBuilder#startSpan} method.
*
*
{@code Span} must be ended by calling {@link #end()}.
*/
@ThreadSafe
public interface Span extends ImplicitContextKeyed {
/**
* Returns the {@link Span} from the current {@link Context}, falling back to a default, no-op
* {@link Span} if there is no span in the current context.
*/
static Span current() {
Span span = Context.current().get(SpanContextKey.KEY);
return span == null ? getInvalid() : span;
}
/**
* Returns the {@link Span} from the specified {@link Context}, falling back to a default, no-op
* {@link Span} if there is no span in the context.
*/
static Span fromContext(Context context) {
if (context == null) {
ValidationUtil.log("context is null");
return Span.getInvalid();
}
Span span = context.get(SpanContextKey.KEY);
return span == null ? getInvalid() : span;
}
/**
* Returns the {@link Span} from the specified {@link Context}, or {@code null} if there is no
* span in the context.
*/
@Nullable
static Span fromContextOrNull(Context context) {
if (context == null) {
ValidationUtil.log("context is null");
return null;
}
return context.get(SpanContextKey.KEY);
}
/**
* Returns an invalid {@link Span}. An invalid {@link Span} is used when tracing is disabled,
* usually because there is no OpenTelemetry SDK installed.
*/
static Span getInvalid() {
return PropagatedSpan.INVALID;
}
/**
* Returns a non-recording {@link Span} that holds the provided {@link SpanContext} but has no
* functionality. It will not be exported and all tracing operations are no-op, but it can be used
* to propagate a valid {@link SpanContext} downstream.
*/
static Span wrap(SpanContext spanContext) {
if (spanContext == null) {
ValidationUtil.log("context is null");
return getInvalid();
}
if (!spanContext.isValid()) {
return getInvalid();
}
return PropagatedSpan.create(spanContext);
}
/**
* Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for
* the key, the old value is replaced by the specified value.
*
*
If a null or empty String {@code value} is passed in, the behavior is undefined, and hence
* strongly discouraged.
*
*
Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and
* pre-allocate your keys, if possible.
*
* @param key the key for this attribute.
* @param value the value for this attribute.
* @return this.
*/
default Span setAttribute(String key, String value) {
return setAttribute(AttributeKey.stringKey(key), value);
}
/**
* Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for
* the key, the old value is replaced by the specified value.
*
*
Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and
* pre-allocate your keys, if possible.
*
* @param key the key for this attribute.
* @param value the value for this attribute.
* @return this.
*/
default Span setAttribute(String key, long value) {
return setAttribute(AttributeKey.longKey(key), value);
}
/**
* Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for
* the key, the old value is replaced by the specified value.
*
*
Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and
* pre-allocate your keys, if possible.
*
* @param key the key for this attribute.
* @param value the value for this attribute.
* @return this.
*/
default Span setAttribute(String key, double value) {
return setAttribute(AttributeKey.doubleKey(key), value);
}
/**
* Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for
* the key, the old value is replaced by the specified value.
*
*
Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and
* pre-allocate your keys, if possible.
*
* @param key the key for this attribute.
* @param value the value for this attribute.
* @return this.
*/
default Span setAttribute(String key, boolean value) {
return setAttribute(AttributeKey.booleanKey(key), value);
}
/**
* Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for
* the key, the old value is replaced by the specified value.
*
*
Note: the behavior of null values is undefined, and hence strongly discouraged.
*
* @param key the key for this attribute.
* @param value the value for this attribute.
* @return this.
*/
Span setAttribute(AttributeKey key, T value);
/**
* Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for
* the key, the old value is replaced by the specified value.
*
* @param key the key for this attribute.
* @param value the value for this attribute.
* @return this.
*/
default Span setAttribute(AttributeKey key, int value) {
return setAttribute(key, (long) value);
}
/**
* Sets attributes to the {@link Span}. If the {@link Span} previously contained a mapping for any
* of the keys, the old values are replaced by the specified values.
*
* @param attributes the attributes
* @return this.
* @since 1.2.0
*/
@SuppressWarnings("unchecked")
default Span setAllAttributes(Attributes attributes) {
if (attributes == null || attributes.isEmpty()) {
return this;
}
attributes.forEach(
(attributeKey, value) -> this.setAttribute((AttributeKey