Line data Source code
1 : /*
2 : ___________________________________________
3 : | _ ___ _ |
4 : | | | |__ \ | | |
5 : | | |__ ) |__ _ __ _ ___ _ __ | |_ |
6 : | | '_ \ / // _` |/ _` |/ _ \ '_ \| __| | HTTP/2 AGENT FOR MOCK TESTING
7 : | | | | |/ /| (_| | (_| | __/ | | | |_ | Version 0.0.z
8 : | |_| |_|____\__,_|\__, |\___|_| |_|\__| | https://github.com/testillano/h2agent
9 : | __/ | |
10 : | |___/ |
11 : |___________________________________________|
12 :
13 : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
14 : SPDX-License-Identifier: MIT
15 : Copyright (c) 2021 Eduardo Ramos
16 :
17 : Permission is hereby granted, free of charge, to any person obtaining a copy
18 : of this software and associated documentation files (the "Software"), to deal
19 : in the Software without restriction, including without limitation the rights
20 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 : copies of the Software, and to permit persons to whom the Software is
22 : furnished to do so, subject to the following conditions:
23 :
24 : The above copyright notice and this permission notice shall be included in all
25 : copies or substantial portions of the Software.
26 :
27 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 : SOFTWARE.
34 : */
35 :
36 : #include <sstream>
37 : #include <string>
38 : #include <algorithm>
39 :
40 : #include <nlohmann/json.hpp>
41 :
42 : #include <ert/tracing/Logger.hpp>
43 :
44 : #include <AdminClientEndpoint.hpp>
45 :
46 :
47 : #include <functions.hpp>
48 :
49 :
50 : namespace h2agent
51 : {
52 : namespace model
53 : {
54 :
55 3 : void AdminClientEndpoint::connect(bool fromScratch) {
56 :
57 3 : if (fromScratch) {
58 1 : client_.reset();
59 : }
60 3 : if (!client_) {
61 :
62 3 : client_ = std::make_shared<h2agent::http2::MyTrafficHttp2Client>("h2agent_traffic_client", host_, std::to_string(port_), secure_, nullptr);
63 : try {
64 3 : client_->enableMetrics(metrics_, response_delay_seconds_histogram_bucket_boundaries_, message_size_bytes_histogram_bucket_boundaries_, application_name_ + "_" + h2agent::model::fixMetricsName(key_)/*source label*/);
65 : }
66 0 : catch(std::exception &e)
67 : {
68 0 : client_->enableMetrics(nullptr); // force no metrics again
69 0 : std::string msg = ert::tracing::Logger::asString("Cannot enable metrics for client endpoint '%s': %s", key_.c_str(), e.what());
70 0 : ert::tracing::Logger::error(msg, ERT_FILE_LOCATION);
71 0 : }
72 : }
73 3 : }
74 :
75 17 : void AdminClientEndpoint::setMetricsData(ert::metrics::Metrics *metrics, const ert::metrics::bucket_boundaries_t &responseDelaySecondsHistogramBucketBoundaries,
76 : const ert::metrics::bucket_boundaries_t &messageSizeBytesHistogramBucketBoundaries, const std::string &applicationName) {
77 :
78 17 : metrics_ = metrics;
79 17 : response_delay_seconds_histogram_bucket_boundaries_ = responseDelaySecondsHistogramBucketBoundaries;
80 17 : message_size_bytes_histogram_bucket_boundaries_ = messageSizeBytesHistogramBucketBoundaries;
81 17 : application_name_ = applicationName;
82 17 : }
83 :
84 25 : bool AdminClientEndpoint::load(const nlohmann::json &j) {
85 :
86 : // Store whole document (useful for GET operation)
87 25 : json_ = j;
88 :
89 : // Mandatory
90 25 : auto it = j.find("id");
91 25 : key_ = *it;
92 :
93 25 : it = j.find("host");
94 25 : host_ = *it;
95 :
96 25 : it = j.find("port");
97 25 : port_ = *it;
98 :
99 : // Optionals
100 25 : it = j.find("secure");
101 25 : secure_ = false;
102 25 : if (it != j.end() && it->is_boolean()) {
103 14 : secure_ = *it;
104 : }
105 25 : it = j.find("permit");
106 25 : permit_ = true;
107 25 : if (it != j.end() && it->is_boolean()) {
108 8 : permit_ = *it;
109 : }
110 :
111 : // Validations not in schema:
112 25 : if (key_.empty()) {
113 4 : ert::tracing::Logger::error("Invalid client endpoint identifier: provide a non-empty string", ERT_FILE_LOCATION);
114 4 : return false;
115 : }
116 :
117 21 : if (host_.empty()) {
118 1 : ert::tracing::Logger::error("Invalid client endpoint host: provide a non-empty string", ERT_FILE_LOCATION);
119 1 : return false;
120 : }
121 :
122 20 : return true;
123 : }
124 :
125 6 : nlohmann::json AdminClientEndpoint::asJson() const
126 : {
127 6 : if (!client_) return json_;
128 :
129 2 : nlohmann::json result = json_;
130 2 : result["status"] = client_->getConnectionStatus();
131 :
132 2 : return result;
133 2 : }
134 :
135 0 : std::uint64_t AdminClientEndpoint::getGeneralUniqueClientSequence() const
136 : {
137 0 : if (!client_) return 1;
138 :
139 0 : return client_->getGeneralUniqueClientSequence();
140 : }
141 :
142 : }
143 : }
144 :
|