Skip to content

Commit ca38dc4

Browse files
committed
chore: Refactored to derive hostname from segment name
Signed-off-by: mrickard <[email protected]>
1 parent b1990f2 commit ca38dc4

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/spans/span-event.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ class SpanEvent {
106106

107107
let span = null
108108
if (HttpSpanEvent.testSegment(segment)) {
109+
// Get external host from the segment name
110+
const nameParams = segment.name.split('/')
111+
const external = nameParams.indexOf('External')
112+
if (external > -1) {
113+
attributes.host = nameParams[external + 1]
114+
}
109115
span = new HttpSpanEvent(attributes, customAttributes)
110116
} else if (DatastoreSpanEvent.testSegment(segment)) {
111117
span = new DatastoreSpanEvent(attributes, customAttributes)
@@ -191,10 +197,14 @@ class HttpSpanEvent extends SpanEvent {
191197

192198
if (attributes.url) {
193199
this.addAttribute('http.url', attributes.url)
194-
this.addAttribute('server.address', new URL(attributes.url).hostname)
195200
attributes.url = null
196201
}
197202

203+
if (attributes.host) {
204+
this.addAttribute('server.address', attributes.host)
205+
attributes.host = null
206+
}
207+
198208
if (attributes.procedure) {
199209
this.addAttribute('http.method', attributes.procedure)
200210
this.addAttribute('http.request.method', attributes.procedure)

lib/spans/streaming-span-event.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ class StreamingSpanEvent {
128128

129129
let span = null
130130
if (StreamingHttpSpanEvent.isHttpSegment(segment)) {
131+
// Get external host from the segment name
132+
const nameParams = segment.name.split('/')
133+
const external = nameParams.indexOf('External')
134+
if (external > -1) {
135+
agentAttributes.host = nameParams[external + 1]
136+
}
131137
span = new StreamingHttpSpanEvent(traceId, agentAttributes, customAttributes)
132138
} else if (StreamingDatastoreSpanEvent.isDatastoreSegment(segment)) {
133139
span = new StreamingDatastoreSpanEvent(traceId, agentAttributes, customAttributes)
@@ -194,10 +200,14 @@ class StreamingHttpSpanEvent extends StreamingSpanEvent {
194200

195201
if (agentAttributes.url) {
196202
this.addAgentAttribute('http.url', agentAttributes.url)
197-
this.addAgentAttribute('server.address', new URL(agentAttributes.url).hostname)
198203
agentAttributes.url = null
199204
}
200205

206+
if (agentAttributes.host) {
207+
this.addAgentAttribute('server.address', agentAttributes.host)
208+
agentAttributes.host = null
209+
}
210+
201211
if (agentAttributes.procedure) {
202212
this.addAgentAttribute('http.method', agentAttributes.procedure)
203213
this.addAgentAttribute('http.request.method', agentAttributes.procedure)

0 commit comments

Comments
 (0)