Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions packages/dd-trace/src/process-tags/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict'

const path = require('node:path')
const pkg = require('../pkg')
const { tags } = require('../config_defaults')

Check failure on line 5 in packages/dd-trace/src/process-tags/index.js

View workflow job for this annotation

GitHub Actions / lint

'tags' is assigned a value but never used

const CURRENT_WORKING_DIRECTORY = process.cwd()
const ENTRYPOINT_PATH = require.main?.filename || ''
console.log('ENTRYPOINT_PATH:', ENTRYPOINT_PATH)

Check failure on line 9 in packages/dd-trace/src/process-tags/index.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
console.log('CURRENT_WORKING_DIRECTORY:', CURRENT_WORKING_DIRECTORY)

Check failure on line 10 in packages/dd-trace/src/process-tags/index.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

// if we don't have a value we should send nothing at all
// e.g. undefined which is dropped upon JSON serialization

// $ cd /foo/bar && node baz/banana.js
// entrypoint.workdir = bar
// entrypoint.name = banana
// entrypoint.type = script
// entrypoint.basedir = baz
// package.json.name = <from package.json>

module.exports = function getProcessTags () {
// this list is sorted alphabetically for consistent serialization
const tags = [
// the immediate parent directory name of the entrypoint script, e.g. /foo/bar/baz/banana.js -> baz
['entrypoint.basedir', ENTRYPOINT_PATH === '' ? undefined : path.basename(path.dirname(ENTRYPOINT_PATH))],

// the entrypoint script filename without the extension, e.g. /foo/bar/baz/banana.js -> banana
['entrypoint.name', path.basename(ENTRYPOINT_PATH, path.extname(ENTRYPOINT_PATH)) || undefined],

// always script for JavaScript applications
['entrypoint.type', 'script'],

// last segment of the current working directory, e.g. /foo/bar/baz/ -> baz
['entrypoint.workdir', path.basename(CURRENT_WORKING_DIRECTORY) || undefined],

// the .name field from the application's package.json
['package.json.name', pkg.name || undefined]
]

const serialized = serialize(tags)

return {
tags,
serialized
}
}

function serialize(tags) {

Check failure on line 49 in packages/dd-trace/src/process-tags/index.js

View workflow job for this annotation

GitHub Actions / lint

Missing space before function parentheses
const intermediary = []
for (let [name, value] of tags) {

Check failure on line 51 in packages/dd-trace/src/process-tags/index.js

View workflow job for this annotation

GitHub Actions / lint

'value' is never reassigned. Use 'const' instead

Check failure on line 51 in packages/dd-trace/src/process-tags/index.js

View workflow job for this annotation

GitHub Actions / lint

'name' is never reassigned. Use 'const' instead
if (value === undefined) continue
intermediary.push(`${name}:${sanitize(value)}`)
}
return intermediary.join(',')
}

/**
* Sanitize a process tag value
*

Check failure on line 60 in packages/dd-trace/src/process-tags/index.js

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
* @param {string} value

Check failure on line 61 in packages/dd-trace/src/process-tags/index.js

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
* @returns {string}
*/
function sanitize(value) {

Check failure on line 64 in packages/dd-trace/src/process-tags/index.js

View workflow job for this annotation

GitHub Actions / lint

Missing space before function parentheses
return String(value)
.toLowerCase()
.replaceAll(/[^a-zA-Z0-9/_.-]/g, '_')
}

Check failure on line 68 in packages/dd-trace/src/process-tags/index.js

View workflow job for this annotation

GitHub Actions / lint

Newline required at end of file but not found
2 changes: 2 additions & 0 deletions packages/dd-trace/src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
removeBaggageItem,
removeAllBaggageItems
} = require('./baggage')
const getProcessTags = require('./process-tags')

class LazyModule {
constructor (provider) {
Expand Down Expand Up @@ -78,6 +79,7 @@ class Tracer extends NoopProxy {
this.getAllBaggageItems = getAllBaggageItems
this.removeBaggageItem = removeBaggageItem
this.removeAllBaggageItems = removeAllBaggageItems
this.processTags = getProcessTags()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine that the fact that this is setup here ensures that we'll only "compute" the tag values once ?


// these requires must work with esm bundler
this._modules = {
Expand Down
Loading