From 8c4fca5c754bb98fcb0101e5865e5b7e70ecf659 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Thu, 30 Jul 2020 08:56:11 -0600 Subject: [PATCH 1/2] Update environment variable names (#904) --- docs/examples/django/README.rst | 2 +- ext/opentelemetry-ext-django/CHANGELOG.md | 2 + ext/opentelemetry-ext-django/README.rst | 4 +- .../tests/conftest.py | 2 +- .../CHANGELOG.md | 4 +- .../ext/elasticsearch/__init__.py | 5 +-- .../tests/test_elasticsearch.py | 2 +- ext/opentelemetry-ext-flask/CHANGELOG.md | 4 +- ext/opentelemetry-ext-flask/README.rst | 4 +- ext/opentelemetry-ext-pyramid/CHANGELOG.md | 6 +-- ext/opentelemetry-ext-pyramid/README.rst | 4 +- opentelemetry-api/CHANGELOG.md | 5 ++- .../opentelemetry/configuration/__init__.py | 37 ++++++++++--------- .../src/opentelemetry/context/__init__.py | 2 +- .../tests/configuration/test_configuration.py | 30 +++++++-------- opentelemetry-sdk/CHANGELOG.md | 5 ++- opentelemetry-sdk/tests/conftest.py | 6 +-- 17 files changed, 63 insertions(+), 61 deletions(-) diff --git a/docs/examples/django/README.rst b/docs/examples/django/README.rst index e8a043684f6..6f441dd333d 100644 --- a/docs/examples/django/README.rst +++ b/docs/examples/django/README.rst @@ -36,7 +36,7 @@ Execution of the Django app Set these environment variables first: -#. ``export OPENTELEMETRY_PYTHON_DJANGO_INSTRUMENT=True`` +#. ``export OTEL_PYTHON_DJANGO_INSTRUMENT=True`` #. ``export DJANGO_SETTINGS_MODULE=instrumentation_example.settings`` The way to achieve OpenTelemetry instrumentation for your Django app is to use diff --git a/ext/opentelemetry-ext-django/CHANGELOG.md b/ext/opentelemetry-ext-django/CHANGELOG.md index 6e3cc9c9827..0328ccaa20a 100644 --- a/ext/opentelemetry-ext-django/CHANGELOG.md +++ b/ext/opentelemetry-ext-django/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Update environment variable names, prefix changed from `OPENTELEMETRY` to `OTEL` ([#904](https://github.com/open-telemetry/opentelemetry-python/pull/904)) + ## Version 0.11b0 Released 2020-07-28 diff --git a/ext/opentelemetry-ext-django/README.rst b/ext/opentelemetry-ext-django/README.rst index 1759cb6602a..b7a822f1979 100644 --- a/ext/opentelemetry-ext-django/README.rst +++ b/ext/opentelemetry-ext-django/README.rst @@ -20,13 +20,13 @@ Configuration Exclude lists ************* -To exclude certain URLs from being tracked, set the environment variable ``OPENTELEMETRY_PYTHON_DJANGO_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude. +To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_DJANGO_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude. For example, :: - export OPENTELEMETRY_PYTHON_DJANGO_EXCLUDED_URLS="client/.*/info,healthcheck" + export OTEL_PYTHON_DJANGO_EXCLUDED_URLS="client/.*/info,healthcheck" will exclude requests such as ``https://site/client/123/info`` and ``https://site/xyz/healthcheck``. diff --git a/ext/opentelemetry-ext-django/tests/conftest.py b/ext/opentelemetry-ext-django/tests/conftest.py index b2b39bc049a..8797bfc3060 100644 --- a/ext/opentelemetry-ext-django/tests/conftest.py +++ b/ext/opentelemetry-ext-django/tests/conftest.py @@ -16,4 +16,4 @@ def pytest_sessionstart(session): # pylint: disable=unused-argument - environ.setdefault("OPENTELEMETRY_PYTHON_DJANGO_INSTRUMENT", "True") + environ.setdefault("OTEL_PYTHON_DJANGO_INSTRUMENT", "True") diff --git a/ext/opentelemetry-ext-elasticsearch/CHANGELOG.md b/ext/opentelemetry-ext-elasticsearch/CHANGELOG.md index 7425aa5e1ec..ede33c6646e 100644 --- a/ext/opentelemetry-ext-elasticsearch/CHANGELOG.md +++ b/ext/opentelemetry-ext-elasticsearch/CHANGELOG.md @@ -2,8 +2,10 @@ ## Unreleased +- Update environment variable names, prefix changed from `OPENTELEMETRY` to `OTEL` ([#904](https://github.com/open-telemetry/opentelemetry-python/pull/904)) + ## Version 0.10b0 Released 2020-06-23 -- Initial release \ No newline at end of file +- Initial release diff --git a/ext/opentelemetry-ext-elasticsearch/src/opentelemetry/ext/elasticsearch/__init__.py b/ext/opentelemetry-ext-elasticsearch/src/opentelemetry/ext/elasticsearch/__init__.py index 7c41ba3f0da..6746d1ac70a 100644 --- a/ext/opentelemetry-ext-elasticsearch/src/opentelemetry/ext/elasticsearch/__init__.py +++ b/ext/opentelemetry-ext-elasticsearch/src/opentelemetry/ext/elasticsearch/__init__.py @@ -40,7 +40,7 @@ --- Elasticsearch instrumentation prefixes operation names with the string "Elasticsearch". This -can be changed to a different string by either setting the `OPENTELEMETRY_PYTHON_ELASTICSEARCH_NAME_PREFIX` +can be changed to a different string by either setting the `OTEL_PYTHON_ELASTICSEARCH_NAME_PREFIX` environment variable or by passing the prefix as an argument to the instrumentor. For example, @@ -88,8 +88,7 @@ class ElasticsearchInstrumentor(BaseInstrumentor): def __init__(self, span_name_prefix=None): if not span_name_prefix: span_name_prefix = environ.get( - "OPENTELEMETRY_PYTHON_ELASTICSEARCH_NAME_PREFIX", - "Elasticsearch", + "OTEL_PYTHON_ELASTICSEARCH_NAME_PREFIX", "Elasticsearch", ) self._span_name_prefix = span_name_prefix.strip() super().__init__() diff --git a/ext/opentelemetry-ext-elasticsearch/tests/test_elasticsearch.py b/ext/opentelemetry-ext-elasticsearch/tests/test_elasticsearch.py index f6ca6bdae1f..3c4fe7f70f0 100644 --- a/ext/opentelemetry-ext-elasticsearch/tests/test_elasticsearch.py +++ b/ext/opentelemetry-ext-elasticsearch/tests/test_elasticsearch.py @@ -95,7 +95,7 @@ def test_prefix_arg(self, request_mock): def test_prefix_env(self, request_mock): prefix = "prefix-from-args" - env_var = "OPENTELEMETRY_PYTHON_ELASTICSEARCH_NAME_PREFIX" + env_var = "OTEL_PYTHON_ELASTICSEARCH_NAME_PREFIX" os.environ[env_var] = prefix ElasticsearchInstrumentor().uninstrument() ElasticsearchInstrumentor().instrument() diff --git a/ext/opentelemetry-ext-flask/CHANGELOG.md b/ext/opentelemetry-ext-flask/CHANGELOG.md index 3109c159023..334084c1494 100644 --- a/ext/opentelemetry-ext-flask/CHANGELOG.md +++ b/ext/opentelemetry-ext-flask/CHANGELOG.md @@ -2,9 +2,9 @@ ## Unreleased -## Version 0.11b0 +- Update environment variable names, prefix changed from `OPENTELEMETRY` to `OTEL` ([#904](https://github.com/open-telemetry/opentelemetry-python/pull/904)) -Released 2020-07-28 +## Version 0.11b0 - Use one general exclude list instead of two ([#872](https://github.com/open-telemetry/opentelemetry-python/pull/872)) diff --git a/ext/opentelemetry-ext-flask/README.rst b/ext/opentelemetry-ext-flask/README.rst index 395053972ef..a75a0cfbaad 100644 --- a/ext/opentelemetry-ext-flask/README.rst +++ b/ext/opentelemetry-ext-flask/README.rst @@ -21,13 +21,13 @@ Configuration Exclude lists ************* -To exclude certain URLs from being tracked, set the environment variable ``OPENTELEMETRY_PYTHON_FLASK_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude. +To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_FLASK_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude. For example, :: - export OPENTELEMETRY_PYTHON_FLASK_EXCLUDED_URLS="client/.*/info,healthcheck" + export OTEL_PYTHON_FLASK_EXCLUDED_URLS="client/.*/info,healthcheck" will exclude requests such as ``https://site/client/123/info`` and ``https://site/xyz/healthcheck``. diff --git a/ext/opentelemetry-ext-pyramid/CHANGELOG.md b/ext/opentelemetry-ext-pyramid/CHANGELOG.md index f871a0aa5f8..2e20d5a0bdf 100644 --- a/ext/opentelemetry-ext-pyramid/CHANGELOG.md +++ b/ext/opentelemetry-ext-pyramid/CHANGELOG.md @@ -2,9 +2,9 @@ ## Unreleased -## Version 0.11b0 +- Update environment variable names, prefix changed from `OPENTELEMETRY` to `OTEL` ([#904](https://github.com/open-telemetry/opentelemetry-python/pull/904)) -Released 2020-07-28 +## Version 0.11b0 - Use one general exclude list instead of two ([#872](https://github.com/open-telemetry/opentelemetry-python/pull/872)) @@ -12,4 +12,4 @@ Released 2020-07-28 Released 2020-06-10 -- Initial release \ No newline at end of file +- Initial release diff --git a/ext/opentelemetry-ext-pyramid/README.rst b/ext/opentelemetry-ext-pyramid/README.rst index 319d6bff4ce..6ec38b83c90 100644 --- a/ext/opentelemetry-ext-pyramid/README.rst +++ b/ext/opentelemetry-ext-pyramid/README.rst @@ -15,13 +15,13 @@ Installation Exclude lists ************* -To exclude certain URLs from being tracked, set the environment variable ``OPENTELEMETRY_PYTHON_PYRAMID_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude. +To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_PYRAMID_EXCLUDED_URLS`` with comma delimited regexes representing which URLs to exclude. For example, :: - export OPENTELEMETRY_PYTHON_PYRAMID_EXCLUDED_URLS="client/.*/info,healthcheck" + export OTEL_PYTHON_PYRAMID_EXCLUDED_URLS="client/.*/info,healthcheck" will exclude requests such as ``https://site/client/123/info`` and ``https://site/xyz/healthcheck``. diff --git a/opentelemetry-api/CHANGELOG.md b/opentelemetry-api/CHANGELOG.md index da976ed37e4..2226b5d0273 100644 --- a/opentelemetry-api/CHANGELOG.md +++ b/opentelemetry-api/CHANGELOG.md @@ -2,9 +2,10 @@ ## Unreleased -## Version 0.11b0 +- Update environment variable names, prefix changed from `OPENTELEMETRY` to `OTEL` + ([#904](https://github.com/open-telemetry/opentelemetry-python/pull/904)) -Released 2020-07-28 +## Version 0.11b0 - Return INVALID_SPAN if no TracerProvider set for get_current_span ([#751](https://github.com/open-telemetry/opentelemetry-python/pull/751)) diff --git a/opentelemetry-api/src/opentelemetry/configuration/__init__.py b/opentelemetry-api/src/opentelemetry/configuration/__init__.py index 36c0950c0b0..0c606b5af4d 100644 --- a/opentelemetry-api/src/opentelemetry/configuration/__init__.py +++ b/opentelemetry-api/src/opentelemetry/configuration/__init__.py @@ -16,30 +16,33 @@ Simple configuration manager This is a configuration manager for OpenTelemetry. It reads configuration -values from environment variables prefixed with ``OPENTELEMETRY_PYTHON_`` whose -characters are only alphanumeric characters and unserscores, except for the -first character after ``OPENTELEMETRY_PYTHON_`` which must not be a number. +values from environment variables prefixed with ``OTEL_`` (for environment +variables that apply to any OpenTelemetry implementation) or with +``OTEL_PYTHON_`` (for environment variables that are specific to the Python +implementation of OpenTelemetry) whose characters are only alphanumeric +characters and unserscores, except for the first character after ``OTEL_`` or +``OTEL_PYTHON_`` which must not be a number. For example, these environment variables will be read: -1. ``OPENTELEMETRY_PYTHON_SOMETHING`` -2. ``OPENTELEMETRY_PYTHON_SOMETHING_ELSE_`` -3. ``OPENTELEMETRY_PYTHON_SOMETHING_ELSE_AND__ELSE`` -4. ``OPENTELEMETRY_PYTHON_SOMETHING_ELSE_AND_else`` -5. ``OPENTELEMETRY_PYTHON_SOMETHING_ELSE_AND_else2`` +1. ``OTEL_SOMETHING`` +2. ``OTEL_SOMETHING_ELSE_`` +3. ``OTEL_SOMETHING_ELSE_AND__ELSE`` +4. ``OTEL_SOMETHING_ELSE_AND_else`` +5. ``OTEL_SOMETHING_ELSE_AND_else2`` These won't: 1. ``OPENTELEMETRY_PYTH_SOMETHING`` -2. ``OPENTELEMETRY_PYTHON_2_SOMETHING_AND__ELSE`` -3. ``OPENTELEMETRY_PYTHON_SOMETHING_%_ELSE`` +2. ``OTEL_2_SOMETHING_AND__ELSE`` +3. ``OTEL_SOMETHING_%_ELSE`` The values stored in the environment variables can be found in an instance of ``opentelemetry.configuration.Configuration``. This class can be instantiated freely because instantiating it returns always the same object. For example, if the environment variable -``OPENTELEMETRY_PYTHON_METER_PROVIDER`` value is ``my_meter_provider``, then +``OTEL_PYTHON_METER_PROVIDER`` value is ``my_meter_provider``, then ``Configuration().meter_provider == "my_meter_provider"`` would be ``True``. Non defined attributes will always return ``None``. This is intended to make it @@ -49,8 +52,8 @@ Environment variables used by OpenTelemetry ------------------------------------------- -1. OPENTELEMETRY_PYTHON_METER_PROVIDER -2. OPENTELEMETRY_PYTHON_TRACER_PROVIDER +1. OTEL_PYTHON_METER_PROVIDER +2. OTEL_PYTHON_TRACER_PROVIDER The value of these environment variables should be the name of the entry point that points to the class that implements either provider. This OpenTelemetry @@ -70,7 +73,7 @@ } To use the meter provider above, then the -``OPENTELEMETRY_PYTHON_METER_PROVIDER`` should be set to +``OTEL_PYTHON_METER_PROVIDER`` should be set to ``"default_meter_provider"`` (this is not actually necessary since the OpenTelemetry API provided providers are the default ones used if no configuration is found in the environment variables). @@ -110,13 +113,11 @@ def __new__(cls) -> "Configuration": instance = super().__new__(cls) for key, value_str in environ.items(): - match = fullmatch( - r"OPENTELEMETRY_PYTHON_([A-Za-z_][\w_]*)", key - ) + match = fullmatch(r"OTEL_(PYTHON_)?([A-Za-z_][\w_]*)", key) if match is not None: - key = match.group(1) + key = match.group(2) value = value_str # type: ConfigValue if value_str == "True": diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index adf1bc0869f..a5f24118d34 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -55,7 +55,7 @@ def wrapper( default_context = "contextvars_context" configured_context = environ.get( - "OPENTELEMETRY_CONTEXT", default_context + "OTEL_CONTEXT", default_context ) # type: str try: _RUNTIME_CONTEXT = next( diff --git a/opentelemetry-api/tests/configuration/test_configuration.py b/opentelemetry-api/tests/configuration/test_configuration.py index 316847a0044..557591513b8 100644 --- a/opentelemetry-api/tests/configuration/test_configuration.py +++ b/opentelemetry-api/tests/configuration/test_configuration.py @@ -32,10 +32,10 @@ def test_singleton(self) -> None: @patch.dict( "os.environ", # type: ignore { - "OPENTELEMETRY_PYTHON_METER_PROVIDER": "meter_provider", - "OPENTELEMETRY_PYTHON_TRACER_PROVIDER": "tracer_provider", - "OPENTELEMETRY_PYTHON_OThER": "other", - "OPENTELEMETRY_PYTHON_OTHER_7": "other_7", + "OTEL_PYTHON_METER_PROVIDER": "meter_provider", + "OTEL_PYTHON_TRACER_PROVIDER": "tracer_provider", + "OTEL_OThER": "other", + "OTEL_OTHER_7": "other_7", "OPENTELEMETRY_PTHON_TRACEX_PROVIDER": "tracex_provider", }, ) @@ -56,7 +56,7 @@ def test_environment_variables(self) -> None: @patch.dict( "os.environ", # type: ignore - {"OPENTELEMETRY_PYTHON_TRACER_PROVIDER": "tracer_provider"}, + {"OTEL_PYTHON_TRACER_PROVIDER": "tracer_provider"}, ) def test_property(self) -> None: with self.assertRaises(AttributeError): @@ -79,8 +79,7 @@ def test_getattr(self) -> None: def test_reset(self) -> None: environ_patcher = patch.dict( - "os.environ", - {"OPENTELEMETRY_PYTHON_TRACER_PROVIDER": "tracer_provider"}, + "os.environ", {"OTEL_PYTHON_TRACER_PROVIDER": "tracer_provider"}, ) environ_patcher.start() @@ -99,10 +98,7 @@ def test_reset(self) -> None: @patch.dict( "os.environ", # type: ignore - { - "OPENTELEMETRY_PYTHON_TRUE": "True", - "OPENTELEMETRY_PYTHON_FALSE": "False", - }, + {"OTEL_TRUE": "True", "OTEL_FALSE": "False"}, ) def test_boolean(self) -> None: self.assertIsInstance( @@ -117,9 +113,9 @@ def test_boolean(self) -> None: @patch.dict( "os.environ", # type: ignore { - "OPENTELEMETRY_PYTHON_POSITIVE_INTEGER": "123", - "OPENTELEMETRY_PYTHON_NEGATIVE_INTEGER": "-123", - "OPENTELEMETRY_PYTHON_NON_INTEGER": "-12z3", + "OTEL_POSITIVE_INTEGER": "123", + "OTEL_NEGATIVE_INTEGER": "-123", + "OTEL_NON_INTEGER": "-12z3", }, ) def test_integer(self) -> None: @@ -136,9 +132,9 @@ def test_integer(self) -> None: @patch.dict( "os.environ", # type: ignore { - "OPENTELEMETRY_PYTHON_POSITIVE_FLOAT": "123.123", - "OPENTELEMETRY_PYTHON_NEGATIVE_FLOAT": "-123.123", - "OPENTELEMETRY_PYTHON_NON_FLOAT": "-12z3.123", + "OTEL_POSITIVE_FLOAT": "123.123", + "OTEL_NEGATIVE_FLOAT": "-123.123", + "OTEL_NON_FLOAT": "-12z3.123", }, ) def test_float(self) -> None: diff --git a/opentelemetry-sdk/CHANGELOG.md b/opentelemetry-sdk/CHANGELOG.md index cbb96eabcd5..bf7618e77fc 100644 --- a/opentelemetry-sdk/CHANGELOG.md +++ b/opentelemetry-sdk/CHANGELOG.md @@ -2,9 +2,10 @@ ## Unreleased -## Version 0.11b0 +- Update environment variable names, prefix changed from `OPENTELEMETRY` to `OTEL` + ([#904](https://github.com/open-telemetry/opentelemetry-python/pull/904)) -Released 2020-07-28 +## Version 0.11b0 - Add support for resources and resource detector ([#853](https://github.com/open-telemetry/opentelemetry-python/pull/853)) diff --git a/opentelemetry-sdk/tests/conftest.py b/opentelemetry-sdk/tests/conftest.py index 80a79222a1f..ed5853b43d3 100644 --- a/opentelemetry-sdk/tests/conftest.py +++ b/opentelemetry-sdk/tests/conftest.py @@ -20,11 +20,11 @@ def pytest_sessionstart(session): # pylint: disable=unused-argument if version_info < (3, 5): # contextvars are not supported in 3.4, use thread-local storage - environ["OPENTELEMETRY_CONTEXT"] = "threadlocal_context" + environ["OTEL_CONTEXT"] = "threadlocal_context" else: - environ["OPENTELEMETRY_CONTEXT"] = "contextvars_context" + environ["OTEL_CONTEXT"] = "contextvars_context" def pytest_sessionfinish(session): # pylint: disable=unused-argument - environ.pop("OPENTELEMETRY_CONTEXT") + environ.pop("OTEL_CONTEXT") From 1112792b18bb177ba58fa11ebaecc9597041a73c Mon Sep 17 00:00:00 2001 From: Emmanuel Courreges Date: Thu, 30 Jul 2020 17:18:38 +0200 Subject: [PATCH 2/2] Add proper length zero padding to hex strings of traceId, spanId, parentId sent on the wire (#908) --- .../CHANGELOG.md | 4 +- .../opentelemetry/exporter/zipkin/__init__.py | 9 +-- .../tests/test_zipkin_exporter.py | 66 +++++++++++++++++++ 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/exporter/opentelemetry-exporter-zipkin/CHANGELOG.md b/exporter/opentelemetry-exporter-zipkin/CHANGELOG.md index 2218d44c20b..1063d35cf53 100644 --- a/exporter/opentelemetry-exporter-zipkin/CHANGELOG.md +++ b/exporter/opentelemetry-exporter-zipkin/CHANGELOG.md @@ -4,6 +4,8 @@ - Change package name to opentelemetry-exporter-zipkin ([#953](https://github.com/open-telemetry/opentelemetry-python/pull/953)) +- Add proper length zero padding to hex strings of traceId, spanId, parentId sent on the wire, for compatibility with jaeger-collector + ([#908](https://github.com/open-telemetry/opentelemetry-python/pull/908)) ## 0.8b0 @@ -23,4 +25,4 @@ Released 2020-05-12 Released 2020-02-21 -- Initial release \ No newline at end of file +- Initial release diff --git a/exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py b/exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py index 10bd6e93e97..b0eb1bce0f1 100644 --- a/exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py +++ b/exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py @@ -168,8 +168,9 @@ def _translate_to_zipkin(self, spans: Sequence[Span]): duration_mus = _nsec_to_usec_round(span.end_time - span.start_time) zipkin_span = { - "traceId": format(trace_id, "x"), - "id": format(span_id, "x"), + # Ensure left-zero-padding of traceId, spanId, parentId + "traceId": format(trace_id, "032x"), + "id": format(span_id, "016x"), "name": span.name, "timestamp": start_timestamp_mus, "duration": duration_mus, @@ -184,10 +185,10 @@ def _translate_to_zipkin(self, spans: Sequence[Span]): if isinstance(span.parent, Span): zipkin_span["parentId"] = format( - span.parent.get_context().span_id, "x" + span.parent.get_context().span_id, "016x" ) elif isinstance(span.parent, SpanContext): - zipkin_span["parentId"] = format(span.parent.span_id, "x") + zipkin_span["parentId"] = format(span.parent.span_id, "016x") zipkin_spans.append(zipkin_span) return zipkin_spans diff --git a/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py b/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py index 9f6da2e240f..f6e24a1495a 100644 --- a/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py +++ b/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py @@ -264,6 +264,72 @@ def test_export(self): headers={"Content-Type": "application/json"}, ) + # pylint: disable=too-many-locals + def test_zero_padding(self): + """test that hex ids starting with 0 + are properly padded to 16 or 32 hex chars + when exported + """ + + span_names = "testZeroes" + trace_id = 0x0E0C63257DE34C926F9EFCD03927272E + span_id = 0x04BF92DEEFC58C92 + parent_id = 0x0AAAAAAAAAAAAAAA + + start_time = 683647322 * 10 ** 9 # in ns + duration = 50 * 10 ** 6 + end_time = start_time + duration + + 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 + ) + + otel_span = trace.Span( + name=span_names[0], context=span_context, parent=parent_context, + ) + + otel_span.start(start_time=start_time) + otel_span.end(end_time=end_time) + + service_name = "test-service" + local_endpoint = {"serviceName": service_name, "port": 9411} + + exporter = ZipkinSpanExporter(service_name) + # Check traceId are properly lowercase 16 or 32 hex + expected = [ + { + "traceId": "0e0c63257de34c926f9efcd03927272e", + "id": "04bf92deefc58c92", + "name": span_names[0], + "timestamp": start_time // 10 ** 3, + "duration": duration // 10 ** 3, + "localEndpoint": local_endpoint, + "kind": None, + "tags": {}, + "annotations": None, + "debug": True, + "parentId": "0aaaaaaaaaaaaaaa", + } + ] + + mock_post = MagicMock() + with patch("requests.post", mock_post): + mock_post.return_value = MockResponse(200) + status = exporter.export([otel_span]) + 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)