Skip to content

Commit 6ed24ab

Browse files
committed
[EXAMPLE] Rename async example to manual propagation
After the changes, the new reframing should be: This PR adds an example that demonstrates how to manually propagate tracing context across asynchronous calls between generic clients and servers. It simulates context injections and extractions showing how to **explicitly/manually** set parent span_id's in different situations, when the scope is not available, or different threads are run in parallel. The proposed pattern may be helpful for use cases which involve asynchronous operations. It defines auxiliary functions that can be easily adapted to other code bases together with a README documenting the flow and expected outcome. Apart from markdown lining and clang formatting, BUILD and relevant CMakeLists have been adapted.
1 parent f1062e2 commit 6ed24ab

File tree

6 files changed

+32
-30
lines changed

6 files changed

+32
-30
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ add_subdirectory(metrics_simple)
2828
add_subdirectory(multithreaded)
2929
add_subdirectory(multi_processor)
3030
add_subdirectory(environment_carrier)
31-
add_subdirectory(async)
31+
add_subdirectory(manual_propagation)
3232

3333
if(WITH_EXAMPLES_HTTP)
3434
add_subdirectory(http)

examples/async/CMakeLists.txt

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
55

66
cc_binary(
7-
name = "example_async",
7+
name = "example_manual_propagation",
88
srcs = [
99
"main.cc",
1010
],
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
add_executable(example_manual_propagation main.cc)
5+
target_link_libraries(
6+
example_manual_propagation PRIVATE opentelemetry-cpp::trace
7+
opentelemetry-cpp::ostream_span_exporter)
8+
9+
if(BUILD_TESTING)
10+
add_test(NAME examples.manual_propagation COMMAND "$<TARGET_FILE:example_manual_propagation>")
11+
endif()
Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
# OpenTelemetry C++ ASYNC Example
1+
# OpenTelemetry C++ Manual Asynchronous Context Propagation Example
22

3-
This is a simple example that demonstrates tracing async requests between
4-
generic clients and servers. It simulates context injections and extractions
5-
and shows how to explicitly set parent span_id's in different situations, when
6-
the scope is not available, or different threads are run in parallel.
3+
This example demonstrates how to manually propagate tracing context across
4+
asynchronous calls between generic clients and servers. It simulates context
5+
injections and extractions showing how to **explicitly/manually** set parent
6+
span_id's in different situations, when the scope is not available, or
7+
different threads are run in parallel.
78

8-
The proposed pattern may be used for any kind of asynchronous client/server
9-
communication.
9+
The proposed pattern may be helpful for use cases which involve asynchronous
10+
operations.
1011

1112
## Running the example
1213

@@ -19,15 +20,14 @@ requests have distributed tracing context `injected` into a map, simulating
1920
headers.
2021

2122
* These requests then arrive to a server, which `extracts` the span information
22-
and creates child spans sharing the same trace id's with the client request,
23-
and marking it as parent. After that, other spans are `nested`, simulating
24-
server work.
23+
and creates child spans sharing the same trace id's with the client request.
24+
After that, other spans are `nested`, simulating server work.
2525

26-
* Answers contain again the context `injected`, and the client extracts them
27-
without more context than the headers arriving from the
26+
* Answers contain again the context `injected`, and the client propagates it
27+
without more context than the headers arriving in the answer.
2828

29-
* The Parent spans that originated the only 2 `trace-id`'s of this example are
30-
kept alive during the whole example, and ended at the end.
29+
* The parent spans that originated the only 2 `trace-id`'s simulated here are
30+
kept alive until the end of the example.
3131

3232
```text
3333
[Client] [Server]
@@ -45,7 +45,7 @@ process_answer
4545
## Span Hierarchy
4646

4747
Only 2 traces are generated in this example. Each one contains 4 spans in
48-
total, created across both a server and a client. The Span hierarchy is
48+
total, and are propagated across a server and a client. The Span hierarchy is
4949
shown below:
5050

5151
```text
@@ -67,7 +67,9 @@ create child spans from a previous one, independently on the active span.
6767

6868
It's worth noting that `shared_ptr`'s are used because it helps for keeping
6969
spans alive and passing them across different lambda functions or execution
70-
scopes for more complex use cases. These functions are:
70+
scopes for more complex use cases.
71+
72+
The auxiliary functions used in this example are explained below:
7173

7274
### Creating Child Spans
7375

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void InitTracer()
4242
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
4343
trace_sdk::TracerProviderFactory::Create(std::move(processor),
4444
opentelemetry::sdk::resource::Resource::Create({}));
45-
// Set the global trace provider
45+
// Set the global tracer provider
4646
trace_sdk::Provider::SetTracerProvider(provider);
4747

4848
ctx::propagation::GlobalTextMapPropagator::SetGlobalPropagator(

0 commit comments

Comments
 (0)