Skip to content

Commit 746d056

Browse files
sdk/trace: add Exporter interface and SimpleExportSpanProcessor
Exporter is an interface that allows different services to export recorded spans in its own format. SimpleExportSpanProcessor is an implementation of SpanProcessor that passes ended spans directly to a configured Exporter. The current interface for exporters directly receives an SDK Span, it could be improved in the future to receive a different object containing a representation of the Span.
1 parent 974a148 commit 746d056

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
import sys
16+
import typing
17+
from enum import Enum
18+
19+
from .. import Span, SpanProcessor
20+
21+
22+
class SpanExportResult(Enum):
23+
SUCCESS = 0
24+
FAILED_RETRYABLE = 1
25+
FAILED_NONE_RETRYABLE = 2
26+
27+
28+
class SpanExporter:
29+
"""Interface that allows different services to export recorded spans in
30+
its own format.
31+
32+
To export data this MUST be registered to the :class`..Tracer` using a
33+
`SimpleExportSpanProcessor` or a `BatchSpanProcessor`.
34+
"""
35+
36+
def export(self, spans: typing.Sequence[Span]) -> "SpanExportResult":
37+
"""Exports a batch of telemetry data.
38+
39+
Args:
40+
spans: The list of `Span`s to be exported
41+
42+
Returns:
43+
The result of the export
44+
"""
45+
46+
def shutdown(self) -> None:
47+
"""Shuts down the exporter.
48+
49+
Called when the SDK is shut down.
50+
"""
51+
52+
53+
class SimpleExportSpanProcessor(SpanProcessor):
54+
"""An implementation of `SpanProcessor` that passes ended spans directly
55+
to the configured `SpanExporter`.
56+
"""
57+
58+
def __init__(self, span_exporter: SpanExporter):
59+
self.span_exporter = span_exporter
60+
61+
def on_start(self, span: Span) -> None:
62+
pass # do nothing
63+
64+
def on_end(self, span: Span) -> None:
65+
try:
66+
self.span_exporter.export((span,))
67+
except Exception as exc:
68+
print(
69+
"Exception while exporting data: {}".format(exc),
70+
file=sys.stderr,
71+
)
72+
73+
def shutdown(self) -> None:
74+
self.span_exporter.shutdown()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
import unittest
16+
17+
from opentelemetry.sdk import trace
18+
from opentelemetry.sdk.trace import export
19+
20+
21+
class TestSimpleExportSpanProcessor(unittest.TestCase):
22+
def test_simple_span_processor(self):
23+
class MySpanExporter(export.SpanExporter):
24+
def __init__(self, destination):
25+
self.destination = destination
26+
27+
def export(self, spans: trace.Span) -> export.SpanExportResult:
28+
self.destination.extend(span.name for span in spans)
29+
return export.SpanExportResult.SUCCESS
30+
31+
tracer = trace.Tracer()
32+
33+
spans_names_list = []
34+
35+
my_exporter = MySpanExporter(destination=spans_names_list)
36+
span_processor = export.SimpleExportSpanProcessor(my_exporter)
37+
tracer.add_span_processor(span_processor)
38+
39+
with tracer.start_span("foo"):
40+
with tracer.start_span("bar"):
41+
with tracer.start_span("xxx"):
42+
pass
43+
44+
self.assertListEqual(["xxx", "bar", "foo"], spans_names_list)

0 commit comments

Comments
 (0)