Skip to content

Commit 30a1bb8

Browse files
alrexc24t
alrex
authored andcommitted
Removing add_link and add_lazy_link from api/sdk (#259)
Prevents adding links after spans are created for OTEP 0006.
1 parent d5b580f commit 30a1bb8

File tree

6 files changed

+95
-67
lines changed

6 files changed

+95
-67
lines changed

ext/opentelemetry-ext-jaeger/examples/jaeger_exporter_example.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@
3333
time.sleep(0.1)
3434
foo.set_attribute("my_atribbute", True)
3535
foo.add_event("event in foo", {"name": "foo1"})
36-
with tracer.start_as_current_span("bar") as bar:
36+
with tracer.start_as_current_span(
37+
"bar", links=[trace.Link(foo.get_context())]
38+
) as bar:
3739
time.sleep(0.2)
3840
bar.set_attribute("speed", 100.0)
39-
bar.add_link(foo.get_context())
4041

4142
with tracer.start_as_current_span("baz") as baz:
4243
time.sleep(0.3)

ext/opentelemetry-ext-opentracing-shim/src/opentelemetry/ext/opentracing_shim/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import opentracing
1818
from deprecated import deprecated
1919

20+
import opentelemetry.trace as trace_api
2021
from opentelemetry import propagators
2122
from opentelemetry.ext.opentracing_shim import util
2223

@@ -231,11 +232,15 @@ def start_span(
231232
# Use the specified parent or the active span if possible. Otherwise,
232233
# use a `None` parent, which triggers the creation of a new trace.
233234
parent = child_of.unwrap() if child_of else None
234-
span = self._otel_tracer.create_span(operation_name, parent)
235235

236+
links = []
236237
if references:
237238
for ref in references:
238-
span.add_link(ref.referenced_context.unwrap())
239+
links.append(trace_api.Link(ref.referenced_context.unwrap()))
240+
241+
span = self._otel_tracer.create_span(
242+
operation_name, parent, links=links
243+
)
239244

240245
if tags:
241246
for key, value in tags.items():

opentelemetry-api/src/opentelemetry/trace/__init__.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ def __init__(
8080
self, context: "SpanContext", attributes: types.Attributes = None
8181
) -> None:
8282
self._context = context
83-
self._attributes = attributes
83+
if attributes is None:
84+
self._attributes = {} # type: types.Attributes
85+
else:
86+
self._attributes = attributes
8487

8588
@property
8689
def context(self) -> "SpanContext":
@@ -198,23 +201,6 @@ def add_lazy_event(self, event: Event) -> None:
198201
Adds an `Event` that has previously been created.
199202
"""
200203

201-
def add_link(
202-
self,
203-
link_target_context: "SpanContext",
204-
attributes: types.Attributes = None,
205-
) -> None:
206-
"""Adds a `Link` to another span.
207-
208-
Adds a single `Link` from this Span to another Span identified by the
209-
`SpanContext` passed as argument.
210-
"""
211-
212-
def add_lazy_link(self, link: "Link") -> None:
213-
"""Adds a `Link` to another span.
214-
215-
Adds a `Link` that has previously been created.
216-
"""
217-
218204
def update_name(self, name: str) -> None:
219205
"""Updates the `Span` name.
220206
@@ -416,6 +402,8 @@ def start_span(
416402
name: str,
417403
parent: ParentSpan = CURRENT_SPAN,
418404
kind: SpanKind = SpanKind.INTERNAL,
405+
attributes: typing.Optional[types.Attributes] = None,
406+
links: typing.Sequence[Link] = (),
419407
) -> "Span":
420408
"""Starts a span.
421409
@@ -444,6 +432,8 @@ def start_span(
444432
parent: The span's parent. Defaults to the current span.
445433
kind: The span's kind (relationship to parent). Note that is
446434
meaningful even if there is no parent.
435+
attributes: The span's attributes.
436+
links: Links span to other spans
447437
448438
Returns:
449439
The newly-created span.
@@ -457,6 +447,8 @@ def start_as_current_span(
457447
name: str,
458448
parent: ParentSpan = CURRENT_SPAN,
459449
kind: SpanKind = SpanKind.INTERNAL,
450+
attributes: typing.Optional[types.Attributes] = None,
451+
links: typing.Sequence[Link] = (),
460452
) -> typing.Iterator["Span"]:
461453
"""Context manager for creating a new span and set it
462454
as the current span in this tracer's context.
@@ -492,6 +484,8 @@ def start_as_current_span(
492484
parent: The span's parent. Defaults to the current span.
493485
kind: The span's kind (relationship to parent). Note that is
494486
meaningful even if there is no parent.
487+
attributes: The span's attributes.
488+
links: Links span to other spans
495489
496490
Yields:
497491
The newly-created span.
@@ -505,6 +499,8 @@ def create_span(
505499
name: str,
506500
parent: ParentSpan = CURRENT_SPAN,
507501
kind: SpanKind = SpanKind.INTERNAL,
502+
attributes: typing.Optional[types.Attributes] = None,
503+
links: typing.Sequence[Link] = (),
508504
) -> "Span":
509505
"""Creates a span.
510506
@@ -534,6 +530,8 @@ def create_span(
534530
parent: The span's parent. Defaults to the current span.
535531
kind: The span's kind (relationship to parent). Note that is
536532
meaningful even if there is no parent.
533+
attributes: The span's attributes.
534+
links: Links span to other spans
537535
538536
Returns:
539537
The newly-created span.

opentelemetry-api/src/opentelemetry/trace/sampling.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
# pylint: disable=unused-import
1919
from opentelemetry.trace import Link, SpanContext
20-
from opentelemetry.util.types import AttributeValue
20+
from opentelemetry.util.types import Attributes, AttributeValue
2121

2222

2323
class Decision:
@@ -53,6 +53,7 @@ def should_sample(
5353
trace_id: int,
5454
span_id: int,
5555
name: str,
56+
attributes: Optional[Attributes] = None,
5657
links: Sequence["Link"] = (),
5758
) -> "Decision":
5859
pass
@@ -70,6 +71,7 @@ def should_sample(
7071
trace_id: int,
7172
span_id: int,
7273
name: str,
74+
attributes: Optional[Attributes] = None,
7375
links: Sequence["Link"] = (),
7476
) -> "Decision":
7577
return self._decision
@@ -107,6 +109,7 @@ def should_sample(
107109
trace_id: int,
108110
span_id: int,
109111
name: str,
112+
attributes: Optional[Attributes] = None, # TODO
110113
links: Sequence["Link"] = (),
111114
) -> "Decision":
112115
if parent_context is not None:

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def __init__(
130130
resource: None = None, # TODO
131131
attributes: types.Attributes = None, # TODO
132132
events: Sequence[trace_api.Event] = None, # TODO
133-
links: Sequence[trace_api.Link] = None, # TODO
133+
links: Sequence[trace_api.Link] = (),
134134
kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL,
135135
span_processor: SpanProcessor = SpanProcessor(),
136136
) -> None:
@@ -226,30 +226,6 @@ def add_lazy_event(self, event: trace_api.Event) -> None:
226226
return
227227
self.events.append(event)
228228

229-
def add_link(
230-
self,
231-
link_target_context: "trace_api.SpanContext",
232-
attributes: types.Attributes = None,
233-
) -> None:
234-
if attributes is None:
235-
attributes = (
236-
Span.empty_attributes
237-
) # TODO: empty_attributes is not a Dict. Use Mapping?
238-
self.add_lazy_link(trace_api.Link(link_target_context, attributes))
239-
240-
def add_lazy_link(self, link: "trace_api.Link") -> None:
241-
with self._lock:
242-
if not self.is_recording_events():
243-
return
244-
has_ended = self.end_time is not None
245-
if not has_ended:
246-
if self.links is Span.empty_links:
247-
self.links = BoundedList(MAX_NUM_LINKS)
248-
if has_ended:
249-
logger.warning("Calling add_link() on an ended span.")
250-
return
251-
self.links.append(link)
252-
253229
def start(self, start_time: Optional[int] = None) -> None:
254230
with self._lock:
255231
if not self.is_recording_events():
@@ -343,10 +319,12 @@ def start_span(
343319
name: str,
344320
parent: trace_api.ParentSpan = trace_api.Tracer.CURRENT_SPAN,
345321
kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL,
322+
attributes: Optional[types.Attributes] = None,
323+
links: Sequence[trace_api.Link] = (),
346324
) -> "Span":
347325
"""See `opentelemetry.trace.Tracer.start_span`."""
348326

349-
span = self.create_span(name, parent, kind)
327+
span = self.create_span(name, parent, kind, attributes, links)
350328
span.start()
351329
return span
352330

@@ -355,17 +333,21 @@ def start_as_current_span(
355333
name: str,
356334
parent: trace_api.ParentSpan = trace_api.Tracer.CURRENT_SPAN,
357335
kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL,
336+
attributes: Optional[types.Attributes] = None,
337+
links: Sequence[trace_api.Link] = (),
358338
) -> Iterator[trace_api.Span]:
359339
"""See `opentelemetry.trace.Tracer.start_as_current_span`."""
360340

361-
span = self.start_span(name, parent, kind)
341+
span = self.start_span(name, parent, kind, attributes, links)
362342
return self.use_span(span, end_on_exit=True)
363343

364344
def create_span(
365345
self,
366346
name: str,
367347
parent: trace_api.ParentSpan = trace_api.Tracer.CURRENT_SPAN,
368348
kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL,
349+
attributes: Optional[types.Attributes] = None,
350+
links: Sequence[trace_api.Link] = (),
369351
) -> "trace_api.Span":
370352
"""See `opentelemetry.trace.Tracer.create_span`.
371353
@@ -412,18 +394,26 @@ def create_span(
412394
context.trace_id,
413395
context.span_id,
414396
name,
415-
{}, # TODO: links
397+
attributes,
398+
links,
416399
)
417400

418401
if sampling_decision.sampled:
402+
if attributes is None:
403+
span_attributes = sampling_decision.attributes
404+
else:
405+
# apply sampling decision attributes after initial attributes
406+
span_attributes = attributes.copy()
407+
span_attributes.update(sampling_decision.attributes)
419408
return Span(
420409
name=name,
421410
context=context,
422411
parent=parent,
423412
sampler=self.sampler,
424-
attributes=sampling_decision.attributes,
413+
attributes=span_attributes,
425414
span_processor=self._active_span_processor,
426415
kind=kind,
416+
links=links,
427417
)
428418

429419
return trace_api.DefaultSpan(context=context)

opentelemetry-sdk/tests/trace/test_trace.py

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,46 @@ def test_attributes(self):
248248
self.assertEqual(root.attributes["misc.pi"], 3.14)
249249
self.assertEqual(root.attributes["attr-key"], "attr-value2")
250250

251+
attributes = {
252+
"attr-key": "val",
253+
"attr-key2": "val2",
254+
"attr-in-both": "span-attr",
255+
}
256+
with self.tracer.start_as_current_span(
257+
"root2", attributes=attributes
258+
) as root:
259+
self.assertEqual(len(root.attributes), 3)
260+
self.assertEqual(root.attributes["attr-key"], "val")
261+
self.assertEqual(root.attributes["attr-key2"], "val2")
262+
self.assertEqual(root.attributes["attr-in-both"], "span-attr")
263+
264+
decision_attributes = {
265+
"sampler-attr": "sample-val",
266+
"attr-in-both": "decision-attr",
267+
}
268+
self.tracer.sampler = sampling.StaticSampler(
269+
sampling.Decision(sampled=True, attributes=decision_attributes)
270+
)
271+
272+
with self.tracer.start_as_current_span("root2") as root:
273+
self.assertEqual(len(root.attributes), 2)
274+
self.assertEqual(root.attributes["sampler-attr"], "sample-val")
275+
self.assertEqual(root.attributes["attr-in-both"], "decision-attr")
276+
277+
attributes = {
278+
"attr-key": "val",
279+
"attr-key2": "val2",
280+
"attr-in-both": "span-attr",
281+
}
282+
with self.tracer.start_as_current_span(
283+
"root2", attributes=attributes
284+
) as root:
285+
self.assertEqual(len(root.attributes), 4)
286+
self.assertEqual(root.attributes["attr-key"], "val")
287+
self.assertEqual(root.attributes["attr-key2"], "val2")
288+
self.assertEqual(root.attributes["sampler-attr"], "sample-val")
289+
self.assertEqual(root.attributes["attr-in-both"], "decision-attr")
290+
251291
def test_events(self):
252292
self.assertIsNone(self.tracer.get_current_span())
253293

@@ -297,13 +337,12 @@ def test_links(self):
297337
trace_id=trace.generate_trace_id(),
298338
span_id=trace.generate_span_id(),
299339
)
300-
301-
with self.tracer.start_as_current_span("root") as root:
302-
root.add_link(other_context1)
303-
root.add_link(other_context2, {"name": "neighbor"})
304-
root.add_lazy_link(
305-
trace_api.Link(other_context3, {"component": "http"})
306-
)
340+
links = [
341+
trace_api.Link(other_context1),
342+
trace_api.Link(other_context2, {"name": "neighbor"}),
343+
trace_api.Link(other_context3, {"component": "http"}),
344+
]
345+
with self.tracer.start_as_current_span("root", links=links) as root:
307346

308347
self.assertEqual(len(root.links), 3)
309348
self.assertEqual(
@@ -374,11 +413,6 @@ def test_span_override_start_and_end_time(self):
374413
def test_ended_span(self):
375414
""""Events, attributes are not allowed after span is ended"""
376415

377-
other_context1 = trace_api.SpanContext(
378-
trace_id=trace.generate_trace_id(),
379-
span_id=trace.generate_span_id(),
380-
)
381-
382416
with self.tracer.start_as_current_span("root") as root:
383417
# everything should be empty at the beginning
384418
self.assertEqual(len(root.attributes), 0)
@@ -400,9 +434,6 @@ def test_ended_span(self):
400434
root.add_event("event1")
401435
self.assertEqual(len(root.events), 0)
402436

403-
root.add_link(other_context1)
404-
self.assertEqual(len(root.links), 0)
405-
406437
root.update_name("xxx")
407438
self.assertEqual(root.name, "root")
408439

0 commit comments

Comments
 (0)