Skip to content

Commit 19fa37c

Browse files
GaganjunejaGagan Juneja
andauthored
[Tracing Framework] Add support for SpanKind (#10122)
* [Tracing Framework] Add support for SpanKind Signed-off-by: Gagan Juneja <gjjuneja@amazon.com> * Add CHANGELOG Signed-off-by: Gagan Juneja <gjjuneja@amazon.com> * Fix test case Signed-off-by: Gagan Juneja <gjjuneja@amazon.com> * Address review comment Signed-off-by: Gagan Juneja <gjjuneja@amazon.com> * Address review comment Signed-off-by: Gagan Juneja <gjjuneja@amazon.com> * Address review comment Signed-off-by: Gagan Juneja <gjjuneja@amazon.com> --------- Signed-off-by: Gagan Juneja <gjjuneja@amazon.com> Co-authored-by: Gagan Juneja <gjjuneja@amazon.com>
1 parent 346bcfc commit 19fa37c

21 files changed

Lines changed: 353 additions & 240 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
108108
- Mute the query profile IT with concurrent execution ([#9840](https://github.com/opensearch-project/OpenSearch/pull/9840))
109109
- Force merge with `only_expunge_deletes` honors max segment size ([#10036](https://github.com/opensearch-project/OpenSearch/pull/10036))
110110
- Add instrumentation in transport service. ([#10042](https://github.com/opensearch-project/OpenSearch/pull/10042))
111+
- [Tracing Framework] Add support for SpanKind. ([#10122](https://github.com/opensearch-project/OpenSearch/pull/10122))
111112

112113
### Deprecated
113114

@@ -122,4 +123,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
122123
### Security
123124

124125
[Unreleased 3.0]: https://github.com/opensearch-project/OpenSearch/compare/2.x...HEAD
125-
[Unreleased 2.x]: https://github.com/opensearch-project/OpenSearch/compare/2.11...2.x
126+
[Unreleased 2.x]: https://github.com/opensearch-project/OpenSearch/compare/2.11...2.x

libs/telemetry/src/main/java/org/opensearch/telemetry/tracing/DefaultTracer.java

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package org.opensearch.telemetry.tracing;
1010

1111
import org.opensearch.common.annotation.InternalApi;
12-
import org.opensearch.telemetry.tracing.attributes.Attributes;
1312

1413
import java.io.Closeable;
1514
import java.io.IOException;
@@ -44,27 +43,13 @@ public DefaultTracer(TracingTelemetry tracingTelemetry, TracerContextStorage<Str
4443

4544
@Override
4645
public Span startSpan(SpanCreationContext context) {
47-
return startSpan(context.getSpanName(), context.getAttributes());
48-
}
49-
50-
@Override
51-
public Span startSpan(String spanName) {
52-
return startSpan(spanName, Attributes.EMPTY);
53-
}
54-
55-
@Override
56-
public Span startSpan(String spanName, Attributes attributes) {
57-
return startSpan(spanName, (SpanContext) null, attributes);
58-
}
59-
60-
@Override
61-
public Span startSpan(String spanName, SpanContext parentSpan, Attributes attributes) {
62-
Span span = null;
63-
if (parentSpan != null) {
64-
span = createSpan(spanName, parentSpan.getSpan(), attributes);
46+
Span parentSpan = null;
47+
if (context.getParent() != null) {
48+
parentSpan = context.getParent().getSpan();
6549
} else {
66-
span = createSpan(spanName, getCurrentSpanInternal(), attributes);
50+
parentSpan = getCurrentSpanInternal();
6751
}
52+
Span span = createSpan(context, parentSpan);
6853
setCurrentSpanInContext(span);
6954
addDefaultAttributes(span);
7055
return span;
@@ -87,12 +72,7 @@ public SpanContext getCurrentSpan() {
8772

8873
@Override
8974
public ScopedSpan startScopedSpan(SpanCreationContext spanCreationContext) {
90-
return startScopedSpan(spanCreationContext, null);
91-
}
92-
93-
@Override
94-
public ScopedSpan startScopedSpan(SpanCreationContext spanCreationContext, SpanContext parentSpan) {
95-
Span span = startSpan(spanCreationContext.getSpanName(), parentSpan, spanCreationContext.getAttributes());
75+
Span span = startSpan(spanCreationContext);
9676
SpanScope spanScope = withSpanInScope(span);
9777
return new DefaultScopedSpan(span, spanScope);
9878
}
@@ -102,8 +82,8 @@ public SpanScope withSpanInScope(Span span) {
10282
return DefaultSpanScope.create(span, tracerContextStorage).attach();
10383
}
10484

105-
private Span createSpan(String spanName, Span parentSpan, Attributes attributes) {
106-
return tracingTelemetry.createSpan(spanName, parentSpan, attributes);
85+
private Span createSpan(SpanCreationContext spanCreationContext, Span parentSpan) {
86+
return tracingTelemetry.createSpan(spanCreationContext, parentSpan);
10787
}
10888

10989
private void setCurrentSpanInContext(Span span) {
@@ -121,11 +101,7 @@ protected void addDefaultAttributes(Span span) {
121101
@Override
122102
public Span startSpan(SpanCreationContext spanCreationContext, Map<String, List<String>> headers) {
123103
Optional<Span> propagatedSpan = tracingTelemetry.getContextPropagator().extractFromHeaders(headers);
124-
return startSpan(
125-
spanCreationContext.getSpanName(),
126-
propagatedSpan.map(SpanContext::new).orElse(null),
127-
spanCreationContext.getAttributes()
128-
);
104+
return startSpan(spanCreationContext.parent(propagatedSpan.map(SpanContext::new).orElse(null)));
129105
}
130106

131107
}

libs/telemetry/src/main/java/org/opensearch/telemetry/tracing/SpanCreationContext.java

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,74 @@
1818
*/
1919
@ExperimentalApi
2020
public final class SpanCreationContext {
21-
private final String spanName;
22-
private final Attributes attributes;
21+
private String spanName;
22+
private Attributes attributes;
23+
private SpanKind spanKind = SpanKind.INTERNAL;
24+
private SpanContext parent;
2325

2426
/**
2527
* Constructor.
28+
*/
29+
private SpanCreationContext() {}
30+
31+
/**
32+
* Sets the span type to server.
33+
* @return spanCreationContext
34+
*/
35+
public static SpanCreationContext server() {
36+
SpanCreationContext spanCreationContext = new SpanCreationContext();
37+
spanCreationContext.spanKind = SpanKind.SERVER;
38+
return spanCreationContext;
39+
}
40+
41+
/**
42+
* Sets the span type to client.
43+
* @return spanCreationContext
44+
*/
45+
public static SpanCreationContext client() {
46+
SpanCreationContext spanCreationContext = new SpanCreationContext();
47+
spanCreationContext.spanKind = SpanKind.CLIENT;
48+
return spanCreationContext;
49+
}
50+
51+
/**
52+
* Sets the span type to internal.
53+
* @return spanCreationContext
54+
*/
55+
public static SpanCreationContext internal() {
56+
SpanCreationContext spanCreationContext = new SpanCreationContext();
57+
spanCreationContext.spanKind = SpanKind.INTERNAL;
58+
return spanCreationContext;
59+
}
60+
61+
/**
62+
* Sets the span name.
2663
* @param spanName span name.
27-
* @param attributes attributes.
64+
* @return spanCreationContext
2865
*/
29-
public SpanCreationContext(String spanName, Attributes attributes) {
66+
public SpanCreationContext name(String spanName) {
3067
this.spanName = spanName;
68+
return this;
69+
}
70+
71+
/**
72+
* Sets the span attributes.
73+
* @param attributes attributes.
74+
* @return spanCreationContext
75+
*/
76+
public SpanCreationContext attributes(Attributes attributes) {
3177
this.attributes = attributes;
78+
return this;
79+
}
80+
81+
/**
82+
* Sets the parent for spann
83+
* @param parent parent
84+
* @return spanCreationContext
85+
*/
86+
public SpanCreationContext parent(SpanContext parent) {
87+
this.parent = parent;
88+
return this;
3289
}
3390

3491
/**
@@ -46,4 +103,20 @@ public String getSpanName() {
46103
public Attributes getAttributes() {
47104
return attributes;
48105
}
106+
107+
/**
108+
* Returns the span kind.
109+
* @return spankind.
110+
*/
111+
public SpanKind getSpanKind() {
112+
return spanKind;
113+
}
114+
115+
/**
116+
* Returns the parent span
117+
* @return parent.
118+
*/
119+
public SpanContext getParent() {
120+
return parent;
121+
}
49122
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.telemetry.tracing;
10+
11+
import org.opensearch.common.annotation.PublicApi;
12+
13+
/**
14+
* Type of Span.
15+
*/
16+
@PublicApi(since = "2.11.0")
17+
public enum SpanKind {
18+
/**
19+
* Span represents the client side code.
20+
*/
21+
CLIENT,
22+
/**
23+
* Span represents the server side code.
24+
*/
25+
SERVER,
26+
27+
/**
28+
* Span represents the internal calls. This is the default value of a span type.
29+
*/
30+
INTERNAL;
31+
}

libs/telemetry/src/main/java/org/opensearch/telemetry/tracing/Tracer.java

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package org.opensearch.telemetry.tracing;
1010

1111
import org.opensearch.common.annotation.ExperimentalApi;
12-
import org.opensearch.telemetry.tracing.attributes.Attributes;
1312
import org.opensearch.telemetry.tracing.http.HttpTracer;
1413

1514
import java.io.Closeable;
@@ -32,34 +31,6 @@ public interface Tracer extends HttpTracer, Closeable {
3231
*/
3332
Span startSpan(SpanCreationContext context);
3433

35-
/**
36-
* Starts the {@link Span} with given name
37-
*
38-
* @param spanName span name
39-
* @return span, must be closed.
40-
*/
41-
Span startSpan(String spanName);
42-
43-
/**
44-
* Starts the {@link Span} with given name and attributes. This is required in cases when some attribute based
45-
* decision needs to be made before starting the span. Very useful in the case of Sampling.
46-
*
47-
* @param spanName span name.
48-
* @param attributes attributes to be added.
49-
* @return span, must be closed.
50-
*/
51-
Span startSpan(String spanName, Attributes attributes);
52-
53-
/**
54-
* Starts the {@link Span} with the given name, parent and attributes.
55-
*
56-
* @param spanName span name.
57-
* @param parentSpan parent span.
58-
* @param attributes attributes to be added.
59-
* @return span, must be closed.
60-
*/
61-
Span startSpan(String spanName, SpanContext parentSpan, Attributes attributes);
62-
6334
/**
6435
* Returns the current span.
6536
* @return current wrapped span.
@@ -74,15 +45,6 @@ public interface Tracer extends HttpTracer, Closeable {
7445
*/
7546
ScopedSpan startScopedSpan(SpanCreationContext spanCreationContext);
7647

77-
/**
78-
* Start the span and scoped it. This must be used for scenarios where {@link SpanScope} and {@link Span} lifecycles
79-
* are same and ends within the same thread where created.
80-
* @param spanCreationContext span creation context
81-
* @param parentSpan parent span.
82-
* @return scope of the span, must be closed with explicit close or with try-with-resource
83-
*/
84-
ScopedSpan startScopedSpan(SpanCreationContext spanCreationContext, SpanContext parentSpan);
85-
8648
/**
8749
* Creates the Span Scope for a current thread. It's mandatory to scope the span just after creation so that it will
8850
* automatically manage the attach /detach to the current thread.

libs/telemetry/src/main/java/org/opensearch/telemetry/tracing/TracingTelemetry.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package org.opensearch.telemetry.tracing;
1010

1111
import org.opensearch.common.annotation.ExperimentalApi;
12-
import org.opensearch.telemetry.tracing.attributes.Attributes;
1312

1413
import java.io.Closeable;
1514

@@ -24,12 +23,11 @@ public interface TracingTelemetry extends Closeable {
2423
/**
2524
* Creates span with provided arguments
2625
*
27-
* @param spanName name of the span
28-
* @param parentSpan span's parent span
29-
* @param attributes attributes to be added.
26+
* @param spanCreationContext span creation context.
27+
* @param parentSpan parent span.
3028
* @return span instance
3129
*/
32-
Span createSpan(String spanName, Span parentSpan, Attributes attributes);
30+
Span createSpan(SpanCreationContext spanCreationContext, Span parentSpan);
3331

3432
/**
3533
* provides tracing context propagator

libs/telemetry/src/main/java/org/opensearch/telemetry/tracing/noop/NoopTracer.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.opensearch.telemetry.tracing.SpanCreationContext;
1616
import org.opensearch.telemetry.tracing.SpanScope;
1717
import org.opensearch.telemetry.tracing.Tracer;
18-
import org.opensearch.telemetry.tracing.attributes.Attributes;
1918

2019
import java.util.List;
2120
import java.util.Map;
@@ -40,21 +39,6 @@ public Span startSpan(SpanCreationContext context) {
4039
return NoopSpan.INSTANCE;
4140
}
4241

43-
@Override
44-
public Span startSpan(String spanName) {
45-
return NoopSpan.INSTANCE;
46-
}
47-
48-
@Override
49-
public Span startSpan(String spanName, Attributes attributes) {
50-
return NoopSpan.INSTANCE;
51-
}
52-
53-
@Override
54-
public Span startSpan(String spanName, SpanContext parentSpan, Attributes attributes) {
55-
return NoopSpan.INSTANCE;
56-
}
57-
5842
@Override
5943
public SpanContext getCurrentSpan() {
6044
return new SpanContext(NoopSpan.INSTANCE);
@@ -65,11 +49,6 @@ public ScopedSpan startScopedSpan(SpanCreationContext spanCreationContext) {
6549
return ScopedSpan.NO_OP;
6650
}
6751

68-
@Override
69-
public ScopedSpan startScopedSpan(SpanCreationContext spanCreationContext, SpanContext parentSpan) {
70-
return ScopedSpan.NO_OP;
71-
}
72-
7352
@Override
7453
public SpanScope withSpanInScope(Span span) {
7554
return SpanScope.NO_OP;

libs/telemetry/src/main/java/org/opensearch/telemetry/tracing/runnable/TraceableRunnable.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,32 @@
99
package org.opensearch.telemetry.tracing.runnable;
1010

1111
import org.opensearch.telemetry.tracing.ScopedSpan;
12-
import org.opensearch.telemetry.tracing.SpanContext;
1312
import org.opensearch.telemetry.tracing.SpanCreationContext;
1413
import org.opensearch.telemetry.tracing.Tracer;
15-
import org.opensearch.telemetry.tracing.attributes.Attributes;
1614

1715
/**
1816
* Wraps the runnable and add instrumentation to trace the {@link Runnable}
1917
*/
2018
public class TraceableRunnable implements Runnable {
2119
private final Runnable runnable;
22-
private final SpanContext parent;
20+
private final SpanCreationContext spanCreationContext;
2321
private final Tracer tracer;
24-
private final String spanName;
25-
private final Attributes attributes;
2622

2723
/**
2824
* Constructor.
2925
* @param tracer tracer
30-
* @param spanName spanName
31-
* @param parent parent Span.
32-
* @param attributes attributes.
26+
* @param spanCreationContext spanCreationContext
3327
* @param runnable runnable.
3428
*/
35-
public TraceableRunnable(Tracer tracer, String spanName, SpanContext parent, Attributes attributes, Runnable runnable) {
29+
public TraceableRunnable(Tracer tracer, SpanCreationContext spanCreationContext, Runnable runnable) {
3630
this.tracer = tracer;
37-
this.spanName = spanName;
38-
this.parent = parent;
39-
this.attributes = attributes;
31+
this.spanCreationContext = spanCreationContext;
4032
this.runnable = runnable;
4133
}
4234

4335
@Override
4436
public void run() {
45-
try (ScopedSpan spanScope = tracer.startScopedSpan(new SpanCreationContext(spanName, attributes), parent)) {
37+
try (ScopedSpan spanScope = tracer.startScopedSpan(spanCreationContext)) {
4638
runnable.run();
4739
}
4840
}

0 commit comments

Comments
 (0)