mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-08-02 02:52:18 +08:00
Revert "Rename exporter packages from "ext" to "exporter" (#953)"
This reverts commit b2d3d56e5267dc179eaa20af8280e08803b6fbe0.
This commit is contained in:
@ -1,26 +0,0 @@
|
||||
# Changelog
|
||||
|
||||
## Unreleased
|
||||
|
||||
- Change package name to opentelemetry-exporter-zipkin
|
||||
([#953](https://github.com/open-telemetry/opentelemetry-python/pull/953))
|
||||
|
||||
## 0.8b0
|
||||
|
||||
Released 2020-05-27
|
||||
|
||||
- Transform resource to tags when exporting
|
||||
([#707](https://github.com/open-telemetry/opentelemetry-python/pull/707))
|
||||
|
||||
## 0.7b1
|
||||
|
||||
Released 2020-05-12
|
||||
|
||||
- bugfix: 'debug' field is now correct
|
||||
([#549](https://github.com/open-telemetry/opentelemetry-python/pull/549))
|
||||
|
||||
## 0.4a0
|
||||
|
||||
Released 2020-02-21
|
||||
|
||||
- Initial release
|
@ -1,201 +0,0 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
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.
|
@ -1,9 +0,0 @@
|
||||
graft src
|
||||
graft tests
|
||||
global-exclude *.pyc
|
||||
global-exclude *.pyo
|
||||
global-exclude __pycache__/*
|
||||
include CHANGELOG.md
|
||||
include MANIFEST.in
|
||||
include README.rst
|
||||
include LICENSE
|
@ -1,24 +0,0 @@
|
||||
OpenTelemetry Zipkin Exporter
|
||||
=============================
|
||||
|
||||
|pypi|
|
||||
|
||||
.. |pypi| image:: https://badge.fury.io/py/opentelemetry-exporter-zipkin.svg
|
||||
:target: https://pypi.org/project/opentelemetry-exporter-zipkin/
|
||||
|
||||
This library allows to export tracing data to `Zipkin <https://zipkin.io/>`_.
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
::
|
||||
|
||||
pip install opentelemetry-exporter-zipkin
|
||||
|
||||
|
||||
References
|
||||
----------
|
||||
|
||||
* `OpenTelemetry Zipkin Exporter <https://opentelemetry-python.readthedocs.io/en/latest/exporter/zipkin/zipkin.html>`_
|
||||
* `Zipkin <https://zipkin.io/>`_
|
||||
* `OpenTelemetry Project <https://opentelemetry.io/>`_
|
@ -1,51 +0,0 @@
|
||||
# Copyright The 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.
|
||||
#
|
||||
[metadata]
|
||||
name = opentelemetry-exporter-zipkin
|
||||
description = Zipkin Span Exporter for OpenTelemetry
|
||||
long_description = file: README.rst
|
||||
long_description_content_type = text/x-rst
|
||||
author = OpenTelemetry Authors
|
||||
author_email = cncf-opentelemetry-contributors@lists.cncf.io
|
||||
url = https://github.com/open-telemetry/opentelemetry-python/tree/master/ext/opentelemetry-exporter-zipkin
|
||||
platforms = any
|
||||
license = Apache-2.0
|
||||
classifiers =
|
||||
Development Status :: 4 - Beta
|
||||
Intended Audience :: Developers
|
||||
License :: OSI Approved :: Apache Software License
|
||||
Programming Language :: Python
|
||||
Programming Language :: Python :: 3
|
||||
Programming Language :: Python :: 3.4
|
||||
Programming Language :: Python :: 3.5
|
||||
Programming Language :: Python :: 3.6
|
||||
Programming Language :: Python :: 3.7
|
||||
Programming Language :: Python :: 3.8
|
||||
|
||||
[options]
|
||||
python_requires = >=3.4
|
||||
package_dir=
|
||||
=src
|
||||
packages=find_namespace:
|
||||
install_requires =
|
||||
requests ~= 2.7
|
||||
opentelemetry-api == 0.12.dev0
|
||||
opentelemetry-sdk == 0.12.dev0
|
||||
|
||||
[options.packages.find]
|
||||
where = src
|
||||
|
||||
[options.extras_require]
|
||||
test =
|
@ -1,26 +0,0 @@
|
||||
# Copyright The 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 os
|
||||
|
||||
import setuptools
|
||||
|
||||
BASE_DIR = os.path.dirname(__file__)
|
||||
VERSION_FILENAME = os.path.join(
|
||||
BASE_DIR, "src", "opentelemetry", "exporter", "zipkin", "version.py"
|
||||
)
|
||||
PACKAGE_INFO = {}
|
||||
with open(VERSION_FILENAME) as f:
|
||||
exec(f.read(), PACKAGE_INFO)
|
||||
|
||||
setuptools.setup(version=PACKAGE_INFO["__version__"])
|
@ -1,235 +0,0 @@
|
||||
# Copyright The 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 library allows to export tracing data to `Zipkin <https://zipkin.io/>`_.
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
The **OpenTelemetry Zipkin Exporter** allows to export `OpenTelemetry`_ traces to `Zipkin`_.
|
||||
This exporter always send traces to the configured Zipkin collector using HTTP.
|
||||
|
||||
|
||||
.. _Zipkin: https://zipkin.io/
|
||||
.. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/
|
||||
|
||||
.. code:: python
|
||||
|
||||
from opentelemetry import trace
|
||||
from opentelemetry.exporter import zipkin
|
||||
from opentelemetry.sdk.trace import TracerProvider
|
||||
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
|
||||
|
||||
trace.set_tracer_provider(TracerProvider())
|
||||
tracer = trace.get_tracer(__name__)
|
||||
|
||||
# create a ZipkinSpanExporter
|
||||
zipkin_exporter = zipkin.ZipkinSpanExporter(
|
||||
service_name="my-helloworld-service",
|
||||
# optional:
|
||||
# host_name="localhost",
|
||||
# port=9411,
|
||||
# endpoint="/api/v2/spans",
|
||||
# protocol="http",
|
||||
# ipv4="",
|
||||
# ipv6="",
|
||||
# retry=False,
|
||||
)
|
||||
|
||||
# Create a BatchExportSpanProcessor and add the exporter to it
|
||||
span_processor = BatchExportSpanProcessor(zipkin_exporter)
|
||||
|
||||
# add to the tracer
|
||||
trace.get_tracer_provider().add_span_processor(span_processor)
|
||||
|
||||
with tracer.start_as_current_span("foo"):
|
||||
print("Hello world!")
|
||||
|
||||
API
|
||||
---
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
from typing import Optional, Sequence
|
||||
|
||||
import requests
|
||||
|
||||
from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult
|
||||
from opentelemetry.trace import Span, SpanContext, SpanKind
|
||||
|
||||
DEFAULT_ENDPOINT = "/api/v2/spans"
|
||||
DEFAULT_HOST_NAME = "localhost"
|
||||
DEFAULT_PORT = 9411
|
||||
DEFAULT_PROTOCOL = "http"
|
||||
DEFAULT_RETRY = False
|
||||
ZIPKIN_HEADERS = {"Content-Type": "application/json"}
|
||||
|
||||
SPAN_KIND_MAP = {
|
||||
SpanKind.INTERNAL: None,
|
||||
SpanKind.SERVER: "SERVER",
|
||||
SpanKind.CLIENT: "CLIENT",
|
||||
SpanKind.PRODUCER: "PRODUCER",
|
||||
SpanKind.CONSUMER: "CONSUMER",
|
||||
}
|
||||
|
||||
SUCCESS_STATUS_CODES = (200, 202)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ZipkinSpanExporter(SpanExporter):
|
||||
"""Zipkin span exporter for OpenTelemetry.
|
||||
|
||||
Args:
|
||||
service_name: Service that logged an annotation in a trace.Classifier
|
||||
when query for spans.
|
||||
host_name: The host name of the Zipkin server
|
||||
port: The port of the Zipkin server
|
||||
endpoint: The endpoint of the Zipkin server
|
||||
protocol: The protocol used for the request.
|
||||
ipv4: Primary IPv4 address associated with this connection.
|
||||
ipv6: Primary IPv6 address associated with this connection.
|
||||
retry: Set to True to configure the exporter to retry on failure.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
service_name: str,
|
||||
host_name: str = DEFAULT_HOST_NAME,
|
||||
port: int = DEFAULT_PORT,
|
||||
endpoint: str = DEFAULT_ENDPOINT,
|
||||
protocol: str = DEFAULT_PROTOCOL,
|
||||
ipv4: Optional[str] = None,
|
||||
ipv6: Optional[str] = None,
|
||||
retry: Optional[str] = DEFAULT_RETRY,
|
||||
):
|
||||
self.service_name = service_name
|
||||
self.host_name = host_name
|
||||
self.port = port
|
||||
self.endpoint = endpoint
|
||||
self.protocol = protocol
|
||||
self.url = "{}://{}:{}{}".format(
|
||||
self.protocol, self.host_name, self.port, self.endpoint
|
||||
)
|
||||
self.ipv4 = ipv4
|
||||
self.ipv6 = ipv6
|
||||
self.retry = retry
|
||||
|
||||
def export(self, spans: Sequence[Span]) -> SpanExportResult:
|
||||
zipkin_spans = self._translate_to_zipkin(spans)
|
||||
result = requests.post(
|
||||
url=self.url, data=json.dumps(zipkin_spans), headers=ZIPKIN_HEADERS
|
||||
)
|
||||
|
||||
if result.status_code not in SUCCESS_STATUS_CODES:
|
||||
logger.error(
|
||||
"Traces cannot be uploaded; status code: %s, message %s",
|
||||
result.status_code,
|
||||
result.text,
|
||||
)
|
||||
|
||||
if self.retry:
|
||||
return SpanExportResult.FAILURE
|
||||
return SpanExportResult.FAILURE
|
||||
return SpanExportResult.SUCCESS
|
||||
|
||||
def _translate_to_zipkin(self, spans: Sequence[Span]):
|
||||
|
||||
local_endpoint = {"serviceName": self.service_name, "port": self.port}
|
||||
|
||||
if self.ipv4 is not None:
|
||||
local_endpoint["ipv4"] = self.ipv4
|
||||
|
||||
if self.ipv6 is not None:
|
||||
local_endpoint["ipv6"] = self.ipv6
|
||||
|
||||
zipkin_spans = []
|
||||
for span in spans:
|
||||
context = span.get_context()
|
||||
trace_id = context.trace_id
|
||||
span_id = context.span_id
|
||||
|
||||
# Timestamp in zipkin spans is int of microseconds.
|
||||
# see: https://zipkin.io/pages/instrumenting.html
|
||||
start_timestamp_mus = _nsec_to_usec_round(span.start_time)
|
||||
duration_mus = _nsec_to_usec_round(span.end_time - span.start_time)
|
||||
|
||||
zipkin_span = {
|
||||
"traceId": format(trace_id, "x"),
|
||||
"id": format(span_id, "x"),
|
||||
"name": span.name,
|
||||
"timestamp": start_timestamp_mus,
|
||||
"duration": duration_mus,
|
||||
"localEndpoint": local_endpoint,
|
||||
"kind": SPAN_KIND_MAP[span.kind],
|
||||
"tags": _extract_tags_from_span(span),
|
||||
"annotations": _extract_annotations_from_events(span.events),
|
||||
}
|
||||
|
||||
if context.trace_flags.sampled:
|
||||
zipkin_span["debug"] = True
|
||||
|
||||
if isinstance(span.parent, Span):
|
||||
zipkin_span["parentId"] = format(
|
||||
span.parent.get_context().span_id, "x"
|
||||
)
|
||||
elif isinstance(span.parent, SpanContext):
|
||||
zipkin_span["parentId"] = format(span.parent.span_id, "x")
|
||||
|
||||
zipkin_spans.append(zipkin_span)
|
||||
return zipkin_spans
|
||||
|
||||
def shutdown(self) -> None:
|
||||
pass
|
||||
|
||||
|
||||
def _extract_tags_from_dict(tags_dict):
|
||||
tags = {}
|
||||
if not tags_dict:
|
||||
return tags
|
||||
for attribute_key, attribute_value in tags_dict.items():
|
||||
if isinstance(attribute_value, (int, bool, float)):
|
||||
value = str(attribute_value)
|
||||
elif isinstance(attribute_value, str):
|
||||
value = attribute_value[:128]
|
||||
else:
|
||||
logger.warning("Could not serialize tag %s", attribute_key)
|
||||
continue
|
||||
tags[attribute_key] = value
|
||||
return tags
|
||||
|
||||
|
||||
def _extract_tags_from_span(span: Span):
|
||||
tags = _extract_tags_from_dict(getattr(span, "attributes", None))
|
||||
if span.resource:
|
||||
tags.update(_extract_tags_from_dict(span.resource.labels))
|
||||
return tags
|
||||
|
||||
|
||||
def _extract_annotations_from_events(events):
|
||||
return (
|
||||
[
|
||||
{"timestamp": _nsec_to_usec_round(e.timestamp), "value": e.name}
|
||||
for e in events
|
||||
]
|
||||
if events
|
||||
else None
|
||||
)
|
||||
|
||||
|
||||
def _nsec_to_usec_round(nsec):
|
||||
"""Round nanoseconds to microseconds"""
|
||||
return (nsec + 500) // 10 ** 3
|
@ -1,15 +0,0 @@
|
||||
# Copyright The 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.
|
||||
|
||||
__version__ = "0.12.dev0"
|
@ -1,13 +0,0 @@
|
||||
# Copyright The 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.
|
@ -1,273 +0,0 @@
|
||||
# Copyright The 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 json
|
||||
import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from opentelemetry import trace as trace_api
|
||||
from opentelemetry.exporter.zipkin import ZipkinSpanExporter
|
||||
from opentelemetry.sdk import trace
|
||||
from opentelemetry.sdk.trace import Resource
|
||||
from opentelemetry.sdk.trace.export import SpanExportResult
|
||||
from opentelemetry.trace import TraceFlags
|
||||
|
||||
|
||||
class MockResponse:
|
||||
def __init__(self, status_code):
|
||||
self.status_code = status_code
|
||||
self.text = status_code
|
||||
|
||||
|
||||
class TestZipkinSpanExporter(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# create and save span to be used in tests
|
||||
context = trace_api.SpanContext(
|
||||
trace_id=0x000000000000000000000000DEADBEEF,
|
||||
span_id=0x00000000DEADBEF0,
|
||||
is_remote=False,
|
||||
)
|
||||
|
||||
self._test_span = trace.Span("test_span", context=context)
|
||||
self._test_span.start()
|
||||
self._test_span.end()
|
||||
|
||||
def test_constructor_default(self):
|
||||
"""Test the default values assigned by constructor."""
|
||||
service_name = "my-service-name"
|
||||
host_name = "localhost"
|
||||
port = 9411
|
||||
endpoint = "/api/v2/spans"
|
||||
exporter = ZipkinSpanExporter(service_name)
|
||||
ipv4 = None
|
||||
ipv6 = None
|
||||
protocol = "http"
|
||||
url = "http://localhost:9411/api/v2/spans"
|
||||
|
||||
self.assertEqual(exporter.service_name, service_name)
|
||||
self.assertEqual(exporter.host_name, host_name)
|
||||
self.assertEqual(exporter.port, port)
|
||||
self.assertEqual(exporter.endpoint, endpoint)
|
||||
self.assertEqual(exporter.ipv4, ipv4)
|
||||
self.assertEqual(exporter.ipv6, ipv6)
|
||||
self.assertEqual(exporter.protocol, protocol)
|
||||
self.assertEqual(exporter.url, url)
|
||||
|
||||
def test_constructor_explicit(self):
|
||||
"""Test the constructor passing all the options."""
|
||||
service_name = "my-opentelemetry-zipkin"
|
||||
host_name = "opentelemetry.io"
|
||||
port = 15875
|
||||
endpoint = "/myapi/traces?format=zipkin"
|
||||
ipv4 = "1.2.3.4"
|
||||
ipv6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
|
||||
protocol = "https"
|
||||
url = "https://opentelemetry.io:15875/myapi/traces?format=zipkin"
|
||||
exporter = ZipkinSpanExporter(
|
||||
service_name=service_name,
|
||||
host_name=host_name,
|
||||
port=port,
|
||||
endpoint=endpoint,
|
||||
ipv4=ipv4,
|
||||
ipv6=ipv6,
|
||||
protocol=protocol,
|
||||
)
|
||||
|
||||
self.assertEqual(exporter.service_name, service_name)
|
||||
self.assertEqual(exporter.host_name, host_name)
|
||||
self.assertEqual(exporter.port, port)
|
||||
self.assertEqual(exporter.endpoint, endpoint)
|
||||
self.assertEqual(exporter.ipv4, ipv4)
|
||||
self.assertEqual(exporter.ipv6, ipv6)
|
||||
self.assertEqual(exporter.protocol, protocol)
|
||||
self.assertEqual(exporter.url, url)
|
||||
|
||||
# pylint: disable=too-many-locals
|
||||
def test_export(self):
|
||||
|
||||
span_names = ("test1", "test2", "test3", "test4")
|
||||
trace_id = 0x6E0C63257DE34C926F9EFCD03927272E
|
||||
span_id = 0x34BF92DEEFC58C92
|
||||
parent_id = 0x1111111111111111
|
||||
other_id = 0x2222222222222222
|
||||
|
||||
base_time = 683647322 * 10 ** 9 # in ns
|
||||
start_times = (
|
||||
base_time,
|
||||
base_time + 150 * 10 ** 6,
|
||||
base_time + 300 * 10 ** 6,
|
||||
base_time + 400 * 10 ** 6,
|
||||
)
|
||||
durations = (50 * 10 ** 6, 100 * 10 ** 6, 200 * 10 ** 6, 300 * 10 ** 6)
|
||||
end_times = (
|
||||
start_times[0] + durations[0],
|
||||
start_times[1] + durations[1],
|
||||
start_times[2] + durations[2],
|
||||
start_times[3] + durations[3],
|
||||
)
|
||||
|
||||
span_context = trace_api.SpanContext(
|
||||
trace_id,
|
||||
span_id,
|
||||
is_remote=False,
|
||||
trace_flags=TraceFlags(TraceFlags.SAMPLED),
|
||||
)
|
||||
parent_context = trace_api.SpanContext(
|
||||
trace_id, parent_id, is_remote=False
|
||||
)
|
||||
other_context = trace_api.SpanContext(
|
||||
trace_id, other_id, is_remote=False
|
||||
)
|
||||
|
||||
event_attributes = {
|
||||
"annotation_bool": True,
|
||||
"annotation_string": "annotation_test",
|
||||
"key_float": 0.3,
|
||||
}
|
||||
|
||||
event_timestamp = base_time + 50 * 10 ** 6
|
||||
event = trace.Event(
|
||||
name="event0",
|
||||
timestamp=event_timestamp,
|
||||
attributes=event_attributes,
|
||||
)
|
||||
|
||||
link_attributes = {"key_bool": True}
|
||||
|
||||
link = trace_api.Link(
|
||||
context=other_context, attributes=link_attributes
|
||||
)
|
||||
|
||||
otel_spans = [
|
||||
trace.Span(
|
||||
name=span_names[0],
|
||||
context=span_context,
|
||||
parent=parent_context,
|
||||
events=(event,),
|
||||
links=(link,),
|
||||
),
|
||||
trace.Span(
|
||||
name=span_names[1], context=parent_context, parent=None
|
||||
),
|
||||
trace.Span(name=span_names[2], context=other_context, parent=None),
|
||||
trace.Span(name=span_names[3], context=other_context, parent=None),
|
||||
]
|
||||
|
||||
otel_spans[0].start(start_time=start_times[0])
|
||||
# added here to preserve order
|
||||
otel_spans[0].set_attribute("key_bool", False)
|
||||
otel_spans[0].set_attribute("key_string", "hello_world")
|
||||
otel_spans[0].set_attribute("key_float", 111.22)
|
||||
otel_spans[0].end(end_time=end_times[0])
|
||||
|
||||
otel_spans[1].start(start_time=start_times[1])
|
||||
otel_spans[1].resource = Resource(
|
||||
labels={"key_resource": "some_resource"}
|
||||
)
|
||||
otel_spans[1].end(end_time=end_times[1])
|
||||
|
||||
otel_spans[2].start(start_time=start_times[2])
|
||||
otel_spans[2].set_attribute("key_string", "hello_world")
|
||||
otel_spans[2].resource = Resource(
|
||||
labels={"key_resource": "some_resource"}
|
||||
)
|
||||
otel_spans[2].end(end_time=end_times[2])
|
||||
|
||||
otel_spans[3].start(start_time=start_times[3])
|
||||
otel_spans[3].end(end_time=end_times[3])
|
||||
|
||||
service_name = "test-service"
|
||||
local_endpoint = {"serviceName": service_name, "port": 9411}
|
||||
|
||||
exporter = ZipkinSpanExporter(service_name)
|
||||
expected = [
|
||||
{
|
||||
"traceId": format(trace_id, "x"),
|
||||
"id": format(span_id, "x"),
|
||||
"name": span_names[0],
|
||||
"timestamp": start_times[0] // 10 ** 3,
|
||||
"duration": durations[0] // 10 ** 3,
|
||||
"localEndpoint": local_endpoint,
|
||||
"kind": None,
|
||||
"tags": {
|
||||
"key_bool": "False",
|
||||
"key_string": "hello_world",
|
||||
"key_float": "111.22",
|
||||
},
|
||||
"annotations": [
|
||||
{
|
||||
"timestamp": event_timestamp // 10 ** 3,
|
||||
"value": "event0",
|
||||
}
|
||||
],
|
||||
"debug": True,
|
||||
"parentId": format(parent_id, "x"),
|
||||
},
|
||||
{
|
||||
"traceId": format(trace_id, "x"),
|
||||
"id": format(parent_id, "x"),
|
||||
"name": span_names[1],
|
||||
"timestamp": start_times[1] // 10 ** 3,
|
||||
"duration": durations[1] // 10 ** 3,
|
||||
"localEndpoint": local_endpoint,
|
||||
"kind": None,
|
||||
"tags": {"key_resource": "some_resource"},
|
||||
"annotations": None,
|
||||
},
|
||||
{
|
||||
"traceId": format(trace_id, "x"),
|
||||
"id": format(other_id, "x"),
|
||||
"name": span_names[2],
|
||||
"timestamp": start_times[2] // 10 ** 3,
|
||||
"duration": durations[2] // 10 ** 3,
|
||||
"localEndpoint": local_endpoint,
|
||||
"kind": None,
|
||||
"tags": {
|
||||
"key_string": "hello_world",
|
||||
"key_resource": "some_resource",
|
||||
},
|
||||
"annotations": None,
|
||||
},
|
||||
{
|
||||
"traceId": format(trace_id, "x"),
|
||||
"id": format(other_id, "x"),
|
||||
"name": span_names[3],
|
||||
"timestamp": start_times[3] // 10 ** 3,
|
||||
"duration": durations[3] // 10 ** 3,
|
||||
"localEndpoint": local_endpoint,
|
||||
"kind": None,
|
||||
"tags": {},
|
||||
"annotations": None,
|
||||
},
|
||||
]
|
||||
|
||||
mock_post = MagicMock()
|
||||
with patch("requests.post", mock_post):
|
||||
mock_post.return_value = MockResponse(200)
|
||||
status = exporter.export(otel_spans)
|
||||
self.assertEqual(SpanExportResult.SUCCESS, status)
|
||||
|
||||
mock_post.assert_called_with(
|
||||
url="http://localhost:9411/api/v2/spans",
|
||||
data=json.dumps(expected),
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
|
||||
@patch("requests.post")
|
||||
def test_invalid_response(self, mock_post):
|
||||
mock_post.return_value = MockResponse(404)
|
||||
spans = []
|
||||
exporter = ZipkinSpanExporter("test-service")
|
||||
status = exporter.export(spans)
|
||||
self.assertEqual(SpanExportResult.FAILURE, status)
|
Reference in New Issue
Block a user