Skip to content

Commit 82c5b65

Browse files
authored
Merge branch 'main' into rads-1996/redact-url
2 parents f01b75d + f2cfd13 commit 82c5b65

4 files changed

Lines changed: 42 additions & 8 deletions

File tree

api/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.
99

1010
### :rocket: (Enhancement)
1111

12+
* feat(api): improve isValidSpanId, isValidTraceId performance [#5714](https://github.com/open-telemetry/opentelemetry-js/pull/5714) @seemk
1213
* feat(diag): change types in `DiagComponentLogger` from `any` to `unknown`[#5478](https://github.com/open-telemetry/opentelemetry-js/pull/5478) @loganrosen
1314

1415
### :bug: (Bug Fix)

api/src/trace/spancontext-utils.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,43 @@ import { NonRecordingSpan } from './NonRecordingSpan';
1818
import { Span } from './span';
1919
import { SpanContext } from './span_context';
2020

21-
const VALID_TRACEID_REGEX = /^([0-9a-f]{32})$/i;
22-
const VALID_SPANID_REGEX = /^[0-9a-f]{16}$/i;
21+
// Valid characters (0-9, a-f, A-F) are marked as 1.
22+
const isHex = new Uint8Array([
23+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
25+
1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
26+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
27+
]);
28+
29+
function isValidHex(id: string, length: number): boolean {
30+
// As of 1.9.0 the id was allowed to be a non-string value,
31+
// even though it was not possible in the types.
32+
if (typeof id !== 'string' || id.length !== length) return false;
33+
34+
let r = 0;
35+
for (let i = 0; i < id.length; i += 4) {
36+
r +=
37+
(isHex[id.charCodeAt(i)] | 0) +
38+
(isHex[id.charCodeAt(i + 1)] | 0) +
39+
(isHex[id.charCodeAt(i + 2)] | 0) +
40+
(isHex[id.charCodeAt(i + 3)] | 0);
41+
}
42+
43+
return r === length;
44+
}
2345

2446
/**
2547
* @since 1.0.0
2648
*/
2749
export function isValidTraceId(traceId: string): boolean {
28-
return VALID_TRACEID_REGEX.test(traceId) && traceId !== INVALID_TRACEID;
50+
return isValidHex(traceId, 32) && traceId !== INVALID_TRACEID;
2951
}
3052

3153
/**
3254
* @since 1.0.0
3355
*/
3456
export function isValidSpanId(spanId: string): boolean {
35-
return VALID_SPANID_REGEX.test(spanId) && spanId !== INVALID_SPANID;
57+
return isValidHex(spanId, 16) && spanId !== INVALID_SPANID;
3658
}
3759

3860
/**

api/test/common/trace/spancontext-utils.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ describe('spancontext-utils', function () {
3737
assert.ok(!context.isSpanContextValid(spanContext));
3838
});
3939

40+
it('should return false when traceId is malformed', function () {
41+
// 0x4141 is not a hex character, but doing a bitwise AND with 0xFF
42+
// would yield a valid character 'A'.
43+
const spanContext = {
44+
traceId: 'd4cda95b652f4a1592b449d5929fda1\u4141',
45+
spanId: '6e0c63257de34c92',
46+
traceFlags: TraceFlags.NONE,
47+
};
48+
assert.ok(!context.isSpanContextValid(spanContext));
49+
});
50+
4051
it('should return false when spanId is invalid', function () {
4152
const spanContext = {
4253
traceId: 'd4cda95b652f4a1592b449d5929fda1b',

experimental/packages/exporter-logs-otlp-proto/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ To override the default timeout duration, use the following options:
5050

5151
- Set with environment variables:
5252

53-
| Environment variable | Description |
54-
------------------------------|----------------------|-------------|
55-
| OTEL_EXPORTER_OTLP_LOGS_TIMEOUT | The maximum waiting time, in milliseconds, allowed to send each OTLP trace batch. Default is 10000. |
56-
| OTEL_EXPORTER_OTLP_TIMEOUT | The maximum waiting time, in milliseconds, allowed to send each OTLP trace and metric batch. Default is 10000. |
53+
| Environment variable | Description |
54+
|-----------------------------------|-------------|
55+
| `OTEL_EXPORTER_OTLP_LOGS_TIMEOUT` | The maximum waiting time, in milliseconds, allowed to send each OTLP trace batch. Default is 10000. |
56+
| `OTEL_EXPORTER_OTLP_TIMEOUT` | The maximum waiting time, in milliseconds, allowed to send each OTLP trace and metric batch. Default is 10000. |
5757

5858
> `OTEL_EXPORTER_OTLP_LOGS_TIMEOUT` takes precedence and overrides `OTEL_EXPORTER_OTLP_TIMEOUT`.
5959

0 commit comments

Comments
 (0)