Skip to content

Commit 9a48a86

Browse files
committed
fix(awsxrayexporter): fix http url generation with url.path and url.query
1 parent 723a25e commit 9a48a86

File tree

3 files changed

+83
-10
lines changed

3 files changed

+83
-10
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: awsxrayexporter
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fix incorrect http url generation in trace segment when url.path is present
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [40809]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

exporter/awsxrayexporter/internal/translator/http.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package translator // import "github.com/open-telemetry/opentelemetry-collector-
66
import (
77
"net"
88
"strconv"
9+
"strings"
910

1011
"github.com/aws/aws-sdk-go-v2/aws"
1112
"go.opentelemetry.io/collector/pdata/pcommon"
@@ -59,8 +60,8 @@ func makeHTTP(span ptrace.Span) (map[string]pcommon.Value, *awsxray.HTTPData) {
5960
urlParts[key] = value.Str()
6061
hasHTTP = true
6162
hasHTTPRequestURLAttributes = true
62-
case string(conventionsv112.HTTPTargetKey), string(conventions.URLQueryKey):
63-
urlParts[string(conventionsv112.HTTPTargetKey)] = value.Str()
63+
case string(conventionsv112.HTTPTargetKey):
64+
urlParts[key] = value.Str()
6465
hasHTTP = true
6566
case string(conventionsv112.HTTPServerNameKey):
6667
urlParts[key] = value.Str()
@@ -110,6 +111,9 @@ func makeHTTP(span ptrace.Span) (map[string]pcommon.Value, *awsxray.HTTPData) {
110111
case string(conventions.URLPathKey):
111112
urlParts[key] = value.Str()
112113
hasHTTP = true
114+
case string(conventions.URLQueryKey):
115+
urlParts[key] = value.Str()
116+
hasHTTP = true
113117
case string(conventions.ServerAddressKey):
114118
urlParts[key] = value.Str()
115119
hasHTTPRequestURLAttributes = true
@@ -205,7 +209,19 @@ func constructClientURL(urlParts map[string]string) string {
205209
if ok {
206210
url += target
207211
} else {
208-
url += "/"
212+
path, ok := urlParts[string(conventions.URLPathKey)]
213+
if ok {
214+
url += path
215+
} else {
216+
url += "/"
217+
}
218+
query, ok := urlParts[string(conventions.URLQueryKey)]
219+
if ok {
220+
if !strings.HasPrefix(query, "?") {
221+
query = "?" + query
222+
}
223+
url += query
224+
}
209225
}
210226
return url
211227
}
@@ -259,6 +275,13 @@ func constructServerURL(urlParts map[string]string) string {
259275
} else {
260276
url += "/"
261277
}
278+
query, ok := urlParts[string(conventions.URLQueryKey)]
279+
if ok {
280+
if !strings.HasPrefix(query, "?") {
281+
query = "?" + query
282+
}
283+
url += query
284+
}
262285
}
263286
return url
264287
}

exporter/awsxrayexporter/internal/translator/http_test.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ func TestClientSpanWithSchemeHostTargetAttributesStable(t *testing.T) {
7979
attributes[string(conventions.HTTPRequestMethodKey)] = "GET"
8080
attributes[string(conventions.URLSchemeKey)] = "https"
8181
attributes[string(conventionsv112.HTTPHostKey)] = "api.example.com"
82-
attributes[string(conventions.URLQueryKey)] = "/users/junit"
82+
attributes[string(conventions.URLPathKey)] = "/users/junit"
83+
attributes[string(conventions.URLQueryKey)] = "v=1"
8384
attributes[string(conventions.HTTPResponseStatusCodeKey)] = 200
8485
attributes["user.id"] = "junit"
8586
span := constructHTTPClientSpan(attributes)
@@ -92,7 +93,7 @@ func TestClientSpanWithSchemeHostTargetAttributesStable(t *testing.T) {
9293
require.NoError(t, w.Encode(httpData))
9394
jsonStr := w.String()
9495
testWriters.release(w)
95-
assert.Contains(t, jsonStr, "https://api.example.com/users/junit")
96+
assert.Contains(t, jsonStr, "https://api.example.com/users/junit?v=1")
9697
}
9798

9899
func TestClientSpanWithPeerAttributes(t *testing.T) {
@@ -128,7 +129,7 @@ func TestClientSpanWithPeerAttributesStable(t *testing.T) {
128129
attributes[string(conventionsv112.NetPeerNameKey)] = "kb234.example.com"
129130
attributes[string(conventionsv112.NetPeerPortKey)] = 8080
130131
attributes[string(conventionsv112.NetPeerIPKey)] = "10.8.17.36"
131-
attributes[string(conventions.URLQueryKey)] = "/users/junit"
132+
attributes[string(conventions.URLQueryKey)] = "users=junit"
132133
attributes[string(conventions.HTTPResponseStatusCodeKey)] = 200
133134
span := constructHTTPClientSpan(attributes)
134135

@@ -143,7 +144,7 @@ func TestClientSpanWithPeerAttributesStable(t *testing.T) {
143144
require.NoError(t, w.Encode(httpData))
144145
jsonStr := w.String()
145146
testWriters.release(w)
146-
assert.Contains(t, jsonStr, "http://kb234.example.com:8080/users/junit")
147+
assert.Contains(t, jsonStr, "http://kb234.example.com:8080/?users=junit")
147148
}
148149

149150
func TestClientSpanWithHttpPeerAttributes(t *testing.T) {
@@ -279,7 +280,7 @@ func TestServerSpanWithSchemeHostTargetAttributesStable(t *testing.T) {
279280
attributes[string(conventions.HTTPRequestMethodKey)] = http.MethodGet
280281
attributes[string(conventions.URLSchemeKey)] = "https"
281282
attributes[string(conventions.ServerAddressKey)] = "api.example.com"
282-
attributes[string(conventions.URLQueryKey)] = "/users/junit"
283+
attributes[string(conventions.URLPathKey)] = "/users/junit"
283284
attributes[string(conventions.ClientAddressKey)] = "192.168.15.32"
284285
attributes[string(conventions.HTTPResponseStatusCodeKey)] = 200
285286
span := constructHTTPServerSpan(attributes)
@@ -295,6 +296,28 @@ func TestServerSpanWithSchemeHostTargetAttributesStable(t *testing.T) {
295296
assert.Contains(t, jsonStr, "https://api.example.com/users/junit")
296297
}
297298

299+
func TestServerSpanWithNewConventionsWithURLPath(t *testing.T) {
300+
attributes := make(map[string]any)
301+
attributes[string(conventions.HTTPRequestMethodKey)] = http.MethodGet
302+
attributes[string(conventions.ServerAddressKey)] = "localhost"
303+
attributes[string(conventions.URLSchemeKey)] = "http"
304+
attributes[string(conventions.URLQueryKey)] = "?version=test"
305+
attributes[string(conventions.URLPathKey)] = "/api"
306+
attributes[string(conventions.ClientAddressKey)] = "127.0.0.1"
307+
attributes[string(conventions.HTTPResponseStatusCodeKey)] = 200
308+
span := constructHTTPServerSpan(attributes)
309+
310+
filtered, httpData := makeHTTP(span)
311+
312+
assert.NotNil(t, httpData)
313+
assert.NotNil(t, filtered)
314+
w := testWriters.borrow()
315+
require.NoError(t, w.Encode(httpData))
316+
jsonStr := w.String()
317+
testWriters.release(w)
318+
assert.Contains(t, jsonStr, "http://localhost/api?version=test")
319+
}
320+
298321
func TestServerSpanWithSchemeServernamePortTargetAttributes(t *testing.T) {
299322
attributes := make(map[string]any)
300323
attributes[string(conventionsv112.HTTPMethodKey)] = http.MethodGet
@@ -323,7 +346,7 @@ func TestServerSpanWithSchemeServernamePortTargetAttributesStable(t *testing.T)
323346
attributes[string(conventions.URLSchemeKey)] = "https"
324347
attributes[string(conventions.ServerAddressKey)] = "api.example.com"
325348
attributes[string(conventions.ServerPortKey)] = 443
326-
attributes[string(conventions.URLQueryKey)] = "/users/junit"
349+
attributes[string(conventions.URLQueryKey)] = "users=junit"
327350
attributes[string(conventions.ClientAddressKey)] = "192.168.15.32"
328351
attributes[string(conventions.HTTPResponseStatusCodeKey)] = 200
329352
span := constructHTTPServerSpan(attributes)
@@ -336,7 +359,7 @@ func TestServerSpanWithSchemeServernamePortTargetAttributesStable(t *testing.T)
336359
require.NoError(t, w.Encode(httpData))
337360
jsonStr := w.String()
338361
testWriters.release(w)
339-
assert.Contains(t, jsonStr, "https://api.example.com/users/junit")
362+
assert.Contains(t, jsonStr, "https://api.example.com/?users=junit")
340363
}
341364

342365
func TestServerSpanWithSchemeNamePortTargetAttributes(t *testing.T) {

0 commit comments

Comments
 (0)