From 5450e6decfbc82b9fa1d2b159272efa33b112c97 Mon Sep 17 00:00:00 2001 From: Dilip M <55284676+dmarar@users.noreply.github.com> Date: Tue, 8 Dec 2020 22:10:41 +0530 Subject: [PATCH] Fix to run the Auto instrumentation example in the docs (#1435) * Fix to run the auto-instrumentation example successfully * reverting the changes in ConsoleSpanExporter, setup.cfg Modifying the README.rst accordingly. * changes to include a consolespanexporter and consolemetricsexporter as per review comments. * Change as per review comments to replace **kwargs with service_name * moving the service_name as the first parameter in the ConsoleSpanExporter since some py35-exporter-zipkin test were failing. * changes to add the service_name to the exported span * pass the service name to the exported span fix the unit tests for the above change. * update the setup.cfg to the correct path of ConsoleMetricsExporter * revert the changes to add the service_name to the exported span. Co-authored-by: Leighton Chen --- docs/examples/auto-instrumentation/README.rst | 22 ++++++++++++++----- .../server_instrumented.py | 5 ++--- opentelemetry-sdk/setup.cfg | 3 +++ .../sdk/trace/export/__init__.py | 4 ++++ .../tests/trace/export/test_export.py | 3 ++- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/docs/examples/auto-instrumentation/README.rst b/docs/examples/auto-instrumentation/README.rst index 36132daf5e2..d7e59eedefc 100644 --- a/docs/examples/auto-instrumentation/README.rst +++ b/docs/examples/auto-instrumentation/README.rst @@ -37,9 +37,8 @@ Manually instrumented server def server_request(): with tracer.start_as_current_span( "server_request", - parent=propagators.extract( - lambda dict_, key: dict_.get(key, []), request.headers - )["current-span"], + context=propagators.extract(DictGetter(), request.headers + ), ): print(request.args.get("param")) return "served" @@ -137,7 +136,12 @@ similar to the following example: "http.flavor": "1.1" }, "events": [], - "links": [] + "links": [], + "resource": { + "telemetry.sdk.language": "python", + "telemetry.sdk.name": "opentelemetry", + "telemetry.sdk.version": "0.16b1" + } } Execute an automatically instrumented server @@ -148,7 +152,7 @@ and run the following command instead: .. code:: sh - $ opentelemetry-instrument python server_uninstrumented.py + $ opentelemetry-instrument -e console_span,console_metrics python server_uninstrumented.py In the console where you previously executed ``client.py``, run the following command again: @@ -192,7 +196,13 @@ similar to the following example: "http.status_code": 200 }, "events": [], - "links": [] + "links": [], + "resource": { + "telemetry.sdk.language": "python", + "telemetry.sdk.name": "opentelemetry", + "telemetry.sdk.version": "0.16b1", + "service.name": "" + } } You can see that both outputs are the same because automatic instrumentation does diff --git a/docs/examples/auto-instrumentation/server_instrumented.py b/docs/examples/auto-instrumentation/server_instrumented.py index c8562828be9..6212ec33336 100644 --- a/docs/examples/auto-instrumentation/server_instrumented.py +++ b/docs/examples/auto-instrumentation/server_instrumented.py @@ -21,6 +21,7 @@ ConsoleSpanExporter, SimpleExportSpanProcessor, ) +from opentelemetry.trace.propagation.textmap import DictGetter app = Flask(__name__) @@ -36,9 +37,7 @@ def server_request(): with tracer.start_as_current_span( "server_request", - parent=propagators.extract( - lambda dict_, key: dict_.get(key, []), request.headers - )["current-span"], + context=propagators.extract(DictGetter(), request.headers), kind=trace.SpanKind.SERVER, attributes=collect_request_attributes(request.environ), ): diff --git a/opentelemetry-sdk/setup.cfg b/opentelemetry-sdk/setup.cfg index 4acb6ca1fcb..6da0d4642ca 100644 --- a/opentelemetry-sdk/setup.cfg +++ b/opentelemetry-sdk/setup.cfg @@ -53,6 +53,9 @@ opentelemetry_tracer_provider = sdk_tracer_provider = opentelemetry.sdk.trace:TracerProvider opentelemetry_propagator = b3 = opentelemetry.sdk.trace.propagation.b3_format:B3Format +opentelemetry_exporter = + console_span = opentelemetry.sdk.trace.export:ConsoleSpanExporter + console_metrics = opentelemetry.sdk.metrics.export:ConsoleMetricsExporter [options.extras_require] test = diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py index 53f85dcbb2e..d8786e6d21a 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py @@ -13,12 +13,14 @@ # limitations under the License. import collections +import json import logging import os import sys import threading import typing from enum import Enum +from typing import Optional from opentelemetry.configuration import Configuration from opentelemetry.context import Context, attach, detach, set_value @@ -370,12 +372,14 @@ class ConsoleSpanExporter(SpanExporter): def __init__( self, + service_name: Optional[str] = None, out: typing.IO = sys.stdout, formatter: typing.Callable[[Span], str] = lambda span: span.to_json() + os.linesep, ): self.out = out self.formatter = formatter + self.service_name = service_name def export(self, spans: typing.Sequence[Span]) -> SpanExportResult: for span in spans: diff --git a/opentelemetry-sdk/tests/trace/export/test_export.py b/opentelemetry-sdk/tests/trace/export/test_export.py index 4ad23dd8631..92d4f65d136 100644 --- a/opentelemetry-sdk/tests/trace/export/test_export.py +++ b/opentelemetry-sdk/tests/trace/export/test_export.py @@ -477,14 +477,15 @@ def test_batch_span_processor_parameters(self): class TestConsoleSpanExporter(unittest.TestCase): def test_export(self): # pylint: disable=no-self-use """Check that the console exporter prints spans.""" - exporter = export.ConsoleSpanExporter() + exporter = export.ConsoleSpanExporter() # Mocking stdout interferes with debugging and test reporting, mock on # the exporter instance instead. span = trace._Span("span name", trace_api.INVALID_SPAN_CONTEXT) with mock.patch.object(exporter, "out") as mock_stdout: exporter.export([span]) mock_stdout.write.assert_called_once_with(span.to_json() + os.linesep) + self.assertEqual(mock_stdout.write.call_count, 1) self.assertEqual(mock_stdout.flush.call_count, 1)