span = new AtomicReference<>();
public SpanHolder(SpanHolder spanHolder) {
- this.span.getAndSet(spanHolder.span.get());
+ if (spanHolder != null) {
+ this.span.set(spanHolder.span.get());
+ }
}
public SpanHolder(Span span) {
- this.span.getAndSet(span);
+ this.span.set(span);
}
public Span getSpan() {
@@ -27,6 +29,6 @@ public Span getSpan() {
}
public void setSpan(Span span) {
- this.span.getAndSet(span);
+ this.span.set(span);
}
}
diff --git a/server/src/main/java/org/opensearch/tracer/Tracer.java b/server/src/main/java/org/opensearch/tracing/Tracer.java
similarity index 68%
rename from server/src/main/java/org/opensearch/tracer/Tracer.java
rename to server/src/main/java/org/opensearch/tracing/Tracer.java
index 9ebf620a89895..64495ef4b2418 100644
--- a/server/src/main/java/org/opensearch/tracer/Tracer.java
+++ b/server/src/main/java/org/opensearch/tracing/Tracer.java
@@ -6,8 +6,11 @@
* compatible open source license.
*/
-package org.opensearch.tracer;
+package org.opensearch.tracing;
+import org.opensearch.common.settings.Setting;
+
+import java.util.Arrays;
import java.util.Map;
public interface Tracer {
@@ -17,8 +20,8 @@ public interface Tracer {
*
* Returns a span reference which can be used for ending it.
*/
- default Span startTrace(String spanName, Map attributes, Level level) {
- return startTrace(spanName, attributes, null, level);
+ default void startSpan(String spanName, Map attributes, Level level) {
+ startSpan(spanName, attributes, null, level);
}
/**
@@ -36,44 +39,20 @@ default Span startTrace(String spanName, Map attributes, Level l
* the configured level will be published. Level of a child span can't be higher than the parent span so that it shouldn't get into a situation where parent
* span is filtered out based on the level and child still exists; it will lead to a parent child linking issue and the child will be orphaned.
*/
- Span startTrace(String spanName, Map attributes, Span parentSpan, Level level);
+ void startSpan(String spanName, Map attributes, Span parentSpan, Level level);
/**
* Ends the scope of the trace. It is mandatory to end each span explicitly.
*/
- void endTrace();
+ void endSpan();
/**
* Adds string attribute to the span.
*
* @param key attribute key
- * @param value attribute String value
- */
- void addAttribute(String key, String value);
-
- /**
- * Adds boolean attribute to the span.
- *
- * @param key attribute key
- * @param value attribute boolean value
+ * @param value attribute value
*/
- void addAttribute(String key, boolean value);
-
- /**
- * Adds long attribute to the span.
- *
- * @param key attribute key
- * @param value attribute long value
- */
- void addAttribute(String key, long value);
-
- /**
- * Adds double attribute to the span.
- *
- * @param key attribute key
- * @param value attribute double value
- */
- void addAttribute(String key, double value);
+ void addAttribute(String key, Object value);
/**
* Adds an event to the current span.
@@ -86,20 +65,30 @@ default Span startTrace(String spanName, Map attributes, Level l
* Level is an OpenSearch specific concept which
*/
enum Level {
- UNKNOWN(0), LOWEST(1), LOW(2), MID(3), HIGH(4);
+ TRACE(0), DEBUG(1), INFO(2), TERSE(3), ROOT(4), DISABLED(5);
private final int order;
Level(int order) {
this.order = order;
}
- public boolean isHigherThan(Level level) {
- if (level != null) {
- return this.order >= level.order;
- } else {
- return false;
+ public int getOrder() {
+ return order;
+ }
+
+ public static Level fromString(String type) {
+ for (Level level : values()) {
+ if (level.name().equalsIgnoreCase(type)) {
+ return level;
+ }
}
+ throw new IllegalArgumentException("invalid value for tracing level [" + type + "], " +
+ "must be in " + Arrays.asList(Level.values()));
}
}
+ Setting TRACER_LEVEL_SETTING = new Setting<>(
+ "tracer.level", Level.DISABLED.name(),
+ Level::fromString, Setting.Property.NodeScope, Setting.Property.Dynamic);
+
}
diff --git a/server/src/main/java/org/opensearch/tracing/TracerFactory.java b/server/src/main/java/org/opensearch/tracing/TracerFactory.java
new file mode 100644
index 0000000000000..7bf8082cdf585
--- /dev/null
+++ b/server/src/main/java/org/opensearch/tracing/TracerFactory.java
@@ -0,0 +1,49 @@
+/*
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * The OpenSearch Contributors require contributions made to
+ * this file be licensed under the Apache-2.0 license or a
+ * compatible open source license.
+ */
+
+package org.opensearch.tracing;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.opensearch.cluster.service.ClusterService;
+import org.opensearch.threadpool.ThreadPool;
+
+import static org.opensearch.tracing.Tracer.TRACER_LEVEL_SETTING;
+
+public class TracerFactory {
+
+ private static final Logger logger = LogManager.getLogger(TracerFactory.class);
+
+ private static final Tracer noopTracer = new NoopTracer();
+
+ private static ThreadPool threadPool;
+
+ private static ClusterService clusterSvc;
+
+ public static synchronized void initializeTracer(ThreadPool thPool, ClusterService clusterService) {
+ threadPool = thPool;
+ clusterSvc = clusterService;
+ }
+
+ public static Tracer getInstance() {
+ return isTracingDisabled() ? noopTracer : DefaultTracerInstanceHolder.INSTANCE;
+ }
+
+ private static boolean isTracingDisabled() {
+ return clusterSvc != null &&
+ clusterSvc.getClusterSettings() != null &&
+ clusterSvc.getClusterSettings().get(TRACER_LEVEL_SETTING) == Tracer.Level.DISABLED;
+ }
+
+ private static class DefaultTracerInstanceHolder {
+ private static final io.opentelemetry.api.trace.Tracer openTelemetryTracer =
+ OTelMain.OTelHolder.OPEN_TELEMETRY.getTracer("os-tracer");
+ static final Tracer INSTANCE = new DefaultTracer(openTelemetryTracer, threadPool, clusterSvc);
+ }
+
+}
diff --git a/server/src/main/java/org/opensearch/tracing/TracerUtils.java b/server/src/main/java/org/opensearch/tracing/TracerUtils.java
new file mode 100644
index 0000000000000..ad276d4e45ee7
--- /dev/null
+++ b/server/src/main/java/org/opensearch/tracing/TracerUtils.java
@@ -0,0 +1,75 @@
+/*
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * The OpenSearch Contributors require contributions made to
+ * this file be licensed under the Apache-2.0 license or a
+ * compatible open source license.
+ */
+
+package org.opensearch.tracing;
+
+import io.opentelemetry.context.Context;
+import io.opentelemetry.context.propagation.TextMapGetter;
+import io.opentelemetry.context.propagation.TextMapSetter;
+
+import java.util.Map;
+
+import static org.opensearch.tracing.DefaultTracer.CURRENT_SPAN;
+
+public class TracerUtils {
+
+ public static void addTracerContextToHeader(Map requestHeaders, Map transientHeaders) {
+ TextMapSetter