-
Notifications
You must be signed in to change notification settings - Fork 609
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
5ca9e06
commit 2d27e21
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Copyright 2019, OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import logging | ||
import typing | ||
from enum import Enum | ||
|
||
from .. import Span, SpanProcessor | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class SpanExportResult(Enum): | ||
SUCCESS = 0 | ||
FAILED_RETRYABLE = 1 | ||
FAILED_NOT_RETRYABLE = 2 | ||
|
||
|
||
class SpanExporter: | ||
"""Interface for exporting spans. | ||
Interface to be implemented by services that want to export recorded in | ||
its own format. | ||
To export data this MUST be registered to the :class`..Tracer` using a | ||
`SimpleExportSpanProcessor` or a `BatchSpanProcessor`. | ||
""" | ||
|
||
def export(self, spans: typing.Sequence[Span]) -> "SpanExportResult": | ||
"""Exports a batch of telemetry data. | ||
Args: | ||
spans: The list of `Span`s to be exported | ||
Returns: | ||
The result of the export | ||
""" | ||
|
||
def shutdown(self) -> None: | ||
"""Shuts down the exporter. | ||
Called when the SDK is shut down. | ||
""" | ||
|
||
|
||
class SimpleExportSpanProcessor(SpanProcessor): | ||
"""Simple SpanProcessor implementation. | ||
SimpleExportSpanProcessor is an implementation of `SpanProcessor` that | ||
passes ended spans directly to the configured `SpanExporter`. | ||
""" | ||
|
||
def __init__(self, span_exporter: SpanExporter): | ||
self.span_exporter = span_exporter | ||
|
||
def on_start(self, span: Span) -> None: | ||
pass | ||
|
||
def on_end(self, span: Span) -> None: | ||
try: | ||
self.span_exporter.export((span,)) | ||
except Exception as exc: | ||
logger.warning("Exception while exporting data: %s", exc) | ||
|
||
def shutdown(self) -> None: | ||
self.span_exporter.shutdown() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 2019, OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright 2019, OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest | ||
|
||
from opentelemetry.sdk import trace | ||
from opentelemetry.sdk.trace import export | ||
|
||
|
||
class TestSimpleExportSpanProcessor(unittest.TestCase): | ||
def test_simple_span_processor(self): | ||
class MySpanExporter(export.SpanExporter): | ||
def __init__(self, destination): | ||
self.destination = destination | ||
|
||
def export(self, spans: trace.Span) -> export.SpanExportResult: | ||
self.destination.extend(span.name for span in spans) | ||
return export.SpanExportResult.SUCCESS | ||
|
||
tracer = trace.Tracer() | ||
|
||
spans_names_list = [] | ||
|
||
my_exporter = MySpanExporter(destination=spans_names_list) | ||
span_processor = export.SimpleExportSpanProcessor(my_exporter) | ||
tracer.add_span_processor(span_processor) | ||
|
||
with tracer.start_span("foo"): | ||
with tracer.start_span("bar"): | ||
with tracer.start_span("xxx"): | ||
pass | ||
|
||
self.assertListEqual(["xxx", "bar", "foo"], spans_names_list) |