Skip to content

Commit fe4e2d9

Browse files
committed
Add initial tracer API
1 parent 2307b47 commit fe4e2d9

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed

opentelemetry/__init__.py

Whitespace-only changes.

opentelemetry/api/__init__.py

Whitespace-only changes.

opentelemetry/api/trace/__init__.py

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
# Copyright 2019, OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
The OpenTelemetry tracing API describes the classes used to generate
17+
distributed traces.
18+
19+
The :class:`.Tracer` class controls access to the execution context, and
20+
manages span creation. Each operation in a trace is represented by a
21+
:class:`.Span`, which records the start, end time, and metadata associated with
22+
the operation.
23+
24+
This module provides abstract (i.e. unimplemented) classes required for
25+
tracing, and a concrete no-op ``BlankSpan`` that allows applications to use the
26+
API package alone without a supporting implementation.
27+
28+
The tracer supports implicit and explicit context propagation. By default spans
29+
are created as children of the currently active span, and the newly-created
30+
span becomes the new active span::
31+
32+
from opentelemetry.sdk.trace import tracer
33+
34+
# Create a new root span, set it as the current span in context
35+
with tracer.span("parent"):
36+
# Attach a new child and update the current span
37+
with tracer.span("child"):
38+
do_work():
39+
# Close child span, set parent as current
40+
# Close parent span, set default span as current
41+
42+
Under explicit context propagation there is no concept of an active span, and
43+
the caller is responsible for managing the span's lifetime::
44+
45+
from opentelemetry.sdk.trace import tracer
46+
from your.integration import deserialize_span
47+
48+
parent = deserialize_span(serialized_span)
49+
# Explicit parent span assignment
50+
with tracer.span("child", parent=parent) as child:
51+
do_work(span=child)
52+
53+
Applications should generally use a single global tracer, and use either
54+
implicit or explicit context propagation consistently throughout.
55+
56+
.. versionadded:: 0.1.0
57+
"""
58+
59+
from __future__ import annotations
60+
61+
from typing import Iterator
62+
from contextlib import contextmanager
63+
64+
65+
class Tracer(object):
66+
"""Handles span creation and in-process context propagation.
67+
68+
This class provides methods for manipulating the context, creating spans,
69+
and controlling spans' lifecycles.
70+
"""
71+
72+
def get_current_span(self) -> Span:
73+
"""Get the currently active span from the context.
74+
75+
Returns:
76+
The currently active :class:`.Span`.
77+
"""
78+
raise NotImplementedError
79+
80+
@contextmanager
81+
def span(self, name: str, parent: Span=None) -> Iterator[None]:
82+
"""Context manager for span creation.
83+
84+
Create a new child of the current span, or create a root span if no
85+
current span exists. Start the span and set it as the current span in
86+
this tracer's context.
87+
88+
On exiting the context manager stop the span and set its parent as the
89+
current span.
90+
91+
Example::
92+
93+
with tracer.span("one") as parent:
94+
parent.add_event("parent's event")
95+
with tracer.span("two") as child:
96+
child.add_event("child's event")
97+
tracer.get_current_span() # returns child
98+
tracer.get_current_span() # returns parent
99+
tracer.get_current_span() # returns the previously active span
100+
101+
Args:
102+
name: The name of the span to be created.
103+
parent: This span's parent.
104+
105+
Raises:
106+
ValueError: if ``name`` is null.
107+
"""
108+
raise NotImplementedError
109+
110+
def create_span(self, name: str, parent: Span=None) -> Span:
111+
"""Create a new span as a child of the currently active span.
112+
113+
If called with ``parent`` this method won't affect the tracer's
114+
context.
115+
116+
See ``span`` for a context manager that controls the span's lifetime.
117+
118+
Args:
119+
name: The name of the span to be created.
120+
parent: This span's parent.
121+
122+
Raises:
123+
ValueError: if ``name`` is null.
124+
"""
125+
raise NotImplementedError
126+
127+
128+
class Span(object):
129+
"""A span represents a single operation within a trace.
130+
"""
131+
132+
def start(self) -> None:
133+
"""Set the current time as the span's start time.
134+
135+
Each span represents a single operation. The span's start time is the
136+
wall time at which the operation started.
137+
138+
Only the first call to ``start`` should modify the span, and
139+
implementations are free to ignore or raise on further calls.
140+
"""
141+
raise NotImplementedError
142+
143+
def end(self) -> None:
144+
"""Set the current time as the span's end time.
145+
146+
The span's end time is the wall time at which the operation finished.
147+
148+
Only the first call to ``end`` should modify the span, and
149+
implementations are free to ignore or raise on further calls.
150+
"""
151+
raise NotImplementedError
152+
153+
def get_context(self) -> SpanContext:
154+
"""Get an immutable snapshot of this span.
155+
156+
Returns:
157+
A :class:`.SpanContext` with a copy of this span's immutable state.
158+
"""
159+
raise NotImplementedError
160+
161+
162+
class SpanContext(object):
163+
"""The state of a Span to propagate between processes.
164+
165+
This class includes the immutable attributes of a :class:`.Span` that must
166+
be propagated to a span's children and across process boundaries.
167+
168+
Args:
169+
trace_id: The ID of the trace that this span belongs to.
170+
span_id: This span's ID.
171+
options: Global trace options to propagate.
172+
state: Global tracing-system-specific info to propagate.
173+
"""
174+
175+
def __init__(self,
176+
trace_id: str,
177+
span_id: str,
178+
options: TraceOptions,
179+
state: TraceState) -> SpanContext:
180+
pass
181+
182+
183+
# TODO
184+
class TraceOptions(int):
185+
pass
186+
187+
188+
# TODO
189+
class TraceState(dict):
190+
pass

0 commit comments

Comments
 (0)