forked from newrelic/node-newrelic
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Refactored otel attribute reconciling
- Loading branch information
1 parent
258ad7d
commit 38c8ead
Showing
2 changed files
with
139 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2025 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const urltils = require('../util/urltils') | ||
const constants = require('./constants') | ||
|
||
const hostKeys = [ | ||
constants.ATTR_NET_HOST_NAME, | ||
constants.ATTR_NET_PEER_NAME, | ||
constants.ATTR_SERVER_ADDRESS | ||
] | ||
|
||
class AttributeReconciler { | ||
#agent | ||
|
||
constructor({ agent }) { | ||
this.#agent = agent | ||
} | ||
|
||
#resolveHost(hostname) { | ||
if (urltils.isLocalhost(hostname)) { | ||
return this.#agent.config.getHostnameSafe(hostname) | ||
} | ||
return hostname | ||
} | ||
|
||
#isHostnameKey(key) { | ||
return hostKeys.includes(key) | ||
} | ||
|
||
reconcile({ segment, otelSpan, mapper = {} }) { | ||
for (const [key, srcValue] of Object.entries(otelSpan.attributes)) { | ||
let value = srcValue | ||
|
||
if (this.#isHostnameKey(key) === true) { | ||
value = this.#resolveHost(srcValue) | ||
} | ||
|
||
if (Object.prototype.hasOwnProperty.call(mapper, key) === true) { | ||
mapper[key](value) | ||
} else { | ||
segment.addAttribute(key, value) | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = AttributeReconciler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters