Skip to content

Commit 5450e6d

Browse files
dmararlzchen
andauthored
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 <[email protected]>
1 parent 0803a0f commit 5450e6d

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

docs/examples/auto-instrumentation/README.rst

+16-6
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ Manually instrumented server
3737
def server_request():
3838
with tracer.start_as_current_span(
3939
"server_request",
40-
parent=propagators.extract(
41-
lambda dict_, key: dict_.get(key, []), request.headers
42-
)["current-span"],
40+
context=propagators.extract(DictGetter(), request.headers
41+
),
4342
):
4443
print(request.args.get("param"))
4544
return "served"
@@ -137,7 +136,12 @@ similar to the following example:
137136
"http.flavor": "1.1"
138137
},
139138
"events": [],
140-
"links": []
139+
"links": [],
140+
"resource": {
141+
"telemetry.sdk.language": "python",
142+
"telemetry.sdk.name": "opentelemetry",
143+
"telemetry.sdk.version": "0.16b1"
144+
}
141145
}
142146
143147
Execute an automatically instrumented server
@@ -148,7 +152,7 @@ and run the following command instead:
148152

149153
.. code:: sh
150154
151-
$ opentelemetry-instrument python server_uninstrumented.py
155+
$ opentelemetry-instrument -e console_span,console_metrics python server_uninstrumented.py
152156
153157
In the console where you previously executed ``client.py``, run the following
154158
command again:
@@ -192,7 +196,13 @@ similar to the following example:
192196
"http.status_code": 200
193197
},
194198
"events": [],
195-
"links": []
199+
"links": [],
200+
"resource": {
201+
"telemetry.sdk.language": "python",
202+
"telemetry.sdk.name": "opentelemetry",
203+
"telemetry.sdk.version": "0.16b1",
204+
"service.name": ""
205+
}
196206
}
197207
198208
You can see that both outputs are the same because automatic instrumentation does

docs/examples/auto-instrumentation/server_instrumented.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
ConsoleSpanExporter,
2222
SimpleExportSpanProcessor,
2323
)
24+
from opentelemetry.trace.propagation.textmap import DictGetter
2425

2526
app = Flask(__name__)
2627

@@ -36,9 +37,7 @@
3637
def server_request():
3738
with tracer.start_as_current_span(
3839
"server_request",
39-
parent=propagators.extract(
40-
lambda dict_, key: dict_.get(key, []), request.headers
41-
)["current-span"],
40+
context=propagators.extract(DictGetter(), request.headers),
4241
kind=trace.SpanKind.SERVER,
4342
attributes=collect_request_attributes(request.environ),
4443
):

opentelemetry-sdk/setup.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ opentelemetry_tracer_provider =
5353
sdk_tracer_provider = opentelemetry.sdk.trace:TracerProvider
5454
opentelemetry_propagator =
5555
b3 = opentelemetry.sdk.trace.propagation.b3_format:B3Format
56+
opentelemetry_exporter =
57+
console_span = opentelemetry.sdk.trace.export:ConsoleSpanExporter
58+
console_metrics = opentelemetry.sdk.metrics.export:ConsoleMetricsExporter
5659

5760
[options.extras_require]
5861
test =

opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
# limitations under the License.
1414

1515
import collections
16+
import json
1617
import logging
1718
import os
1819
import sys
1920
import threading
2021
import typing
2122
from enum import Enum
23+
from typing import Optional
2224

2325
from opentelemetry.configuration import Configuration
2426
from opentelemetry.context import Context, attach, detach, set_value
@@ -370,12 +372,14 @@ class ConsoleSpanExporter(SpanExporter):
370372

371373
def __init__(
372374
self,
375+
service_name: Optional[str] = None,
373376
out: typing.IO = sys.stdout,
374377
formatter: typing.Callable[[Span], str] = lambda span: span.to_json()
375378
+ os.linesep,
376379
):
377380
self.out = out
378381
self.formatter = formatter
382+
self.service_name = service_name
379383

380384
def export(self, spans: typing.Sequence[Span]) -> SpanExportResult:
381385
for span in spans:

opentelemetry-sdk/tests/trace/export/test_export.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,15 @@ def test_batch_span_processor_parameters(self):
477477
class TestConsoleSpanExporter(unittest.TestCase):
478478
def test_export(self): # pylint: disable=no-self-use
479479
"""Check that the console exporter prints spans."""
480-
exporter = export.ConsoleSpanExporter()
481480

481+
exporter = export.ConsoleSpanExporter()
482482
# Mocking stdout interferes with debugging and test reporting, mock on
483483
# the exporter instance instead.
484484
span = trace._Span("span name", trace_api.INVALID_SPAN_CONTEXT)
485485
with mock.patch.object(exporter, "out") as mock_stdout:
486486
exporter.export([span])
487487
mock_stdout.write.assert_called_once_with(span.to_json() + os.linesep)
488+
488489
self.assertEqual(mock_stdout.write.call_count, 1)
489490
self.assertEqual(mock_stdout.flush.call_count, 1)
490491

0 commit comments

Comments
 (0)