Skip to content

Commit

Permalink
chore: Refactored to derive hostname from segment name
Browse files Browse the repository at this point in the history
Signed-off-by: mrickard <maurice@mauricerickard.com>
  • Loading branch information
mrickard committed Apr 29, 2024
1 parent b1990f2 commit ca38dc4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lib/spans/span-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ class SpanEvent {

let span = null
if (HttpSpanEvent.testSegment(segment)) {
// Get external host from the segment name
const nameParams = segment.name.split('/')
const external = nameParams.indexOf('External')
if (external > -1) {
attributes.host = nameParams[external + 1]
}
span = new HttpSpanEvent(attributes, customAttributes)
} else if (DatastoreSpanEvent.testSegment(segment)) {
span = new DatastoreSpanEvent(attributes, customAttributes)
Expand Down Expand Up @@ -191,10 +197,14 @@ class HttpSpanEvent extends SpanEvent {

if (attributes.url) {
this.addAttribute('http.url', attributes.url)
this.addAttribute('server.address', new URL(attributes.url).hostname)
attributes.url = null
}

if (attributes.host) {
this.addAttribute('server.address', attributes.host)
attributes.host = null
}

if (attributes.procedure) {
this.addAttribute('http.method', attributes.procedure)
this.addAttribute('http.request.method', attributes.procedure)
Expand Down
12 changes: 11 additions & 1 deletion lib/spans/streaming-span-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ class StreamingSpanEvent {

let span = null
if (StreamingHttpSpanEvent.isHttpSegment(segment)) {
// Get external host from the segment name
const nameParams = segment.name.split('/')
const external = nameParams.indexOf('External')
if (external > -1) {
agentAttributes.host = nameParams[external + 1]
}
span = new StreamingHttpSpanEvent(traceId, agentAttributes, customAttributes)
} else if (StreamingDatastoreSpanEvent.isDatastoreSegment(segment)) {
span = new StreamingDatastoreSpanEvent(traceId, agentAttributes, customAttributes)
Expand Down Expand Up @@ -194,10 +200,14 @@ class StreamingHttpSpanEvent extends StreamingSpanEvent {

if (agentAttributes.url) {
this.addAgentAttribute('http.url', agentAttributes.url)
this.addAgentAttribute('server.address', new URL(agentAttributes.url).hostname)
agentAttributes.url = null
}

if (agentAttributes.host) {
this.addAgentAttribute('server.address', agentAttributes.host)
agentAttributes.host = null
}

if (agentAttributes.procedure) {
this.addAgentAttribute('http.method', agentAttributes.procedure)
this.addAgentAttribute('http.request.method', agentAttributes.procedure)
Expand Down

0 comments on commit ca38dc4

Please sign in to comment.