Line data Source code
1 : #include "propagation_style.h" 2 : 3 : #include <cassert> 4 : 5 : #include "json.hpp" 6 : #include "parse_util.h" 7 : 8 : namespace datadog { 9 : namespace tracing { 10 : 11 2052 : nlohmann::json to_json(PropagationStyle style) { 12 : // Note: Make sure that these strings are consistent (modulo case) with 13 : // `parse_propagation_styles` in `tracer_config.cpp`. 14 2052 : switch (style) { 15 1012 : case PropagationStyle::DATADOG: 16 1012 : return "Datadog"; 17 28 : case PropagationStyle::B3: 18 28 : return "B3"; 19 1011 : case PropagationStyle::W3C: 20 1011 : return "tracecontext"; // for compatibility with OpenTelemetry 21 1 : default: 22 1 : assert(style == PropagationStyle::NONE); 23 1 : return "none"; 24 : } 25 : } 26 : 27 1016 : nlohmann::json to_json(const std::vector<PropagationStyle>& styles) { 28 1016 : std::vector<nlohmann::json> styles_json; 29 3033 : for (const auto style : styles) { 30 2017 : styles_json.push_back(to_json(style)); 31 : } 32 2032 : return styles_json; 33 1016 : } 34 : 35 66 : Optional<PropagationStyle> parse_propagation_style(StringView text) { 36 66 : auto token = std::string{text}; 37 66 : to_lower(token); 38 : 39 : // Note: Make sure that these strings are consistent (modulo case) with 40 : // `to_json`, above. 41 66 : if (token == "datadog") { 42 16 : return PropagationStyle::DATADOG; 43 50 : } else if (token == "b3" || token == "b3multi") { 44 45 : return PropagationStyle::B3; 45 5 : } else if (token == "tracecontext") { 46 0 : return PropagationStyle::W3C; 47 5 : } else if (token == "none") { 48 2 : return PropagationStyle::NONE; 49 : } 50 : 51 3 : return nullopt; 52 66 : } 53 : 54 : } // namespace tracing 55 : } // namespace datadog