From 347a57d51ec62943cfa0ab53a97522c7cd525790 Mon Sep 17 00:00:00 2001 From: Daniel Kimsey <90741+dekimsey@users.noreply.github.com> Date: Fri, 8 Apr 2022 11:21:19 -0500 Subject: [PATCH] Correct CurlLogger's URL to actual destination The CurlLogger hardcodes `http://localhost:9200`. This change instead uses the given configuration values. Signed-off-by: Daniel Kimsey Signed-off-by: Daniel Kimsey --- CHANGELOG.md | 3 ++- opensearchtransport/logger.go | 8 ++++++-- opensearchtransport/logger_internal_test.go | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a921074e6..7b36e862f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,8 +16,9 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Fixed - Renamed the sequence number struct tag to if_seq_no to fix optimistic concurrency control ([#166](https://github.com/opensearch-project/opensearch-go/pull/166)) + - Correct curl logging to emit the correct URL destination ([#101](https://github.com/opensearch-project/opensearch-go/pull/101)) ### Security -[Unreleased]: https://github.com/opensearch-project/opensearch-go/compare/2.1...HEAD \ No newline at end of file +[Unreleased]: https://github.com/opensearch-project/opensearch-go/compare/2.1...HEAD diff --git a/opensearchtransport/logger.go b/opensearchtransport/logger.go index 223b0165d..f089e9e63 100644 --- a/opensearchtransport/logger.go +++ b/opensearchtransport/logger.go @@ -248,8 +248,12 @@ func (l *CurlLogger) LogRoundTrip(req *http.Request, res *http.Response, err err } } - b.WriteString(" 'http://localhost:9200") - b.WriteString(req.URL.Path) + // If by some oddity we end up with a nil req.URL, we handle it gracefully. + if req.URL == nil { + b.WriteString(" '") + } else { + b.WriteString(fmt.Sprintf(" '%s://%s%s", req.URL.Scheme, req.URL.Host, req.URL.Path)) + } b.WriteString("?pretty") if query != "" { fmt.Fprintf(&b, "&%s", query) diff --git a/opensearchtransport/logger_internal_test.go b/opensearchtransport/logger_internal_test.go index c5d7e7b67..27beae7b7 100644 --- a/opensearchtransport/logger_internal_test.go +++ b/opensearchtransport/logger_internal_test.go @@ -285,7 +285,7 @@ func TestTransportLogger(t *testing.T) { t.Fatalf("Expected 9 lines, got %d", len(lines)) } - if !strings.Contains(lines[0], "curl -X GET 'http://localhost:9200/abc?pretty&q=a%2Cb'") { + if !strings.Contains(lines[0], "curl -X GET 'http://foo/abc?pretty&q=a%2Cb'") { t.Errorf("Unexpected output: %s", lines[0]) } })