userver: userver/baggage/baggage_manager.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
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#include <userver/baggage/baggage_settings.hpp>
8
9#include <userver/components/component_config.hpp>
10#include <userver/components/component_context.hpp>
11#include <userver/components/loggable_component_base.hpp>
12#include <userver/dynamic_config/source.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace baggage {
17
18/// @ingroup userver_clients
19///
20/// Client for manipulation with Baggage header content.
21///
22/// Usually retrieved from baggage::BaggageManagerComponent.
23class BaggageManager final {
24 public:
25 explicit BaggageManager(const dynamic_config::Source& config_source);
26
27 /// @brief Returns if baggage is enabled
28 bool IsEnabled() const;
29
30 /// @brief Add entry to baggage header.
31 /// @throws BaggageException If key, value or properties
32 /// don't match with requirements or if allowed_keys
33 /// don't contain selected key
34 void AddEntry(std::string key, std::string value,
35 BaggageProperties properties) const;
36
37 /// @brief Get const pointer to baggage value from task inherited variable
38 static const Baggage* TryGetBaggage();
39
40 /// @brief Set new baggage value to task inherited variable
41 void SetBaggage(std::string header) const;
42
43 /// @brief Delete header from task inherited variable
44 static void ResetBaggage();
45
46 private:
47 dynamic_config::Source config_source_;
48};
49
50/// @ingroup userver_components
51///
52/// @brief Component for relationship with header baggage.
53///
54/// ## Static options:
55/// Inherits options from components::LoggableComponentBase.
56class BaggageManagerComponent final : public components::LoggableComponentBase {
57 public:
58 /// @ingroup userver_component_names
59 /// @brief The default name of baggage::BaggageManagerComponent
60 static constexpr std::string_view kName = "baggage-manager";
61
62 BaggageManagerComponent(const components::ComponentConfig& config,
63 const components::ComponentContext& context);
64
65 BaggageManager& GetManager();
66
67 static yaml_config::Schema GetStaticConfigSchema();
68
69 private:
70 BaggageManager baggage_manager_;
71};
72
73} // namespace baggage
74
75USERVER_NAMESPACE_END