Skip to content

Commit 3910b04

Browse files
authored
Fix URL in ES exporter, fix ipv6 supporting for http client. (open-telemetry#3081)
1 parent 7b82473 commit 3910b04

File tree

3 files changed

+92
-3
lines changed

3 files changed

+92
-3
lines changed

exporters/elasticsearch/src/es_log_record_exporter.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ sdk::common::ExportResult ElasticsearchLogRecordExporter::Export(
323323
}
324324

325325
// Create a connection to the ElasticSearch instance
326-
auto session = http_client_->CreateSession(options_.host_ + std::to_string(options_.port_));
326+
auto session = http_client_->CreateSession(options_.host_ + ":" + std::to_string(options_.port_));
327327
auto request = session->CreateRequest();
328328

329329
// Populate the request with headers and methods

ext/include/opentelemetry/ext/http/common/url_parser.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class UrlParser
6565
cpos = pos1 + 1;
6666
}
6767
}
68-
pos = url_.find_first_of(":", cpos);
68+
pos = FindPortPosition(url_, cpos);
6969
bool is_port = false;
7070
if (pos == std::string::npos)
7171
{
@@ -130,6 +130,40 @@ class UrlParser
130130
query_ = std::string(url_.begin() + cpos, url_.begin() + url_.length());
131131
}
132132
}
133+
134+
private:
135+
static std::string::size_type FindPortPosition(const std::string &url,
136+
std::string::size_type offset)
137+
{
138+
// @see https://www.rfc-editor.org/rfc/rfc3986#page-18
139+
size_t sub_expression_counter = 0;
140+
for (std::string::size_type i = offset; i < url.size(); ++i)
141+
{
142+
char c = url[i];
143+
if (0 == sub_expression_counter && c == ':')
144+
{
145+
return i;
146+
}
147+
148+
if (c == '[')
149+
{
150+
++sub_expression_counter;
151+
}
152+
else if (c == ']')
153+
{
154+
if (sub_expression_counter > 0)
155+
{
156+
--sub_expression_counter;
157+
}
158+
}
159+
else if (0 == sub_expression_counter && c == '/')
160+
{
161+
break;
162+
}
163+
}
164+
165+
return std::string::npos;
166+
}
133167
};
134168

135169
class UrlDecoder

ext/test/http/url_parser_test.cc

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,62 @@ TEST(UrlParserTests, BasicTests)
123123
{"path", "/path1@bbb/path2"},
124124
{"query", "q1=a1&q2=a2"},
125125
{"success", "true"}}},
126-
126+
{"http://1.2.3.4/path1/path2?q1=a1&q2=a2",
127+
{{"host", "1.2.3.4"},
128+
{"port", "80"},
129+
{"scheme", "http"},
130+
{"path", "/path1/path2"},
131+
{"query", "q1=a1&q2=a2"},
132+
{"success", "true"}}},
133+
{"user:[email protected]:8080/path1/path2?q1=a1&q2=a2",
134+
{{"host", "1.2.3.4"},
135+
{"port", "8080"},
136+
{"scheme", "http"},
137+
{"path", "/path1/path2"},
138+
{"query", "q1=a1&q2=a2"},
139+
{"success", "true"}}},
140+
{"https://[email protected]/path1/path2?q1=a1&q2=a2",
141+
{{"host", "1.2.3.4"},
142+
{"port", "443"},
143+
{"scheme", "https"},
144+
{"path", "/path1/path2"},
145+
{"query", "q1=a1&q2=a2"},
146+
{"success", "true"}}},
147+
{"http://1.2.3.4/path1@bbb/path2?q1=a1&q2=a2",
148+
{{"host", "1.2.3.4"},
149+
{"port", "80"},
150+
{"scheme", "http"},
151+
{"path", "/path1@bbb/path2"},
152+
{"query", "q1=a1&q2=a2"},
153+
{"success", "true"}}},
154+
{"http://[fe80::225:93da:bfde:b5f5]/path1/path2?q1=a1&q2=a2",
155+
{{"host", "[fe80::225:93da:bfde:b5f5]"},
156+
{"port", "80"},
157+
{"scheme", "http"},
158+
{"path", "/path1/path2"},
159+
{"query", "q1=a1&q2=a2"},
160+
{"success", "true"}}},
161+
{"user:password@[fe80::225:93da:bfde:b5f5]:8080/path1/path2?q1=a1&q2=a2",
162+
{{"host", "[fe80::225:93da:bfde:b5f5]"},
163+
{"port", "8080"},
164+
{"scheme", "http"},
165+
{"path", "/path1/path2"},
166+
{"query", "q1=a1&q2=a2"},
167+
{"success", "true"}}},
168+
{"https://user@[fe80::225:93da:bfde:b5f5]/path1/path2?q1=a1&q2=a2",
169+
{{"host", "[fe80::225:93da:bfde:b5f5]"},
170+
{"port", "443"},
171+
{"scheme", "https"},
172+
{"path", "/path1/path2"},
173+
{"query", "q1=a1&q2=a2"},
174+
{"success", "true"}}},
175+
{"http://[fe80::225:93da:bfde:b5f5]/path1@bbb/path2?q1=a1&q2=a2",
176+
{{"host", "[fe80::225:93da:bfde:b5f5]"},
177+
{"port", "80"},
178+
{"scheme", "http"},
179+
{"path", "/path1@bbb/path2"},
180+
{"query", "q1=a1&q2=a2"},
181+
{"success", "true"}}},
127182
};
128183
for (auto &url_map : urls_map)
129184
{

0 commit comments

Comments
 (0)