Skip to content

Commit

Permalink
sdk/trace: add Exporter interface and SimpleExportSpanProcessor
Browse files Browse the repository at this point in the history
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
mauriciovasquezbernal committed Sep 5, 2019
1 parent 32cf44b commit a599ef0
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
71 changes: 71 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# 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


class SpanExportResult(Enum):
SUCCESS = 0
FAILED_RETRYABLE = 1
FAILED_NOT_RETRYABLE = 2


class SpanExporter:
"""Interface that allows different services to export recorded spans 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):
"""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 # do nothing

def on_end(self, span: Span) -> None:
try:
self.span_exporter.export((span,))
except Exception as exc:
logging.warning("Exception while exporting data: %s", exc)

def shutdown(self) -> None:
self.span_exporter.shutdown()
13 changes: 13 additions & 0 deletions opentelemetry-sdk/tests/trace/export/__init__.py
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.
44 changes: 44 additions & 0 deletions opentelemetry-sdk/tests/trace/export/test_export.py
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)

0 comments on commit a599ef0

Please sign in to comment.