Skip to content

Commit 79d07ac

Browse files
committed
serializer
1 parent 4f6bd12 commit 79d07ac

File tree

1 file changed

+51
-9
lines changed
  • packages/dd-trace/src/process-tags

1 file changed

+51
-9
lines changed

packages/dd-trace/src/process-tags/index.js

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,67 @@
22

33
const path = require('node:path')
44
const pkg = require('../pkg')
5+
const { tags } = require('../config_defaults')
56

67
const CURRENT_WORKING_DIRECTORY = process.cwd()
78
const ENTRYPOINT_PATH = require.main?.filename || ''
9+
console.log('ENTRYPOINT_PATH:', ENTRYPOINT_PATH)
10+
console.log('CURRENT_WORKING_DIRECTORY:', CURRENT_WORKING_DIRECTORY)
811

9-
module.exports = function processTags () {
10-
return {
11-
// last segment of the current working directory, e.g. /foo/bar/baz/ -> baz
12-
'entrypoint.workdir': path.basename(CURRENT_WORKING_DIRECTORY),
12+
// if we don't have a value we should send nothing at all
13+
// e.g. undefined which is dropped upon JSON serialization
14+
15+
// $ cd /foo/bar && node baz/banana.js
16+
// entrypoint.workdir = bar
17+
// entrypoint.name = banana
18+
// entrypoint.type = script
19+
// entrypoint.basedir = baz
20+
// package.json.name = <from package.json>
21+
22+
module.exports = function getProcessTags () {
23+
// this list is sorted alphabetically for consistent serialization
24+
const tags = [
25+
// the immediate parent directory name of the entrypoint script, e.g. /foo/bar/baz/banana.js -> baz
26+
['entrypoint.basedir', ENTRYPOINT_PATH === '' ? undefined : path.basename(path.dirname(ENTRYPOINT_PATH))],
1327

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

1731
// always script for JavaScript applications
18-
'entrypoint.type': 'script',
32+
['entrypoint.type', 'script'],
1933

20-
// the immediate parent directory name of the entrypoint script, e.g. /foo/bar/baz/banana.js -> baz
21-
'entrypoint.basedir': path.basename(path.dirname(ENTRYPOINT_PATH)),
34+
// last segment of the current working directory, e.g. /foo/bar/baz/ -> baz
35+
['entrypoint.workdir', path.basename(CURRENT_WORKING_DIRECTORY) || undefined],
2236

2337
// the .name field from the application's package.json
24-
'package.json.name': pkg.name
38+
['package.json.name', pkg.name || undefined]
39+
]
40+
41+
const serialized = serialize(tags)
42+
43+
return {
44+
tags,
45+
serialized
2546
}
2647
}
48+
49+
function serialize(tags) {
50+
const intermediary = []
51+
for (let [name, value] of tags) {
52+
if (value === undefined) continue
53+
intermediary.push(`${name}:${sanitize(value)}`)
54+
}
55+
return intermediary.join(',')
56+
}
57+
58+
/**
59+
* Sanitize a process tag value
60+
*
61+
* @param {string} value
62+
* @returns {string}
63+
*/
64+
function sanitize(value) {
65+
return String(value)
66+
.toLowerCase()
67+
.replaceAll(/[^a-zA-Z0-9/_.-]/g, '_')
68+
}

0 commit comments

Comments
 (0)