Repository: opensearch-project/opensearch-go
Pull request #101 (2022-04-08 16:22:10)

Correct CurlLogger's URL to actual destination
### Description
The CurlLogger hardcodes `http://localhost:9200`. This change instead uses the given configuration values.
 
### Issues Resolved
n/a
 
### Check List
- [x] Commits are signed per the DCO using --signoff 

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check [here](https://github.com/opensearch-project/OpenSearch/blob/main/CONTRIBUTING.md#developer-certificate-of-origin).


VachaShah: @dekimsey Thank you for this change! Can you please rebase and get in the latest changes from main?
dekimsey: Sorry for not seeing your request @dblock, it has been a busy year and I missed your request. I'm updating it now. I also fixed the tests to validate the change.
dekimsey: @VachaShah I believe this is complete now. Let me know what you think, thank you for pinging me on this!
dblock: Apologies for having missed this. I resolved the CHANGELOG conflict, let's merge this once CI passes.

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e427151..d35548a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -31,8 +31,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))
  - Fix `RetryOnConflict` on bulk indexer ([#215](https://github.com/opensearch-project/opensearch-go/pull/215))
+ - 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 223b016..f089e9e 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)


Test patch:
diff --git a/opensearchtransport/logger_internal_test.go b/opensearchtransport/logger_internal_test.go
index c5d7e7b..27beae7 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])
 		}
 	})