Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dbb3be8

Browse files
Oberon00reyang
authored andcommitted
Add SpanKind option at span creation (closes open-telemetry#103). (open-telemetry#139)
1 parent b6e97fc commit dbb3be8

File tree

7 files changed

+62
-11
lines changed

7 files changed

+62
-11
lines changed

ext/opentelemetry-ext-http-requests/src/opentelemetry/ext/http_requests/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
from requests.sessions import Session
2424

25+
from opentelemetry.trace import SpanKind
26+
2527

2628
# NOTE: Currently we force passing a tracer. But in turn, this forces the user
2729
# to configure a SDK before enabling this integration. In turn, this means that
@@ -61,11 +63,8 @@ def instrumented_request(self, method, url, *args, **kwargs):
6163
path = "<URL parses to None>"
6264
path = parsed_url.path
6365

64-
with tracer.start_span(path) as span:
66+
with tracer.start_span(path, kind=SpanKind.CLIENT) as span:
6567
span.set_attribute("component", "http")
66-
# TODO: The OTel spec says "SpanKind" MUST be "Client" but that
67-
# seems to be a leftover, as Spans have no explicit field for
68-
# kind.
6968
span.set_attribute("http.method", method.upper())
7069
span.set_attribute("http.url", url)
7170

ext/opentelemetry-ext-http-requests/tests/test_requests_integration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ def test_basic(self):
5656
url = "https://www.example.org/foo/bar?x=y#top"
5757
requests.get(url=url)
5858
self.assertEqual(1, len(self.send.call_args_list))
59-
self.tracer.start_span.assert_called_with("/foo/bar")
59+
self.tracer.start_span.assert_called_with(
60+
"/foo/bar", kind=trace.SpanKind.CLIENT
61+
)
6062
self.span_context_manager.__enter__.assert_called_with()
6163
self.span_context_manager.__exit__.assert_called_with(None, None, None)
6264
self.assertEqual(

ext/opentelemetry-ext-wsgi/src/opentelemetry/ext/wsgi/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def __call__(self, environ, start_response):
8888
tracer = trace.tracer()
8989
path_info = environ["PATH_INFO"] or "/"
9090

91-
with tracer.start_span(path_info) as span:
91+
with tracer.start_span(path_info, kind=trace.SpanKind.SERVER) as span:
9292
self._add_request_attributes(span, environ)
9393
start_response = self._create_start_response(span, start_response)
9494

ext/opentelemetry-ext-wsgi/tests/test_wsgi_middleware.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ def validate_response(self, response, error=None):
125125
self.assertIsNone(self.exc_info)
126126

127127
# Verify that start_span has been called
128-
self.start_span.assert_called_once_with("/")
128+
self.start_span.assert_called_once_with(
129+
"/", kind=trace_api.SpanKind.SERVER
130+
)
129131

130132
def test_basic_wsgi_call(self):
131133
app = OpenTelemetryMiddleware(simple_wsgi)

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
.. versionadded:: 0.1.0
6262
"""
6363

64+
import enum
6465
import typing
6566
from contextlib import contextmanager
6667

@@ -111,6 +112,33 @@ def timestamp(self) -> int:
111112
return self._timestamp
112113

113114

115+
class SpanKind(enum.Enum):
116+
"""Specifies additional details on how this span relates to its parent span.
117+
118+
Note that this enumeration is experimental and likely to change. See
119+
https://github.com/open-telemetry/opentelemetry-specification/pull/226.
120+
"""
121+
122+
#: Default value. Indicates that the span is used internally in the application.
123+
INTERNAL = 0
124+
125+
#: Indicates that the span describes an operation that handles a remote request.
126+
SERVER = 1
127+
128+
#: Indicates that the span describes a request to some remote service.
129+
CLIENT = 2
130+
131+
#: Indicates that the span describes a producer sending a message to a
132+
#: broker. Unlike client and server, there is usually no direct critical
133+
#: path latency relationship between producer and consumer spans.
134+
PRODUCER = 3
135+
136+
#: Indicates that the span describes consumer receiving a message from a
137+
#: broker. Unlike client and server, there is usually no direct critical
138+
#: path latency relationship between producer and consumer spans.
139+
CONSUMER = 4
140+
141+
114142
class Span:
115143
"""A span represents a single operation within a trace."""
116144

@@ -350,7 +378,10 @@ def get_current_span(self) -> "Span":
350378

351379
@contextmanager # type: ignore
352380
def start_span(
353-
self, name: str, parent: ParentSpan = CURRENT_SPAN
381+
self,
382+
name: str,
383+
parent: ParentSpan = CURRENT_SPAN,
384+
kind: SpanKind = SpanKind.INTERNAL,
354385
) -> typing.Iterator["Span"]:
355386
"""Context manager for span creation.
356387
@@ -390,6 +421,8 @@ def start_span(
390421
Args:
391422
name: The name of the span to be created.
392423
parent: The span's parent. Defaults to the current span.
424+
kind: The span's kind (relationship to parent). Note that is
425+
meaningful even if there is no parent.
393426
394427
Yields:
395428
The newly-created span.
@@ -398,7 +431,10 @@ def start_span(
398431
yield INVALID_SPAN
399432

400433
def create_span(
401-
self, name: str, parent: ParentSpan = CURRENT_SPAN
434+
self,
435+
name: str,
436+
parent: ParentSpan = CURRENT_SPAN,
437+
kind: SpanKind = SpanKind.INTERNAL,
402438
) -> "Span":
403439
"""Creates a span.
404440
@@ -426,6 +462,8 @@ def create_span(
426462
Args:
427463
name: The name of the span to be created.
428464
parent: The span's parent. Defaults to the current span.
465+
kind: The span's kind (relationship to parent). Note that is
466+
meaningful even if there is no parent.
429467
430468
Returns:
431469
The newly-created span.

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ def __init__(
241241
attributes: types.Attributes = None, # TODO
242242
events: typing.Sequence[trace_api.Event] = None, # TODO
243243
links: typing.Sequence[trace_api.Link] = None, # TODO
244+
kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL,
244245
span_processor: SpanProcessor = SpanProcessor(),
245246
) -> None:
246247

@@ -253,6 +254,8 @@ def __init__(
253254
self.attributes = attributes
254255
self.events = events
255256
self.links = links
257+
self.kind = kind
258+
256259
self.span_processor = span_processor
257260
self._lock = threading.Lock()
258261

@@ -417,15 +420,17 @@ def start_span(
417420
self,
418421
name: str,
419422
parent: trace_api.ParentSpan = trace_api.Tracer.CURRENT_SPAN,
423+
kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL,
420424
) -> typing.Iterator["Span"]:
421425
"""See `opentelemetry.trace.Tracer.start_span`."""
422-
with self.use_span(self.create_span(name, parent)) as span:
426+
with self.use_span(self.create_span(name, parent, kind)) as span:
423427
yield span
424428

425429
def create_span(
426430
self,
427431
name: str,
428432
parent: trace_api.ParentSpan = trace_api.Tracer.CURRENT_SPAN,
433+
kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL,
429434
) -> "Span":
430435
"""See `opentelemetry.trace.Tracer.create_span`."""
431436
span_id = generate_span_id()
@@ -451,6 +456,7 @@ def create_span(
451456
context=context,
452457
parent=parent,
453458
span_processor=self._active_span_processor,
459+
kind=kind,
454460
)
455461

456462
@contextmanager

opentelemetry-sdk/tests/trace/test_trace.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ def test_start_span_implicit(self):
3636

3737
self.assertIsNotNone(root.start_time)
3838
self.assertIsNone(root.end_time)
39+
self.assertEqual(root.kind, trace_api.SpanKind.INTERNAL)
3940

40-
with tracer.start_span("child") as child:
41+
with tracer.start_span(
42+
"child", kind=trace_api.SpanKind.CLIENT
43+
) as child:
4144
self.assertIs(tracer.get_current_span(), child)
4245
self.assertIs(child.parent, root)
46+
self.assertEqual(child.kind, trace_api.SpanKind.CLIENT)
4347

4448
self.assertIsNotNone(child.start_time)
4549
self.assertIsNone(child.end_time)

0 commit comments

Comments
 (0)