Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
network_tracing_example.cpp File Reference

Demonstrates distributed tracing for network operations. More...

#include <kcenon/network/tracing/tracing.h>
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
Include dependency graph for network_tracing_example.cpp:

Go to the source code of this file.

Functions

void simulate_database_query (tracing::trace_context &ctx)
 
void simulate_cache_lookup (tracing::trace_context &ctx)
 
int main ()
 

Detailed Description

Demonstrates distributed tracing for network operations.

Definition in file network_tracing_example.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 51 of file network_tracing_example.cpp.

52{
53 std::cout << "=== Network Tracing Example ===" << std::endl;
54
55 // 1. Create a trace context for an incoming request
56 std::cout << "\n1. Creating trace context:" << std::endl;
57 auto ctx = tracing::trace_context::create("handle_request");
58 std::cout << " Trace context created for 'handle_request'" << std::endl;
59
60 // 2. Create spans for sub-operations
61 std::cout << "\n2. Creating nested spans:" << std::endl;
62 {
63 auto request_span = ctx.create_span("process_request");
66 std::cout << " Request processing complete" << std::endl;
67 }
68
69 // 3. Propagate context via headers
70 std::cout << "\n3. Header propagation:" << std::endl;
71 auto headers = ctx.to_headers();
72 std::cout << " Trace headers (" << headers.size() << "):" << std::endl;
73 for (const auto& [key, value] : headers)
74 {
75 std::cout << " " << key << ": " << value << std::endl;
76 }
77
78 // 4. Reconstruct context from headers (simulating receiving service)
79 std::cout << "\n4. Reconstructing context from headers:" << std::endl;
80 std::unordered_map<std::string, std::string> header_map;
81 for (const auto& [key, value] : headers)
82 {
83 header_map[key] = value;
84 }
85 auto downstream_ctx = tracing::trace_context::from_headers(header_map);
86 auto downstream_span = downstream_ctx.create_span("downstream_service");
87 std::cout << " Downstream context reconstructed" << std::endl;
88 std::cout << " Downstream span created" << std::endl;
89
90 std::cout << "\nDone." << std::endl;
91 return 0;
92}
static auto from_headers(const std::vector< std::pair< std::string, std::string > > &headers) -> trace_context
Parse trace context from HTTP headers.
void simulate_database_query(tracing::trace_context &ctx)
void simulate_cache_lookup(tracing::trace_context &ctx)

References kcenon::network::tracing::trace_context::from_headers(), simulate_cache_lookup(), and simulate_database_query().

Here is the call graph for this function:

◆ simulate_cache_lookup()

void simulate_cache_lookup ( tracing::trace_context & ctx)
Examples
network_tracing_example.cpp.

Definition at line 43 of file network_tracing_example.cpp.

44{
45 auto cache_span = ctx.create_span("cache_lookup");
46 std::cout << " Checking cache..." << std::endl;
47 std::this_thread::sleep_for(std::chrono::milliseconds(5));
48 std::cout << " Cache miss" << std::endl;
49}
static auto create_span(std::string_view name) -> span
Create a new root span with a new trace context.

References kcenon::network::tracing::trace_context::create_span().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ simulate_database_query()

void simulate_database_query ( tracing::trace_context & ctx)
Examples
network_tracing_example.cpp.

Definition at line 34 of file network_tracing_example.cpp.

35{
36 auto db_span = ctx.create_span("database_query");
37 std::cout << " Executing database query..." << std::endl;
38 std::this_thread::sleep_for(std::chrono::milliseconds(50));
39 std::cout << " Query completed" << std::endl;
40 // span ends automatically (RAII)
41}

References kcenon::network::tracing::trace_context::create_span().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function: