Skip to content

Commit

Permalink
Fix URL in ES exporter, fix ipv6 supporting for http client.
Browse files Browse the repository at this point in the history
  • Loading branch information
owent committed Oct 7, 2024
1 parent 7b82473 commit ef725c9
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
2 changes: 1 addition & 1 deletion exporters/elasticsearch/src/es_log_record_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ sdk::common::ExportResult ElasticsearchLogRecordExporter::Export(
}

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

// Populate the request with headers and methods
Expand Down
36 changes: 35 additions & 1 deletion ext/include/opentelemetry/ext/http/common/url_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class UrlParser
cpos = pos1 + 1;
}
}
pos = url_.find_first_of(":", cpos);
pos = FindPortPosition(url_, cpos);
bool is_port = false;
if (pos == std::string::npos)
{
Expand Down Expand Up @@ -130,6 +130,40 @@ class UrlParser
query_ = std::string(url_.begin() + cpos, url_.begin() + url_.length());
}
}

private:
static std::string::size_type FindPortPosition(const std::string &url,
std::string::size_type offset)
{
// @see https://www.rfc-editor.org/rfc/rfc3986#page-18
size_t sub_expression_counter = 0;
for (std::string::size_type i = offset; i < url.size(); ++i)
{
char c = url[i];
if (0 == sub_expression_counter && c == ':')
{
return i;
}

if (c == '[')
{
++sub_expression_counter;
}
else if (c == ']')
{
if (sub_expression_counter > 0)
{
--sub_expression_counter;
}
}
else if (0 == sub_expression_counter && c == '/')
{
break;
}
}

return std::string::npos;
}
};

class UrlDecoder
Expand Down
57 changes: 56 additions & 1 deletion ext/test/http/url_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,62 @@ TEST(UrlParserTests, BasicTests)
{"path", "/path1@bbb/path2"},
{"query", "q1=a1&q2=a2"},
{"success", "true"}}},

{"http://1.2.3.4/path1/path2?q1=a1&q2=a2",
{{"host", "1.2.3.4"},
{"port", "80"},
{"scheme", "http"},
{"path", "/path1/path2"},
{"query", "q1=a1&q2=a2"},
{"success", "true"}}},
{"user:[email protected]:8080/path1/path2?q1=a1&q2=a2",
{{"host", "1.2.3.4"},
{"port", "8080"},
{"scheme", "http"},
{"path", "/path1/path2"},
{"query", "q1=a1&q2=a2"},
{"success", "true"}}},
{"https://[email protected]/path1/path2?q1=a1&q2=a2",
{{"host", "1.2.3.4"},
{"port", "443"},
{"scheme", "https"},
{"path", "/path1/path2"},
{"query", "q1=a1&q2=a2"},
{"success", "true"}}},
{"http://1.2.3.4/path1@bbb/path2?q1=a1&q2=a2",
{{"host", "1.2.3.4"},
{"port", "80"},
{"scheme", "http"},
{"path", "/path1@bbb/path2"},
{"query", "q1=a1&q2=a2"},
{"success", "true"}}},
{"http://[fe80::225:93da:bfde:b5f5]/path1/path2?q1=a1&q2=a2",
{{"host", "[fe80::225:93da:bfde:b5f5]"},
{"port", "80"},
{"scheme", "http"},
{"path", "/path1/path2"},
{"query", "q1=a1&q2=a2"},
{"success", "true"}}},
{"user:password@[fe80::225:93da:bfde:b5f5]:8080/path1/path2?q1=a1&q2=a2",
{{"host", "[fe80::225:93da:bfde:b5f5]"},
{"port", "8080"},
{"scheme", "http"},
{"path", "/path1/path2"},
{"query", "q1=a1&q2=a2"},
{"success", "true"}}},
{"https://user@[fe80::225:93da:bfde:b5f5]/path1/path2?q1=a1&q2=a2",
{{"host", "[fe80::225:93da:bfde:b5f5]"},
{"port", "443"},
{"scheme", "https"},
{"path", "/path1/path2"},
{"query", "q1=a1&q2=a2"},
{"success", "true"}}},
{"http://[fe80::225:93da:bfde:b5f5]/path1@bbb/path2?q1=a1&q2=a2",
{{"host", "[fe80::225:93da:bfde:b5f5]"},
{"port", "80"},
{"scheme", "http"},
{"path", "/path1@bbb/path2"},
{"query", "q1=a1&q2=a2"},
{"success", "true"}}},
};
for (auto &url_map : urls_map)
{
Expand Down

0 comments on commit ef725c9

Please sign in to comment.