userver: userver/baggage/baggage_manager.hpp Source File
Loading...
Searching...
No Matches
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_base.hpp>
10#include <userver/components/component_config.hpp>
11#include <userver/components/component_context.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 {
24public:
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, BaggageProperties properties) const;
35
36 /// @brief Get const pointer to baggage value from task inherited variable
37 static const Baggage* TryGetBaggage();
38
39 /// @brief Set new baggage value to task inherited variable
40 void SetBaggage(std::string header) const;
41
42 /// @brief Delete header from task inherited variable
43 static void ResetBaggage();
44
45private:
46 dynamic_config::Source config_source_;
47};
48
49/// @ingroup userver_components
50///
51/// @brief Component for relationship with header baggage.
52///
53/// ## Static options:
54/// Inherits options from components::ComponentBase.
55class BaggageManagerComponent final : public components::ComponentBase {
56public:
57 /// @ingroup userver_component_names
58 /// @brief The default name of baggage::BaggageManagerComponent
59 static constexpr std::string_view kName = "baggage-manager";
60
61 BaggageManagerComponent(const components::ComponentConfig& config, const components::ComponentContext& context);
62
63 BaggageManager& GetManager();
64
65 static yaml_config::Schema GetStaticConfigSchema();
66
67private:
68 BaggageManager baggage_manager_;
69};
70
71} // namespace baggage
72
73USERVER_NAMESPACE_END