Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EXPORTER] Fix URL in ES exporter, fix ipv6 supporting for http client. #3081

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading