Skip to content

Commit 790135b

Browse files
suranjayshiv0408
authored andcommitted
Move span operation to Scope from Tracer (opensearch-project#8411)
* Move span operation to Scope from Tracer Signed-off-by: suranjay <surajkumar.tu@gmail.com> * Add changelog entry Signed-off-by: suranjay <surajkumar.tu@gmail.com> * Change method to setError Signed-off-by: suranjay <surajkumar.tu@gmail.com> * Removed unused classes Signed-off-by: suranjay <surajkumar.tu@gmail.com> * Fix merge issue Signed-off-by: suranjay <surajkumar.tu@gmail.com> --------- Signed-off-by: suranjay <surajkumar.tu@gmail.com> Signed-off-by: Shivansh Arora <hishiv@amazon.com>
1 parent 05cf3a3 commit 790135b

File tree

15 files changed

+309
-224
lines changed

15 files changed

+309
-224
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5151
- Improve summary error message for invalid setting updates ([#4792](https://github.com/opensearch-project/OpenSearch/pull/4792))
5252
- Pass localNode info to all plugins on node start ([#7919](https://github.com/opensearch-project/OpenSearch/pull/7919))
5353
- Improved performance of parsing floating point numbers ([#7909](https://github.com/opensearch-project/OpenSearch/pull/7909))
54+
- Move span actions to Scope ([#8411](https://github.com/opensearch-project/OpenSearch/pull/8411))
5455

5556
### Deprecated
5657

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 java.util.function.Consumer;
12+
13+
/**
14+
* Default implementation of Scope
15+
*/
16+
public class DefaultSpanScope implements SpanScope {
17+
18+
private final Span span;
19+
20+
private final Consumer<Span> onCloseConsumer;
21+
22+
/**
23+
* Creates Scope instance for the given span
24+
*
25+
* @param span underlying span
26+
* @param onCloseConsumer consumer to execute on scope close
27+
*/
28+
public DefaultSpanScope(Span span, Consumer<Span> onCloseConsumer) {
29+
this.span = span;
30+
this.onCloseConsumer = onCloseConsumer;
31+
}
32+
33+
@Override
34+
public void addSpanAttribute(String key, String value) {
35+
span.addAttribute(key, value);
36+
}
37+
38+
@Override
39+
public void addSpanAttribute(String key, long value) {
40+
span.addAttribute(key, value);
41+
}
42+
43+
@Override
44+
public void addSpanAttribute(String key, double value) {
45+
span.addAttribute(key, value);
46+
}
47+
48+
@Override
49+
public void addSpanAttribute(String key, boolean value) {
50+
span.addAttribute(key, value);
51+
}
52+
53+
@Override
54+
public void addSpanEvent(String event) {
55+
span.addEvent(event);
56+
}
57+
58+
@Override
59+
public void setError(Exception exception) {
60+
span.setError(exception);
61+
}
62+
63+
/**
64+
* Executes the runnable to end the scope
65+
*/
66+
@Override
67+
public void close() {
68+
onCloseConsumer.accept(span);
69+
}
70+
}

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

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
/**
1515
*
16-
* The default tracer implementation. This class implements the basic logic for span lifecycle and its state management.
17-
* It also handles tracing context propagation between spans.
16+
* The default tracer implementation. It handles tracing context propagation between spans by maintaining
17+
* current active span in its storage
1818
*
1919
*
2020
*/
@@ -36,41 +36,11 @@ public DefaultTracer(TracingTelemetry tracingTelemetry, TracerContextStorage<Str
3636
}
3737

3838
@Override
39-
public Scope startSpan(String spanName) {
39+
public SpanScope startSpan(String spanName) {
4040
Span span = createSpan(spanName, getCurrentSpan());
4141
setCurrentSpanInContext(span);
4242
addDefaultAttributes(span);
43-
return new ScopeImpl(() -> endSpan(span));
44-
}
45-
46-
@Override
47-
public void addSpanAttribute(String key, String value) {
48-
Span currentSpan = getCurrentSpan();
49-
currentSpan.addAttribute(key, value);
50-
}
51-
52-
@Override
53-
public void addSpanAttribute(String key, long value) {
54-
Span currentSpan = getCurrentSpan();
55-
currentSpan.addAttribute(key, value);
56-
}
57-
58-
@Override
59-
public void addSpanAttribute(String key, double value) {
60-
Span currentSpan = getCurrentSpan();
61-
currentSpan.addAttribute(key, value);
62-
}
63-
64-
@Override
65-
public void addSpanAttribute(String key, boolean value) {
66-
Span currentSpan = getCurrentSpan();
67-
currentSpan.addAttribute(key, value);
68-
}
69-
70-
@Override
71-
public void addSpanEvent(String event) {
72-
Span currentSpan = getCurrentSpan();
73-
currentSpan.addEvent(event);
43+
return new DefaultSpanScope(span, (scopeSpan) -> endSpan(scopeSpan));
7444
}
7545

7646
@Override

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

Lines changed: 0 additions & 26 deletions
This file was deleted.

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

Lines changed: 0 additions & 33 deletions
This file was deleted.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ public interface Span {
6262
*/
6363
void addAttribute(String key, Boolean value);
6464

65+
/**
66+
* Records error in the span
67+
*
68+
* @param exception exception to be recorded
69+
*/
70+
void setError(Exception exception);
71+
6572
/**
6673
* Adds an event in the span
6774
*
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.telemetry.tracing.noop.NoopSpanScope;
12+
13+
/**
14+
* An auto-closeable that represents scope of the span.
15+
* It provides interface for all the span operations.
16+
*/
17+
public interface SpanScope extends AutoCloseable {
18+
/**
19+
* No-op Scope implementation
20+
*/
21+
SpanScope NO_OP = new NoopSpanScope();
22+
23+
/**
24+
* Adds string attribute to the {@link Span}.
25+
*
26+
* @param key attribute key
27+
* @param value attribute value
28+
*/
29+
void addSpanAttribute(String key, String value);
30+
31+
/**
32+
* Adds long attribute to the {@link Span}.
33+
*
34+
* @param key attribute key
35+
* @param value attribute value
36+
*/
37+
void addSpanAttribute(String key, long value);
38+
39+
/**
40+
* Adds double attribute to the {@link Span}.
41+
*
42+
* @param key attribute key
43+
* @param value attribute value
44+
*/
45+
void addSpanAttribute(String key, double value);
46+
47+
/**
48+
* Adds boolean attribute to the {@link Span}.
49+
*
50+
* @param key attribute key
51+
* @param value attribute value
52+
*/
53+
void addSpanAttribute(String key, boolean value);
54+
55+
/**
56+
* Adds an event to the {@link Span}.
57+
*
58+
* @param event event name
59+
*/
60+
void addSpanEvent(String event);
61+
62+
/**
63+
* Records error in the span
64+
*
65+
* @param exception exception to be recorded
66+
*/
67+
void setError(Exception exception);
68+
69+
/**
70+
* closes the scope
71+
*/
72+
@Override
73+
void close();
74+
}

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

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.io.Closeable;
1212

1313
/**
14-
* Tracer is the interface used to create a {@link Span} and interact with current active {@link Span}.
14+
* Tracer is the interface used to create a {@link Span}
1515
* It automatically handles the context propagation between threads, tasks, nodes etc.
1616
*
1717
* All methods on the Tracer object are multi-thread safe.
@@ -24,44 +24,6 @@ public interface Tracer extends Closeable {
2424
* @param spanName span name
2525
* @return scope of the span, must be closed with explicit close or with try-with-resource
2626
*/
27-
Scope startSpan(String spanName);
27+
SpanScope startSpan(String spanName);
2828

29-
/**
30-
* Adds string attribute to the current active {@link Span}.
31-
*
32-
* @param key attribute key
33-
* @param value attribute value
34-
*/
35-
void addSpanAttribute(String key, String value);
36-
37-
/**
38-
* Adds long attribute to the current active {@link Span}.
39-
*
40-
* @param key attribute key
41-
* @param value attribute value
42-
*/
43-
void addSpanAttribute(String key, long value);
44-
45-
/**
46-
* Adds double attribute to the current active {@link Span}.
47-
*
48-
* @param key attribute key
49-
* @param value attribute value
50-
*/
51-
void addSpanAttribute(String key, double value);
52-
53-
/**
54-
* Adds boolean attribute to the current active {@link Span}.
55-
*
56-
* @param key attribute key
57-
* @param value attribute value
58-
*/
59-
void addSpanAttribute(String key, boolean value);
60-
61-
/**
62-
* Adds an event to the current active {@link Span}.
63-
*
64-
* @param event event name
65-
*/
66-
void addSpanEvent(String event);
6729
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.noop;
10+
11+
import org.opensearch.telemetry.tracing.SpanScope;
12+
13+
/**
14+
* No-op implementation of SpanScope
15+
*/
16+
public final class NoopSpanScope implements SpanScope {
17+
18+
/**
19+
* No-args constructor
20+
*/
21+
public NoopSpanScope() {}
22+
23+
@Override
24+
public void addSpanAttribute(String key, String value) {
25+
26+
}
27+
28+
@Override
29+
public void addSpanAttribute(String key, long value) {
30+
31+
}
32+
33+
@Override
34+
public void addSpanAttribute(String key, double value) {
35+
36+
}
37+
38+
@Override
39+
public void addSpanAttribute(String key, boolean value) {
40+
41+
}
42+
43+
@Override
44+
public void addSpanEvent(String event) {
45+
46+
}
47+
48+
@Override
49+
public void setError(Exception exception) {
50+
51+
}
52+
53+
@Override
54+
public void close() {
55+
56+
}
57+
}

0 commit comments

Comments
 (0)