Skip to content

Commit 2d27e21

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 5ca9e06 commit 2d27e21

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 logging
16+
import typing
17+
from enum import Enum
18+
19+
from .. import Span, SpanProcessor
20+
21+
logger = logging.getLogger(__name__)
22+
23+
24+
class SpanExportResult(Enum):
25+
SUCCESS = 0
26+
FAILED_RETRYABLE = 1
27+
FAILED_NOT_RETRYABLE = 2
28+
29+
30+
class SpanExporter:
31+
"""Interface for exporting spans.
32+
33+
Interface to be implemented by services that want to export recorded in
34+
its own format.
35+
36+
To export data this MUST be registered to the :class`..Tracer` using a
37+
`SimpleExportSpanProcessor` or a `BatchSpanProcessor`.
38+
"""
39+
40+
def export(self, spans: typing.Sequence[Span]) -> "SpanExportResult":
41+
"""Exports a batch of telemetry data.
42+
43+
Args:
44+
spans: The list of `Span`s to be exported
45+
46+
Returns:
47+
The result of the export
48+
"""
49+
50+
def shutdown(self) -> None:
51+
"""Shuts down the exporter.
52+
53+
Called when the SDK is shut down.
54+
"""
55+
56+
57+
class SimpleExportSpanProcessor(SpanProcessor):
58+
"""Simple SpanProcessor implementation.
59+
60+
SimpleExportSpanProcessor is an implementation of `SpanProcessor` that
61+
passes ended spans directly to the configured `SpanExporter`.
62+
"""
63+
64+
def __init__(self, span_exporter: SpanExporter):
65+
self.span_exporter = span_exporter
66+
67+
def on_start(self, span: Span) -> None:
68+
pass
69+
70+
def on_end(self, span: Span) -> None:
71+
try:
72+
self.span_exporter.export((span,))
73+
except Exception as exc:
74+
logger.warning("Exception while exporting data: %s", exc)
75+
76+
def shutdown(self) -> None:
77+
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)