forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add request tracing framework (opensearch-project#7648)
* Add request tracing framework Signed-off-by: suranjay <surajkumar.tu@gmail.com> * Introduce ThreadContextStatePropagator to propagate request and transient headers within ThreadContext Signed-off-by: Andriy Redko <andriy.redko@aiven.io> --------- Signed-off-by: suranjay <surajkumar.tu@gmail.com> Signed-off-by: Andriy Redko <andriy.redko@aiven.io> Co-authored-by: Andriy Redko <andriy.redko@aiven.io>
- Loading branch information
Showing
84 changed files
with
4,298 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,3 +62,7 @@ zstd = 1.5.5-3 | |
jzlib = 1.1.3 | ||
|
||
resteasy = 6.2.4.Final | ||
|
||
# opentelemetry dependencies | ||
opentelemetry = 1.26.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* 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. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
dependencies { | ||
testImplementation "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}" | ||
testImplementation "junit:junit:${versions.junit}" | ||
testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}" | ||
testImplementation(project(":test:framework")) { | ||
exclude group: 'org.opensearch', module: 'opensearch-telemetry' | ||
} | ||
} | ||
|
||
tasks.named('forbiddenApisMain').configure { | ||
replaceSignatureFiles 'jdk-signatures' | ||
} |
31 changes: 31 additions & 0 deletions
31
libs/telemetry/src/main/java/org/opensearch/telemetry/Telemetry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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.telemetry; | ||
|
||
import org.opensearch.telemetry.metrics.MetricsTelemetry; | ||
import org.opensearch.telemetry.tracing.TracingTelemetry; | ||
|
||
/** | ||
* Interface defining telemetry | ||
*/ | ||
public interface Telemetry { | ||
|
||
/** | ||
* Provides tracing telemetry | ||
* @return tracing telemetry instance | ||
*/ | ||
TracingTelemetry getTracingTelemetry(); | ||
|
||
/** | ||
* Provides metrics telemetry | ||
* @return metrics telemetry instance | ||
*/ | ||
MetricsTelemetry getMetricsTelemetry(); | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
libs/telemetry/src/main/java/org/opensearch/telemetry/metrics/MetricsTelemetry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* 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.telemetry.metrics; | ||
|
||
/** | ||
* Interface for metrics telemetry providers | ||
*/ | ||
public interface MetricsTelemetry { | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
libs/telemetry/src/main/java/org/opensearch/telemetry/metrics/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* Contains metrics related classes | ||
*/ | ||
package org.opensearch.telemetry.metrics; |
12 changes: 12 additions & 0 deletions
12
libs/telemetry/src/main/java/org/opensearch/telemetry/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* Contains telemetry related classes | ||
*/ | ||
package org.opensearch.telemetry; |
45 changes: 45 additions & 0 deletions
45
libs/telemetry/src/main/java/org/opensearch/telemetry/tracing/AbstractSpan.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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.telemetry.tracing; | ||
|
||
/** | ||
* Base span | ||
*/ | ||
public abstract class AbstractSpan implements Span { | ||
|
||
/** | ||
* name of the span | ||
*/ | ||
private final String spanName; | ||
/** | ||
* span's parent span | ||
*/ | ||
private final Span parentSpan; | ||
|
||
/** | ||
* Base constructor | ||
* @param spanName name of the span | ||
* @param parentSpan span's parent span | ||
*/ | ||
protected AbstractSpan(String spanName, Span parentSpan) { | ||
this.spanName = spanName; | ||
this.parentSpan = parentSpan; | ||
} | ||
|
||
@Override | ||
public Span getParentSpan() { | ||
return parentSpan; | ||
} | ||
|
||
@Override | ||
public String getSpanName() { | ||
return spanName; | ||
} | ||
|
||
} |
109 changes: 109 additions & 0 deletions
109
libs/telemetry/src/main/java/org/opensearch/telemetry/tracing/DefaultTracer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* 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.telemetry.tracing; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
|
||
/** | ||
* | ||
* The default tracer implementation. This class implements the basic logic for span lifecycle and its state management. | ||
* It also handles tracing context propagation between spans. | ||
* | ||
* | ||
*/ | ||
public class DefaultTracer implements Tracer { | ||
static final String THREAD_NAME = "th_name"; | ||
|
||
private final TracingTelemetry tracingTelemetry; | ||
private final TracerContextStorage<String, Span> tracerContextStorage; | ||
|
||
/** | ||
* Creates DefaultTracer instance | ||
* | ||
* @param tracingTelemetry tracing telemetry instance | ||
* @param tracerContextStorage storage used for storing current span context | ||
*/ | ||
public DefaultTracer(TracingTelemetry tracingTelemetry, TracerContextStorage<String, Span> tracerContextStorage) { | ||
this.tracingTelemetry = tracingTelemetry; | ||
this.tracerContextStorage = tracerContextStorage; | ||
} | ||
|
||
@Override | ||
public Scope startSpan(String spanName) { | ||
Span span = createSpan(spanName, getCurrentSpan()); | ||
setCurrentSpanInContext(span); | ||
addDefaultAttributes(span); | ||
return new ScopeImpl(() -> endSpan(span)); | ||
} | ||
|
||
@Override | ||
public void addSpanAttribute(String key, String value) { | ||
Span currentSpan = getCurrentSpan(); | ||
currentSpan.addAttribute(key, value); | ||
} | ||
|
||
@Override | ||
public void addSpanAttribute(String key, long value) { | ||
Span currentSpan = getCurrentSpan(); | ||
currentSpan.addAttribute(key, value); | ||
} | ||
|
||
@Override | ||
public void addSpanAttribute(String key, double value) { | ||
Span currentSpan = getCurrentSpan(); | ||
currentSpan.addAttribute(key, value); | ||
} | ||
|
||
@Override | ||
public void addSpanAttribute(String key, boolean value) { | ||
Span currentSpan = getCurrentSpan(); | ||
currentSpan.addAttribute(key, value); | ||
} | ||
|
||
@Override | ||
public void addSpanEvent(String event) { | ||
Span currentSpan = getCurrentSpan(); | ||
currentSpan.addEvent(event); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
((Closeable) tracingTelemetry).close(); | ||
} | ||
|
||
// Visible for testing | ||
Span getCurrentSpan() { | ||
return tracerContextStorage.get(TracerContextStorage.CURRENT_SPAN); | ||
} | ||
|
||
private void endSpan(Span span) { | ||
if (span != null) { | ||
span.endSpan(); | ||
setCurrentSpanInContext(span.getParentSpan()); | ||
} | ||
} | ||
|
||
private Span createSpan(String spanName, Span parentSpan) { | ||
return tracingTelemetry.createSpan(spanName, parentSpan); | ||
} | ||
|
||
private void setCurrentSpanInContext(Span span) { | ||
tracerContextStorage.put(TracerContextStorage.CURRENT_SPAN, span); | ||
} | ||
|
||
/** | ||
* Adds default attributes in the span | ||
* @param span the current active span | ||
*/ | ||
protected void addDefaultAttributes(Span span) { | ||
span.addAttribute(THREAD_NAME, Thread.currentThread().getName()); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
libs/telemetry/src/main/java/org/opensearch/telemetry/tracing/Scope.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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.telemetry.tracing; | ||
|
||
/** | ||
* An auto-closeable that represents scope of the span. | ||
* It is recommended that you use this class with a try-with-resources block: | ||
*/ | ||
public interface Scope extends AutoCloseable { | ||
/** | ||
* No-op Scope implementation | ||
*/ | ||
Scope NO_OP = () -> {}; | ||
|
||
/** | ||
* closes the scope | ||
*/ | ||
@Override | ||
void close(); | ||
} |
33 changes: 33 additions & 0 deletions
33
libs/telemetry/src/main/java/org/opensearch/telemetry/tracing/ScopeImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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.telemetry.tracing; | ||
|
||
/** | ||
* Executes the runnable on close | ||
*/ | ||
public class ScopeImpl implements Scope { | ||
|
||
private Runnable runnableOnClose; | ||
|
||
/** | ||
* Creates Scope instance | ||
* @param runnableOnClose runnable to execute on scope close | ||
*/ | ||
public ScopeImpl(Runnable runnableOnClose) { | ||
this.runnableOnClose = runnableOnClose; | ||
} | ||
|
||
/** | ||
* Executes the runnable to end the scope | ||
*/ | ||
@Override | ||
public void close() { | ||
runnableOnClose.run(); | ||
} | ||
} |
Oops, something went wrong.