userver: userver/baggage/baggage_manager.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
baggage_manager.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/baggage/baggage_manager.hpp
4/// @brief Baggage manipulation client and component.
5
6#include <userver/baggage/baggage.hpp>
7
8#include <userver/components/component_base.hpp>
9#include <userver/components/component_config.hpp>
10#include <userver/components/component_context.hpp>
11#include <userver/dynamic_config/source.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace baggage {
16
17/// @ingroup userver_clients
18///
19/// Client for manipulation with Baggage header content.
20///
21/// Usually retrieved from baggage::BaggageManagerComponent.
22class BaggageManager final {
23public:
24 explicit BaggageManager(const dynamic_config::Source& config_source);
25
26 /// @brief Returns if baggage is enabled
27 bool IsEnabled() const;
28
29 /// @brief Add entry to baggage header.
30 /// @throws BaggageException If key, value or properties
31 /// don't match with requirements or if allowed_keys
32 /// don't contain selected key
33 void AddEntry(std::string key, std::string value, BaggageProperties properties) const;
34
35 /// @brief Get const pointer to baggage value from task inherited variable
36 static const Baggage* TryGetBaggage();
37
38 /// @brief Set new baggage value to task inherited variable
39 void SetBaggage(std::string header) const;
40
41 /// @brief Delete header from task inherited variable
42 static void ResetBaggage();
43
44private:
45 dynamic_config::Source config_source_;
46};
47
48/// @ingroup userver_components
49///
50/// @brief Component for relationship with header baggage.
51///
52/// ## Static options:
53/// Inherits options from components::ComponentBase.
54class BaggageManagerComponent final : public components::ComponentBase {
55public:
56 /// @ingroup userver_component_names
57 /// @brief The default name of baggage::BaggageManagerComponent
58 static constexpr std::string_view kName = "baggage-manager";
59
60 BaggageManagerComponent(const components::ComponentConfig& config, const components::ComponentContext& context);
61
62 BaggageManager& GetManager();
63
64 static yaml_config::Schema GetStaticConfigSchema();
65
66private:
67 BaggageManager baggage_manager_;
68};
69
70} // namespace baggage
71
72USERVER_NAMESPACE_END