Skip to content

Commit 6bf742c

Browse files
GaganjunejaGagan Juneja
authored andcommitted
Adds attributes to startSpan (opensearch-project#9199)
* Adds attributes to startSpan Signed-off-by: Gagan Juneja <[email protected]> * Update Changelog Signed-off-by: Gagan Juneja <[email protected]> * Adds attributes to startSpan Signed-off-by: Gagan Juneja <[email protected]> * Refactor code Signed-off-by: Gagan Juneja <[email protected]> * Add java doc Signed-off-by: Gagan Juneja <[email protected]> * Refactor code Signed-off-by: Gagan Juneja <[email protected]> * Refactor code Signed-off-by: Gagan Juneja <[email protected]> * Refactor code Signed-off-by: Gagan Juneja <[email protected]> * Removes dependency Signed-off-by: Gagan Juneja <[email protected]> --------- Signed-off-by: Gagan Juneja <[email protected]> Co-authored-by: Gagan Juneja <[email protected]>
1 parent 6b90bb5 commit 6bf742c

File tree

19 files changed

+440
-40
lines changed

19 files changed

+440
-40
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
127127
- [Remove] Deprecated Fractional ByteSizeValue support #9005 ([#9005](https://github.com/opensearch-project/OpenSearch/pull/9005))
128128
- Make MultiBucketConsumerService thread safe to use across slices during search ([#9047](https://github.com/opensearch-project/OpenSearch/pull/9047))
129129
- Change shard_size and shard_min_doc_count evaluation to happen in shard level reduce phase ([#9085](https://github.com/opensearch-project/OpenSearch/pull/9085))
130+
- Add attributes to startSpan methods ([#9199](https://github.com/opensearch-project/OpenSearch/pull/9199))
130131

131132
### Deprecated
132133

@@ -138,4 +139,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
138139
### Security
139140

140141
[Unreleased 3.0]: https://github.com/opensearch-project/OpenSearch/compare/2.x...HEAD
141-
[Unreleased 2.x]: https://github.com/opensearch-project/OpenSearch/compare/2.10...2.x
142+
[Unreleased 2.x]: https://github.com/opensearch-project/OpenSearch/compare/2.10...2.x

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.io.Closeable;
1212
import java.io.IOException;
13+
import org.opensearch.telemetry.tracing.attributes.Attributes;
1314

1415
/**
1516
*
@@ -37,16 +38,21 @@ public DefaultTracer(TracingTelemetry tracingTelemetry, TracerContextStorage<Str
3738

3839
@Override
3940
public SpanScope startSpan(String spanName) {
40-
return startSpan(spanName, null);
41+
return startSpan(spanName, Attributes.EMPTY);
4142
}
4243

4344
@Override
44-
public SpanScope startSpan(String spanName, SpanContext parentSpan) {
45+
public SpanScope startSpan(String spanName, Attributes attributes) {
46+
return startSpan(spanName, null, attributes);
47+
}
48+
49+
@Override
50+
public SpanScope startSpan(String spanName, SpanContext parentSpan, Attributes attributes) {
4551
Span span = null;
4652
if (parentSpan != null) {
47-
span = createSpan(spanName, parentSpan.getSpan());
53+
span = createSpan(spanName, parentSpan.getSpan(), attributes);
4854
} else {
49-
span = createSpan(spanName, getCurrentSpanInternal());
55+
span = createSpan(spanName, getCurrentSpanInternal(), attributes);
5056
}
5157
setCurrentSpanInContext(span);
5258
addDefaultAttributes(span);
@@ -74,8 +80,8 @@ private void endSpan(Span span) {
7480
}
7581
}
7682

77-
private Span createSpan(String spanName, Span parentSpan) {
78-
return tracingTelemetry.createSpan(spanName, parentSpan);
83+
private Span createSpan(String spanName, Span parentSpan, Attributes attributes) {
84+
return tracingTelemetry.createSpan(spanName, parentSpan, attributes);
7985
}
8086

8187
private void setCurrentSpanInContext(Span span) {

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

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

1111
import java.io.Closeable;
12+
import org.opensearch.telemetry.tracing.attributes.Attributes;
1213

1314
/**
1415
* Tracer is the interface used to create a {@link Span}
@@ -27,12 +28,22 @@ public interface Tracer extends Closeable {
2728
SpanScope startSpan(String spanName);
2829

2930
/**
30-
* Started the {@link Span} with the given name and parent.
31+
* Starts the {@link Span} with given name and attributes. This is required in cases when some attribute based
32+
* decision needs to be made before starting the span. Very useful in the case of Sampling.
33+
* @param spanName span name.
34+
* @param attributes attributes to be added.
35+
* @return scope of the span, must be closed with explicit close or with try-with-resource
36+
*/
37+
SpanScope startSpan(String spanName, Attributes attributes);
38+
39+
/**
40+
* Starts the {@link Span} with the given name, parent and attributes.
3141
* @param spanName span name.
3242
* @param parentSpan parent span.
43+
* @param attributes attributes to be added.
3344
* @return scope of the span, must be closed with explicit close or with try-with-resource
3445
*/
35-
SpanScope startSpan(String spanName, SpanContext parentSpan);
46+
SpanScope startSpan(String spanName, SpanContext parentSpan, Attributes attributes);
3647

3748
/**
3849
* Returns the current span.

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

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

1111
import java.io.Closeable;
12+
import org.opensearch.telemetry.tracing.attributes.Attributes;
1213

1314
/**
1415
* Interface for tracing telemetry providers
@@ -21,9 +22,10 @@ public interface TracingTelemetry extends Closeable {
2122
* Creates span with provided arguments
2223
* @param spanName name of the span
2324
* @param parentSpan span's parent span
25+
* @param attributes attributes to be added.
2426
* @return span instance
2527
*/
26-
Span createSpan(String spanName, Span parentSpan);
28+
Span createSpan(String spanName, Span parentSpan, Attributes attributes);
2729

2830
/**
2931
* provides tracing context propagator
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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.attributes;
10+
11+
import java.util.Collections;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import java.util.Objects;
15+
16+
/**
17+
* Class to create attributes for a span.
18+
*/
19+
public class Attributes {
20+
private final Map<String, Object> attributesMap;
21+
/**
22+
* Empty value.
23+
*/
24+
public final static Attributes EMPTY = new Attributes(Collections.emptyMap());
25+
26+
/**
27+
* Factory method.
28+
* @return attributes.
29+
*/
30+
public static Attributes create() {
31+
return new Attributes(new HashMap<>());
32+
}
33+
34+
/**
35+
* Constructor.
36+
*/
37+
private Attributes(Map<String, Object> attributesMap) {
38+
this.attributesMap = attributesMap;
39+
}
40+
41+
/**
42+
* Add String attribute.
43+
* @param key key
44+
* @param value value
45+
* @return Same instance.
46+
*/
47+
public Attributes addAttribute(String key, String value) {
48+
Objects.requireNonNull(value, "value cannot be null");
49+
attributesMap.put(key, value);
50+
return this;
51+
}
52+
53+
/**
54+
* Add long attribute.
55+
* @param key key
56+
* @param value value
57+
* @return Same instance.
58+
*/
59+
public Attributes addAttribute(String key, long value) {
60+
attributesMap.put(key, value);
61+
return this;
62+
};
63+
64+
/**
65+
* Add double attribute.
66+
* @param key key
67+
* @param value value
68+
* @return Same instance.
69+
*/
70+
public Attributes addAttribute(String key, double value) {
71+
attributesMap.put(key, value);
72+
return this;
73+
};
74+
75+
/**
76+
* Add boolean attribute.
77+
* @param key key
78+
* @param value value
79+
* @return Same instance.
80+
*/
81+
public Attributes addAttribute(String key, boolean value) {
82+
attributesMap.put(key, value);
83+
return this;
84+
};
85+
86+
/**
87+
* Returns the attribute map.
88+
* @return attributes map
89+
*/
90+
public Map<String, ?> getAttributesMap() {
91+
return Collections.unmodifiableMap(attributesMap);
92+
}
93+
94+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
/**
10+
* Contains No-op implementations
11+
*/
12+
package org.opensearch.telemetry.tracing.attributes;

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.opensearch.telemetry.tracing.SpanScope;
1212
import org.opensearch.telemetry.tracing.Tracer;
1313
import org.opensearch.telemetry.tracing.SpanContext;
14+
import org.opensearch.telemetry.tracing.attributes.Attributes;
1415

1516
/**
1617
* No-op implementation of Tracer
@@ -32,15 +33,20 @@ public SpanScope startSpan(String spanName) {
3233
}
3334

3435
@Override
35-
public SpanContext getCurrentSpan() {
36-
return null;
36+
public SpanScope startSpan(String spanName, Attributes attributes) {
37+
return SpanScope.NO_OP;
3738
}
3839

3940
@Override
40-
public SpanScope startSpan(String spanName, SpanContext parentSpan) {
41+
public SpanScope startSpan(String spanName, SpanContext parentSpan, Attributes attributes) {
4142
return SpanScope.NO_OP;
4243
}
4344

45+
@Override
46+
public SpanContext getCurrentSpan() {
47+
return null;
48+
}
49+
4450
@Override
4551
public void close() {
4652

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.opensearch.telemetry.tracing.SpanContext;
1212
import org.opensearch.telemetry.tracing.SpanScope;
1313
import org.opensearch.telemetry.tracing.Tracer;
14+
import org.opensearch.telemetry.tracing.attributes.Attributes;
1415

1516
/**
1617
* Wraps the runnable and add instrumentation to trace the {@link Runnable}
@@ -20,24 +21,27 @@ public class TraceableRunnable implements Runnable {
2021
private final SpanContext parent;
2122
private final Tracer tracer;
2223
private final String spanName;
24+
private final Attributes attributes;
2325

2426
/**
2527
* Constructor.
2628
* @param tracer tracer
2729
* @param spanName spanName
2830
* @param parent parent Span.
31+
* @param attributes attributes.
2932
* @param runnable runnable.
3033
*/
31-
public TraceableRunnable(Tracer tracer, String spanName, SpanContext parent, Runnable runnable) {
34+
public TraceableRunnable(Tracer tracer, String spanName, SpanContext parent, Attributes attributes, Runnable runnable) {
3235
this.tracer = tracer;
3336
this.spanName = spanName;
3437
this.parent = parent;
38+
this.attributes = attributes;
3539
this.runnable = runnable;
3640
}
3741

3842
@Override
3943
public void run() {
40-
try (SpanScope spanScope = tracer.startSpan(spanName, parent)) {
44+
try (SpanScope spanScope = tracer.startSpan(spanName, parent, attributes)) {
4145
runnable.run();
4246
}
4347
}

libs/telemetry/src/test/java/org/opensearch/telemetry/tracing/DefaultTracerTests.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@
1111
import org.junit.Assert;
1212
import org.opensearch.common.settings.Settings;
1313
import org.opensearch.common.util.concurrent.ThreadContext;
14+
import org.opensearch.telemetry.tracing.attributes.Attributes;
1415
import org.opensearch.test.OpenSearchTestCase;
1516

1617
import java.io.IOException;
18+
import org.opensearch.test.telemetry.tracing.MockSpan;
1719
import org.opensearch.test.telemetry.tracing.MockTracingTelemetry;
1820

21+
import static org.mockito.ArgumentMatchers.any;
22+
import static org.mockito.ArgumentMatchers.eq;
1923
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.never;
2025
import static org.mockito.Mockito.when;
2126
import static org.mockito.Mockito.verify;
2227

@@ -46,6 +51,42 @@ public void testCreateSpan() {
4651
Assert.assertEquals("span_name", defaultTracer.getCurrentSpan().getSpan().getSpanName());
4752
}
4853

54+
public void testCreateSpanWithAttributesWithMock() {
55+
DefaultTracer defaultTracer = new DefaultTracer(mockTracingTelemetry, mockTracerContextStorage);
56+
Attributes attributes = Attributes.create().addAttribute("name", "value");
57+
when(mockTracingTelemetry.createSpan("span_name", mockParentSpan, attributes)).thenReturn(mockSpan);
58+
defaultTracer.startSpan("span_name", attributes);
59+
verify(mockTracingTelemetry).createSpan("span_name", mockParentSpan, attributes);
60+
}
61+
62+
public void testCreateSpanWithAttributesWithParentMock() {
63+
DefaultTracer defaultTracer = new DefaultTracer(mockTracingTelemetry, mockTracerContextStorage);
64+
Attributes attributes = Attributes.create().addAttribute("name", "value");
65+
when(mockTracingTelemetry.createSpan("span_name", mockParentSpan, attributes)).thenReturn(mockSpan);
66+
defaultTracer.startSpan("span_name", new SpanContext(mockParentSpan), attributes);
67+
verify(mockTracingTelemetry).createSpan("span_name", mockParentSpan, attributes);
68+
verify(mockTracerContextStorage, never()).get(TracerContextStorage.CURRENT_SPAN);
69+
}
70+
71+
public void testCreateSpanWithAttributes() {
72+
TracingTelemetry tracingTelemetry = new MockTracingTelemetry();
73+
DefaultTracer defaultTracer = new DefaultTracer(
74+
tracingTelemetry,
75+
new ThreadContextBasedTracerContextStorage(new ThreadContext(Settings.EMPTY), tracingTelemetry)
76+
);
77+
78+
defaultTracer.startSpan(
79+
"span_name",
80+
Attributes.create().addAttribute("key1", 1.0).addAttribute("key2", 2l).addAttribute("key3", true).addAttribute("key4", "key4")
81+
);
82+
83+
Assert.assertEquals("span_name", defaultTracer.getCurrentSpan().getSpan().getSpanName());
84+
Assert.assertEquals(1.0, ((MockSpan) defaultTracer.getCurrentSpan().getSpan()).getAttribute("key1"));
85+
Assert.assertEquals(2l, ((MockSpan) defaultTracer.getCurrentSpan().getSpan()).getAttribute("key2"));
86+
Assert.assertEquals(true, ((MockSpan) defaultTracer.getCurrentSpan().getSpan()).getAttribute("key3"));
87+
Assert.assertEquals("key4", ((MockSpan) defaultTracer.getCurrentSpan().getSpan()).getAttribute("key4"));
88+
}
89+
4990
public void testCreateSpanWithParent() {
5091
TracingTelemetry tracingTelemetry = new MockTracingTelemetry();
5192
DefaultTracer defaultTracer = new DefaultTracer(
@@ -57,7 +98,7 @@ public void testCreateSpanWithParent() {
5798

5899
SpanContext parentSpan = defaultTracer.getCurrentSpan();
59100

60-
defaultTracer.startSpan("span_name_1", parentSpan);
101+
defaultTracer.startSpan("span_name_1", parentSpan, Attributes.EMPTY);
61102

62103
Assert.assertEquals("span_name_1", defaultTracer.getCurrentSpan().getSpan().getSpanName());
63104
Assert.assertEquals(parentSpan.getSpan(), defaultTracer.getCurrentSpan().getSpan().getParentSpan());
@@ -70,7 +111,7 @@ public void testCreateSpanWithNullParent() {
70111
new ThreadContextBasedTracerContextStorage(new ThreadContext(Settings.EMPTY), tracingTelemetry)
71112
);
72113

73-
defaultTracer.startSpan("span_name", null);
114+
defaultTracer.startSpan("span_name", null, Attributes.EMPTY);
74115

75116
Assert.assertEquals("span_name", defaultTracer.getCurrentSpan().getSpan().getSpanName());
76117
Assert.assertEquals(null, defaultTracer.getCurrentSpan().getSpan().getParentSpan());
@@ -105,6 +146,6 @@ private void setupMocks() {
105146
when(mockParentSpan.getSpanId()).thenReturn("parent_span_id");
106147
when(mockParentSpan.getTraceId()).thenReturn("trace_id");
107148
when(mockTracerContextStorage.get(TracerContextStorage.CURRENT_SPAN)).thenReturn(mockParentSpan, mockSpan);
108-
when(mockTracingTelemetry.createSpan("span_name", mockParentSpan)).thenReturn(mockSpan);
149+
when(mockTracingTelemetry.createSpan(eq("span_name"), eq(mockParentSpan), any(Attributes.class))).thenReturn(mockSpan);
109150
}
110151
}

0 commit comments

Comments
 (0)