Skip to content

Commit 77c7719

Browse files
authored
chore: generate more narrow config types (#8081)
This updates our configuration types by checking if the calculated configs make them always assigned or not. Next to that, update a few other manual config entries that are outdated due to in-between fixes.
1 parent a156a78 commit 77c7719

3 files changed

Lines changed: 82 additions & 11 deletions

File tree

packages/dd-trace/src/config/config-types.d.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,15 @@ export interface ConfigProperties extends GeneratedConfig {
1010
}
1111
commitSHA: string | undefined
1212
debug: boolean
13-
gcpPubSubPushSubscriptionEnabled: boolean
1413
instrumentationSource: 'manual' | 'ssi'
15-
isAzureFunction: boolean
1614
isCiVisibility: boolean
17-
isGCPFunction: boolean
1815
isServiceNameInferred: boolean
1916
isServiceUserProvided: boolean
2017
logger: import('../../../../index').TracerOptions['logger'] | undefined
2118
lookup: NonNullable<import('../../../../index').TracerOptions['lookup']>
2219
readonly parsedDdTags: Record<string, string>
2320
plugins: boolean
2421
repositoryUrl: string | undefined
25-
rules: import('../../../../index').SamplingRule[]
2622
sampler: {
2723
rateLimit: number
2824
rules: import('../../../../index').SamplingRule[]

packages/dd-trace/src/config/generated-config-types.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ export interface GeneratedConfig {
482482
OTEL_EXPORTER_OTLP_HEADERS: Record<string, string> | undefined;
483483
OTEL_EXPORTER_OTLP_LOGS_HEADERS: Record<string, string> | undefined;
484484
OTEL_EXPORTER_OTLP_METRICS_HEADERS: Record<string, string> | undefined;
485-
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: string | undefined;
485+
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: string;
486486
OTEL_EXPORTER_OTLP_TRACES_HEADERS: Record<string, string> | undefined;
487487
OTEL_EXPORTER_OTLP_TRACES_PROTOCOL: "http/json";
488488
OTEL_EXPORTER_OTLP_TRACES_TIMEOUT: number;
@@ -497,7 +497,7 @@ export interface GeneratedConfig {
497497
otelLogsEnabled: boolean;
498498
otelLogsProtocol: string;
499499
otelLogsTimeout: number;
500-
otelLogsUrl: string | undefined;
500+
otelLogsUrl: string;
501501
otelMaxExportBatchSize: number;
502502
otelMaxQueueSize: number;
503503
otelMetricsEnabled: boolean;
@@ -506,7 +506,7 @@ export interface GeneratedConfig {
506506
otelMetricsProtocol: string;
507507
otelMetricsTemporalityPreference: "DELTA" | "CUMULATIVE" | "LOWMEMORY";
508508
otelMetricsTimeout: number;
509-
otelMetricsUrl: string | undefined;
509+
otelMetricsUrl: string;
510510
otelProtocol: string;
511511
otelTimeout: number;
512512
peerServiceMapping: Record<string, string>;
@@ -537,7 +537,7 @@ export interface GeneratedConfig {
537537
sampleRate: number | undefined;
538538
samplingRules: import('../../../../index').SamplingRule[];
539539
scope: string | undefined;
540-
service: string | undefined;
540+
service: string;
541541
serviceMapping: Record<string, string>;
542542
site: string;
543543
spanAttributeSchema: "v0" | "v1";

scripts/generate-config-types.js

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ const OUTPUT_PATH = path.join(
1515
'..',
1616
'packages/dd-trace/src/config/generated-config-types.d.ts'
1717
)
18+
const CONFIG_INDEX_PATH = path.join(
19+
__dirname,
20+
'..',
21+
'packages/dd-trace/src/config/index.js'
22+
)
1823

1924
const BASE_TYPES = {
2025
array: 'string[]',
@@ -54,8 +59,78 @@ function getPropertyName (canonicalName, entry) {
5459
return configurationNames?.[0] ?? canonicalName
5560
}
5661

57-
function withUndefined (type, entry) {
58-
return entry.default === null ? `${type} | undefined` : type
62+
const FALLBACK_PATTERN =
63+
/if\s*\(\s*!\s*this\.([\w.]+)\s*\)\s*\{[\s\S]*?setAndTrack\s*\(\s*this\s*,\s*['"]([\w.]+)['"]\s*,/g
64+
65+
// Expression whose tail (after any top-level `||`/`??`, or the whole expression) is a string or
66+
// template literal — i.e. the result is guaranteed defined at runtime.
67+
const GUARANTEED_DEFINED = /(?:^|\|\||\?\?)\s*(?:'[^']*'|"[^"]*"|`(?:\$\{[^}`]*\}|[^`])*`)\s*$/
68+
69+
// Returns the index right after the `close` that balances the `open` preceding `start`, or -1 if
70+
// unbalanced. Skips over string and template literals so their contents don't affect depth.
71+
function balancedEnd (s, start, open, close) {
72+
let depth = 1
73+
let i = start
74+
while (i < s.length) {
75+
const ch = s[i]
76+
if (ch === open) {
77+
depth++
78+
i++
79+
} else if (ch === close) {
80+
i++
81+
if (--depth === 0) return i
82+
} else if (ch === '"' || ch === '\'' || ch === '`') {
83+
i = skipQuoted(s, i, ch)
84+
} else {
85+
i++
86+
}
87+
}
88+
return -1
89+
}
90+
91+
function skipQuoted (s, i, quote) {
92+
const isTemplate = quote === '`'
93+
i++
94+
while (i < s.length) {
95+
if (s[i] === '\\') { i += 2; continue }
96+
if (s[i] === quote) return i + 1
97+
if (isTemplate && s[i] === '$' && s[i + 1] === '{') {
98+
i = balancedEnd(s, i + 2, '{', '}')
99+
if (i === -1) return s.length
100+
continue
101+
}
102+
i++
103+
}
104+
return i
105+
}
106+
107+
function findCalculatedFallbackProperties () {
108+
const source = readFileSync(CONFIG_INDEX_PATH, 'utf8')
109+
const marker = /#applyCalculated\s*\(\s*\)\s*\{/.exec(source)
110+
if (!marker) throw new Error('Could not locate #applyCalculated() in config/index.js')
111+
112+
const bodyStart = marker.index + marker[0].length
113+
const body = source.slice(bodyStart, balancedEnd(source, bodyStart, '{', '}') - 1)
114+
115+
const properties = new Set()
116+
let match
117+
while ((match = FALLBACK_PATTERN.exec(body)) !== null) {
118+
if (match[1] !== match[2]) continue
119+
const valueStart = match.index + match[0].length
120+
const valueEnd = balancedEnd(body, valueStart, '(', ')')
121+
if (valueEnd === -1) continue
122+
const value = body.slice(valueStart, valueEnd - 1).trim()
123+
if (GUARANTEED_DEFINED.test(value)) properties.add(match[1])
124+
}
125+
return properties
126+
}
127+
128+
const CALCULATED_FALLBACK_PROPERTIES = findCalculatedFallbackProperties()
129+
130+
function withUndefined (type, entry, propertyName) {
131+
if (entry.default !== null) return type
132+
if (CALCULATED_FALLBACK_PROPERTIES.has(propertyName)) return type
133+
return `${type} | undefined`
59134
}
60135

61136
function getAllowedType (entry) {
@@ -93,7 +168,7 @@ function getTypeForEntry (propertyName, entry) {
93168
throw new Error(`Unsupported configuration type for ${propertyName}: ${entry.type}`)
94169
}
95170

96-
return withUndefined(override, entry)
171+
return withUndefined(override, entry, propertyName)
97172
}
98173

99174
function addProperty (root, propertyName, type) {

0 commit comments

Comments
 (0)