Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
network_tracing_example.cpp
Go to the documentation of this file.
1/*****************************************************************************
2BSD 3-Clause License
3
4Copyright (c) 2024, 🍀☀🌕🌥 🌊
5All rights reserved.
6*****************************************************************************/
7
26
27#include <chrono>
28#include <iostream>
29#include <string>
30#include <thread>
31
32using namespace kcenon::network;
33
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}
42
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}
50
51int main()
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}
Immutable trace context for distributed tracing.
static auto create_span(std::string_view name) -> span
Create a new root span with a new trace context.
static auto from_headers(const std::vector< std::pair< std::string, std::string > > &headers) -> trace_context
Parse trace context from HTTP headers.
Main namespace for all Network System components.
void simulate_database_query(tracing::trace_context &ctx)
void simulate_cache_lookup(tracing::trace_context &ctx)
Unified tracing header for network_system.