Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add request tracing framework #7648

Merged
merged 43 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
53ca1dc
Add request tracing framework
suranjay May 21, 2023
f275db8
Extract Otel dependencies to a separate module
suranjay May 28, 2023
0d67058
Fix javadocs error
suranjay May 29, 2023
a2dd91d
Add request tracing framework
suranjay May 29, 2023
d8bfde9
Address PR comments
suranjay May 30, 2023
ddfdbf6
Fixed test
suranjay May 30, 2023
bf36b6b
Add unit tests
suranjay May 30, 2023
11c95ba
Refactor code
suranjay May 31, 2023
b1647f1
Add unit tests
suranjay May 31, 2023
881fedd
Add unit tests
suranjay May 31, 2023
49a10de
dummy commit
suranjay May 31, 2023
3a23e46
Adding instrumentation in search flow
suranjay May 31, 2023
4a6ded3
Revert "Adding instrumentation in search flow"
suranjay Jun 15, 2023
e394149
Extract foundation telemetry classes to libs
suranjay Jun 15, 2023
ec378e3
Fixed unit tests
suranjay Jun 16, 2023
e8332dc
Merge branch 'main' into main-opentelemetry
suranjay Jun 16, 2023
3317ae7
Fix type cast error
suranjay Jun 16, 2023
278d4ac
Fix type cast error in tests
suranjay Jun 16, 2023
117762d
Address PR comments
suranjay Jun 18, 2023
a4d0c2e
Merge branch 'main' into main-opentelemetry
suranjay Jun 18, 2023
ba4dd16
Return close on span creation
suranjay Jun 18, 2023
aa7ef69
Ensure span level is less than parent's level
suranjay Jun 19, 2023
8d096c2
Address PR comments
suranjay Jun 20, 2023
51d2e3f
Remove Level
suranjay Jun 20, 2023
55e5039
Remove parent override
suranjay Jun 20, 2023
4714934
Merge branch 'main' into main-opentelemetry
suranjay Jun 20, 2023
abc3029
Address PR comments
suranjay Jun 20, 2023
c5b1153
fix javadoc errors
suranjay Jun 20, 2023
39aa3a7
Address PR comments
suranjay Jun 22, 2023
8595058
move otel to plugin
suranjay Jun 24, 2023
d034a6b
Merge branch 'main' into main-opentelemetry
suranjay Jun 24, 2023
5565027
Fix javadocs error
suranjay Jun 24, 2023
c15cc62
Fix javadocs error
suranjay Jun 24, 2023
59b54df
Address PR comments
suranjay Jun 26, 2023
4157401
Fix javadocs error
suranjay Jun 26, 2023
01acc4a
Add unit tests coverage
suranjay Jun 26, 2023
b1d87af
Introduce ThreadContextStatePropagator to propagate request and trans…
reta Jun 26, 2023
830af09
Remove optional telemetry
suranjay Jun 26, 2023
5d28259
Fix NPE
suranjay Jun 26, 2023
7a8f1c7
Use created span
suranjay Jun 27, 2023
f2b1417
Rename plugin class
suranjay Jun 27, 2023
7637b37
Remove endSpan method
suranjay Jun 27, 2023
7591c1f
merge changes from main
suranjay Jun 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Bump `io.opencensus:opencensus-api` from 0.18.0 to 0.31.1 ([#7291](https://github.com/opensearch-project/OpenSearch/pull/7291))
- OpenJDK Update (April 2023 Patch releases) ([#7344](https://github.com/opensearch-project/OpenSearch/pull/7344)
- Bump `com.google.http-client:google-http-client:1.43.2` from 1.42.0 to 1.43.2 ([7928](https://github.com/opensearch-project/OpenSearch/pull/7928)))
- Add Opentelemetry dependencies ([#7543](https://github.com/opensearch-project/OpenSearch/issues/7543))

### Changed
- [CCR] Add getHistoryOperationsFromTranslog method to fetch the history snapshot from translogs ([#3948](https://github.com/opensearch-project/OpenSearch/pull/3948))
Expand Down Expand Up @@ -83,6 +84,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Update components of segrep backpressure to support remote store. ([#8020](https://github.com/opensearch-project/OpenSearch/pull/8020))
- Make remote cluster connection setup in async ([#8038](https://github.com/opensearch-project/OpenSearch/pull/8038))
- Add API to initialize extensions ([#8029]()https://github.com/opensearch-project/OpenSearch/pull/8029)
- Add distributed tracing framework ([#7543](https://github.com/opensearch-project/OpenSearch/issues/7543))

### Dependencies
- Bump `com.azure:azure-storage-common` from 12.21.0 to 12.21.1 (#7566, #7814)
Expand Down
4 changes: 4 additions & 0 deletions buildSrc/version.properties
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ zstd = 1.5.5-3
jzlib = 1.1.3

resteasy = 6.2.4.Final

# opentelemetry dependencies
opentelemetry = 1.26.0

23 changes: 23 additions & 0 deletions libs/telemetry/build.gradle
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'
}
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();

}
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 {

}
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;
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;
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;
}

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

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